forked from radiodan/provision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision
executable file
·161 lines (130 loc) · 3.61 KB
/
provision
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
#!/usr/bin/env ruby
require "open3"
require "logger"
require "tempfile"
LOG_LEVEL = ENV.has_key?("LOG_LEVEL") ? ENV["LOG_LEVEL"].upcase : "DEBUG"
ROOT = File.dirname(__FILE__)
Console = Logger.new(STDOUT)
Console.level = Logger.const_get(LOG_LEVEL)
Console.formatter = proc do |level, datetime, progname, msg|
msg.chomp + "\n"
end
SCRIPT_ENV = {'DEBIAN_FRONTEND' => 'noninteractive'}.merge(ENV)
module CommandLineHelper
def self.help
<<-EOF
Provision a radiodan application device.
#{__FILE__} [steps]
Valid Steps: #{Steps.valid_steps.join(", ")}.
EOF
end
def self.invalid
<<-EOF
Supplied steps are invalid.
EOF
end
end
module Steps
def self.valid?(steps)
sorted_steps = steps.sort
(valid_steps & sorted_steps) == sorted_steps
end
def self.execute(steps)
@install_steps = []
@config_steps = []
@failed_steps = []
if steps.include?("all")
enqueue_all
else
steps.each{|s| enqueue(s)}
end
execute!
Console.info "#{@install_steps.length} steps selected, " +
"#{@failed_steps.length} failed."
unless @failed_steps.empty?
Console.error "Failed steps: #{@failed_steps.join(", ")}"
end
end
def self.valid_steps
@valid_steps ||= (["all"] +
Dir.entries(ROOT+"/steps").reject{|x| x[0] == "." || x[0] == "_" }).sort
end
def self.enqueue_all
enqueue("_pre-install")
valid_steps[1..-1].each{|s| enqueue(s)}
enqueue("_post-install")
end
def self.enqueue(step_name)
@install_steps << step_name
@config_steps << step_name
end
def self.execute!
@install_steps.each_with_index do |step, index|
script = parse_script(step, "install")
unless script
Console.warn "No install step for #{step}"
next
end
Console.info "Installing #{step} (#{index+1}/#{@install_steps.count})"
Open3.popen2e(SCRIPT_ENV, "/bin/sh", script) do |stdin, response, status|
until (s = response.gets).nil? do
Console.debug "#{step}: #{s}"
end
unless status.value.success?
@failed_steps << step
Console.error "Failed #{step} install."
end
end
end
@config_steps.each_with_index do |step, index|
if @failed_steps.include?(step)
Console.debug "Skipped #{step} config, due to presence in failed steps"
next
end
script = parse_script(step, "configure")
unless script
Console.debug "No config step for #{step}"
next
end
Console.info "Configuring #{step} (#{index+1}/#{@config_steps.count})"
Open3.popen2e(SCRIPT_ENV, "/bin/sh", script) do |stdin, response, status|
until (s = response.gets).nil? do
Console.debug "#{step}: #{s}"
end
unless status.value.success?
@failed_steps << step
Console.error "Failed #{step} configure."
end
end
end
end
def self.parse_script(step, script_name)
dir = File.join(ROOT, "steps", step)
conf_dir = File.join(dir, "files")
file = File.join(dir, "#{script_name}.sh")
if File.exist?(file)
# Add set -e so script fails on first error
src = "set -e \n" + File.read(file)
parsed = src.gsub "${RADIODAN_CONF}", conf_dir
tmp = Tempfile.new("provision_script_#{script_name}_#{step}")
tmp.write parsed
tmp.close
tmp.path
else
false
end
end
end
if %x{whoami}.chomp != "root"
abort "Must be root to execute commands"
end
if ARGV.length == 0
Console.fatal CommandLineHelper.help
abort
end
if Steps.valid?(ARGV)
Steps.execute(ARGV)
else
Console.fatal CommandLineHelper.invalid
abort
end