-
Notifications
You must be signed in to change notification settings - Fork 3
/
glass_stone.rb
369 lines (302 loc) · 8.44 KB
/
glass_stone.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/ruby
require 'rake'
require File.join(File.dirname(__FILE__), 'stone')
require 'fileutils'
require 'net/http'
class GlassStone < Stone
def GlassStone.svc(option, a_service_name)
fail "service directory #{a_service_name} does not exist" unless File.directory?(a_service_name)
fail "svc -#{option} #{a_service_name} failed" unless system("svc -#{option} #{a_service_name}")
end
def GlassStone.clear_status
svc('o', '/service/clear')
end
class Service
@@gemstone_scripts_directory = File.expand_path(File.dirname(__FILE__))
def initialize(stone)
@stone = stone
end
def directory
"/service/#{@stone.name}"
end
def run_symlink
File.join(directory, 'run')
end
def service_skeleton_template
File.join(@@gemstone_scripts_directory, 'service_skeleton')
end
def create_daemontools_structure
if File.exists? directory then
puts "Service directory #{directory} already exists, not going to overwrite it"
else
FileUtils.mkdir_p("#{directory}/log")
system("cd #{service_skeleton_template}; find -path .git -prune -o -print | cpio -p #{directory}")
fixup_run_symlink
FileUtils.touch("#{directory}/down")
end
end
def fixup_run_symlink
if File.exists?(run_symlink) or File.symlink?(run_symlink)
File.delete(run_symlink)
end
system("ln -s #{run_file_name} #{run_symlink}")
end
def svc(option)
GlassStone.svc(option, directory)
end
def start
option = @stone.name == 'development' ? 'o' : 'u'
puts("starting service #{directory}")
svc(option)
end
def start_fg
error_message = 'Environment variable LANG not set, you are probably running this from a ' +
'restricted shell - bailing out'
raise error_message unless ENV['LANG']
fixup_run_symlink
logfile = "#{@stone.log_directory}/#{log_file_base}.log"
write_start_time_to_log(logfile)
[STDOUT, STDERR].each do
|stream|
stream.reopen(logfile, 'a')
stream.flush # Ensure any as-yet unflushed output hits the file
end
exec(glass_command)
end
def write_start_time_to_log(logfile)
log = File.open(logfile, 'a')
log.write("Start time: #{Time.new.inspect}\n")
log.close unless log.closed?
end
def log_file_base
self.class.name.split('::').last
end
def stop
puts("stopping #{directory}")
svc('d')
end
def restart
stop
sleep 3
start
end
def kill
puts("killing #{directory}")
svc('k')
end
def running?
is_running = `svstat #{directory}` =~ /#{directory}: up/
fail "failed to determine if #{directory} is running" if $? != 0
is_running
end
def monitor
end
def alive?
true
end
end
class HyperService < Service
def initialize(stone, port)
super(stone)
@port = port
end
def directory
"#{super}-#{@port}"
end
def log_file_base
"#{super}-#{@port}"
end
def run_file_name
File.join(@@gemstone_scripts_directory, 'run_hyper_service')
end
def glass_command
"exec #{@@gemstone_scripts_directory}/glass_hyper #{@port} '#{@stone.name}'"
end
def monitor
if running? and not alive?
puts "Monitor is restarting #{pid_of_process} at #{Time::now}"
send_dump_stack_signal
restart
end
end
def send_dump_stack_signal
Process.kill('USR1', pid_of_process)
end
def alive?
super and process_listening? and responding?
end
class NoProcessOnPortException < Exception
end
def pid_of_process
output = `fuser -n tcp #{@port} 2>/dev/null`
fail NoProcessOnPortException, "no process listening on port #{@port}" if $? != 0
output.strip.to_i
end
def process_listening?
begin
pid_of_process
stat_service
true
rescue Exception
stat_service
false
end
end
def stat_service
system("svstat #{directory}")
end
def get_proc_stat_contents
stat_file_name = "/proc/#{pid_of_process}/stat"
IO::read(stat_file_name)
end
def responding?
alive = (@stone.http_get_ok?("http://localhost:#{@port}") or some_cpu_activity?)
puts "!!! hyper on port #{@port} is dead" unless alive
alive
end
def remember_proc_stat_contents
@proc_stat_contents = get_proc_stat_contents
end
def some_cpu_activity?
remember_proc_stat_contents
begin
some_activity = false
tries = 0
while not some_activity and tries < 5 do
sleep 1
tries += 1
some_activity = proc_stat_contents_changed?
end
puts "process on port #{@port} #{some_activity ? '' : 'not '}busy with something big"
some_activity
rescue NoProcessOnPortException => e
puts "cannot determine activity on port #{@port} (#{e.message})"
false
end
end
def proc_stat_contents_changed?
@proc_stat_contents != get_proc_stat_contents
end
end
class MaintenanceService < Service
def directory
"#{super}-maintenance"
end
def run_file_name
File.join(@@gemstone_scripts_directory, 'run_maintenance_service')
end
def glass_command
"exec #{@@gemstone_scripts_directory}/glass_maintenance '#{@stone.name}'"
end
def alive?
system("svstat #{directory}")
end
end
def http_get_ok?(url)
uri = URI.parse(url)
req = Net::HTTP::Get.new("/")
begin
res = Net::HTTP.start(uri.host, uri.port) { |http| http.request(req) }
ok = (res.code == '200' || res.code == '301')
unless ok then puts "Response code 200/301 expected from #{url} but got #{res.code}" end
ok
rescue Exception => e
puts "get #{url} failed with #{e.message}"
false
end
end
def all_services
[maintenance_service] + hyper_services
end
def maintenance_service
MaintenanceService.new(self)
end
def hyper_service(port)
HyperService.new(self, port)
end
def hyper_services
service_ports_nginx.collect { | port | hyper_service(port) }
end
def create_daemontools_structure
all_services.each do | service |
service.create_daemontools_structure
end
end
def start_services
GlassStone.clear_status
all_services.each { | service | service.start }
end
def start_system
super
start_services
end
def wait_for_services_to_stop(services, timeout_in_seconds = 20)
counter = 0
while any_service_process_running?(services) and (counter < timeout_in_seconds) do
sleep 1
counter = counter + 1
end
if counter >= timeout_in_seconds
services.each { |service| service.kill }
sleep 3
end
end
def stop_services
all_services.each { | service | service.stop }
wait_for_services_to_stop(all_services)
end
def stop_system
stop_services
super
end
def stop
fail 'Service process still running; consider stop_services.' if any_service_process_running?(all_services)
super
end
def any_service_process_running?(services)
services.any? { | service | service.running? }
end
def status
if running?
super
all_services.each { | service | service.alive? }
else
puts "#{name} not running"
end
end
def monitor_services
all_services.each do | service |
service.monitor
end
end
def nginx_config
Dir['/etc/nginx/sites-enabled/99-*.conf'].collect do | config_file_name |
File.open(config_file_name) { | file | file.read }
end
end
def service_ports_nginx
nginx_config.detect(lambda{fail "Could not find ports for #{name} in #{nginx_config}"}) do | config |
/upstream #{name}.backend \{/ =~ config
end
$~.post_match.scan(/server localhost:(\d{4})/).flatten
end
def bootstrapped_with_mc?
result = topaz_commands(["run", "(System myUserProfile objectNamed: #MCPlatformSupport) notNil", "%"])
if result =~ /^\[.* Boolean\] true/
true
else
false
end
end
alias :run_topaz_commands_raw :run_topaz_commands
def run_topaz_commands(*commands)
if bootstrapped_with_mc?
super(commands.unshift("MCPlatformSupport autoCommit: false; autoMigrate: false"))
else
run_topaz_commands_raw(commands)
end
end
def seaside_bin_directory
"#{gemstone_installation_directory}/seaside/bin"
end
end