Class: BrocadeREST::PuppetManifest

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

Instance Method Summary collapse

Methods inherited from Manifest

#addRequiredParam, #decodeJSON, #dump, #genBinary, #genParams, #genTemplate, #genTemplateString, #getData, #getJSON, #getType, #isBinary, #setBinary, #setData, #setJSON, #writeBinFile

Constructor Details

#initialize(type, uri, restVersion, object, root = "properties", traverseArray = false) ⇒ PuppetManifest

Returns a new instance of PuppetManifest.



13
14
15
16
# File 'lib/brocade/puppetmanifest.rb', line 13

def initialize(type, uri, restVersion, object, root="properties", traverseArray=false)
	super(type,uri,restVersion,object,root,traverseArray)
	@PUP4X = true;
end

Instance Method Details

#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.



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

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
							ro_[0] = ro_[0].downcase
							requires += " Class[pulsevtm::#{ro_}_#{item_}], "
						else
							escaped = item.gsub(' ', '%20')
							if reqObject == "Rules"
								escaped = escaped.gsub(/^\/{0,1}(.*?)\*{0,1}$/,"\\1")
							end
							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')
					if reqObject == "Rules"
						escaped = escaped.gsub(/^\/{0,1}(.*?)\*{0,1}$/,"\\1")
					end
					requires += " Pulsevtm::#{reqObject}['#{escaped}'], "
				end
			end
		end
		if requires != '['
			@params["require"] = requires + "]"
		end
	end
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.



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

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

#genManifest(outputDir, isClass = false) ⇒ Object

Generate the manifest.



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

def genManifest(outputDir,isClass=false)

	# If we have documentation available in the skel/docs folder then read it.
	# Else if we have a parent type, refer to that manifest.
	docfile = "skel/docs/#{@type_}.doc"
	if ( File.exist?(docfile) )
		documentation = File.read(docfile)
		if isClass
			documentation.sub!("<CLASS-OR-TYPE-DECLARATION>","class {'pulsevtm::#{@type_}':")
		else 
			documentation.sub!("<CLASS-OR-TYPE-DECLARATION>","pulsevtm::#{@type_} { 'example':")
		end
	elsif (@template != nil)
		parent = @template.chomp(".erb")
		documentation = "#\n# This class is a direct implementation of brocadvtm::#{parent}\n"
		documentation += "#\n# Please refer to the documentation in that module for more information\n"
		documentation += "#\n"
	else
		documentation = ""
	end

	# build the parameters hash from the raw JSON
	decodeJSON()

	# Built in objects should be classes, while types should get defines.
	# There is only one Ping monitor, but theyre are lots of monitors
	if isClass
		desc = "# === class: pulsevtm::#{@type_}\n"
		code = "class pulsevtm::#{@type_} (\n"
		@maxKeyLength >= 6 ? sp = " " * ( @maxKeyLength - 6 ) : sp = " "
		code += "  \$ensure#{sp} = present,\n"
	else
		desc = "# === Define: pulsevtm::#{@type_}\n"
		code = "define pulsevtm::#{@type_} (\n"
		code += "  \$ensure,\n"
	end

	# content and ensure are the only params for binaries.
	if @isBinary 
		if isClass 
			code += "  \$content = file('pulsevtm/#{@type_}.data'),\n){\n"
		else
			code += "  \$content,\n){\n"
		end
	else

		# required keys have no default, puppet will force the user to enter
		# a value when they declare an instance of this type
		@required.each_key do |key|
			$stderr.puts($key)
			code += "  \$#{key},\n"
		end

		# Everything else is optional, and we use the defaults found by
		# walking the API
		@params.each do |key,value|
			value = inspectValue(value)
			sp = " " * ( @maxKeyLength - key.length )
			code += "  \$#{key}#{sp} = #{value},\n"
		end
		code += "){\n"
	end

	# Our manifests all look the same, include the parent class and it's params
	code += "  include pulsevtm\n"
	code += "  $ip              = $pulsevtm::rest_ip\n"
	code += "  $port            = $pulsevtm::rest_port\n"
	code += "  $user            = $pulsevtm::rest_user\n"
	code += "  $pass            = $pulsevtm::rest_pass\n"
	code += "  $purge           = $pulsevtm::purge\n"
	code += "  $purge_state_dir = $pulsevtm::purge_state_dir\n\n"
	code += "  info (\"Configuring #{@type_} ${name}\")\n"

	# Now configure our customer type: vtmrest
	if isClass
		code += "  vtmrest { '#{@type}':\n"
	else
		code += "  vtmrest { \"#{@type}/\${name}\":\n"
	end
	code += "    ensure   => $ensure,\n"
	code += "    before   => Class[pulsevtm::purge],\n"
	code += "    endpoint => \"https://\${ip}:\${port}/api/tm/#{@restVersion}/config/active\",\n"
	code += "    username => $user,\n"
	code += "    password => $pass,\n"
	if @isBinary
		code += "    content  => $content,\n"
		code += "    type     => 'application/octet-stream',\n"
	else
		if @template == nil
			code += "    content  => template('pulsevtm/#{@type_}.erb'),\n"
		else
			code += "    content  => template('pulsevtm/#{@template}'),\n"
		end
		code += "    type     => 'application/json',\n"
		code += "    internal => '#{@type_}',\n"
		code += "    failfast => $pulsevtm::failfast,\n"
	end
	code += "    debug    => $pulsevtm::debug,\n"
	code += "  }\n\n"

	# Now the purge section. figure out where this manifest should store it's name if purge
	# is enabled.
	code += "  if ( $purge ) {\n"
	if (isClass)
		if @template
			cFile = @template.chomp(".erb")
		else
			if @type.include?('/')
				cFile = @type[0..@type.rindex('/')-1].gsub(/\//,'_')
			else
				cFile = @type
			end
		end
		code += "    ensure_resource('file', \"${purge_state_dir}/#{cFile}\", {ensure => present})\n"
		code += "    file_line { '#{@type}':\n"
		code += "      line => '#{@type}',\n"
		code += "      path => \"${purge_state_dir}/#{cFile}\",\n"
		code += "    }\n"
	else
		code += "    ensure_resource('file', \"${purge_state_dir}/#{@type_}\", {ensure => present})\n"
		code += "    file_line { \"#{@type}/\${name}\":\n"
		code += "      line => \"#{@type}/${name}\",\n"
		code += "      path => \"${purge_state_dir}/#{@type_}\",\n"
		code += "    }\n"
	end
	code += "  }\n"
	code += "}\n"

	# Finally write the manifest to disk
	filename = "#{outputDir}/#{@type_}.pp"
	manifest = File.open(filename, "w")
	manifest.puts desc
	manifest.puts documentation
	manifest.puts code
	manifest.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)



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

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

#inspectValue(value) ⇒ Object

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



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/brocade/puppetmanifest.rb', line 376

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] 
			value = "'" + value.gsub("'"){"\\'"} + "'"
		end
		if @PUP4X
			value = value.gsub("\\\\"){"\\\\\\\\"}
		end
	end
	return value
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



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/brocade/puppetmanifest.rb', line 352

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