This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
razor.rb
219 lines (195 loc) · 5.97 KB
/
razor.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
require 'net/http'
require 'pathname'
require Pathname.new(__FILE__).dirname.expand_path
module PuppetX::PuppetLabs
class Razor
def initialize(razor=nil)
@razor = razor
end
def get_images
begin
# This command does not support '-w' flag yet, so processing the output.
images = razor 'image', 'get'
Puppet.debug(images.inspect)
@images = images.split("\n\n").collect{ |x| Hash[*(x.split(/\n|=>/) - ['Images']).collect{|y| y.strip!}] }
rescue
Puppet.debug "Failed to gather results from command 'razor image get'."
@images = {}
end
@images = image_map(@images)
end
def get_models
output = razor '-w', 'model', 'get', 'default'
uuids = parse(output).collect{ |x| x['@uuid'] if x.include? '@uuid'}.compact
models = uuids.collect do |id|
output = razor '-w', 'model', 'get', id
res = strip_at(parse(output).first)
metadata = Hash.new
res[:req_metadata_hash].keys.each do |k|
metadata[k] = res[k] if res.include? k
end
model = {
:name => res[:label],
:description => res[:description],
:image => get_image_name(res[:image_uuid]),
:metadata => metadata,
:template => res[:name],
:uuid => res[:uuid],
}
end
end
def get_policies
output = razor '-w', 'policy', 'get', 'default'
uuids = parse(output).collect{ |x| x['@uuid'] if x.include? '@uuid'}.compact
policies = uuids.collect do |id|
output = razor '-w', 'policy', 'get', id
res = strip_at(parse(output).first)
policy = {
:name => res[:label],
:description => res[:description],
:uuid => res[:uuid],
:model => res[:model][:label],
:tags => res[:tags],
:enabled => res[:enabled],
:maximum => res[:maximum_count],
:broker => res[:broker] ? res[:broker][:name] : 'none',
}
end
end
def get_tags
output = razor '-w', 'tag', 'get'
uuids = parse(output).collect{ |x| x['@uuid'] if x.include? '@uuid'}.compact
tags = uuids.collect do |id|
output = razor '-w', 'tag', 'get', id
res = strip_at(parse(output).first)
matchers = res[:tag_matchers].collect{ |m| strip_at(m) }
matchers = matchers.collect do |m|
m.delete_if{ |k, v| [:classname, :id, :is_template, :uri, :version].include? k }
m
end
tag = {
:name => res[:name],
:uuid => res[:uuid],
:tag_matcher => matchers,
:tag_label => res[:tag],
}
end
end
def add_tag(value)
output = razor '-w', 'tag', 'add', value
uuid = parse(output).collect{ |x| x['@uuid'] if x.include? '@uuid'}.first
end
def get_brokers
output = razor '-w', 'broker', 'get', 'default'
uuids = parse(output).collect{ |x| x['@uuid'] if x.include? '@uuid'}.compact
brokers = uuids.collect do |id|
output = razor '-w', 'broker', 'get', id
res = strip_at(parse(output).first)
broker = {
:name => res[:name],
:description => res[:description],
:uuid => res[:uuid],
:plugin => res[:plugin],
:servers => res[:servers],
:version => res[:broker_version],
}
end
end
def get_model_uuid(name)
begin
model = models.find{|x| x[:name] == name}
model[:uuid]
rescue Exception => e
Puppet.debug e.message
raise Puppet::Error, "Failed to find model uuid for label #{name}."
end
end
def get_image_name(uuid)
begin
image = images.find{|x| x[:uuid] == uuid}
image[:name]
rescue Exception => e
Puppet.debug e.message
raise Puppet::Error, "Failed to find image name for uuid #{uuid}."
end
end
def get_image_uuid(name)
begin
image = images.find{|x| x[:name] == name}
image[:uuid]
rescue Exception => e
Puppet.debug e.message
raise Puppet::Error, "Failed to find image uuid for name #{name}."
end
end
def get_broker_uuid(name)
begin
broker = brokers.find{|x| x[:name] == name}
broker[:uuid]
rescue Exception => e
Puppet.debug e.message
raise Puppet::Error, "Failed to find broker uuid."
end
end
private
define_method(:razor) do |*args|
@razor.call args
end
def brokers
@brokers ||= get_brokers
end
def images
@images ||= get_images
end
def models
@models ||= get_models
end
def parse(response)
result = PSON.parse(response)
if result.include? 'response'
result['response']
else
raise Puppet::Error, "Failed to parse output from razor: \n#{response.inspect}"
end
end
def strip_at(val)
result = Hash.new
val.each do |k, v|
if v.is_a? Hash
result[k.delete('@').to_sym] = strip_at(v)
else
result[k.delete('@').to_sym] = v
end
end
result
end
def image_map(images)
image_keymap = {
'ISO Filename' => :isoname,
'OS Name' => :name,
'Version' => :version,
'OS Version' => :version,
'Type' => :type,
'UUID' => :uuid,
'Path' => :path,
'Status' => :status,
}
image_type = {
'MicroKernel Image' => 'mk',
'OS Install' => 'os',
'VMware Hypervisor Install' => 'esxi',
}
result = images.collect do |i|
image = i.map do |k, v|
if k == 'Type'
[image_keymap[k], image_type[v]]
elsif image_keymap.include? k
[image_keymap[k], v]
end
end
Hash[image.compact]
end
result
end
end
end