# frozen_string_literal: true PG_CONFIG = "/usr/pgsql-9.3/bin/pg_config" def load_template(name) template_str = File.binread(File.join(File.dirname(__FILE__), name)) template = ERB.new(template_str, trim_mode: "-") template.result end def environments_dir "#{fetch :deploy_to}/shared/config/environments" end def secrets_config_file "#{fetch :deploy_to}/shared/config/secrets.yml" end def database_config_file "#{fetch :deploy_to}/shared/config/database.yml" end def rails_config_file "#{environments_dir}/#{fetch :stage}.rb" end def vhost_config_file "/etc/httpd/conf.d/#{fetch :application_name}.conf" end def vhost_nginx_config_file "/etc/nginx/sites-available/#{fetch :application_name}.conf" end def vhost_nginx_linked_file "/etc/nginx/sites-enabled/#{fetch :application_name}.conf" end def remote_shared_path(path) "#{fetch(:user)}@#{fetch(:domain)}:#{fetch(:shared_path)}/#{path}" end def active_storage_dir "#{fetch :deploy_to}/shared/storage" end FORCE_OVERWRITE = ENV.fetch("force_overwrite", nil) # rubocop:disable Metrics/BlockLength namespace :deploy_configure do desc "Create database and secrets" task create_configs: :remote_environment do load_project secrets_config = load_template("secrets_config.yml.erb") db_config = load_template("db_config.yml.erb") # TODO: Move environment variables to env.yml rails_config = load_template("rails_config.rb.erb") run :remote do command "mkdir -p #{environments_dir}" unless File.exist? environments_dir end if File.exist? "./config/secrets.yml" invoke :"deploy_configure:copy_secrets" end comment "Writing staging secrets to #{secrets_config_file}" comment "touch #{secrets_config_file}" run :remote do if !(File.exist? secrets_config_file) || FORCE_OVERWRITE command "echo \"#{secrets_config}\" >> #{secrets_config_file}" end end comment "Writing database config to #{database_config_file}" run :remote do if !(File.exist? database_config_file) || FORCE_OVERWRITE command "echo \"#{db_config}\" > #{database_config_file}" end end comment "Writing Rails config to #{rails_config_file}" run :remote do if !(File.exist? rails_config_file) || FORCE_OVERWRITE command "echo \"#{rails_config}\" > #{rails_config_file}" end end comment "Creating storage directory #{active_storage_dir}" run :remote do if !(File.exist? active_storage_dir) || FORCE_OVERWRITE command "mkdir -p #{active_storage_dir}" end end end desc "Copy secrets," task :copy_secrets do path = remote_shared_path("config/secrets.yml") comment "Copying secrets.yml to #{path}." run :local do if !(File.exist? path) || FORCE_OVERWRITE command = "scp ./config/secrets.yml #{path}" puts `#{command}` end end end end # rubocop:enable Metrics/BlockLength # rubocop:disable Metrics/BlockLength namespace :deploy_prepare do desc "Write the virtual host config file. May require an Apache restart." task :create_vhost do load_project vhost_file = vhost_config_file vhost_config = load_template("vhost_config.conf.erb") if fetch(:nginx) vhost_file = vhost_nginx_config_file vhost_config = load_template("vhost_config_nginx.conf.erb") command "sudo chown -R deploy:nginx /etc/nginx/sites-available" command "sudo chown -R deploy:nginx /etc/nginx/sites-enabled" end if !(File.exist? vhost_file) || FORCE_OVERWRITE comment "Writing virtual host config to #{vhost_file}" command "echo \"#{vhost_config}\" > #{vhost_file}" command "ln -s #{vhost_file} #{vhost_nginx_linked_file}" if fetch(:nginx) end end desc "Configure Postgres." task :configure_pg do comment "Configuring Postgres with config #{PG_CONFIG}" command "bundle config build.pg -- --with-pg-config=#{PG_CONFIG}" end desc "Set owner." task :set_owner do owner = fetch(:nginx) ? "nginx" : "apache" comment "Setting owner of #{fetch :deploy_to} #{owner}" command "sudo chgrp -R #{owner} #{fetch :deploy_to}" end end # rubocop:enable Metrics/BlockLength