/
service.rb
102 lines (85 loc) · 2.24 KB
/
service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'unindent'
module Runit
class Service
ARG_OPT_MAP = {
:setuidgid => '-u',
#:root => '-/'
}
attr_reader :name, :properties
def initialize(name, properties)
@name = name.to_s
@properties = properties
end
def chpst_args
args = ARG_OPT_MAP.collect do |key,switch|
if options.has_key? key
[switch, options.fetch(key)].join(' ')
end
end
args.push(*['-e', "/etc/sv/#{name}/env"]) if env_vars.any?
args.join(' ')
end
def dependency_line
"sv check #{dependencies.join(' ')}"
end
def dependencies
properties[:depends] || []
end
def sources
properties[:sources] || []
end
def sources_lines
sources.collect do |source|
"source #{source}".tap do |s|
s.prepend(" "*8) unless sources.first == source
end
end.join("\n")
end
def env_vars
Hash.new.tap do |vars|
properties[:env].collect do |variable, value|
vars[variable.upcase.to_sym] = value
end
end
end
def start_command
commands[:start]
end
def commands
properties[:commands] || {}
end
def options
properties[:options] || {}
end
# man (1) svlogd
# -tt timestamp. Prefix each selected line with a human
# readable, sortable UTC timestamp of the form
# YYYY-MM-DD_HH:MM:SS.xxxxx when writing to log or to
# standard error.
def log_file
<<-EOHEREDOC.unindent
#!/bin/sh -e
exec svlogd -tt #{log_dir}
EOHEREDOC
end
def log_dir
"/var/log/protonet/#{name}/"
end
def chdir_line
"cd #{options[:chdir]}" if options[:chdir]
end
def run_file
<<-EOHEREDOC.unindent
#!/bin/bash -e
exec 2>&1
#{dependencies.any? ? dependency_line : "# No dependencies"}
#{chdir_line || "# No change to pwd" }
#{sources.any? ? sources_lines : "# No sources" }
# http://smarden.org/runit/faq.html#user
chmod 755 ./supervise
chown protonet ./supervise/ok ./supervise/control ./supervise/status
exec chpst #{chpst_args} #{start_command}
EOHEREDOC
end
end
end