require 'capistrano/dsl' require 'capistrano/cookbook/helpers/setup_config_values' require 'capistrano/cookbook/helpers/substitute_strings' require 'capistrano/cookbook/helpers/smart_template' require 'capistrano/cookbook/nginx' require 'capistrano/cookbook/monit' require 'securerandom' namespace :deploy do task :setup_config do conf = ::Capistrano::Cookbook::SetupConfigValues.new on roles(:app) do # make the config dir execute :mkdir, "-p #{shared_path}/config" execute :mkdir, "-p /home/#{fetch(:deploy_user)}/.config/systemd/user" # config files to be uploaded to shared/config, see the # definition of smart_template for details of operation. conf.config_files.each do |file| smart_template(file[:source], file[:destination], file[:as_root]) execute(:chmod, "+x #{file[:destination]}") if file[:executable] end # which of the above files should be marked as executable # conf.executable_config_files.each do |file| # execute :chmod, "+x #{shared_path}/config/#{file}" # end # symlink stuff which should be... symlinked # conf.symlinks.each do |symlink| # sudo "ln -nfs #{shared_path}/config/#{symlink[:source]} #{sub_strings(symlink[:link])}" # end if File.exists?(File.join('config', 'master.key')) upload! File.join('config', 'master.key'), File.join(shared_path, 'config', 'master.key') end end end end # remove the default nginx configuration as it will tend to conflict with our configs before 'deploy:setup_config', 'nginx:remove_default_vhost' # make sure that shared directories etc exist before running otherwise the # initial nginx reload won't work because of the nginx log file directory path # not existing before 'deploy:setup_config', 'deploy:check:directories' before 'deploy:setup_config', 'deploy:check:linked_dirs' # After setup config has generated and setup initial files, run the Capistrano Puma # tasks responsible for uploading config files. Note that `setup_config` creates overrides # for these in `config/deploy/templates` so we're not using the default ones from the gem after 'deploy:setup_config', 'puma:config' after 'deploy:setup_config', 'puma:nginx_config' after 'deploy:setup_config', 'puma:monit:config' after 'deploy:setup_config', 'puma:systemd:config' after 'deploy:setup_config', 'puma:systemd:enable' after 'deploy:setup_config', 'certbot:install' # Enable the sidekiq systemd service so that it's started automatically on (re)boot after 'deploy:setup_config', 'sidekiq:systemd:enable' if (defined?(Capistrano::Sidekiq) == 'constant' && Capistrano::Sidekiq.class == Class) # reload nginx to it will pick up any modified vhosts from setup_config after 'deploy:setup_config', 'nginx:reload' # Restart monit so it will pick up any monit configurations we've added after 'deploy:setup_config', 'monit:reload'