templates/rails_app/config/deploy.rb.tt in railman-0.1.0 vs templates/rails_app/config/deploy.rb.tt in railman-0.1.1

- old
+ new

@@ -1,7 +1,7 @@ # Capistrano deployment tasks -lock '3.1.0' +lock '3.4.1' set :application, '<%= app_name %>' set :repo_url, '<%= @repository.origin %>' set :deploy_to, "/home/deploy/apps/#{fetch(:application)}" set :rbenv_home, '/home/deploy/.rbenv' @@ -12,51 +12,65 @@ %w(ln service start restart stop status).each do |cmd| SSHKit.config.command_map[cmd.to_sym] = "sudo #{cmd}" end SSHKit.config.command_map[:eye] = "#{fetch(:rbenv_home)}/shims/eye" SSHKit.config.command_map[:su_rm] = "sudo rm" -SSHKit.config.command_map[:letsencrypt] = "/opt/letsencrypt/letsencrypt-auto" desc "Setup rails application for the first time on a server" task :setup do on roles(:all) do with fetch(:environment) do - execute :git, :clone, fetch(:repo_url), fetch(:deploy_to) + if test "[ -d #{fetch(:deploy_to)} ]" + within fetch(:deploy_to) do + execute :git, :fetch, 'origin' + execute :git, :reset, '--hard origin/master' + end + else + execute :git, :clone, fetch(:repo_url), fetch(:deploy_to) + end + server_conf_dir = "#{fetch(:deploy_to)}/config/server" + execute :ln, "-s -f #{server_conf_dir}/nginx.conf /etc/nginx/conf.d/#{fetch(:application)}.conf" + execute :ln, "-s -f #{server_conf_dir}/letsencrypt.conf /etc/nginx/letsencrypt/#{fetch(:application)}.conf" + execute :ln, "-s -f #{server_conf_dir}/logrotate.conf /etc/logrotate.d/#{fetch(:application)}" within fetch(:deploy_to) do - execute :bundle, :install - execute :rake, 'db:create' - execute :psql, "-d #{fetch(:application)}_production", "-f db/#{fetch(:application)}.sql" - execute :rake, 'db:migrate' - execute :rake, 'assets:precompile' + execute :bundle, :install, "--without development test" execute :mkdir, "-p #{fetch(:deploy_to)}/tmp/pids" + if test "[ -f #{fetch(:deploy_to)}/.env ]" + execute :rake, 'db:create' + if test "[ -f #{fetch(:deploy_to)}/db/#{fetch(:application)}.sql ]" + execute :psql, "-d #{fetch(:application)}_production", "-f db/#{fetch(:application)}.sql" + end + execute :rake, 'db:migrate' + execute :rake, 'assets:precompile' + execute :eye, :load, 'Eyefile' + execute :eye, :start, fetch(:application) + execute :service, "nginx restart" + else + warn "TODO 1: Create .env on the server by copying from .env.example.production and modify your database and smtp settings." + warn "TODO 2: Create rails secret token with 'rake secret' and insert it into .env" + warn "TODO 3: Check if you have ssl certificates for #{fetch(:application)} in /etc/letsencrypt/live/<%= @domain %>" + warn "TODO 4: If not, create ssl certificates by running the following command as root: " + warn " /opt/letsencrypt/letsencrypt-auto --config /etc/nginx/letsencrypt/#{fetch(:application)}.conf --standalone certonly" + warn "TODO 5: Run 'cap ENV setup' again!" + end end - server_conf_dir = "#{fetch(:deploy_to)}/config/server" - execute :ln, "-s #{server_conf_dir}/nginx.conf /etc/nginx/conf.d/#{fetch(:application)}.conf" - execute :ln, "-s #{server_conf_dir}/letsencrypt.conf /etc/nginx/letsencrypt/#{fetch(:application)}.conf" - execute :ln, "-s #{server_conf_dir}/logrotate.conf /etc/logrotate.d/#{fetch(:application)}" - execute :eye, :load, 'Eyefile' - execute :eye, :start, fetch(:application) - execute :service, "nginx stop" - execute :letsencrypt, "--config /etc/nginx/letsencrypt/#{fetch(:application)}.conf --standalone certonly" - execute :service, "nginx start" end end end desc "Remove the application completely from the server" task :remove do on roles(:all) do with fetch(:environment) do within fetch(:deploy_to) do + execute :eye, :load, 'Eyefile' + execute :eye, :stop, fetch(:application) execute :rake, 'db:drop' + execute :su_rm, "-rf #{fetch(:deploy_to)}" end if test "[ -d #{fetch(:deploy_to)} ]" - execute :eye, :load, 'Eyefile' - execute :eye, :stop, fetch(:application) - execute :su_rm, "-f /etc/init/#{fetch(:application)}*" - execute :su_rm, "-rf #{fetch(:deploy_to)}" execute :su_rm, "-f /etc/nginx/conf.d/#{fetch(:application)}.conf" - execute :su_rm, "-f /etc/nginx/conf.d/#{fetch(:application)}_ssl.conf" + execute :su_rm, "-f /etc/nginx/letsencrypt/#{fetch(:application)}.conf" execute :su_rm, "-f /etc/logrotate.d/#{fetch(:application)}" execute :service, "nginx restart" end end end @@ -101,10 +115,12 @@ execute :eye, :stop, fetch(:application) execute :git, :fetch, 'origin' execute :git, :reset, '--hard origin/master' execute :rake, 'db:drop' execute :rake, 'db:create' - execute :psql, "-d #{fetch(:application)}_production", "-f db/#{fetch(:application)}.sql" + if test "[ -f #{fetch(:deploy_to)}/db/#{fetch(:application)}.sql ]" + execute :psql, "-d #{fetch(:application)}_production", "-f db/#{fetch(:application)}.sql" + end execute :rake, 'db:migrate' execute :eye, :start, fetch(:application) execute :service, "nginx restart" end end