Class: Capistrano::Cable::Systemd

Inherits:
Plugin
  • Object
show all
Defined in:
lib/capistrano/cable/systemd.rb

Instance Method Summary collapse

Instance Method Details

#cable_bindObject



106
107
108
109
110
# File 'lib/capistrano/cable/systemd.rb', line 106

def cable_bind
  Array(fetch(:cable_bind)).collect do |bind|
    "bind '#{bind}'"
  end.join("\n")
end

#cable_bindsObject



158
159
160
161
162
163
# File 'lib/capistrano/cable/systemd.rb', line 158

def cable_binds
  Array(fetch(:cable_bind)).map do |m|
    etype, address = /(tcp|unix|ssl):\/{1,2}(.+)/.match(m).captures
    Bind.new(m, etype.to_sym, address)
  end
end

#cable_switch_user(role, &block) ⇒ Object

From Common



87
88
89
90
91
92
93
94
95
96
# File 'lib/capistrano/cable/systemd.rb', line 87

def cable_switch_user(role, &block)
  user = cable_user(role)
  if user == role.user
    block.call
  else
    backend.as user do
      block.call
    end
  end
end

#cable_user(role) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/capistrano/cable/systemd.rb', line 98

def cable_user(role)
  properties = role.properties
  properties.fetch(:cable_user) || # local property for cable only
    fetch(:cable_user) ||
    properties.fetch(:run_as) || # global property across multiple capistrano gems
    role.user
end

#compiled_template_cable(from, role) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/capistrano/cable/systemd.rb', line 135

def compiled_template_cable(from, role)
  @role = role
  file = [
    "lib/capistrano/templates/#{from}-#{role.hostname}-#{fetch(:stage)}.rb",
    "lib/capistrano/templates/#{from}-#{role.hostname}.rb",
    "lib/capistrano/templates/#{from}-#{fetch(:stage)}.rb",
    "lib/capistrano/templates/#{from}.rb.erb",
    "lib/capistrano/templates/#{from}.rb",
    "lib/capistrano/templates/#{from}.erb",
    "config/deploy/templates/#{from}.rb.erb",
    "config/deploy/templates/#{from}.rb",
    "config/deploy/templates/#{from}.erb",
    File.expand_path("../../templates/#{from}.erb", __FILE__),
    File.expand_path("../../templates/#{from}.rb.erb", __FILE__)
  ].detect { |path| File.file?(path) }
  erb = File.read(file)
  StringIO.new(ERB.new(erb, trim_mode: "-").result(binding))
end

#define_tasksObject



11
12
13
# File 'lib/capistrano/cable/systemd.rb', line 11

def define_tasks
  eval_rakefile File.expand_path("../../tasks/systemd.rake", __FILE__)
end

#execute_systemd(*args) ⇒ Object



81
82
83
# File 'lib/capistrano/cable/systemd.rb', line 81

def execute_systemd(*args)
  sudo_if_needed(*systemd_command(*args))
end

#expanded_bundle_commandObject



50
51
52
# File 'lib/capistrano/cable/systemd.rb', line 50

def expanded_bundle_command
  backend.capture(:echo, SSHKit.config.command_map[:bundle]).strip
end

#fetch_systemd_unit_pathObject



54
55
56
57
58
59
60
61
# File 'lib/capistrano/cable/systemd.rb', line 54

def fetch_systemd_unit_path
  if fetch(:cable_systemctl_user) == :system
    "/etc/systemd/system/"
  else
    home_dir = backend.capture :pwd
    File.join(home_dir, ".config", "systemd", "user")
  end
end

#puma_optionsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/capistrano/cable/systemd.rb', line 120

def puma_options
  options = []
  options << "--no-config"
  if fetch(:cable_ssl_certificate) && fetch(:cable_ssl_certificate_key)
    options << "--bind 'ssl://0.0.0.0:#{fetch(:cable_port)}?key=#{fetch(:cable_ssl_certificate_key)}&cert=#{fetch(:cable_ssl_certificate)}'"
  else
    options << "--port #{fetch(:cable_port)}"
  end
  options << "--environment #{fetch(:cable_env)}"
  options << "--pidfile #{fetch(:cable_pidfile)}" if fetch(:cable_pidfile)
  options << "--threads #{fetch(:cable_threads)}" if fetch(:cable_threads)
  options << "--workers #{fetch(:cable_workers)}" if fetch(:cable_workers)
  options.join(" ")
end

#register_hooksObject



7
8
9
# File 'lib/capistrano/cable/systemd.rb', line 7

def register_hooks
  after "deploy:finished", "cable:smart_restart"
end

#service_unit_typeObject



112
113
114
115
116
117
118
# File 'lib/capistrano/cable/systemd.rb', line 112

def service_unit_type
  ## Jruby don't support notify
  return "simple" if RUBY_ENGINE == "jruby"
  fetch(:cable_service_unit_type,
    ## Check if sd_notify is available in the bundle
    Gem::Specification.find_all_by_name("sd_notify").any? ? "notify" : "simple")
end

#set_defaultsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/capistrano/cable/systemd.rb', line 15

def set_defaults
  set_if_empty :cable_role, :web
  set_if_empty :cable_port, 29292
  set_if_empty :cable_rackup_file, 'cable/config.ru'
  set_if_empty :cable_dir, -> { File.join(release_path, "cable") }
  set_if_empty :cable_pidfile, -> { File.join(shared_path, "tmp", "pids", "cable.pid") }
  set_if_empty :cable_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
  set_if_empty :cable_access_log, -> { File.join(shared_path, "log", "cable.access.log") }
  set_if_empty :cable_error_log, -> { File.join(shared_path, "log", "cable.error.log") }
  set_if_empty :cable_phased_restart, -> { true }

  set_if_empty :cable_systemctl_bin, -> { fetch(:systemctl_bin, "/bin/systemctl") }
  set_if_empty :cable_service_unit_name, -> { "#{fetch(:application)}_cable_#{fetch(:stage)}" }
  set_if_empty :cable_enable_socket_service, false
  set_if_empty :cable_socket_unit_name, -> { "#{fetch(:application)}_cable_#{fetch(:stage)}.socket" }
  # set_if_empty :cable_bind, -> { "unix:/tmp/#{fetch(:app_domain)}.sock" }

  set_if_empty :cable_service_unit_env_files, -> { fetch(:service_unit_env_files, []) }
  set_if_empty :cable_service_unit_env_vars, -> { fetch(:service_unit_env_vars, []) }

  set_if_empty :cable_systemctl_user, -> { fetch(:systemctl_user, :user) }
  set_if_empty :cable_enable_lingering, -> { fetch(:cable_systemctl_user) != :system }
  set_if_empty :cable_lingering_user, -> { fetch(:lingering_user, fetch(:user)) }

  set_if_empty :cable_service_templates_path, fetch(:service_templates_path, "config/deploy/templates")

  # Chruby, Rbenv and RVM integration
  append :chruby_map_bins, "puma", "pumactl" if fetch(:chruby_map_bins)
  append :rbenv_map_bins, "puma", "pumactl" if fetch(:rbenv_map_bins)
  append :rvm_map_bins, "puma", "pumactl" if fetch(:rvm_map_bins)

  # Bundler integration
  append :bundle_bins, "puma", "pumactl"
end

#sudo_if_needed(*command) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/capistrano/cable/systemd.rb', line 73

def sudo_if_needed(*command)
  if fetch(:cable_systemctl_user) == :system
    backend.sudo command.map(&:to_s).join(" ")
  else
    backend.execute(*command)
  end
end

#systemd_command(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/capistrano/cable/systemd.rb', line 63

def systemd_command(*args)
  command = [fetch(:cable_systemctl_bin)]

  unless fetch(:cable_systemctl_user) == :system
    command << "--user"
  end

  command + args
end

#upload_template_cable(from, to, role) ⇒ Object



154
155
156
# File 'lib/capistrano/cable/systemd.rb', line 154

def upload_template_cable(from, to, role)
  backend.upload! compiled_template_cable(from, role), to
end