-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsm.rb
executable file
·261 lines (230 loc) · 5.84 KB
/
sm.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/ruby
# 20140830 rschmutz@netlabs.ch
module SM
require "pstore"
# monkey-patch String.match?
class String
def match?(regex)
!!self.match(regex)
end
end
# control collection of services
class Controller
@services = []
@states = nil
private
def filter(pattern)
if pattern.nil?
@services
else
@services.select do |service|
if service.description.match?(pattern) or service.name.match?(pattern) or service.state.to_s.match?(pattern)
true
else
false
end
end
end
end
private
def add_service(service)
@services << service
@states.transaction do
begin
service.state = @states[service.name]
rescue
@states[service.name] = service.state
@states.commit
end
end
end
public
def initialize(directory)
@states = PStore.new(directory + "/" + ".states")
@services = Array.new
Dir.foreach directory do |file|
next if file.match /^\./
service = Service.new
service.configure do
eval(File.open(directory + "/" + file).read)
if name.nil? # no identifier given, use the file name instead
identifier file
end
end
service.file = directory + "/" + file
add_service service
end
end
private
def start_service(service)
service.start_commands.each do |command|
service.processes << fork do
exec command
end
end
service.processes.each { |pid| Process::detach pid }
@states.transaction do
@states[service.name] = (service.state = :running)
@states.commit
end
end
public
def start(pattern = nil)
filter(pattern).each do |service|
start_service service
end
end
private
def stop_service(service)
if service.stop_commands.empty? # kill processes the old way
pid_from_files = service.pidfiles.collect do |pidfile|
begin
File.read(pidfile)
rescue
end
end
(service.processes & pid_from_files).each do |pid|
begin
Process::kill "KILL", pid
rescue
end
end
else # use provided stop commands
service.stop_commands.each do |command|
system command
end
end
@states.transaction do
@states[service.name] = service.state = :stopped
@states.commit
end
end
public
def stop(pattern = nil)
filter(pattern).each do |service|
stop_service service
end
end
private
def status_service(service)
check = nil
if service.status_commands.empty? # kill processes the old way
if service.pidfiles.empty?
pids = service.processes
else
pids = service.pidfiles.collect do |pidfile|
begin
File.read(pidfile)
rescue
end
end
end
if pids.all? do |pid|
begin
Process::kill 0, pid
check = :running
rescue
check = :stopped
end
end
end
else # use provided status commands
service.status_commands.all? do |command|
fork { exec command }
end
end
result = :unknown
if service.state != :uninitialized
if check == service.state
result = :running
else
result = :stopped
end
end
return result
end
public
def status(pattern = nil)
printf "%-20s%-15s%s\n", "service", "state", "check"
printf "%--20s%--15s%s\n", "-------", "-----", "-----"
filter(pattern).each do |service|
state = status_service service
printf "%-20s%-15s%s\n", service.name, service.state.to_s, state.to_s
end
end
public
def services(pattern = nil)
printf "%-20s%s\n", "service", "definition-file"
printf "%-20s%s\n", "-------", "---------------"
@services.each do |service|
printf "%-20s%s\n", service.name, service.file
end
end
private
def synchronize_service(service) # ensure service state match persistent state, ie start services in running state
@states.transaction(true) do # read-only
begin
if @states[service.name] == :running
state = status_service service
if state != :running
start_service service
end
end
rescue
end
end
end
end
# container class for a (single despite the legacy arrays) service
class Service
attr_reader :name, :start_commands, :stop_commands, :status_commands, :pidfiles
attr_accessor :state, :processes, :file, :description
@description = ""
@name = ""
@processes = []
@start_commands = []
@stop_commands = []
@status_commands = []
@state = :down
@file = ""
@pidfiles = []
def initialize
@start_commands = Array.new
@stop_commands = Array.new
@status_commands = Array.new
@pidfiles = Array.new
@processes = Array.new
@state = :uninitialized
@description = ""
end
def configure(&block)
instance_eval &block
end
# def description(description)
# @description = description
# end
def identifier(name)
@name = name
end
def start(command)
@start_commands << command
end
def stop(command)
@stop_commands << command
end
def status(command)
@status_commands << command
end
def pidfile(pidfile)
@pidfiles << pidfile
end
end
end
#
# sockets socket | [ socket socket... ]
# states: down starting faulty running disabled stopping
# connections remote-socket
#
# render
# logging
# notfication