kuzushi 0.0.35 → 0.0.36
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/kuzushi.rb +377 -344
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.36
|
data/lib/kuzushi.rb
CHANGED
@@ -13,348 +13,381 @@ require 'erb'
|
|
13
13
|
## user configs
|
14
14
|
|
15
15
|
class Kuzushi
|
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
|
-
|
16
|
+
attr_accessor :config
|
17
|
+
|
18
|
+
def initialize(url)
|
19
|
+
@url = url
|
20
|
+
@base_url = File.dirname(url)
|
21
|
+
if @url =~ /s3.amazonaws.com.*\/([^\/]*)[.](\d+)[.]tar[.]gz/
|
22
|
+
@name = $1
|
23
|
+
@version = $2
|
24
|
+
end
|
25
|
+
@configs = []
|
26
|
+
@packages = []
|
27
|
+
@tasks = []
|
28
|
+
end
|
29
|
+
|
30
|
+
def init
|
31
|
+
@init = true
|
32
|
+
start
|
33
|
+
end
|
34
|
+
|
35
|
+
def boot
|
36
|
+
shell "mkdir -p /tmp/kuzushi/"
|
37
|
+
shell "cd /tmp/kuzushi/ ; curl --silent '#{@url}' | tar xzv"
|
38
|
+
@config = JSON.parse(File.read("/tmp/kuzushi/#{@name}/config.json"))
|
39
|
+
end
|
40
|
+
|
41
|
+
def start
|
42
|
+
# load_config_stack(@name)
|
43
|
+
boot
|
44
|
+
run
|
45
|
+
end
|
46
|
+
|
47
|
+
def run
|
48
|
+
process_stack
|
49
|
+
log "----"
|
50
|
+
@tasks.each do |t|
|
51
|
+
log "TASK: #{t[:description]}"
|
52
|
+
t[:blk].call
|
53
|
+
end
|
54
|
+
log "----"
|
55
|
+
end
|
56
|
+
|
57
|
+
protected
|
58
|
+
|
59
|
+
def system
|
60
|
+
ohai = Ohai::System.new
|
61
|
+
ohai.all_plugins
|
62
|
+
ohai
|
63
|
+
end
|
64
|
+
|
65
|
+
def http_get(url)
|
66
|
+
RestClient.get(url)
|
67
|
+
end
|
68
|
+
|
69
|
+
def process_stack
|
70
|
+
script get("before")
|
71
|
+
|
72
|
+
process :packages
|
73
|
+
process :local_packages
|
74
|
+
process :gems
|
75
|
+
process :volumes
|
76
|
+
process :files
|
77
|
+
process :services
|
78
|
+
process :users
|
79
|
+
process :crontab
|
80
|
+
|
81
|
+
script get("after")
|
82
|
+
script get("init") if init?
|
83
|
+
end
|
84
|
+
|
85
|
+
## magic goes here
|
86
|
+
def process(type)
|
87
|
+
## if the file takes no args - just call it once
|
88
|
+
if method("process_#{type}").arity == 0
|
89
|
+
send("process_#{type}")
|
90
|
+
else
|
91
|
+
## else call it once per item
|
92
|
+
get_array(type).each do |item|
|
93
|
+
script item["before"]
|
94
|
+
if item.is_a? Hash
|
95
|
+
send("process_#{type}", OpenStruct.new(item))
|
96
|
+
else
|
97
|
+
send("process_#{type}", item)
|
98
|
+
end
|
99
|
+
script item["after"]
|
100
|
+
script item["init"] if init?
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def service_file(service)
|
106
|
+
file = []
|
107
|
+
file << "start on stopped rc RUNLEVEL=[2345]"
|
108
|
+
file << "stop on runlevel [!2345]"
|
109
|
+
file << "respawn" unless service.respawn == false
|
110
|
+
if service.user
|
111
|
+
file << "exec su -c '#{service.command}' #{service.user}" ## TODO add shell escaping here
|
112
|
+
else
|
113
|
+
file << "exec #{service.command}"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def process_service(service)
|
118
|
+
put_file(service_file(service), "/etc/init/#{service.name}.conf")
|
119
|
+
shell "service #{service.name} start"
|
120
|
+
end
|
121
|
+
|
122
|
+
def process_packages
|
123
|
+
@packages = get_array("packages")
|
124
|
+
task "install packages" do
|
125
|
+
shell "apt-get install -y #{@packages.join(" ")}" unless @packages.empty?
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def process_local_packages(p)
|
130
|
+
package(p) do |file|
|
131
|
+
task "install local package #{p}" do
|
132
|
+
shell "dpkg -i #{file}"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def process_gems(gem)
|
138
|
+
task "install gem #{gem}" do
|
139
|
+
shell "gem install #{gem} --no-rdoc --no-ri"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def process_volumes(v)
|
144
|
+
handle_ebs v if v.media == "ebs"
|
145
|
+
handle_raid v if v.media == "raid"
|
146
|
+
set_readahead v if v.readahead
|
147
|
+
set_scheduler v if v.scheduler
|
148
|
+
handle_format v if v.format
|
149
|
+
handle_mount v if v.mount
|
150
|
+
end
|
151
|
+
|
152
|
+
def handle_ebs(v)
|
153
|
+
task "wait for volume #{v.device}" do
|
154
|
+
wait_for_volume v.device
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def handle_raid(r)
|
159
|
+
task "create raid #{r.device}", :init => true do
|
160
|
+
shell "mdadm --create #{r.device} -n #{r.drives.size} -l #{r.level} -c #{r.chunksize || 64} #{r.drives.join(" ")}"
|
161
|
+
end
|
162
|
+
task "assemble raid #{r.device}" do ## assemble fails a lot with device busy - is udev to blame :(
|
163
|
+
if not dev_exists? r.device
|
164
|
+
shell "service stop udev"
|
165
|
+
shell "mdadm --assemble #{r.device} #{r.drives.join(" ")}"
|
166
|
+
shell "service start udev"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
add_package "mdadm"
|
170
|
+
end
|
171
|
+
|
172
|
+
def mount_options(m)
|
173
|
+
o = []
|
174
|
+
o << m.options if m.options
|
175
|
+
o << "size=#{m.size}M" if m.size and m.media == "tmpfs"
|
176
|
+
o << "mode=#{m.mode}" if m.mode
|
177
|
+
o << "noatime" if o.empty?
|
178
|
+
o.join(",")
|
179
|
+
end
|
180
|
+
|
181
|
+
def handle_mount(m)
|
182
|
+
task "mount #{m.mount}" do
|
183
|
+
unless mounted?(m.mount)
|
184
|
+
shell "mv #{m.mount} #{m.mout}.old" if File.exists?(m.mount)
|
185
|
+
shell "mkdir -p #{m.mount} && mount -o #{mount_options(m)} -t #{m.format || m.media} #{m.device || m.media} #{m.mount}"
|
186
|
+
shell "chown -R #{m.user}:#{m.group} #{m.mount}" if m.user or m.group
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def system_arch
|
192
|
+
system.kernel["machine"]
|
193
|
+
end
|
194
|
+
|
195
|
+
def mounted?(mount)
|
196
|
+
## cant use ohai here b/c it mashes drives together with none or tmpfs devices
|
197
|
+
mount = mount.chop if mount =~ /\/$/
|
198
|
+
!!(File.read("/proc/mounts") =~ / #{mount} /)
|
199
|
+
end
|
200
|
+
|
201
|
+
def package_arch
|
202
|
+
`dpkg --print-architecture`.chomp
|
203
|
+
end
|
204
|
+
|
205
|
+
def erb(data)
|
206
|
+
@system = system
|
207
|
+
ERB.new(data, 0, '<>').result(binding)
|
208
|
+
end
|
209
|
+
|
210
|
+
def process_files(f)
|
211
|
+
file(f) do |tmp|
|
212
|
+
task "write #{f.file}" do
|
213
|
+
cp_file(tmp, f.file)
|
214
|
+
shell "chmod #{f.mode} #{f.file}" if f.mode
|
215
|
+
shell "chown #{f.user} #{f.file}" if f.user
|
216
|
+
shell "chgrp #{f.group} #{f.file}" if f.group
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def process_crontab(cron)
|
222
|
+
user = cron.user || "root"
|
223
|
+
file(cron) do |tmp|
|
224
|
+
task "process crontab for #{user}" do
|
225
|
+
shell "crontab -u #{user} #{tmp}"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def process_users(user)
|
231
|
+
shell "useradd -m #{user}" ## this will just fail for users like 'root'
|
232
|
+
(user.authorized_keys || []).each do |key|
|
233
|
+
task "add authorized_key for user #{user.name}" do
|
234
|
+
shell "su - #{user.name} -c 'mkdir -p .ssh; echo \"#{key}\" >> .ssh/authorized_keys; chmod -R 0600 .ssh'"
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def set_readahead(v)
|
240
|
+
task "set readahead for #{v.device}" do
|
241
|
+
shell "blockdev --setra #{v.readahead} #{v.device}"
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def set_scheduler(v)
|
246
|
+
task "set scheduler for #{v.device}" do
|
247
|
+
shell "echo #{v.scheduler} > /sys/block/#{File.basename(v.device)}/queue/scheduler"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def handle_format(v)
|
252
|
+
return if v.format == "tmpfs"
|
253
|
+
task "formatting #{v.device}", :init => true do
|
254
|
+
label = "-L " + v.label rescue ""
|
255
|
+
shell "mkfs.#{v.format} -q #{label} #{v.device}" unless v.mount && mounted?(v.mount)
|
256
|
+
end
|
257
|
+
add_package "xfsprogs" if v.format == "xfs"
|
258
|
+
end
|
259
|
+
|
260
|
+
def add_package(p)
|
261
|
+
@packages << p unless @packages.include? p
|
262
|
+
end
|
263
|
+
|
264
|
+
def package(p, &block)
|
265
|
+
fetch("/packages/#{p}_#{package_arch}.deb") do |file|
|
266
|
+
block.call(file)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
|
271
|
+
def script(scripts)
|
272
|
+
to_array(scripts).each do |s|
|
273
|
+
if s =~ /^#!/
|
274
|
+
inline_script(s)
|
275
|
+
else
|
276
|
+
external_script(s)
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
def inline_script(script)
|
282
|
+
tmpfile(script) do |tmp|
|
283
|
+
task "run inline script" do
|
284
|
+
shell "#{tmp}"
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def external_script(script)
|
290
|
+
fetch("/scripts/#{script}") do |file|
|
291
|
+
task "run script #{script}" do
|
292
|
+
shell "chmod +x #{file} ; #{file}"
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
def tmpfile(content, file = "tmp_#{rand(1_000_000_000)}", &block)
|
298
|
+
path = "/tmp/kuzushi/#{File.basename(file)}"
|
299
|
+
put_file(content, path)
|
300
|
+
block.call(path) if block
|
301
|
+
path
|
302
|
+
end
|
303
|
+
|
304
|
+
def file(f, &blk)
|
305
|
+
## no magic here - move along
|
306
|
+
if f.template
|
307
|
+
fetch("/templates/#{f.template}", lambda { |data| erb data }, &blk)
|
308
|
+
else f.git
|
309
|
+
git_fetch(f)
|
310
|
+
else
|
311
|
+
fetch("/files/#{f.source || File.basename(f.file)}", &blk) unless f.template
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
def git_fetch(f)
|
316
|
+
FileUtils.mkdir_p(f.dir)
|
317
|
+
shell "cd dir && git init"
|
318
|
+
shell "cd dir && git remote add origin #{f.git}"
|
319
|
+
shell "cd dir && git fetch"
|
320
|
+
shell "cd dir && git checkout master"
|
321
|
+
shell "chown -R #{f.user}:#{f.group} #{f.dir}" if f.user || f.group ## is this needed? handled above in files?
|
322
|
+
end
|
323
|
+
|
324
|
+
### this needs to be brought up to date - way last version - no need to read and filter...
|
325
|
+
def fetch(file, filter = lambda { |d| d }, &block)
|
326
|
+
begin
|
327
|
+
tmpfile(filter.call(File.read("/tmp/kuzushi/#{@name}/#{file}")), file) do |tmp|
|
328
|
+
block.call(tmp)
|
329
|
+
end
|
330
|
+
rescue Object => e
|
331
|
+
error("error fetching file: #{file} : #{e.message}")
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
def error(message)
|
336
|
+
puts "ERROR :#{message}"
|
337
|
+
end
|
338
|
+
|
339
|
+
def get(key)
|
340
|
+
config[key.to_s]
|
341
|
+
end
|
342
|
+
|
343
|
+
def get_array(key)
|
344
|
+
to_array( get(key) )
|
345
|
+
end
|
346
|
+
|
347
|
+
def to_array(value)
|
348
|
+
[ value || [] ].flatten
|
349
|
+
end
|
350
|
+
|
351
|
+
def wait_for_volume(vol)
|
352
|
+
## Maybe use ohai here instead -- FIXME
|
353
|
+
until dev_exists? vol do
|
354
|
+
log "waiting for volume #{vol}"
|
355
|
+
sleep 2
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
def shell(cmd)
|
360
|
+
log "# #{cmd}"
|
361
|
+
Kernel.system cmd ## FIXME - need to handle/report exceptions here
|
362
|
+
end
|
363
|
+
|
364
|
+
def init?
|
365
|
+
@init ||= false
|
366
|
+
end
|
367
|
+
|
368
|
+
def task(description, options = {}, &blk)
|
369
|
+
return if options[:init] and not init?
|
370
|
+
@tasks << { :description => description, :blk => blk }
|
371
|
+
end
|
372
|
+
|
373
|
+
def dev_exists?(dev)
|
374
|
+
File.exists?("/sys/block/#{File.basename(dev)}")
|
375
|
+
end
|
376
|
+
|
377
|
+
def cp_file(src, dest)
|
378
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
379
|
+
FileUtils.cp(src, dest)
|
380
|
+
end
|
381
|
+
|
382
|
+
def put_file(data, dest)
|
383
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
384
|
+
File.open(dest,"w") do |f|
|
385
|
+
f.write(data)
|
386
|
+
f.chmod(0700)
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
def log(message)
|
391
|
+
puts message
|
392
|
+
end
|
360
393
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 36
|
9
|
+
version: 0.0.36
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Orion Henry
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-30 00:00:00 -04:00
|
18
18
|
default_executable: kuzushi
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|