From 617c7ef6160ea787136d2805bfe589b63892d9b2 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kostiuk Date: Sat, 16 Sep 2023 16:08:44 +0300 Subject: [PATCH] Add stating 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 | 4 ++ config/deploy/staging.rb | 9 ++++ config/environments/staging.rb | 1 + config/initializers/action_cable.rb | 2 +- config/initializers/action_mailer.rb | 2 +- config/nginx/stage-ikt.codelabs.site.conf | 48 +++++++++++++++++++ config/puma.rb | 4 +- .../systemd/stage-ikt.codelabs.site.service | 17 +++++++ lib/capistrano/tasks/application.rake | 8 ++-- 14 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 config/credentials/staging.yml.enc create mode 100644 config/deploy/staging.rb create mode 120000 config/environments/staging.rb create mode 100644 config/nginx/stage-ikt.codelabs.site.conf 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..7b4b968a --- /dev/null +++ b/config/credentials/staging.yml.enc @@ -0,0 +1 @@ +KIOh8vYnKxAB1wyV8U25mS34JOt/vfB7daKw5XKpB+doD6aPkAA06yYKH3fKGu1bxwkR5NLFvQMkjJAeRdb+e+JJU3eZNJyDCNP0H7QTn1mjlTA3vcFbUWySYP3ucTN5ve3tAL12w8Nmtkp9GW9rBRL46P/fi53uC4TWWBUIecpAkiscOOwWW8Q39aoptjb+lFbuj1W+zkfU37PoZtGCu2VehnVBDhpRa0zSMUXuTLqmpWViOODB26okjNTYsb7YYMp3xTRPCOdztDYMu2AgQ4aIM/2kBKt1JX65QI9oO4TZlraKtJ4x7w2cUnoCUGWVpZOl9WNV9w4SxmgziXX2/zfcQfuMxTTgfpthxJfnUB/oH2UY/9vWXubmIg6xHIDs2NcZ2lAjjlfPXdrMfwAyNJVUkjSzIY7NuAoZOnZtdhYBBm0q3vFDRzzoZPRTVeEcgOUF92N1K0NH71ASVSQW7XOI1aeBVHoiZUoxUmVuHxnbQnQcWWKn8jX+TqmK0NGvnT7y0/dpTgL6EPRx5XFQ8gZcY0n5O+sWML1vFcvvbn6IdiNTevr/O3EVffscoM9mnoorhujheeE2Td18lZzVt1kdZ44ou4BV7NJxO30W1weORTDZQa8xleb/77fCxlaYxBeX--mdWfAduyads/TMA1--TvZoZ5j5FIAuZky00TgfNg== \ 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 379070cf..37558136 100644 --- a/config/deploy/production.rb +++ b/config/deploy/production.rb @@ -1,3 +1,7 @@ set :branch, :main +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/stage-ikt.codelabs.site.conf b/config/nginx/stage-ikt.codelabs.site.conf new file mode 100644 index 00000000..bf99d3fe --- /dev/null +++ b/config/nginx/stage-ikt.codelabs.site.conf @@ -0,0 +1,48 @@ +upstream stage_ikt_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_ikt_backend { + proxy_pass http://stage_ikt_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_ikt_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_ikt_backend; + } + + location / { + try_files $uri @stage_ikt_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/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 41980663..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 ikt.codelabs.site' + 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 ikt.codelabs.site' + 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 ikt.codelabs.site' + execute :sudo, :systemctl, :restart, service_name end end end