#
# Cookbook:: kosmos_assets
# Recipe:: nginx_site
#

include_recipe "kosmos-nginx"

domain = node["kosmos_assets"]["domain"]

nginx_certbot_site domain

directory "/var/www/#{domain}/site" do
  user node["nginx"]["user"]
  group node["nginx"]["group"]
  mode "0755"
end

git "/var/www/#{domain}/site" do
  user node["nginx"]["user"]
  group node["nginx"]["group"]
  repository node["kosmos_assets"]["repo"]
  revision node["kosmos_assets"]["revision"]
  action :sync
end

template "#{node["nginx"]["dir"]}/sites-available/#{domain}" do
  source "nginx_conf_assets.erb"
  owner node["nginx"]["user"]
  mode 0640
  variables domain: domain,
            ssl_cert:    "/etc/letsencrypt/live/#{domain}/fullchain.pem",
            ssl_key:     "/etc/letsencrypt/live/#{domain}/privkey.pem"
  notifies :reload, "service[nginx]", :delayed
end

nginx_site domain do
  action :enable
end
