Râu Cao 66f5217a41
Refactor Mastodon nginx recipe for proxy usage
Works both as local deployment and proxy (via roles and environments)

* Use upstreams for proxy_pass
* Access static assets from proxy, configure caching for them
* Move Tor config to environment, install via role
* ...
2022-11-30 12:02:17 +01:00

60 lines
1.7 KiB
Ruby

#
# Cookbook Name:: kosmos-mastodon
# Recipe:: nginx
#
include_recipe "kosmos-nginx"
app_dir = node["kosmos-mastodon"]["directory"]
server_name = node["kosmos-mastodon"]["server_name"]
is_proxy = node.roles.include?('nginx_proxy') rescue nil
upstream_hosts = []
if is_proxy
web_root_dir = "/var/www/#{server_name}/public"
search(:node, "role:mastodon").each do |node|
upstream_hosts << node["knife_zero"]["host"]
end
else
web_root_dir = "#{app_dir}/public"
upstream_hosts << "localhost"
end
directory "#{node['nginx']['dir']}/snippets" do
action :create
owner 'www-data'
mode 0640
end
template "#{node['nginx']['dir']}/snippets/mastodon.conf" do
source 'nginx_conf_shared.erb'
owner 'www-data'
mode 0640
variables web_root_dir: web_root_dir,
server_name: server_name
notifies :reload, 'service[nginx]', :delayed
end
nginx_certbot_site server_name
onion_address = File.read("/var/lib/tor/web/hostname").strip rescue nil rescue nil
template "#{node['nginx']['dir']}/sites-available/#{server_name}" do
source 'nginx_conf_mastodon.erb'
owner 'www-data'
mode 0640
variables server_name: server_name,
ssl_cert: "/etc/letsencrypt/live/#{server_name}/fullchain.pem",
ssl_key: "/etc/letsencrypt/live/#{server_name}/privkey.pem",
shared_config_path: "#{node['nginx']['dir']}/snippets/mastodon.conf",
app_port: node["kosmos-mastodon"]["app_port"],
streaming_port: node["kosmos-mastodon"]["streaming_port"],
onion_address: onion_address,
upstream_hosts: upstream_hosts
notifies :reload, 'service[nginx]', :delayed
end
nginx_site server_name do
action :enable
end