From 916dd17873470f6cfb8f50d2d55b146d6f0148c5 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 16:00:07 +0200 Subject: [PATCH 01/14] =?UTF-8?q?domains=5Ffrom=5Fenv=20=E2=86=92=20domain?= =?UTF-8?q?s=5Ffor=5Fstage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/deploy.rake | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/tasks/deploy.rake b/lib/tasks/deploy.rake index 749c22d74..06fa84348 100644 --- a/lib/tasks/deploy.rake +++ b/lib/tasks/deploy.rake @@ -1,16 +1,16 @@ -def domains_from_env(env) - case env +def domains_for_stage(stage) + case stage when 'dev' ['web1.dev', 'web2.dev'] when 'prod' ['web1', 'web2'] else - raise "STAGE #{env} is unknown. It must be either dev or prod" + raise "STAGE #{stage} is unknown. It must be either dev or prod." end end task :deploy do - domains = domains_from_env(ENV.fetch('STAGE')) + domains = domains_for_stage(ENV.fetch('STAGE')) branch = ENV.fetch('BRANCH') domains.each do |domain| @@ -19,7 +19,7 @@ task :deploy do end task :setup do - domains = domains_from_env(ENV['STAGE']) + domains = domains_for_stage(ENV.fetch('STAGE')) domains.each do |domain| sh "mina setup domain=#{domain} force_asset_precompile=true" From eb6132b5cbb0dba5e88c40ff718616dbe840ef71 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:15:36 +0200 Subject: [PATCH 02/14] Move the setup task before the deploy task --- lib/tasks/deploy.rake | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/tasks/deploy.rake b/lib/tasks/deploy.rake index 06fa84348..2bcc57a37 100644 --- a/lib/tasks/deploy.rake +++ b/lib/tasks/deploy.rake @@ -9,6 +9,14 @@ def domains_for_stage(stage) end end +task :setup do + domains = domains_for_stage(ENV.fetch('STAGE')) + + domains.each do |domain| + sh "mina setup domain=#{domain} force_asset_precompile=true" + end +end + task :deploy do domains = domains_for_stage(ENV.fetch('STAGE')) branch = ENV.fetch('BRANCH') @@ -17,11 +25,3 @@ task :deploy do sh "mina deploy domain=#{domain} branch=#{branch} force_asset_precompile=true" end end - -task :setup do - domains = domains_for_stage(ENV.fetch('STAGE')) - - domains.each do |domain| - sh "mina setup domain=#{domain} force_asset_precompile=true" - end -end From db540471d4bb518a2c27c6e77eed8dd667fdd06d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:02:37 +0200 Subject: [PATCH 03/14] Remove a duplicated comment --- config/deploy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/deploy.rb b/config/deploy.rb index 8e1dcedbe..dffb08808 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -30,7 +30,7 @@ set :shared_dirs, [ ] set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" -set :forward_agent, true # SSH forward_agent. +set :forward_agent, true puts "Deploy to #{ENV.fetch('domain')}, branch: #{ENV.fetch('branch')}" From 883c981165d0e46ba39ffbb2ad9d06d432413884 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:03:06 +0200 Subject: [PATCH 04/14] Group single-line parameter definitions --- config/deploy.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index dffb08808..363ab7ee4 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -19,6 +19,8 @@ deploy_to = '/var/www/ds' set :deploy_to, deploy_to set :user, 'ds' set :branch, ENV.fetch('branch') +set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" +set :forward_agent, true # Manually create these paths in shared/ (eg: shared/config/database.yml) in your server. # They will be linked in the 'deploy:link_shared_paths' step. @@ -29,9 +31,6 @@ set :shared_dirs, [ 'tmp/cache' ] -set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" -set :forward_agent, true - puts "Deploy to #{ENV.fetch('domain')}, branch: #{ENV.fetch('branch')}" # This task is the environment that is loaded for most commands, such as From fda1218a5f125578208dae4f5256933f18c00f55 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:03:57 +0200 Subject: [PATCH 05/14] Keep the same order between shared_dirs and setup --- config/deploy.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index 363ab7ee4..a64cb2ef3 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -39,14 +39,14 @@ task :setup do command %[mkdir -p "#{deploy_to}/shared/log"] command %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"] + command %[mkdir -p "#{deploy_to}/shared/sockets"] + command %[chmod g+rx,u+rwx "#{deploy_to}/shared/sockets"] + command %[mkdir -p "#{deploy_to}/shared/tmp/pids"] command %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"] command %[mkdir -p "#{deploy_to}/shared/tmp/cache"] command %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/cache"] - - command %[mkdir -p "#{deploy_to}/shared/sockets"] - command %[chmod g+rx,u+rwx "#{deploy_to}/shared/sockets"] end namespace :yarn do From b9c91cbab431de5a820895bfa21860b20318c2e4 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:06:18 +0200 Subject: [PATCH 06/14] Factorize some code --- config/deploy.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index a64cb2ef3..62f2c117d 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -24,29 +24,23 @@ set :forward_agent, true # Manually create these paths in shared/ (eg: shared/config/database.yml) in your server. # They will be linked in the 'deploy:link_shared_paths' step. -set :shared_dirs, [ +shared_dirs = [ 'log', 'sockets', 'tmp/pids', 'tmp/cache' ] +set :shared_dirs, shared_dirs puts "Deploy to #{ENV.fetch('domain')}, branch: #{ENV.fetch('branch')}" # This task is the environment that is loaded for most commands, such as # `mina deploy` or `mina rake`. task :setup do - command %[mkdir -p "#{deploy_to}/shared/log"] - command %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"] - - command %[mkdir -p "#{deploy_to}/shared/sockets"] - command %[chmod g+rx,u+rwx "#{deploy_to}/shared/sockets"] - - command %[mkdir -p "#{deploy_to}/shared/tmp/pids"] - command %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"] - - command %[mkdir -p "#{deploy_to}/shared/tmp/cache"] - command %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/cache"] + shared_dirs.each do |dir| + command %[mkdir -p "#{deploy_to}/shared/#{dir}"] + command %[chmod g+rx,u+rwx "#{deploy_to}/shared/#{dir}"] + end end namespace :yarn do From 70a3b9737cc3afcf397fab9078bde00b3cd5630d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:07:06 +0200 Subject: [PATCH 07/14] Improve a comment --- config/deploy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/deploy.rb b/config/deploy.rb index 62f2c117d..4fafb187c 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -22,7 +22,7 @@ set :branch, ENV.fetch('branch') set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" set :forward_agent, true -# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server. +# Manually create these paths in shared/ on your server. # They will be linked in the 'deploy:link_shared_paths' step. shared_dirs = [ 'log', From 755965306284914e42d1e9ad4696179241fe4dd5 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:20:38 +0200 Subject: [PATCH 08/14] Make the comments-code order match --- config/deploy.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index 4fafb187c..1cf20f58d 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -14,13 +14,13 @@ require 'mina/rbenv' # user - Username in the server to SSH to set :domain, ENV.fetch('domain') -set :repository, 'https://github.com/betagouv/tps.git' deploy_to = '/var/www/ds' set :deploy_to, deploy_to -set :user, 'ds' +set :repository, 'https://github.com/betagouv/tps.git' set :branch, ENV.fetch('branch') -set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" set :forward_agent, true +set :user, 'ds' +set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" # Manually create these paths in shared/ on your server. # They will be linked in the 'deploy:link_shared_paths' step. From 4026984852aa2e51cdeecffd2b67c0ce81f59a25 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:21:19 +0200 Subject: [PATCH 09/14] Move a comment in the comment block --- config/deploy.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index 1cf20f58d..fdb30022c 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -12,6 +12,8 @@ require 'mina/rbenv' # Advanced settings: # forward_agent - SSH forward_agent # user - Username in the server to SSH to +# shared_dirs - Manually create these paths in shared/ on your server. +# They will be linked in the 'deploy:link_shared_paths' step. set :domain, ENV.fetch('domain') deploy_to = '/var/www/ds' @@ -21,9 +23,6 @@ set :branch, ENV.fetch('branch') set :forward_agent, true set :user, 'ds' set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" - -# Manually create these paths in shared/ on your server. -# They will be linked in the 'deploy:link_shared_paths' step. shared_dirs = [ 'log', 'sockets', From 1c55a38e8146a89127b649ae739b896e3b3e7d51 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:21:45 +0200 Subject: [PATCH 10/14] Move variable definition to the top --- config/deploy.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index fdb30022c..cdd8be7c8 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -15,21 +15,22 @@ require 'mina/rbenv' # shared_dirs - Manually create these paths in shared/ on your server. # They will be linked in the 'deploy:link_shared_paths' step. -set :domain, ENV.fetch('domain') deploy_to = '/var/www/ds' -set :deploy_to, deploy_to -set :repository, 'https://github.com/betagouv/tps.git' -set :branch, ENV.fetch('branch') -set :forward_agent, true -set :user, 'ds' -set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" shared_dirs = [ 'log', 'sockets', 'tmp/pids', 'tmp/cache' ] + +set :domain, ENV.fetch('domain') +set :deploy_to, deploy_to +set :repository, 'https://github.com/betagouv/tps.git' +set :branch, ENV.fetch('branch') +set :forward_agent, true +set :user, 'ds' set :shared_dirs, shared_dirs +set :rbenv_path, "/home/ds/.rbenv/bin/rbenv" puts "Deploy to #{ENV.fetch('domain')}, branch: #{ENV.fetch('branch')}" From 21028bd97bfef0e9bbe84490329e31e696077be1 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:23:41 +0200 Subject: [PATCH 11/14] Extract a task to make things more atomic --- config/deploy.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config/deploy.rb b/config/deploy.rb index cdd8be7c8..0b4a91f29 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -69,6 +69,12 @@ namespace :service do command %{ echo "-----> Restarting puma service" #{echo_cmd %[sudo systemctl restart puma]} + } + end + + desc "Reload nginx" + task :reload_nginx do + command %{ echo "-----> Reloading nginx service" #{echo_cmd %[sudo systemctl reload nginx]} } @@ -101,6 +107,7 @@ task :deploy do on :launch do invoke :'service:restart_puma' + invoke :'service:reload_nginx' invoke :'service:restart_delayed_job' end end From f60ff023943deb9cbf0270fd1c71d206dee84a97 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:24:27 +0200 Subject: [PATCH 12/14] Improve readability --- config/deploy.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/deploy.rb b/config/deploy.rb index 0b4a91f29..0631cba0a 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -93,11 +93,12 @@ desc "Deploys the current version to the server." task :deploy do command 'export PATH=$PATH:/home/ds/.rbenv/bin:/home/ds/.rbenv/shims' command 'source /home/ds/.profile' + deploy do # Put things that will set up an empty directory into a fully set-up # instance of your project. - invoke :'git:clone' + invoke :'git:clone' invoke :'deploy:link_shared_paths' invoke :'bundle:install' invoke :'yarn:install' From a90232e57b364b60fc7bfd122591474a90312daf Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:25:31 +0200 Subject: [PATCH 13/14] Order require statements by alphabetic order --- config/deploy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/deploy.rb b/config/deploy.rb index 0631cba0a..3ddd77da1 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -1,6 +1,6 @@ require 'mina/bundler' -require 'mina/rails' require 'mina/git' +require 'mina/rails' require 'mina/rbenv' # Basic settings: From 5b32638502507757ddb50bf4eeca88b63a591a5d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 24 Oct 2018 14:26:25 +0200 Subject: [PATCH 14/14] Order shared_dirs by alphabetic order --- config/deploy.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index 3ddd77da1..5a36ae2ac 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -19,8 +19,8 @@ deploy_to = '/var/www/ds' shared_dirs = [ 'log', 'sockets', - 'tmp/pids', - 'tmp/cache' + 'tmp/cache', + 'tmp/pids' ] set :domain, ENV.fetch('domain')