Skip to content

Commit 935e006

Browse files
Merge pull request #18 from TalkingQuickly/feature/refactor
Feature/refactor
2 parents 99d8aa5 + 7fb6a2d commit 935e006

18 files changed

+110
-71
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
## 5.0.1 (March 2021)
4+
5+
- Adds full support for deploy (but not config creation) without sudo access
6+
- Refactor config files to use single array of hashes with flags
7+
- Refactor Sidekiq and Monit configurations to copy files directly rather than using symlinks to avoid potential root access leak
8+
- Fixes bug where object identifier was outputted in logs rather than filename
9+
- Fixes nginx not being reloaded after `setup_config` due to shared log directory not yet existing
10+
11+
## 5.0.0 (March 2021)
12+
13+
- Full overhaul to support Rails 6 and Ubuntu 20.04

capistrano-cookbook.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
1919

2020
spec.add_dependency 'capistrano', '~> 3.16'
2121
spec.add_dependency 'capistrano3-puma', '~> 5.0.4'
22+
spec.add_dependency 'capistrano-sidekiq', '~> 2.0'
2223

2324
spec.add_development_dependency "bundler", "~> 1.5"
2425
spec.add_development_dependency "rake"

lib/capistrano/cookbook.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Cookbook
99
require 'capistrano/cookbook/run_tests'
1010
require 'capistrano/cookbook/setup_config'
1111
require 'capistrano/cookbook/create_database'
12-
require 'capistrano/cookbook/systemd'
12+
require 'capistrano/cookbook/puma_systemd'
13+
require 'capistrano/cookbook/sidekiq_systemd'
1314
end
1415
end

lib/capistrano/cookbook/helpers/setup_config_values.rb

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,46 @@
11
module Capistrano
22
module Cookbook
33
class SetupConfigValues
4-
def symlinks
5-
fetch(:symlinks) || symlinks_defaults
6-
end
7-
8-
def executable_config_files
9-
fetch(:executable_config_files) || executable_config_files_defaults
10-
end
11-
124
def config_files
135
fetch(:config_files) || config_files_defaults
146
end
157

168
private
179

18-
def symlinks_defaults
10+
def config_files_defaults
1911
base = [
2012
{
21-
source: "log_rotation",
22-
link: "/etc/logrotate.d/{{full_app_name}}"
13+
source: 'log_rotation',
14+
destination: "/etc/logrotate.d/#{fetch(:full_app_name)}",
15+
executable: false,
16+
as_root: true
17+
},
18+
{
19+
source: 'database.example.yml',
20+
destination: "#{shared_path}/config/database.example.yml",
21+
executable: false,
22+
as_root: false
2323
}
2424
]
25+
2526
return base unless sidekiq_enabled?
2627

2728
base + [
2829
{
29-
source: "sidekiq.service.capistrano",
30-
link: "/etc/systemd/system/#{fetch(:sidekiq_service_unit_name)}.service"
30+
source: 'sidekiq.service.capistrano',
31+
destination: "/home/#{fetch(:deploy_user)}/.config/systemd/user/#{fetch(:sidekiq_service_unit_name)}.service",
32+
executable: false,
33+
as_root: false
3134
},
3235
{
3336
source: "sidekiq_monit",
34-
link: "/etc/monit/conf.d/#{fetch(:full_app_name)}_sidekiq.conf"
37+
destination: "/etc/monit/conf.d/#{fetch(:full_app_name)}_sidekiq.conf",
38+
executable: false,
39+
as_root: true
3540
}
3641
]
3742
end
3843

39-
def executable_config_files_defaults
40-
%w(
41-
)
42-
end
43-
44-
def config_files_defaults
45-
base = %w(
46-
database.example.yml
47-
log_rotation
48-
)
49-
return base unless sidekiq_enabled?
50-
51-
base + %w(
52-
sidekiq.service.capistrano
53-
sidekiq_monit
54-
)
55-
end
56-
5744
def sidekiq_enabled?
5845
defined?(Capistrano::Sidekiq) == 'constant' && Capistrano::Sidekiq.class == Class
5946
end

lib/capistrano/cookbook/helpers/template.rb renamed to lib/capistrano/cookbook/helpers/smart_template.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'securerandom'
2+
require 'stringio'
3+
14
# will first try and copy the file:
25
# config/deploy/#{full_app_name}/#{from}.erb
36
# to:
@@ -11,13 +14,12 @@
1114
# ones to be over-ridden
1215
# if the target file name is the same as the source then
1316
# the second parameter can be left out
14-
def smart_template(from, to=nil)
15-
to ||= from
16-
full_to_path = "#{shared_path}/config/#{to}"
17+
def smart_template(from, to, as_root=false)
1718
if from_erb_path = template_file(from)
1819
from_erb = StringIO.new(ERB.new(File.read(from_erb_path)).result(binding))
19-
upload! from_erb, full_to_path
20-
info "copying: #{from_erb} to: #{full_to_path}"
20+
upload!(from_erb, to) unless as_root
21+
sudo_upload!(from_erb, to) if as_root
22+
info "copying: #{from} to: #{to}"
2123
else
2224
error "error #{from} not found"
2325
end
@@ -35,3 +37,14 @@ def template_file(name)
3537
end
3638
return nil
3739
end
40+
41+
def sudo_upload!(file_path, remote_path, mode: '644', owner: 'root:root')
42+
tmp_path = "/tmp/#{SecureRandom.uuid}"
43+
44+
upload!(file_path, tmp_path)
45+
46+
execute(:sudo, :mkdir, '-p', File.dirname(remote_path))
47+
execute(:sudo, :mv, '-f', tmp_path, remote_path)
48+
execute(:sudo, :chmod, mode, remote_path)
49+
execute(:sudo, :chown, owner, remote_path)
50+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
load File.expand_path("tasks/puma_systemd.cap", File.dirname(__FILE__))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
load File.expand_path("tasks/sidekiq_systemd.cap", File.dirname(__FILE__))

lib/capistrano/cookbook/systemd.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/capistrano/cookbook/tasks/nginx.cap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace :nginx do
33
desc "#{task } Nginx"
44
task task_name do
55
on roles(:app), in: :sequence, wait: 5 do
6-
sudo "/etc/init.d/nginx #{task_name}"
6+
sudo "systemctl #{task_name} nginx"
77
end
88
end
99
end

lib/capistrano/cookbook/tasks/systemd.cap renamed to lib/capistrano/cookbook/tasks/puma_systemd.cap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace :puma do
66
if fetch(:puma_systemctl_user) == :system
77
sudo "#{fetch(:puma_systemctl_bin)} reload-or-restart #{fetch(:puma_service_unit_name)}"
88
else
9-
execute "#{fetch(:puma_systemctl_bin)}", "--user", "reload", fetch(:puma_service_unit_name)
109
execute :loginctl, "enable-linger", fetch(:puma_lingering_user) if fetch(:puma_enable_lingering)
10+
execute "#{fetch(:puma_systemctl_bin)}", "--user", "reload-or-restart", fetch(:puma_service_unit_name)
1111
end
1212
end
1313
end

0 commit comments

Comments
 (0)