-
Notifications
You must be signed in to change notification settings - Fork 10
/
onebootstrap
executable file
·626 lines (480 loc) · 14.7 KB
/
onebootstrap
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#!/usr/bin/env ruby
require "yaml"
require "tempfile"
require "erb"
require "pp"
begin
require "colored"
COLORED=true
rescue Exception => e
COLORED=false
end
DIR = File.dirname(File.realpath(__FILE__))
################################################################################
# Load OpenNebula
################################################################################
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
VAR_LOCATION = "/var/lib/one"
LIB_LOCATION = "/usr/lib/one"
ETC_LOCATION = "/etc/one"
else
RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
VAR_LOCATION = ONE_LOCATION + "/var"
LIB_LOCATION = ONE_LOCATION + "/lib"
ETC_LOCATION = ONE_LOCATION + "/etc"
end
$: << RUBY_LIB_LOCATION
require 'opennebula'
include OpenNebula
CLIENT = Client.new
################################################################################
# Read Configuration
################################################################################
yaml_location = ARGV[0] || File.join(DIR, "bootstrap", "default.d")
if File.directory?(yaml_location)
conf = []
conf_item = nil
Dir[File.join(yaml_location, "*.yaml")].sort.each do |f|
begin
conf_item = YAML.load_file(f)
rescue
STDERR.puts "'#{f}' is not a valid yaml file."
exit 1
end
if conf_item.instance_of? Array
conf += conf_item
elsif conf_item.instance_of? Hash
conf << conf_item
else
STDERR.puts "'#{f}' contains an unknown type."
exit 1
end
end
else
begin
conf = YAML.load_file(yaml_location)
rescue
STDERR.puts "'#{yaml_location}' is not a valid yaml file."
exit 1
end
end
################################################################################
# Helper
################################################################################
def error(e)
m = if e.instance_of?(String)
e
else
e.message
end
if COLORED
STDERR.puts m.red
else
STDERR.puts m
end
exit 1
end
def log(e, level=nil)
if COLORED
m = case level
when :ok
e.green
when :warn
e.cyan
when :info
e.blue
when :error
e.red
else
e
end
else
m = e
end
puts m
end
###############################################################################
# The TemplateParser Class parses a VM template file and builds a hash with
# the info. It does not check syntax.
###############################################################################
class TemplateParser
##########################################################################
# Patterns to parse the template File
##########################################################################
NAME_REG =/[\w\d_-]+/
VARIABLE_REG =/\s*(#{NAME_REG})\s*=\s*/
SIMPLE_VARIABLE_REG =/#{VARIABLE_REG}([^\[]+?)(#.*)?/
SINGLE_VARIABLE_REG =/^#{SIMPLE_VARIABLE_REG}$/
ARRAY_VARIABLE_REG =/^#{VARIABLE_REG}\[(.*?)\]/m
##########################################################################
##########################################################################
def initialize(template_string)
@conf=parse_conf(template_string)
end
def add_configuration_value(key,value)
add_value(@conf,key,value)
end
def [](key)
@conf[key.to_s.upcase]
end
def hash
@conf
end
def self.template_like_str(attributes, indent=true)
if indent
ind_enter="\n"
ind_tab=' '
else
ind_enter=''
ind_tab=' '
end
str=attributes.collect do |key, value|
if value
str_line=""
if value.class==Array
value.each do |value2|
str_line << key.to_s.upcase << "=[" << ind_enter
if value2 && value2.class==Hash
str_line << value2.collect do |key3, value3|
str = ind_tab + key3.to_s.upcase + "="
str += "\"#{value3.to_s}\"" if value3
str
end.compact.join(",\n")
end
str_line << "\n]\n"
end
elsif value.class==Hash
str_line << key.to_s.upcase << "=[" << ind_enter
str_line << value.collect do |key3, value3|
str = ind_tab + key3.to_s.upcase + "="
str += "\"#{value3.to_s}\"" if value3
str
end.compact.join(",\n")
str_line << "\n]\n"
else
str_line<<key.to_s.upcase << "=" << "\"#{value.to_s}\""
end
str_line
end
end.compact.join("\n")
str
end
##########################################################################
##########################################################################
private
def san_key(key)
key.strip.downcase.to_sym
end
def san_value(value)
value.strip.gsub(/"/, '') if value
end
#
#
#
def add_value(conf, key, value)
if conf[key]
if !conf[key].kind_of?(Array)
conf[key] = [conf[key]]
end
conf[key] << value
else
conf[key] = value
end
end
#
# Parses the configuration file and
# creates the configuration hash
#
def parse_conf(template_string)
conf=Hash.new
template_string.scan(SINGLE_VARIABLE_REG) {|m|
key=san_key(m[0])
value=san_value(m[1])
add_value(conf, key, value)
}
template_string.scan(ARRAY_VARIABLE_REG) {|m|
master_key=san_key(m[0])
pieces=m[1].split(',')
vars=Hash.new
pieces.each {|p|
key, value=p.split('=')
vars[san_key(key)]=san_value(value)
}
add_value(conf, master_key, vars)
}
conf
end
end
################################################################################
# Boostrap Classes
################################################################################
class BS
attr_accessor :resource
RETRIES = 600
INTERVAL = 1
def self.factory(hash)
resources = {
"host" => BSHost,
"image" => BSImage,
"template" => BSTemplate,
"net" => BSNet,
"datastore" => BSDatastore,
"user" => BSUser,
"cluster" => BSCluster,
"marketapp" => BSMarketApp
}
return resources[hash["type"]].new(hash)
end
def initialize(hash)
@hash = hash
end
def to_s
hash = "@type:#{@hash["type"]}"
if (name = @hash["name"] || @hash["template"]["name"])
hash << "/@name:#{name}"
end
if (the_id = @hash["id"])
hash << "/@id:#{the_id}"
end
return hash
end
def do!
log("* #{self}", :info)
case action = @hash["action"].to_sym
when :create
if id.nil?
create
wait
log("> Create", :ok)
else
log("> Skipping: resource exists", :warn)
end
when :update
if !id.nil?
update
wait
log("> Update", :ok)
else
log("> Skipping: does not exist", :warn)
end
else
error("Unknown action: '#{action}'.")
end
end
def update
resource_class = self.class.class_variable_get(:@@resource_class)
append = @hash["append"] || true
@resource = resource_class.new_with_id(id, CLIENT)
bstmpl = @hash["template"]
template = TemplateParser.template_like_str(bstmpl) if !bstmpl.nil?
rc = @resource.update(template, append)
error(rc) if OpenNebula.is_error?(rc)
end
def id
if (id = @hash["id"])
return id
end
name = @hash["name"] || @hash["template"]["name"] rescue nil
if name.nil?
error("Fields 'NAME' or 'ID' are required")
return nil
end
pool_class = self.class.class_variable_get(:@@pool_class)
pool = pool_class.new(CLIENT)
rc = pool.info
error(rc) if OpenNebula.is_error?(rc)
pool.each do |e|
return e["ID"] if e["NAME"] == name
end
return nil
end
def wait
return true
end
def wait_states(states)
RETRIES.times do
rc = @resource.info
error(rc) if OpenNebula.is_error?(rc)
if [states].flatten.include?(@resource.state_str)
return true
end
sleep INTERVAL
end
return false
end
def resource_wait(&block)
RETRIES.times do
rc = @resource.info
error(rc) if OpenNebula.is_error?(rc)
return true if yield @resource
sleep INTERVAL
end
return false
end
def set_resource
resource_class = self.class.class_variable_get(:@@resource_class)
@resource = resource_class.new(resource_class.build_xml(id), CLIENT)
end
end
class BSHost < BS
@@resource_class = Host
@@pool_class = HostPool
def create
@resource = @@resource_class.new(@@resource_class.build_xml, CLIENT)
args = [@hash["name"],
@hash["im"],
@hash["vmm"]]
if (cluster_id = @hash["cluster_id"])
args << cluster_id
end
rc = @resource.allocate(*args)
return error(rc) if OpenNebula.is_error?(rc)
@resource.disable if @hash["disable"]
end
def wait
return true if @hash["nowait"]
wait_states(["MONITORING_MONITORED", "MONITORED"])
end
end
class BSImage < BS
@@resource_class = Image
@@pool_class = ImagePool
def create
@resource = @@resource_class.new(@@resource_class.build_xml, CLIENT)
template = TemplateParser.template_like_str(@hash["template"])
rc = @resource.allocate(template, @hash["ds_id"])
error(rc) if OpenNebula.is_error?(rc)
end
def wait
RETRIES.times do |t|
rc = @resource.info
error(rc) if OpenNebula.is_error?(rc)
STDERR.puts "#{t}:#{@resource.state_str}"
if @resource.state_str == "READY"
return true
end
sleep INTERVAL
end
return false
end
end
class BSNet < BS
@@resource_class = VirtualNetwork
@@pool_class = VirtualNetworkPool
def create
@resource = @@resource_class.new(@@resource_class.build_xml, CLIENT)
template = TemplateParser.template_like_str(@hash["template"])
args = [template]
if (cluster_id = @hash["cluster_id"] rescue nil)
args << cluster_id
end
rc = @resource.allocate(*args)
error(rc) if OpenNebula.is_error?(rc)
end
end
class BSDatastore < BS
@@resource_class = Datastore
@@pool_class = DatastorePool
def create
@resource = @@resource_class.new(@@resource_class.build_xml, CLIENT)
template = TemplateParser.template_like_str(@hash["template"])
args = [template]
if (cluster_id = @hash["cluster_id"] rescue nil)
args << cluster_id
end
rc = @resource.allocate(*args)
error(rc) if OpenNebula.is_error?(rc)
end
def wait
return false if !wait_states(["READY"])
unless @hash["nowait"]
resource_wait {|res| res["TOTAL_MB"].to_s != "0" }
end
end
end
class BSTemplate < BS
@@resource_class = Template
@@pool_class = TemplatePool
def create
@resource = @@resource_class.new(@@resource_class.build_xml, CLIENT)
template = TemplateParser.template_like_str(@hash["template"])
rc = @resource.allocate(template)
error(rc) if OpenNebula.is_error?(rc)
end
end
class BSUser < BS
@@resource_class = User
@@pool_class = UserPool
def create
@resource = @@resource_class.new(@@resource_class.build_xml, CLIENT)
args = [@hash["name"], @hash["password"]]
if (driver = @hash["driver"])
args << driver
end
rc = @resource.allocate(*args)
error(rc) if OpenNebula.is_error?(rc)
end
end
class BSCluster < BS
@@resource_class = Cluster
@@pool_class = ClusterPool
def create
@resource = @@resource_class.new(@@resource_class.build_xml, CLIENT)
hosts = @hash["hosts"]
vnets = @hash["vnets"]
dss = @hash["datastores"]
rc = @resource.allocate(@hash["name"])
return error(rc) if OpenNebula.is_error?(rc)
hosts.each { |h|
bsh = BSHost.new({"name" => h})
bshid = bsh.id
@resource.addhost(bshid.to_i) if !bshid.nil?
}
vnets.each { |n|
bsn = BSNet.new({"name" => n})
bsnid = bsn.id
@resource.addvnet(bsnid.to_i) if !bsnid.nil?
}
dss.each { |d|
bsd = BSDatastore.new({"name" => d})
bsdid = bsd.id
@resource.adddatastore(bsdid.to_i) if !bsdid.nil?
}
update
end
end
class BSMarketApp < BS
@@resource_class = MarketPlaceApp
@@pool_class = MarketPlaceAppPool
def create
@resource = @@resource_class.new(@@resource_class.build_xml(@hash["id"]), CLIENT)
rc = @resource.info
error(rc) if OpenNebula.is_error?(rc)
rc = @resource.export(
:dsid => @hash["ds_id"],
:name => @hash["name"]
)
error(rc) if OpenNebula.is_error?(rc)
end
def id
t = BSTemplate.new({"name" => @hash["name"]})
i = BSImage.new({"name" => @hash["name"]})
return t if t.id
return i if i.id
nil
end
def wait
i = BSImage.new({"name" => @hash["name"]})
i.set_resource
i.wait
end
end
################################################################################
# Create Resources
################################################################################
conf.each do |hash|
r = BS.factory(hash)
r.do!
end if conf && !conf.empty?