From bc90cfa7ea97ff560f0e0ec940901bfec7cedcef Mon Sep 17 00:00:00 2001 From: Kostiantyn Kostiuk Date: Sun, 10 Sep 2023 19:38:14 +0300 Subject: [PATCH] Add stage env Signed-off-by: Kostiantyn Kostiuk --- config/deploy.rb | 10 +--- config/deploy/production.rb | 10 +++- config/deploy/staging.rb | 13 +++++ config/environments/staging.rb | 1 + ...pplication.conf => ikt.codelabs.site.conf} | 19 ++++---- config/nginx/stage-ikt.codelabs.site.conf | 48 +++++++++++++++++++ config/systemd/application.service | 17 ------- config/systemd/ikt.codelabs.site.service | 17 +++++++ .../systemd/stage-ikt.codelabs.site.service | 17 +++++++ ...{application.rake => ikt_application.rake} | 8 ++-- lib/capistrano/tasks/stage_application.rake | 28 +++++++++++ 11 files changed, 148 insertions(+), 40 deletions(-) create mode 100644 config/deploy/staging.rb create mode 120000 config/environments/staging.rb rename config/nginx/{application.conf => ikt.codelabs.site.conf} (66%) create mode 100644 config/nginx/stage-ikt.codelabs.site.conf delete mode 100644 config/systemd/application.service create mode 100644 config/systemd/ikt.codelabs.site.service create mode 100644 config/systemd/stage-ikt.codelabs.site.service rename lib/capistrano/tasks/{application.rake => ikt_application.rake} (68%) create mode 100644 lib/capistrano/tasks/stage_application.rake diff --git a/config/deploy.rb b/config/deploy.rb index 34da4edb..cd25b8ab 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -4,16 +4,8 @@ set :rails_env, :production -set :default_env, { path: '$HOME/.rbenv/shims:$PATH' } +set :default_env, { path: '$HOME/.rbenv/versions/3.1.2/bin:$PATH' } append :linked_files, '.env', 'config/master.key' append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system', 'storage' - -set :deploy_to, '/opt/ai/application' - -namespace :deploy do - after :finishing, 'application:restart' - after :finishing, 'nginx:reload' - after :finishing, 'bundler:clean' -end diff --git a/config/deploy/production.rb b/config/deploy/production.rb index 43ddf39f..6418b149 100644 --- a/config/deploy/production.rb +++ b/config/deploy/production.rb @@ -1,3 +1,11 @@ set :branch, :main -server 'ikt.edu.vn.ua:10017', user: 'arch-user', roles: %i[app web db] +set :deploy_to, '/opt/ai/ikt.codelabs.site' + +server 'ikt.codelabs.site:10017', user: 'arch-user', roles: %i[app web db] + +namespace :deploy do + after :finishing, 'ikt_application:restart' + after :finishing, 'nginx:reload' + after :finishing, 'bundler:clean' +end diff --git a/config/deploy/staging.rb b/config/deploy/staging.rb new file mode 100644 index 00000000..7b7a3745 --- /dev/null +++ b/config/deploy/staging.rb @@ -0,0 +1,13 @@ +set :branch, :main + +set :deploy_to, '/opt/ai/stage-ikt.codelabs.site' + +append :linked_files, 'config/credentials/staging.key' + +server 'stage-ikt.codelabs.site:10016', user: 'arch-user', roles: %i[app web db] + +namespace :deploy do + after :finishing, 'stage_application:restart' + after :finishing, 'nginx:reload' + after :finishing, 'bundler:clean' +end diff --git a/config/environments/staging.rb b/config/environments/staging.rb new file mode 120000 index 00000000..293afae8 --- /dev/null +++ b/config/environments/staging.rb @@ -0,0 +1 @@ +production.rb \ No newline at end of file diff --git a/config/nginx/application.conf b/config/nginx/ikt.codelabs.site.conf similarity index 66% rename from config/nginx/application.conf rename to config/nginx/ikt.codelabs.site.conf index d469e564..0487a983 100644 --- a/config/nginx/application.conf +++ b/config/nginx/ikt.codelabs.site.conf @@ -1,9 +1,10 @@ -upstream backend { - server unix:///opt/ai/application/current/tmp/sockets/server.sock fail_timeout=0; +upstream ikt_backend { + server unix:///opt/ai/ikt.codelabs.site/current/tmp/sockets/server.sock fail_timeout=0; } server { listen *:80; + server_name ikt.edu.vn.ua ikt.codelabs.site; return 301 https://$host$request_uri; } @@ -11,16 +12,16 @@ server { listen *:443; server_name ikt.edu.vn.ua ikt.codelabs.site; client_max_body_size 1024M; - root /opt/ai/application/current/public/; - error_log /opt/ai/application/current/log/nginx_errors.log; + root /opt/ai/ikt.codelabs.site/current/public/; + error_log /opt/ai/ikt.codelabs.site/current/log/nginx_errors.log; gzip on; gzip_comp_level 6; gzip_proxied any; gzip_types text/plain text/css application/javascript application/octet-stream; - location @backend { - proxy_pass http://backend; + location @ikt_backend { + proxy_pass http://ikt_backend; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; @@ -28,7 +29,7 @@ server { } location /cable { - proxy_pass http://backend; + proxy_pass http://ikt_backend; proxy_set_header Connection Upgrade; proxy_set_header Upgrade websocket; proxy_set_header Host $host; @@ -38,10 +39,10 @@ server { location /assets { expires max; - try_files $uri @backend; + try_files $uri @ikt_backend; } location / { - try_files $uri @backend; + try_files $uri @ikt_backend; } } diff --git a/config/nginx/stage-ikt.codelabs.site.conf b/config/nginx/stage-ikt.codelabs.site.conf new file mode 100644 index 00000000..d7c65b7d --- /dev/null +++ b/config/nginx/stage-ikt.codelabs.site.conf @@ -0,0 +1,48 @@ +upstream stage_backend { + server unix:///opt/ai/stage-ikt.codelabs.site/current/tmp/sockets/server.sock fail_timeout=0; +} + +server { + listen *:80; + server_name stage-ikt.codelabs.site; + return 301 https://$host$request_uri; +} + +server { + listen *:443; + server_name stage-ikt.codelabs.site; + client_max_body_size 1024M; + root /opt/ai/stage-ikt.codelabs.site/current/public/; + error_log /opt/ai/stage-ikt.codelabs.site/current/log/nginx_errors.log; + + gzip on; + gzip_comp_level 6; + gzip_proxied any; + gzip_types text/plain text/css application/javascript application/octet-stream; + + location @stage_backend { + proxy_pass http://stage_backend; + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + location /cable { + proxy_pass http://stage_backend; + proxy_set_header Connection Upgrade; + proxy_set_header Upgrade websocket; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + location /assets { + expires max; + try_files $uri @stage_backend; + } + + location / { + try_files $uri @stage_backend; + } +} diff --git a/config/systemd/application.service b/config/systemd/application.service deleted file mode 100644 index 0b1adb40..00000000 --- a/config/systemd/application.service +++ /dev/null @@ -1,17 +0,0 @@ -[Unit] - Description = IKT Application - After = network.service - -[Service] - User = arch-user - Group = arch-user - WorkingDirectory = /opt/ai/application/current - Environment = 'RAILS_ENV=production' - Environment = 'SOCKET=unix:///opt/ai/application/current/tmp/sockets/server.sock' - Environment = 'PIDFILE=/opt/ai/application/current/tmp/pids/server.pid' - Environment = 'PATH=/home/arch-user/.rbenv/shims:/usr/local/sbin:/usr/local/bin:/usr/bin' - ExecStart = /usr/bin/env bundle exec puma --config /opt/ai/application/current/config/puma.rb - Restart = always - -[Install] - WantedBy = multi-user.target diff --git a/config/systemd/ikt.codelabs.site.service b/config/systemd/ikt.codelabs.site.service new file mode 100644 index 00000000..72177cdf --- /dev/null +++ b/config/systemd/ikt.codelabs.site.service @@ -0,0 +1,17 @@ +[Unit] + Description = IKT Application + After = network.service + +[Service] + User = arch-user + Group = arch-user + WorkingDirectory = /opt/ai/ikt.codelabs.site/current + Environment = 'RAILS_ENV=production' + Environment = 'SOCKET=unix:///opt/ai/ikt.codelabs.site/current/tmp/sockets/server.sock' + Environment = 'PIDFILE=/opt/ai/ikt.codelabs.site/current/tmp/pids/server.pid' + Environment = 'PATH=/home/arch-user/.rbenv/versions/3.1.2/bin:/usr/local/sbin:/usr/local/bin:/usr/bin' + ExecStart = /usr/bin/env bundle exec puma --config /opt/ai/ikt.codelabs.site/current/config/puma.rb + Restart = always + +[Install] + WantedBy = multi-user.target diff --git a/config/systemd/stage-ikt.codelabs.site.service b/config/systemd/stage-ikt.codelabs.site.service new file mode 100644 index 00000000..3d1bfae2 --- /dev/null +++ b/config/systemd/stage-ikt.codelabs.site.service @@ -0,0 +1,17 @@ +[Unit] + Description = Stage IKT Application + After = network.service + +[Service] + User = arch-user + Group = arch-user + WorkingDirectory = /opt/ai/stage-ikt.codelabs.site/current + Environment = 'RAILS_ENV=staging' + Environment = 'SOCKET=unix:///opt/ai/stage-ikt.codelabs.site/current/tmp/sockets/server.sock' + Environment = 'PIDFILE=/opt/ai/stage-ikt.codelabs.site/current/tmp/pids/server.pid' + Environment = 'PATH=/home/arch-user/.rbenv/versions/3.1.2/bin:/usr/local/sbin:/usr/local/bin:/usr/bin' + ExecStart = /usr/bin/env bundle exec puma --config /opt/ai/stage-ikt.codelabs.site/current/config/puma.rb + Restart = always + +[Install] + WantedBy = multi-user.target diff --git a/lib/capistrano/tasks/application.rake b/lib/capistrano/tasks/ikt_application.rake similarity index 68% rename from lib/capistrano/tasks/application.rake rename to lib/capistrano/tasks/ikt_application.rake index 6e740eb9..f58899c8 100644 --- a/lib/capistrano/tasks/application.rake +++ b/lib/capistrano/tasks/ikt_application.rake @@ -1,9 +1,9 @@ -namespace :application do +namespace :ikt_application do desc 'Start application' task :start do on roles(:app), in: :sequence, wait: 5 do within current_path do - execute 'sudo systemctl start application' + execute 'sudo systemctl start ikt.codelabs.site' end end end @@ -12,7 +12,7 @@ namespace :application do task :stop do on roles(:app), in: :sequence, wait: 5 do within current_path do - execute 'sudo systemctl stop application' + execute 'sudo systemctl stop ikt.codelabs.site' end end end @@ -21,7 +21,7 @@ namespace :application do task :restart do on roles(:app), in: :sequence, wait: 5 do within current_path do - execute 'sudo systemctl restart application' + execute 'sudo systemctl restart ikt.codelabs.site' end end end diff --git a/lib/capistrano/tasks/stage_application.rake b/lib/capistrano/tasks/stage_application.rake new file mode 100644 index 00000000..28ac23b1 --- /dev/null +++ b/lib/capistrano/tasks/stage_application.rake @@ -0,0 +1,28 @@ +namespace :stage_application do + desc 'Start application' + task :start do + on roles(:app), in: :sequence, wait: 5 do + within current_path do + execute 'sudo systemctl start stage-ikt.codelabs.site' + end + end + end + + desc 'Stop application' + task :stop do + on roles(:app), in: :sequence, wait: 5 do + within current_path do + execute 'sudo systemctl stop stage-ikt.codelabs.site' + end + end + end + + desc 'Restart application' + task :restart do + on roles(:app), in: :sequence, wait: 5 do + within current_path do + execute 'sudo systemctl restart stage-ikt.codelabs.site' + end + end + end +end