Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stage env #384

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/credentials/staging.key

.DS_Store
5 changes: 5 additions & 0 deletions config/cable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions config/credentials/staging.yml.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KIOh8vYnKxAB1wyV8U25mS34JOt/vfB7daKw5XKpB+doD6aPkAA06yYKH3fKGu1bxwkR5NLFvQMkjJAeRdb+e+JJU3eZNJyDCNP0H7QTn1mjlTA3vcFbUWySYP3ucTN5ve3tAL12w8Nmtkp9GW9rBRL46P/fi53uC4TWWBUIecpAkiscOOwWW8Q39aoptjb+lFbuj1W+zkfU37PoZtGCu2VehnVBDhpRa0zSMUXuTLqmpWViOODB26okjNTYsb7YYMp3xTRPCOdztDYMu2AgQ4aIM/2kBKt1JX65QI9oO4TZlraKtJ4x7w2cUnoCUGWVpZOl9WNV9w4SxmgziXX2/zfcQfuMxTTgfpthxJfnUB/oH2UY/9vWXubmIg6xHIDs2NcZ2lAjjlfPXdrMfwAyNJVUkjSzIY7NuAoZOnZtdhYBBm0q3vFDRzzoZPRTVeEcgOUF92N1K0NH71ASVSQW7XOI1aeBVHoiZUoxUmVuHxnbQnQcWWKn8jX+TqmK0NGvnT7y0/dpTgL6EPRx5XFQ8gZcY0n5O+sWML1vFcvvbn6IdiNTevr/O3EVffscoM9mnoorhujheeE2Td18lZzVt1kdZ44ou4BV7NJxO30W1weORTDZQa8xleb/77fCxlaYxBeX--mdWfAduyads/TMA1--TvZoZ5j5FIAuZky00TgfNg==
4 changes: 4 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ test:
production:
<<: *default
url: <%= ENV.fetch('DATABASE_URL', nil) %>

staging:
<<: *default
url: <%= ENV.fetch('DATABASE_URL', nil) %>
4 changes: 0 additions & 4 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 5 additions & 1 deletion config/deploy/production.rb
Original file line number Diff line number Diff line change
@@ -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]
9 changes: 9 additions & 0 deletions config/deploy/staging.rb
Original file line number Diff line number Diff line change
@@ -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]
1 change: 1 addition & 0 deletions config/environments/staging.rb
2 changes: 1 addition & 1 deletion config/initializers/action_cable.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/action_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
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;
}

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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

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;
Expand All @@ -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;
}
}
48 changes: 48 additions & 0 deletions config/nginx/stage-ikt.codelabs.site.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
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
end

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`.
Expand Down
17 changes: 0 additions & 17 deletions config/systemd/application.service

This file was deleted.

17 changes: 17 additions & 0 deletions config/systemd/ikt.codelabs.site.service
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions config/systemd/stage-ikt.codelabs.site.service
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions lib/capistrano/tasks/application.rake
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down