# 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, nil, "-") 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 remote_shared_path(path) "#{fetch(:user)}@#{fetch(:domain)}:#{fetch(:shared_path)}/#{path}" end def active_storage_dir "#{fetch :deploy_to}/shared/storage" end # 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}" 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 command "echo \"#{secrets_config}\" >> #{secrets_config_file}" end comment "Writing database config to #{database_config_file}" run :remote do command "echo \"#{db_config}\" > #{database_config_file}" end comment "Writing Rails config to #{rails_config_file}" run :remote do command "echo \"#{rails_config}\" > #{rails_config_file}" end comment "Creating storage directory #{active_storage_dir}" run :remote do command "mkdir -p #{active_storage_dir}" 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 command = "scp ./config/secrets.yml #{path}" puts `#{command}` end end end # rubocop:enable 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_config = load_template("vhost_config.conf.erb") comment "Writing virtual host config to #{vhost_config_file}" command "echo \"#{vhost_config}\" > #{vhost_config_file}" 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 comment "Setting owner of #{fetch :deploy_to} apache" command "sudo chgrp -R apache #{fetch :deploy_to}" end end