Module: Capistrano::Runit

Defined in:
lib/capistrano/runit.rb

Instance Method Summary collapse

Instance Method Details

#check_service(service, namespace = nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/capistrano/runit.rb', line 41

def check_service(service, namespace = nil)
  if fetch("runit_#{service}_default_hooks".to_sym)
    ::Rake::Task['runit:setup'].invoke
    service = "#{service}:#{namespace}" if namespace
    ::Rake::Task["runit:#{service}:setup"].invoke
    ::Rake::Task["runit:#{service}:enable"].invoke
  end
end

#disable_service(service) ⇒ Object



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

def disable_service(service)
  ::Rake::Task["runit:#{service}:stop"].invoke
  enabled_service_dir = enabled_service_dir_for(service)
  on roles fetch("runit_#{service}_role".to_sym) do
    if test "[ -d #{enabled_service_dir}"
      execute :rm, '-f', enabled_service_dir
    else
      error "'#{service}' runit service isn't enabled."
    end
  end
end

#enable_service(service) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/capistrano/runit.rb', line 70

def enable_service(service)
  service_dir         = service_dir_for(service)
  enabled_service_dir = enabled_service_dir_for(service)
  on roles fetch("runit_#{service}_role".to_sym) do
    if test "[ -d #{service_dir} ]"
      if test "[ -d #{enabled_service_dir} ]"
        info "'#{service}' runit service already enabled"
      else
        execute :ln, '-snf', service_dir, enabled_service_dir
      end
    else
      error "'#{service}' runit service isn't found. You should run runit:#{service}:setup first."
    end
  end
end

#enabled_service_dir_for(service) ⇒ Object



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

def enabled_service_dir_for(service)
  ::File.join(deploy_to, 'runit', 'enabled', service)
end

#env_variables ⇒ Object



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

def env_variables
  ::SSHKit.config.default_env.map { |k, v| "#{k.upcase}=\"#{v}\"" }.join(' ')
end

#kill_hup_service(service) ⇒ Object



129
130
131
132
133
# File 'lib/capistrano/runit.rb', line 129

def kill_hup_service(service)
  on roles fetch("runit_#{service}_role".to_sym) do
    runit_execute_command(service, 'hup')
  end
end

#pid_full_path(pid_path) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/capistrano/runit.rb', line 22

def pid_full_path(pid_path)
  if pid_path.start_with?('/')
    pid_path
  else
    "#{shared_path}/#{pid_path}"
  end
end

#restart_service(service, timeout = nil) ⇒ Object



123
124
125
126
127
# File 'lib/capistrano/runit.rb', line 123

def restart_service(service, timeout = nil)
  on roles fetch("runit_#{service}_role".to_sym) do
    runit_execute_command(service, 'restart', timeout)
  end
end

#runit_execute_command(service, command, timeout = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/capistrano/runit.rb', line 135

def runit_execute_command(service, command, timeout = nil)
  # check timeout type
  unless timeout.instance_of?(NilClass) || (timeout.is_a?(Integer) && timeout >= 0)
    raise ArgumentError.new("'timeout' argument in '#runit_execute_command' method must be nil or positive integer.")
  end

  enabled_service_dir = enabled_service_dir_for(service)
  if test "[ -d #{enabled_service_dir} ]"
    execute "#{host.fetch(:runit_sv_path)} #{"-w #{timeout}" unless timeout.nil?} #{command} #{enabled_service_dir}"
  else
    error "'#{service}' runit service isn't enabled."
  end
end

#service_dir_for(service) ⇒ Object



3
4
5
# File 'lib/capistrano/runit.rb', line 3

def service_dir_for(service)
  ::File.join(deploy_to, 'runit', 'available', service)
end

#service_running?(service) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/capistrano/runit.rb', line 30

def service_running?(service)
  service_dir   = enabled_service_dir_for(service)
  supervise_dir = ::File.join(service_dir, 'supervise')
  stat_file     = ::File.join(supervise_dir, 'stat')
  if test("[ -d #{supervise_dir} ]") && test("[ -f #{stat_file} ]")
    capture(:cat, stat_file).chomp == 'run'
  else
    false
  end
end

#setup_service(service, run_command) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/capistrano/runit.rb', line 50

def setup_service(service, run_command)
  service_dir  = service_dir_for(service)
  template_key = "runit_#{service}_run_template".to_sym
  on roles fetch("runit_#{service}_role".to_sym) do
    if test "[ ! -d #{service_dir} ]"
      execute :mkdir, '-v', service_dir
    end
    template_path = fetch(template_key, ::File.expand_path('../templates/run.erb', __FILE__))
    if !template_path.nil? && File.exist?(template_path)
      upload_runit_run_file(
        run_command,
        template_path,
        ::File.join(service_dir, 'run')
      )
    else
      error "Template from '#{template_key}' variable isn't found: #{template_path}"
    end
  end
end

#start_service(service, timeout = nil) ⇒ Object



98
99
100
101
102
# File 'lib/capistrano/runit.rb', line 98

def start_service(service, timeout = nil)
  on roles fetch("runit_#{service}_role".to_sym) do
    runit_execute_command(service, 'start', timeout)
  end
end

#stop_service(service, pidfile = true, timeout = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/capistrano/runit.rb', line 104

def stop_service(service, pidfile = true, timeout = nil)
  pid_path = pid_full_path(fetch("runit_#{service}_pid".to_sym)) if pidfile
  on roles fetch("runit_#{service}_role".to_sym) do
    if pidfile
      if test "[ -f #{pid_path} ]" && service_running?(service)
        runit_execute_command(service, 'stop', timeout)
      else
        info "'#{service}' is not running yet"
      end
    else
      if service_running?(service)
        runit_execute_command(service, 'stop', timeout)
      else
        info "'#{service}' is not running yet"
      end
    end
  end
end

#upload_runit_run_file(_runit_command, template_path, dest) ⇒ Object



15
16
17
18
19
20
# File 'lib/capistrano/runit.rb', line 15

def upload_runit_run_file(_runit_command, template_path, dest)
  template = ERB.new(File.read(template_path))
  stream   = StringIO.new(template.result(binding))
  upload! stream, dest
  execute :chmod, '0755', dest
end