Module: Capistrano::RunitBase

Defined in:
lib/capistrano/base_helper/runit_base.rb

Class Method Summary collapse

Class Method Details

.app_services_control(application, user, command, ignore_error = false) ⇒ Object



161
162
163
164
# File 'lib/capistrano/base_helper/runit_base.rb', line 161

def app_services_control(application, user, command, ignore_error = false)
  c = Capistrano::BaseHelper.get_capistrano_instance
  Capistrano::BaseHelper.get_capistrano_instance.run("[ ! -h /etc/service/#{user}_#{application}_#{Capistrano::BaseHelper.environment} ] || #{c.sudo} sv #{command} #{user}_#{application}_#{Capistrano::BaseHelper.environment}; #{"true" if ignore_error != false}")
end

.app_services_createObject

application name should be “app-environment” or similar in case you deploy staging/production to same host



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/capistrano/base_helper/runit_base.rb', line 105

def app_services_create
  # SEE http://off-the-stack.moorman.nu/posts/5-user-services-with-runit/ for info on scripts
  c = Capistrano::BaseHelper.get_capistrano_instance
  c.run("#{c.sudo} mkdir -p '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path)}'")
  
  commands = []
  commands << "#{c.sudo} chown #{c.fetch(:user)}:root /etc/sv/#{c.fetch(:user)}"
  commands << "#{c.sudo} chown #{c.fetch(:user)}:root /etc/sv/#{Capistrano::BaseHelper.user_app_env_path}"
  c.run(commands.join(" && "))
  Capistrano::BaseHelper.generate_and_upload_config( Capistrano::BaseHelper::get_capistrano_instance.fetch(:runit_local_run), Capistrano::BaseHelper::get_capistrano_instance.fetch(:runit_remote_run), true )
  Capistrano::BaseHelper.generate_and_upload_config( Capistrano::BaseHelper::get_capistrano_instance.fetch(:runit_local_finish), Capistrano::BaseHelper::get_capistrano_instance.fetch(:runit_remote_finish), true )

  commands = []
  commands << "#{c.sudo} chmod u+x '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "run")}'"
  commands << "#{c.sudo} chmod u+x '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "finish")}'"
  commands << "#{c.sudo} chmod g+x '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "run")}'"
  commands << "#{c.sudo} chmod g+x '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "finish")}'"
  commands << "#{c.sudo} chown #{c.fetch(:user)}:root '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "run")}'"
  commands << "#{c.sudo} chown #{c.fetch(:user)}:root '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "finish")}'"
  c.run(commands.join(" && "))

  Capistrano::RunitBase.app_services_create_log_service
end

.app_services_create_log_serviceObject

BEGIN - ALL services functions (functions that affects all services for the app)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/capistrano/base_helper/runit_base.rb', line 86

def app_services_create_log_service
  c = Capistrano::BaseHelper.get_capistrano_instance
  commands = []
  commands << "#{c.sudo} mkdir -p #{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "log")}"
  commands << "#{c.sudo} chown #{c.fetch(:user)}:root #{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "log")}"
  commands << "#{c.sudo} mkdir -p '#{File.join("/var", "log", "service", Capistrano::BaseHelper.user_app_env_path, "runit")}'"
  commands << "#{c.sudo} chown -R #{c.fetch(:runit_log_user)}:#{c.fetch(:runit_log_group)} '#{File.join("/var", "log", "service", Capistrano::BaseHelper.user_app_env_path, "runit")}'"

  c.run(commands.join(" && "))
  Capistrano::BaseHelper.generate_and_upload_config( c.fetch(:runit_local_log_run), c.fetch(:runit_remote_log_run), true )
  commands = []
  commands << "#{c.sudo} chmod u+x '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "log", "run")}'"
  commands << "#{c.sudo} chmod g+x '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "log", "run")}'"
  commands << "#{c.sudo} chown #{c.fetch(:user)}:root '#{File.join("/etc", "sv", Capistrano::BaseHelper.user_app_env_path, "log", "run")}'"
  c.run(commands.join(" && ")) 
end

.app_services_disable(application, user) ⇒ Object



134
135
136
137
# File 'lib/capistrano/base_helper/runit_base.rb', line 134

def app_services_disable(application, user)
  c = Capistrano::BaseHelper.get_capistrano_instance
  Capistrano::BaseHelper.get_capistrano_instance.run("[ ! -h /etc/service/#{user}_#{application}_#{Capistrano::BaseHelper.environment} ] || #{c.sudo} rm -f /etc/service/#{user}_#{application}_#{Capistrano::BaseHelper.environment}")
end

.app_services_enable(application, user) ⇒ Object



129
130
131
132
# File 'lib/capistrano/base_helper/runit_base.rb', line 129

def app_services_enable(application, user)
  c = Capistrano::BaseHelper.get_capistrano_instance
  Capistrano::BaseHelper.get_capistrano_instance.run("[ -h /etc/service/#{user}_#{application}_#{Capistrano::BaseHelper.environment} ] || #{c.sudo} ln -sf /etc/sv/#{Capistrano::BaseHelper.user_app_env_path} /etc/service/#{user}_#{application}_#{Capistrano::BaseHelper.environment}")
end

.app_services_once(application, user, ignore_error = false) ⇒ Object



157
158
159
# File 'lib/capistrano/base_helper/runit_base.rb', line 157

def app_services_once(application, user, ignore_error = false)
  app_services_control(application, user, "once", ignore_error)
end

.app_services_purge(application, user) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/capistrano/base_helper/runit_base.rb', line 139

def app_services_purge(application, user)
  # this should stop ALL services running for the given application
  # true is appended to ignore any errors failing to stop the services
  c = Capistrano::BaseHelper.get_capistrano_instance
  commands = []
  commands << "#{c.sudo} rm -f /etc/service/#{user}_#{application}_#{Capistrano::BaseHelper.environment}"
  commands << "#{c.sudo} rm -rf /etc/sv/#{Capistrano::BaseHelper.user_app_env_path}"
  Capistrano::BaseHelper.get_capistrano_instance.run(commands.join(" && "))
end

.app_services_start(application, user, ignore_error = false) ⇒ Object



153
154
155
# File 'lib/capistrano/base_helper/runit_base.rb', line 153

def app_services_start(application, user, ignore_error = false)
  app_services_control(application, user, "start", ignore_error)
end

.app_services_stop(application, user, ignore_error = false) ⇒ Object



149
150
151
# File 'lib/capistrano/base_helper/runit_base.rb', line 149

def app_services_stop(application, user, ignore_error = false)
  app_services_control(application, user, "stop", ignore_error)
end

.control_service(service_name, service_control_function, ignore_error = false, arguments = "") ⇒ Object



187
188
189
# File 'lib/capistrano/base_helper/runit_base.rb', line 187

def control_service(service_name, service_control_function, ignore_error = false, arguments = "")
  Capistrano::BaseHelper.get_capistrano_instance.run("[ ! -h #{service_path(service_name)}/run ] || sv #{arguments} #{service_control_function} #{service_path(service_name)}")  
end

.create_and_permissions_on_path(log_path, user = nil, group = nil) ⇒ Object

note that the user running the runit service MUST be a member of the group or the same as the log user If not, the log service will not work



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/capistrano/base_helper/runit_base.rb', line 253

def create_and_permissions_on_path(log_path, user = nil, group = nil)
  user  = Capistrano::BaseHelper.get_capistrano_instance.fetch(:user) if user.nil?
  group = "syslog" if group.nil?
  c = Capistrano::BaseHelper.get_capistrano_instance
  # will use sudo
  commands = []
  commands << "#{c.sudo} mkdir -p #{log_path}"
  commands << "#{c.sudo} chown -R #{user}:#{group} #{log_path}"
  commands << "#{c.sudo} chmod u+w #{log_path}"
  commands << "#{c.sudo} chmod g+w #{log_path}"
  Capistrano::BaseHelper.get_capistrano_instance.run commands.join(" && ")
end

.create_service_dir(service_name) ⇒ Object



76
77
78
# File 'lib/capistrano/base_helper/runit_base.rb', line 76

def create_service_dir(service_name)
  Capistrano::BaseHelper.get_capistrano_instance.run("[ -d #{service_path(service_name)} ] || mkdir -p #{service_path(service_name)}")
end

.disable_service(service_name) ⇒ Object



196
197
198
199
# File 'lib/capistrano/base_helper/runit_base.rb', line 196

def disable_service(service_name)
  force_control_service(service_name, "force-stop", true) # force-stop the service before disabling it
  Capistrano::BaseHelper.get_capistrano_instance.run("[ ! -h #{service_path(service_name)}/run ] || rm -f #{service_path(service_name)}/run && rm -f #{service_path(service_name)}/finish")
end

.enable_service(service_name) ⇒ Object



201
202
203
204
# File 'lib/capistrano/base_helper/runit_base.rb', line 201

def enable_service(service_name)
  Capistrano::BaseHelper.get_capistrano_instance.run("cd #{service_path(service_name)} && [ -h ./run ] || ln -sf #{remote_run_config_path(service_name)} ./run")
  Capistrano::BaseHelper.get_capistrano_instance.run("cd #{service_path(service_name)} && [ -h ./finish ] || ln -sf #{remote_finish_config_path(service_name)} ./finish")
end

.force_control_service(service_name, service_control_function, ignore_error = false) ⇒ Object

Will not check if the service exists before trying to force it down



192
193
194
# File 'lib/capistrano/base_helper/runit_base.rb', line 192

def force_control_service(service_name, service_control_function, ignore_error = false)
  Capistrano::BaseHelper.get_capistrano_instance.run("sv #{service_control_function} #{service_path(service_name)}; #{"true" if ignore_error != false}")  
end

.make_service_scripts_executeable(service_name) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/capistrano/base_helper/runit_base.rb', line 230

def make_service_scripts_executeable(service_name)
  commands = []
  if Capistrano::BaseHelper::remote_file_exists?(Capistrano::RunitBase.remote_run_config_path(service_name))
    commands << "chmod u+x #{Capistrano::RunitBase.remote_run_config_path(service_name)}"
    commands << "chmod g+x #{Capistrano::RunitBase.remote_run_config_path(service_name)}"
  end
  if Capistrano::BaseHelper::remote_file_exists?(Capistrano::RunitBase.remote_finish_config_path(service_name))
    commands << "chmod u+x #{Capistrano::RunitBase.remote_finish_config_path(service_name)}"
    commands << "chmod g+x #{Capistrano::RunitBase.remote_finish_config_path(service_name)}"
  end
  if Capistrano::BaseHelper::remote_file_exists?(Capistrano::RunitBase.remote_service_log_run_path(service_name))
    commands << "chmod u+x #{Capistrano::RunitBase.remote_service_log_run_path(service_name)}"
    commands << "chmod g+x #{Capistrano::RunitBase.remote_service_log_run_path(service_name)}"
  end
  if Capistrano::BaseHelper::remote_file_exists?(Capistrano::RunitBase.remote_control_path_root(service_name))
    commands << "chmod u+x -R #{Capistrano::RunitBase.remote_control_path_root(service_name)}"
    commands << "chmod g+x -R #{Capistrano::RunitBase.remote_control_path_root(service_name)}"
  end
  Capistrano::BaseHelper.get_capistrano_instance.run commands.join(" ; ")
end

.purge_service(service_name) ⇒ Object



206
207
208
# File 'lib/capistrano/base_helper/runit_base.rb', line 206

def purge_service(service_name)
  Capistrano::BaseHelper.get_capistrano_instance.run("rm -rf #{service_path(service_name)}")
end

.remote_control_path(service_name, control_letter) ⇒ Object



218
219
220
# File 'lib/capistrano/base_helper/runit_base.rb', line 218

def remote_control_path(service_name, control_letter)
  File.join(remote_control_path_root(service_name), control_letter)
end

.remote_control_path_root(service_name) ⇒ Object



222
223
224
# File 'lib/capistrano/base_helper/runit_base.rb', line 222

def remote_control_path_root(service_name)
  File.join(service_path(service_name), "control")
end

.remote_finish_config_path(service_name) ⇒ Object



214
215
216
# File 'lib/capistrano/base_helper/runit_base.rb', line 214

def remote_finish_config_path(service_name)
  File.join(service_path(service_name), "#{service_name}_finish")
end

.remote_run_config_path(service_name) ⇒ Object



210
211
212
# File 'lib/capistrano/base_helper/runit_base.rb', line 210

def remote_run_config_path(service_name)
  File.join(service_path(service_name), "#{service_name}_run")
end

.remote_service_log_run_path(service_name) ⇒ Object



226
227
228
# File 'lib/capistrano/base_helper/runit_base.rb', line 226

def remote_service_log_run_path(service_name)
  File.join(service_path(service_name), "log", "run")
end

.restart_service(service_name) ⇒ Object



183
184
185
# File 'lib/capistrano/base_helper/runit_base.rb', line 183

def restart_service(service_name)
  control_service(service_name, "restart")
end

.service_path(service_name) ⇒ Object



72
73
74
# File 'lib/capistrano/base_helper/runit_base.rb', line 72

def service_path(service_name)
  "#{Capistrano::BaseHelper.get_capistrano_instance.fetch(:runit_dir)}/#{service_name}"
end

.service_pid(service_name) ⇒ Object



80
81
82
# File 'lib/capistrano/base_helper/runit_base.rb', line 80

def service_pid(service_name)
  File.join(service_path(service_name), "supervise", "pid")
end

.start_service(service_name) ⇒ Object



175
176
177
# File 'lib/capistrano/base_helper/runit_base.rb', line 175

def start_service(service_name)
  control_service(service_name, "start")
end

.start_service_once(service_name) ⇒ Object

BEGIN - Single service functions (functions that affects a single given service)



171
172
173
# File 'lib/capistrano/base_helper/runit_base.rb', line 171

def start_service_once(service_name)
  control_service(service_name, "once")
end

.stop_service(service_name) ⇒ Object



179
180
181
# File 'lib/capistrano/base_helper/runit_base.rb', line 179

def stop_service(service_name)
  control_service(service_name, "stop")
end