Skip to content

Commit e982610

Browse files
sawkasalsemyonov
authored andcommitted
adminos reinstall
1 parent 895babd commit e982610

18 files changed

+383
-59
lines changed

Capfile

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ require 'capistrano/setup'
33

44
# Include default deployment tasks
55
require 'capistrano/deploy'
6+
require 'capistrano/scm/git'
7+
require 'capistrano/sidekiq'
68
require 'capistrano/rvm'
79
require 'capistrano/bundler'
810
require 'capistrano/rails'
911
require 'capistrano/puma'
1012
require 'capistrano-db-tasks'
11-
require 'capistrano/scm/git'
12-
require 'capistrano/sidekiq'
1313
require 'whenever/capistrano'
14+
require 'capistrano/systemd/multiservice'
1415

15-
install_plugin Capistrano::Puma
1616
install_plugin Capistrano::SCM::Git
17+
install_plugin Capistrano::Puma
18+
install_plugin Capistrano::Puma::Nginx
19+
install_plugin Capistrano::Systemd::MultiService.new_service('puma')
20+
install_plugin Capistrano::Systemd::MultiService.new_service('sidekiq')
1721

22+
lib_dir = File.join(__dir__, 'lib')
23+
$LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
1824
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
1925
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
20-
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }

Gemfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ group :development, :test do
5353
gem 'bundler-audit' # from adminos
5454
gem 'minitest' # from adminos
5555
gem 'rspec-rails' # from adminos
56+
gem 'database_cleaner' # from adminos
57+
gem 'factory_bot_rails' # from adminos
58+
gem 'faker' # from adminos
5659
end
5760

5861
group :development do
@@ -71,6 +74,7 @@ group :development do
7174
gem 'guard-livereload', '~> 2.5', require: false # from adminos
7275
gem 'capistrano3-puma'
7376
gem 'capistrano-sidekiq'
77+
gem 'capistrano-systemd-multiservice', '~> 0.1.0.beta6', require: false # from adminos
7478
end
7579

7680
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
@@ -82,9 +86,6 @@ gem 'role_model' # from adminos
8286
gem 'webpacker', '~> 3.5' # from adminos
8387
gem 'actiontext', github: 'rails/actiontext', require: 'action_text', ref: 'cfe4674d3637c746cdb3c2b5131e2de498775529' # from adminos
8488
gem 'image_processing', '~> 1.2' # for Active Storage variants # from adminos
85-
group :production, :staging do
86-
gem 'unicorn' # from adminos
87-
end
8889
gem 'whenever', require: false # from adminos
8990
gem 'capistrano-db-tasks', require: false # from adminos
9091
gem 'sanitize' # from adminos

Gemfile.lock

+10-10
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ GEM
114114
capistrano-sidekiq (1.0.2)
115115
capistrano (>= 3.9.0)
116116
sidekiq (>= 3.4)
117+
capistrano-systemd-multiservice (0.1.0.beta7)
118+
capistrano (>= 3.7.0, < 3.12.0)
117119
capistrano3-puma (3.1.1)
118120
capistrano (~> 3.7)
119121
capistrano-bundler
@@ -154,6 +156,8 @@ GEM
154156
factory_bot_rails (4.11.1)
155157
factory_bot (~> 4.11.1)
156158
railties (>= 3.0.0)
159+
faker (1.9.1)
160+
i18n (>= 0.7)
157161
faraday (0.15.4)
158162
multipart-post (>= 1.2, < 3)
159163
ffaker (2.10.0)
@@ -212,7 +216,6 @@ GEM
212216
activerecord
213217
kaminari-core (= 1.1.1)
214218
kaminari-core (1.1.1)
215-
kgio (2.11.2)
216219
listen (3.1.5)
217220
rb-fsevent (~> 0.9, >= 0.9.4)
218221
rb-inotify (~> 0.9, >= 0.9.7)
@@ -322,17 +325,16 @@ GEM
322325
rake (>= 0.8.7)
323326
thor (>= 0.19.0, < 2.0)
324327
rainbow (3.0.0)
325-
raindrops (0.19.0)
326328
rake (12.3.2)
327329
rb-fsevent (0.10.3)
328330
rb-inotify (0.10.0)
329331
ffi (~> 1.0)
330332
redis (4.1.0)
331333
request_store (1.4.1)
332334
rack (>= 1.4)
333-
responders (2.4.0)
334-
actionpack (>= 4.2.0, < 5.3)
335-
railties (>= 4.2.0, < 5.3)
335+
responders (2.4.1)
336+
actionpack (>= 4.2.0, < 6.0)
337+
railties (>= 4.2.0, < 6.0)
336338
rodf (1.0.0)
337339
activesupport (>= 3.0)
338340
builder (>= 3.0)
@@ -436,9 +438,6 @@ GEM
436438
uglifier (4.1.20)
437439
execjs (>= 0.3.0, < 3)
438440
unicode-display_width (1.4.1)
439-
unicorn (5.4.1)
440-
kgio (~> 2.6)
441-
raindrops (~> 0.7)
442441
warden (1.2.8)
443442
rack (>= 2.0.6)
444443
web-console (3.7.0)
@@ -472,10 +471,12 @@ DEPENDENCIES
472471
capistrano-rails
473472
capistrano-rvm
474473
capistrano-sidekiq
474+
capistrano-systemd-multiservice (~> 0.1.0.beta6)
475475
capistrano3-puma
476476
coffee-rails (~> 4.2)
477477
database_cleaner
478478
factory_bot_rails
479+
faker
479480
ffaker
480481
globalize
481482
guard
@@ -508,7 +509,6 @@ DEPENDENCIES
508509
turbolinks (~> 5)
509510
tzinfo-data
510511
uglifier (>= 1.3.0)
511-
unicorn
512512
web-console (>= 3.3.0)
513513
webpacker (~> 3.5)
514514
whenever
@@ -517,4 +517,4 @@ RUBY VERSION
517517
ruby 2.5.1p57
518518

519519
BUNDLED WITH
520-
1.16.5
520+
1.17.1

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,25 @@ rails db:create db:setup
2222
rails s
2323
./bin/webpack-dev-server
2424
```
25+
26+
## Deployment
27+
28+
For painless deployment, you should configure several sudo commands for specified deploy user
29+
to be executed without asking for password via `sudo visudo` command,
30+
as described in https://capistranorb.com/documentation/getting-started/authentication-and-authorisation/#authorisation
31+
32+
Full list of sudo command could be obtained by running commands mentioned below with `--dry-run` option
33+
(e. g. `bin/cap --dry-run {stage_name} deploy:setup`
34+
35+
1. Set up new deployment in `config/stages/{stage_name}.rb` file
36+
1. Set up new deployment (upload configurations):
37+
38+
`bin/cap {stage_name} deploy:setup`
39+
40+
1. Deploy new version to `{stage_name}`:
41+
42+
`bin/cap {stage_name} deploy`
43+
44+
2. On new deployment (or when systemd templates updated), update & enable systemd services for Puma & Sidekiq:
45+
46+
`bin/cap {stage_name} deploy:setup_systemd`

bin/puma

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'puma' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
require "pathname"
12+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13+
Pathname.new(__FILE__).realpath)
14+
15+
bundle_binstub = File.expand_path("../bundle", __FILE__)
16+
17+
if File.file?(bundle_binstub)
18+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19+
load(bundle_binstub)
20+
else
21+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23+
end
24+
end
25+
26+
require "rubygems"
27+
require "bundler/setup"
28+
29+
load Gem.bin_path("puma", "puma")

bin/pumactl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'pumactl' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
require "pathname"
12+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13+
Pathname.new(__FILE__).realpath)
14+
15+
bundle_binstub = File.expand_path("../bundle", __FILE__)
16+
17+
if File.file?(bundle_binstub)
18+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19+
load(bundle_binstub)
20+
else
21+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23+
end
24+
end
25+
26+
require "rubygems"
27+
require "bundler/setup"
28+
29+
load Gem.bin_path("puma", "pumactl")

config/deploy.rb

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
set :application, 'demo_adminos'
22
set :repo_url, "[email protected]:abuhtoyarov/demo-adminos.git"
33

4-
# Default value for :linked_files is []
5-
set :linked_files, fetch(:linked_files, []).push('config/database.yml', '.env', 'config/master.key')
6-
# Default value for linked_dirs is []
7-
set :linked_dirs, fetch(:linked_dirs, []).push(
8-
'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'storage'
9-
)
4+
set :config_files, %w[config/database.yml .env]
5+
append :linked_files, 'config/database.yml', '.env', 'config/master.key'
6+
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets',
7+
'vendor/bundle', 'public/system', 'public/uploads'
108

119
set :rvm_ruby_version, Pathname(__dir__).join('../.ruby-version').read.chomp
1210
append :rvm_map_bins, 'puma', 'pumactl', 'sidekiq', 'sidekiqctl'
11+
12+
set :db_local_clean, true
13+
set :assets_dir, %w[public/system]
14+
15+
set :nginx_server_name, -> { "#{fetch(:domain)} localhost #{fetch(:application)}.local" }
16+
set :nginx_upstream_name, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
17+
set :nginx_config_name, -> { "#{fetch(:domain)}.conf" }
18+
set :nginx_use_ssl, true
19+
20+
namespace :deploy do
21+
# after :restart, 'systemd:sidekiq:reload-or-restart'
22+
after :publishing, :restart
23+
end

config/deploy/production.rb

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
set :branch, :master
55
set :deploy_to, '/home/app/demo_adminos'
66
set :whenever_path, ->{ release_path }
7+
8+
set :nginx_sites_available_path, "#{fetch(:deploy_to)}/shared"
9+
set :nginx_sites_enabled_path, '/etc/nginx/conf.d'

config/deploy/staging.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
server "#{fetch(:application)}.staging.molinos.ru", user: 'app', roles: %w(web app db), primary: true
2+
3+
set :domain, "#{fetch(:application)}.staging.molinos.ru"
4+
set :keep_releases, 5
5+
set :rails_env, 'staging'
6+
set :branch, :develop
7+
8+
set :user, :app
9+
set :deploy_to, "/home/#{fetch(:user)}/#{fetch(:application)}"
10+
11+
# CentOS
12+
set :nginx_sites_available_path, "#{fetch(:deploy_to)}/shared"
13+
set :nginx_sites_enabled_path, '/etc/nginx/conf.d'
14+
15+
# Ubuntu
16+
# set :nginx_sites_available_path, "/etc/nginx/sites-available"
17+
# set :nginx_sites_enabled_path, "/etc/nginx/sites-enabled"
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<%= fetch(:rails_env) %>:
2+
adapter: postgresql
3+
timeout: 5000
4+
encoding: utf8
5+
reconnect: false
6+
database: <%= "#{fetch(:application)}_#{fetch(:rails_env)}" %>
7+
pool: 5
8+
username: <%= "dbu_#{fetch(:user)}" %>
9+
password:
10+
host:
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
upstream puma_<%= fetch(:nginx_upstream_name) %> { <%
2+
@backends = [fetch(:puma_bind)].flatten.map do |m|
3+
etype, address = /(tcp|unix|ssl):\/{1,2}(.+)/.match(m).captures
4+
if etype == 'unix'
5+
"server #{etype}:#{address} #{fetch(:nginx_socket_flags)};"
6+
else
7+
"server #{address.gsub(/0\.0\.0\.0(.+)/, "127.0.0.1\\1")} #{fetch(:nginx_http_flags)};"
8+
end
9+
end
10+
%><% @backends.each do |server| %>
11+
<%= server %><% end %>
12+
}
13+
<% if fetch(:nginx_use_ssl) -%>
14+
server {
15+
listen 80;
16+
server_name <%= fetch(:nginx_server_name) %>;
17+
return 301 https://$host$1$request_uri;
18+
}
19+
<% end -%>
20+
21+
server {
22+
<% if fetch(:nginx_use_ssl) -%>
23+
listen 443;
24+
ssl on;
25+
<% if fetch(:nginx_ssl_certificate) -%>
26+
ssl_certificate <%= fetch(:nginx_ssl_certificate) %>;
27+
<% else -%>
28+
ssl_certificate /etc/letsencrypt/live/<%= fetch(:domain) %>/fullchain.pem;
29+
<% end -%>
30+
<% if fetch(:nginx_ssl_certificate_key) -%>
31+
ssl_certificate_key <%= fetch(:nginx_ssl_certificate_key) %>;
32+
<% else -%>
33+
ssl_certificate_key /etc/letsencrypt/live/<%= fetch(:domain) %>/privkey.pem;
34+
<% end -%>
35+
include /etc/letsencrypt/options-ssl-nginx.conf;
36+
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
37+
<% else -%>
38+
listen 80;
39+
<% end -%>
40+
server_name <%= fetch(:nginx_server_name) %>;
41+
root <%= current_path %>/public;
42+
try_files $uri/index.html $uri @puma_<%= fetch(:nginx_upstream_name) %>;
43+
44+
client_max_body_size 4G;
45+
keepalive_timeout 10;
46+
47+
error_page 500 502 504 /500.html;
48+
error_page 503 @503;
49+
50+
location @puma_<%= fetch(:nginx_upstream_name) %> {
51+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
52+
proxy_set_header Host $host;
53+
proxy_redirect off;
54+
proxy_set_header Upgrade $http_upgrade;
55+
proxy_set_header Connection "Upgrade";
56+
<% if fetch(:nginx_use_ssl) -%>
57+
proxy_set_header X-Forwarded-Proto https;
58+
<% else -%>
59+
proxy_set_header X-Forwarded-Proto http;
60+
<% end -%>
61+
proxy_pass http://puma_<%= fetch(:nginx_upstream_name) %>;
62+
# limit_req zone=one;
63+
access_log <%= shared_path %>/log/nginx.access.log;
64+
error_log <%= shared_path %>/log/nginx.error.log;
65+
}
66+
67+
location ^~ /assets/ {
68+
gzip_static on;
69+
expires max;
70+
add_header Cache-Control public;
71+
}
72+
73+
location = /50x.html {
74+
root html;
75+
}
76+
77+
location = /404.html {
78+
root html;
79+
}
80+
81+
location @503 {
82+
error_page 405 = /system/maintenance.html;
83+
if (-f $document_root/system/maintenance.html) {
84+
rewrite ^(.*)$ /system/maintenance.html break;
85+
}
86+
rewrite ^(.*)$ /503.html break;
87+
}
88+
89+
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
90+
return 405;
91+
}
92+
93+
if (-f $document_root/system/maintenance.html) {
94+
return 503;
95+
}
96+
}

0 commit comments

Comments
 (0)