# # Cookbook Name:: kosmos-mastodon # Recipe:: nginx # app_dir = node["kosmos-mastodon"]["directory"] server_name = node["kosmos-mastodon"]["domain"] upstream_hosts = [] search(:node, "role:mastodon").each do |node| upstream_hosts << node["knife_zero"]["host"] end if upstream_hosts.any? web_root_dir = "/var/www/#{server_name}/public" directory web_root_dir do action :create recursive true owner 'www-data' group 'www-data' mode 0755 end else web_root_dir = "#{app_dir}/public" upstream_hosts << "localhost" end directory "#{node['openresty']['dir']}/snippets" do action :create owner 'www-data' mode 0640 end template "#{node['openresty']['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, s3_private_url: "#{node["kosmos-mastodon"]["s3_endpoint"]}/#{node["kosmos-mastodon"]["s3_bucket"]}/", s3_public_url: "https://#{node["kosmos-mastodon"]["s3_alias_host"]}/" notifies :reload, 'service[openresty]', :delayed end tls_cert_for server_name do auth "gandi_dns" acme_domain "letsencrypt.kosmos.org" action :create end tor_services = data_bag_item('credentials', 'tor')['services'] onion_address = tor_services['web']['hostname'] openresty_site server_name do template 'nginx_conf_mastodon.erb' 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['openresty']['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 end