Class: BrocadeREST::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/brocade/manifest.rb

Direct Known Subclasses

PuppetManifest

Instance Method Summary collapse

Constructor Details

#initialize(type, uri, restVersion, object, root = nil, traverseArrays = false) ⇒ Manifest

Returns a new instance of Manifest.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/brocade/manifest.rb', line 11

def initialize(type, uri, restVersion, object, root=nil, traverseArrays=false)
	@type = type
	@type_ = type.gsub(/[\/\.-]|%20/, "_").downcase
	@uri = uri.path
	@restVersion = restVersion
	@root = root
	@json = nil
	@data = nil
	@isBinary = false
	@object = object
	@params = {}
	@required = {}
	@maxKeyLength = 0
	@template = nil;
	@traverseArrays = traverseArrays
end

Instance Method Details

#addRequiredParam(name, example) ⇒ Object

Add the given parameter to the list of mandatory params for this manifest



29
30
31
# File 'lib/brocade/manifest.rb', line 29

def addRequiredParam(name, example)
	@required[name] = { class: example.class, example: example }
end

#checkRequires(parentFile, preReq, manifestDir, builtins, outfile) ⇒ Object

This function generates the requires meta-data for puppet. We check the prereqs provided and create a relationship for each object we use. If builtins are set to be excluded, but we rely on one (eg monitors_ping), then include it here.



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
# File 'lib/brocade/manifest.rb', line 346

def checkRequires(parentFile,preReq,manifestDir,builtins,outfile)
	requires = "["
	preReq[parentFile].each do |req|
		reqObject = req.shift
		reqVar = req.shift
		reqType = req.shift
		if (@params.has_key?(reqVar))
			if (@params[reqVar].is_a? Array)
				@params[reqVar].each do |item|
					if item.is_a?(Hash)
						puts(" --!!-- TODO : we do not yet write 'require's for Aptimizer or SNI mappings !!--!!")
					else
						ro_ = reqObject.gsub(/[\/\.\s-]|%20/, "_")
						item_ = item.gsub(/[\/\.\s-]|%20/, "_").downcase
						if File.exist?("#{manifestDir}/#{ro_.downcase}_#{item_}.pp")
							# This is a builtin class
							if (!builtins)
								puts("Relationship found for Built-in object: Including: pulsevtm::#{ro_.downcase}_#{item_}")
								# builtins are disabled, so check and include if needed
								lines = IO.readlines(outfile)
								if ( lines.grep(/pulsevtm::#{ro_.downcase}_#{item_}/).empty? )
									nodefile = File.open(outfile,"a")
									nodefile.puts("\ninclude pulsevtm::#{ro_.downcase}_#{item_}\n")
									nodefile.close()
								end  
							end
							requires += " Class[Pulsevtm::#{ro_}_#{item_}], "
						else
							escaped = item.gsub(' ', '%20')
							requires += " Pulsevtm::#{reqObject}['#{escaped}'], "
						end
					end
				end
			elsif (@params[reqVar].is_a? String)
				if @params[reqVar] == ''
					next
				end
				if req.empty? or ( (!req.empty?) and (!req.include?(@params[reqVar])) )
					escaped = @params[reqVar].gsub(' ', '%20')
					requires += " Pulsevtm::#{reqObject}['#{escaped}'], "
				end
			end
		end
		if requires != '['
			@params["require"] = requires + "]"
		end
	end
end

#decodeJSONObject

decode the JSON structure into the params hash



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/brocade/manifest.rb', line 76

def decodeJSON()
	if @isBinary
		return
	end
	if @json == nil
		puts("** ERROR ** JSON is null for #{@type}")
		return
	end
	parsed = JSON.parse(@json)
	if @root.nil?
		genParams(nil,parsed)
	else
		genParams(nil,parsed[@root])
	end
end

#dumpObject

Dump detailed information about this manifest to stdout



493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/brocade/manifest.rb', line 493

def dump()
	puts( "================================")
	puts("Type: #{@type}")
	puts("URI: #{@uri}")
	puts("Binary: #{@isBinary}")
	puts("object: #{@object}")
	puts("Required: #{@required}")
	if ( @isBinary )
		puts("DATA: #{@data}")
	else
		puts("JSON: #{@json}")
	end
	puts( "================================")
end

#findParent(filename, extension = ".erb") ⇒ Object

Check to see if this filename has a parent object. If the file type is erb, then compare the file with it’s parent. If the file type is pp, then check for Define, ignore Class.



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
# File 'lib/brocade/manifest.rb', line 461

def findParent(filename, extension=".erb")
	parentFile = nil
	parent = nil
	filetree = filename.chomp(extension).split("_")
	filetree.each do |test|
		if parent == nil
			parent = test
		else
			parent += "_" + test
		end
		test = parent+extension
		if ( test == filename )
			break
		elsif ( File.exist?(test) )
			if extension == ".erb"
				if FileUtils.compare_file(test,filename)
					parentFile=test
				end
			elsif extension == ".pp"
				if IO.read(test,7) == "# === D"
					parentFile=test
				end
			end
		end
	end
	if parentFile != nil
		return File.basename parentFile
	end
	return nil
end

#genBinary(outputDir) ⇒ Object

If we’re a binary manifest, then store our data in an external file.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/brocade/manifest.rb', line 193

def genBinary(outputDir)

	if ( ! @isBinary )
		return true
	end

	if ( @data == nil || @data == "" )
		return true
	end

	filename = "#{outputDir}/#{@type_}.data"
	binary = File.open(filename, "w")
	binary.puts @data
	binary.close

end

#genNodeConfig(outfile, allParams, builtins, preReq, manifestDir, binDir = nil) ⇒ Object

Write a node config to the given outfile. By default we write all configuration to the outfile, however… If allParams is false, then ignore params which are using defaults If builtin is false, then don’t create config for built-in objects (unless they’re in use)



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
# File 'lib/brocade/manifest.rb', line 214

def genNodeConfig(outfile, allParams, builtins, preReq, manifestDir, binDir=nil)

	isBuiltin = false
	myfile = "#{manifestDir}/#{@type_}.pp"

	if File.exist?(myfile)
		# I have my own manifest, I'm a builtin class
		isBuiltin = true
		parentFile = File.basename(myfile).chomp(".pp")
	else
		# Locate my type definition file
		parentFile = findParent(myfile, extension=".pp").chomp(".pp")
	end

	# Generate the classHash which will have all the defaults for my type
	if parentFile != nil
		classHash = parseManifest(manifestDir, parentFile)
		name = @uri.sub(/.*?\/config\/active\/.*\/(.*)/,'\1')
	else
		classHash = {}
	end

	# If I'm binary and a builtin class, then read in my default data
	if @isBinary
		if isBuiltin
			myDataFile = classHash["content"].sub("pulsevtm","#{manifestDir}../files")[7..-4]
			if File.exist?(myDataFile) 
				myData = File.read(myDataFile) 
			else
				myData = ""
			end
		else
			myData = ""
		end

	# If I'm a JSON object, then decode the data
	else
		decodeJSON()

		# Process the preReqs to determine which objects we have dependencies on. This will 
		# populate the requires meta-data, and inform puppet of our relationships to ensure
		# objects are created in a sensible order. 
		if preReq.has_key?(parentFile)
			checkRequires(parentFile,preReq,manifestDir,builtins,outfile)
		end

		# If we're generating a sparse config or excluding default built-in classes
		# then check to see which params are using defaults and drop them.
		if (!allParams) or (isBuiltin and !builtins) 
			@params.each do |key,value|
			value = inspectValue(value)
				if classHash.has_key?(key)
					if classHash[key] == value
						@params.delete(key)
					else
						puts("Including/Declaring #{@type}, uses custom setting for: #{key}")
					end
				end
			end
		end
	end

	# If we're a unmodified built-in, and built-ins are excluded, then we're done
	if (!builtins) and isBuiltin 
		if (!@isBinary) and @params.empty?
			puts("Ignoring BuiltIn JSON Object: #{@type_}")
			return
		elsif @isBinary and myData == @data
			puts("Ignoring BuiltIn Binary Object: #{@type_}")
			return
		end
	end

	# Append my configuration to the output file
	nodefile = File.open(outfile,"a")
	if isBuiltin
		if @isBinary

			if (myData != @data) or allParams
				dataOut = writeBinFile(outfile,binDir)
				nodefile.puts("\nclass { 'pulsevtm::#{@type_}':\n")
				nodefile.puts("  ensure => present,\n")
				nodefile.puts("  content => file('#{dataOut}'),\n")
				nodefile.puts("}\n\n")
			else
				nodefile.puts("include pulsevtm::#{@type_}\n")
			end

		else

			if @params.empty?
				nodefile.puts("include pulsevtm::#{@type_}\n")
			else
				nodefile.puts("\nclass { 'pulsevtm::#{@type_}':\n")
				@params.each do |key,value|
					value = inspectValue(value)
					sp = " " * ( @maxKeyLength - key.length )
					nodefile.puts("  #{key}#{sp} => #{value},\n")
				end
				nodefile.puts("}\n\n")
			end

		end
	else

		nodefile.puts("\npulsevtm::#{parentFile} { '#{name}':\n")
		if @isBinary
				dataOut = writeBinFile(outfile,binDir)
				nodefile.puts("  ensure => present,\n")
				nodefile.puts("  content => file('#{dataOut}'),\n")
		else

			@maxKeyLength >= 6 ? sp = " " * ( @maxKeyLength - 6 ) : sp = " "
			nodefile.puts("  ensure#{sp} => present,\n")
			if ! @params.empty?
				@params.each do |key,value|
					value = inspectValue(value)
					sp = " " * ( @maxKeyLength - key.length )
					nodefile.puts("  #{key}#{sp} => #{value},\n")
				end
			end
		end
		nodefile.puts("}\n\n")

	end
	nodefile.close()

end

#genParams(name, data) ⇒ Object

recursive function used by decodeJSON to generate parameter names and calculating spacing for the manifest.



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
# File 'lib/brocade/manifest.rb', line 94

def genParams(name, data)
	if data.is_a?(Hash)
		data.each do |key, value|
			name.nil? ? new=key : new = "#{name}__#{key}"
			genParams(new, value)
		end
	elsif @traverseArrays and data.is_a?(Array) and (!data.empty?) and (!name.include?("__aRrAy"))
		data.each do |item|
			name.nil? ? new="aRrAy" : new="#{name}__aRrAy"
			genParams(new,item)
		end
	else
		@maxKeyLength = name.length if name.length > @maxKeyLength
		if data == nil or data == ""
			data = ""
		end
		if ! @required.has_key?(name)
			if name.include?("__aRrAy")
				if @params.has_key?(name)
					@params[name].push data
				else
					@params[name] = [ data ]
				end
			else
				@params[name] = data 
			end
		end
	end
end

#genTemplate(outputDir, instvar) ⇒ Object

Generate our template



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
# File 'lib/brocade/manifest.rb', line 152

def genTemplate(outputDir, instvar)

	instvar ? prefix="@" : prefix=""

	if @isBinary
		return true;
	end

	parsed = JSON.parse(@json)

	# Generate the template string for each property
	if @root.nil?
		erb = ''
		erb += genTemplateString(nil,parsed,nil,prefix)
	else
		erb = "{\"#{@root}\":"
		erb += genTemplateString(nil,parsed[@root],nil,prefix)
		erb += "}"
	end
	erb = erb.gsub(',}', '}')
	erb = erb.gsub('},}','}}')

	# Write the template to disk
	filename = "#{outputDir}/#{@type_}.erb"
	template = File.open(filename, "w")
	template.puts erb
	template.close

	# Dedupe our template
	parentFile = findParent(filename)
	if parentFile != nil
		@template = parentFile
		puts ("Deleting duplicate template: #{filename}. Using #{parentFile}\n")
		FileUtils.rm filename
	else
		puts("No Parent for: #{filename}")
	end

end

#genTemplateString(name, data, templvar, prefix) ⇒ Object

function to generate the template string for a parameter. This is called by genTemplate()



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
# File 'lib/brocade/manifest.rb', line 125

def genTemplateString(name,data,templvar,prefix)

	if data.is_a?(Hash)
		propStr = "{" 
		data.each do |key, value|
			propStr += '"'+key+'":'
			templvar.nil? ? nextVar=key : nextVar = "#{templvar}__#{key}"
			propStr += genTemplateString(key, value, nextVar, prefix) + ","
		end
		return propStr + "}"
	elsif @traverseArrays and data.is_a?(Array) and (!data.empty?)
		propStr = "[" 
		templvar.nil? ? nextVar="aRrAy" : nextVar = "#{templvar}__aRrAy"
		propStr += genTemplateString(name,data[0],nextVar, prefix) + ","
		return propStr + "]"
	elsif data.is_a?(Integer) or data.is_a?(Float)
		return "<%= "+prefix+templvar+" %>"
	elsif data.is_a?(Array)
		return "<%= "+prefix+templvar+" %>"
	elsif data == false or data == true
		return "<%= "+prefix+templvar+" %>"
	else
		return "\"<%= "+prefix+templvar+" %>\""
	end
end

#getDataObject

Get the current binary data



49
50
51
# File 'lib/brocade/manifest.rb', line 49

def getData
	return @data
end

#getJSONObject

Get the current JSON blob



44
45
46
# File 'lib/brocade/manifest.rb', line 44

def getJSON
	return @json
end

#getTypeObject

Get the type, as it would appear on disk



39
40
41
# File 'lib/brocade/manifest.rb', line 39

def getType
	return @type_
end

#inspectValue(value) ⇒ Object

Convert values from the format used internally by ruby into the correct format for writing to puppet manifests.



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/brocade/manifest.rb', line 441

def inspectValue(value)
	if value == ""
		value = "undef"
	elsif value.is_a?(Array)
		value = "'" + JSON.generate(value) + "'"
	elsif value.is_a?(String)
		if value.start_with?('[ Pulsevtm::')
			value = value.inspect[1...-1] 
		elsif value.start_with?('[ Class[')
			value = value.inspect[1...-1] 
		else
			value = "'" + value.inspect[1...-1] + "'"
		end
	end
	return value
end

#isBinaryObject

Get the isBinary flag



34
35
36
# File 'lib/brocade/manifest.rb', line 34

def isBinary
	return @isBinary
end

#parseManifest(manifestDir, parentFile) ⇒ Object

Generate a classHash of default values from a given manifest. This classHash can then be used in direct comparisson with @params



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/brocade/manifest.rb', line 417

def parseManifest(manifestDir,parentFile)
	classHash = {}
	File.open("#{manifestDir}/#{parentFile}.pp", "r").each_line do |line|
		line.scan(/\s+\$([^\s]+)\s+=\s+['"]{0,1}(.*?)['"]{0,1},\n$/) do |key,value|
			if value == "undef"
				classHash[key] = 'undef'
			elsif value.match(/^[0-9]+$/)
				classHash[key] = Integer(value)
			elsif value == "false"
				classHash[key] = false
			elsif value == "true"
				classHash[key] = true
			elsif value == "[]"
				classHash[key] = "'[]'"
			else
				classHash[key] = "'" + value + "'"
			end
		end
	end
	return classHash
end

#setBinary(isBinary) ⇒ Object

Set this manifest as a binary object



54
55
56
57
58
59
# File 'lib/brocade/manifest.rb', line 54

def setBinary(isBinary)
	@isBinary = isBinary
	if isBinary
		@json = nil
	end
end

#setData(data) ⇒ Object

Set the binary data of this manifest



69
70
71
72
73
# File 'lib/brocade/manifest.rb', line 69

def setData(data)
	@isBinary = true
	@json = nil
	@data = data
end

#setJSON(json) ⇒ Object

Set this manifest as a JSON object



62
63
64
65
66
# File 'lib/brocade/manifest.rb', line 62

def setJSON(json)
	@isBinary = false
	@json = json
	@data = nil
end

#writeBinFile(outfile, binDir) ⇒ Object

Write the binary data out to a file in the binDir. If binDir is not specified then create a file using the outfile as a prefix.



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/brocade/manifest.rb', line 397

def writeBinFile(outfile, binDir)

	if binDir.nil?
		outfile = outfile.gsub(/\./,"_") + "_" + @type_ + ".data"
	else
		outfile = binDir + "/" + @type_ + ".data"
	end

	# Write the file as 8bit ascii to avoid corruption
	outfile = File.expand_path(outfile)
	out = File.open(outfile, "wb:ASCII-8BIT")
	out.write(@data)
	out.close()

	return outfile

end