-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.rb
127 lines (105 loc) · 2.88 KB
/
init.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
# -*- coding: utf-8 -*-
# rubocop:disable GlobalVars, SpecialGlobalVars
# Some reading: http://felipec.wordpress.com/2013/11/04/init/
require 'date'
require 'fileutils'
STDOUT.sync = true
$processes = {}
def log(message)
puts "[#{DateTime.now}] INIT: #{message}"
end
def run!(*args, &block)
log "Starting: #{args}" if ENV['DEBUG']
pid = Process.spawn(*args)
log "Started #{pid}: #{args.join ' '}"
$processes[pid] = block || -> { log "#{args.join ' '}: #{$?}" }
pid
end
def reconfigure!(reason = nil)
if $reconf_pid
if reason
log "#{reason}, but cannot reconfigure: already running"
else
log 'Cannot reconfigure: already running'
end
return
end
if reason
log "#{reason}, reconfiguring"
else
log 'Reconfiguring'
end
$reconf_pid = run! '/usr/bin/chef-compliance-ctl', 'reconfigure' do
log "Reconfiguration finished: #{$?}"
run! 'touch /var/opt/chef-compliance/bootstrapped'
$reconf_pid = nil
end
end
def shutdown!
unless $runsvdir_pid
log 'ERROR: no runsvdir pid at exit'
exit 1
end
if $reconf_pid
log "Reconfigure running as #{$reconf_pid}, stopping..."
Process.kill 'TERM', $reconf_pid
(1..5).each do
if $reconf_pid
sleep 1
else
break
end
end
Process.kill 'KILL', $reconf_pid if $reconf_pid
end
run! '/usr/bin/chef-compliance-ctl', 'stop' do
log 'chef-compliance-ctl stop finished, stopping runsvdir'
Process.kill('HUP', $runsvdir_pid)
end
end
log "Starting #{$PROGRAM_NAME}"
{ shmmax: 17_179_869_184, shmall: 4_194_304 }.each do |param, value|
next unless (actual = File.read("/proc/sys/kernel/#{param}").to_i) < value
log "kernel.#{param} = #{actual}, setting to #{value}."
begin
File.write "/proc/sys/kernel/#{param}", value.to_s
rescue
log "Cannot set kernel.#{param} to #{value}: #{$!}"
log 'You may need to run the container in privileged mode or set sysctl on host.'
raise
end
end
log 'Preparing configuration ...'
FileUtils.mkdir_p %w(/var/opt/chef-compliance/log /var/opt/chef-compliance/etc), verbose: true
FileUtils.cp '/.chef/chef-compliance.rb', '/var/opt/chef-compliance/etc', verbose: true
$runsvdir_pid = run! '/opt/chef-compliance/embedded/bin/runsvdir-start' do
log "runsvdir exited: #{$?}"
if $?.success? || $?.exitstatus == 111
exit
else
exit $?.exitstatus
end
end
Signal.trap 'TERM' do
shutdown!
end
Signal.trap 'INT' do
shutdown!
end
Signal.trap 'HUP' do
reconfigure! 'Got SIGHUP'
end
Signal.trap 'USR1' do
log 'Chef Server status:'
run! '/usr/bin/chef-compliance-ctl', 'status'
end
# Chef Compliance does not create this bootstrapped file and we can
# only guess if the initial reconfigure worked.
unless File.exist? '/var/opt/chef-compliance/bootstrapped'
reconfigure! 'Not bootstrapped'
end
loop do
log $? if ENV['DEBUG']
handler = $processes.delete(Process.wait)
handler.call if handler
end