From 739b9135fa31c35f88b9a4ca285ba4f1812b1117 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 --- .gitignore | 1 + config/cable.yml | 5 ++ config/credentials/staging.yml.enc | 1 + config/database.yml | 4 ++ config/deploy.rb | 4 -- config/deploy/production.rb | 6 ++- config/deploy/staging.rb | 9 ++++ config/environments/staging.rb | 1 + config/initializers/action_cable.rb | 2 +- config/initializers/action_mailer.rb | 2 +- ...pplication.conf => ikt.codelabs.site.conf} | 19 ++++---- config/nginx/stage-ikt.codelabs.site.conf | 48 +++++++++++++++++++ config/puma.rb | 4 +- config/systemd/application.service | 17 ------- config/systemd/ikt.codelabs.site.service | 17 +++++++ .../systemd/stage-ikt.codelabs.site.service | 17 +++++++ lib/capistrano/tasks/application.rake | 8 ++-- 17 files changed, 127 insertions(+), 38 deletions(-) create mode 100644 config/credentials/staging.yml.enc 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 diff --git a/.gitignore b/.gitignore index de4d3455..ca2c89d2 100644 --- a/.gitignore +++ b/.gitignore @@ -27,5 +27,6 @@ # Ignore master key for decrypting credentials and more. /config/master.key +/config/credentials/staging.key .DS_Store diff --git a/config/cable.yml b/config/cable.yml index ce3a62d1..9b651423 100644 --- a/config/cable.yml +++ b/config/cable.yml @@ -8,3 +8,8 @@ production: adapter: redis url: <%= ENV.fetch 'REDIS_URL', 'redis://localhost:6379/1' %> channel_prefix: IKT_production + +stating: + adapter: redis + url: <%= ENV.fetch 'REDIS_URL', 'redis://localhost:6379/1' %> + channel_prefix: IKT_stating diff --git a/config/credentials/staging.yml.enc b/config/credentials/staging.yml.enc new file mode 100644 index 00000000..3bdab2f3 --- /dev/null +++ b/config/credentials/staging.yml.enc @@ -0,0 +1 @@ +D0nwCgGGKi4dRRKzXsDLy/WUYTjEfhlH32itOEDWnpqC4gvK+Rh46aOYw9OdXcBQzyS656Gko7PoxwFHrQd3cEkoYX6u80IDNueByrvYSZ96t6uO1o8mFACdOFsqHcTw6FVuDbjs8eZ7nlCYO8usF6XcXQVD4Y+gUWIEI5kffZd8+QcA23X0/qNiBJCo4HyEZy00nNFusPKVHU6dA+gqB0bI4stF0gvDw4dzO2M+RpyEK7dvbtAGnh2xjd9s6KPKhOkZYmlyr6L4hYpldjyJC2PHux3hVbBJcBEGERSegsNn1yRNIigji9GO8b/mdKij0XmRcTdbJb0Ee//BAt788rxhn7g9jtRRJarfLbeJ8y3jLzTfGG8nfRIh7Qzzj6Zj2SMGbecvl8LI3Trs4rrd+9Ox9ClUBr3P7UcRYMUOLoET9DGwwOLBlZfdAelJ+8Fyx1VooSkdZbE8EGPyBhrzSLiQ2+pPntni0JEAYIkMRa79sxI6K+ZaAOF9Sm+sucf8TSVMbZBtSPtwdgstN2BjBqHbMjI1VWBZmNI+6jFz1yaDacv81BJYY13nVAgIN4JtmpnvDQM9+G0j+2DeAMUe3VD6T2ezVtCAU3wywAjjw+tMb9cSoJ54GC4/9wFNxFYJVYrR--5CdebogqddXr/kbi--4Wb6gGOLohamWJAKQb6Lsw== \ No newline at end of file diff --git a/config/database.yml b/config/database.yml index 100660f5..0871f1eb 100644 --- a/config/database.yml +++ b/config/database.yml @@ -15,3 +15,7 @@ test: production: <<: *default url: <%= ENV.fetch('DATABASE_URL', nil) %> + +staging: + <<: *default + url: <%= ENV.fetch('DATABASE_URL', nil) %> diff --git a/config/deploy.rb b/config/deploy.rb index 34da4edb..a2d7ac00 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -2,16 +2,12 @@ set :repo_url, 'https://github.com/amnis-invictus/ikt.edu.vn.ua.git' -set :rails_env, :production - set :default_env, { path: '$HOME/.rbenv/shims:$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' diff --git a/config/deploy/production.rb b/config/deploy/production.rb index 43ddf39f..37558136 100644 --- a/config/deploy/production.rb +++ b/config/deploy/production.rb @@ -1,3 +1,7 @@ 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' + +set :rails_env, :production + +server 'ikt.codelabs.site:10017', user: 'arch-user', roles: %i[app web db] diff --git a/config/deploy/staging.rb b/config/deploy/staging.rb new file mode 100644 index 00000000..1b408d5e --- /dev/null +++ b/config/deploy/staging.rb @@ -0,0 +1,9 @@ +set :branch, :main + +set :deploy_to, '/opt/ai/stage-ikt.codelabs.site' + +set :rails_env, :staging + +append :linked_files, 'config/credentials/staging.key' + +server 'stage-ikt.codelabs.site:10016', user: 'arch-user', roles: %i[app web db] 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/initializers/action_cable.rb b/config/initializers/action_cable.rb index 1f6bd6d7..c0ab3e13 100644 --- a/config/initializers/action_cable.rb +++ b/config/initializers/action_cable.rb @@ -1,5 +1,5 @@ Rails.application.configure do - if Rails.env.production? + if %w[production staging].include? Rails.env config.action_cable.allowed_request_origins = ENV.fetch('ACTION_CABLE_ALLOWED_ORIGINS').split(';') else config.action_cable.disable_request_forgery_protection = true diff --git a/config/initializers/action_mailer.rb b/config/initializers/action_mailer.rb index a76652bc..23f6f4e9 100644 --- a/config/initializers/action_mailer.rb +++ b/config/initializers/action_mailer.rb @@ -1,6 +1,6 @@ Rails.application.configure do case Rails.env - when 'production' + when 'production', 'staging' host = ENV.fetch 'APPLICATION_HOST' config.action_mailer.smtp_settings = Rails.application.credentials.smtp_settings config.action_mailer.raise_delivery_errors = true 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/puma.rb b/config/puma.rb index c5ce8176..072199c7 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -12,7 +12,7 @@ worker_timeout 3600 if rails_env == 'development' environment rails_env -if rails_env == 'production' +if %w[production staging].include? rails_env bind ENV.fetch 'SOCKET' else port ENV.fetch 'PORT', 3000 @@ -20,7 +20,7 @@ pidfile ENV.fetch 'PIDFILE', 'tmp/pids/server.pid' -if rails_env == 'production' +if %w[production staging].include? rails_env # Specifies the number of `workers` to boot in clustered mode. # Workers are forked web server processes. If using threads and workers together # the concurrency of the application would be max `threads` * `workers`. 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..040b449d --- /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/shims:/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..074d2f53 --- /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/shims:/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/application.rake index 6e740eb9..43809208 100644 --- a/lib/capistrano/tasks/application.rake +++ b/lib/capistrano/tasks/application.rake @@ -1,9 +1,11 @@ +def service_name = fetch(:rails_env) == :staging ? 'stage-ikt.codelabs.site' : 'ikt.codelabs.site' + namespace :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, service_name end end end @@ -12,7 +14,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, service_name end end end @@ -21,7 +23,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, service_name end end end