Class: Parameters

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

Overview

class for validating command line parameters and direct to relevant action

Instance Method Summary collapse

Instance Method Details

#AskUserObject

Get approval from user to revert/shift after reviewing the changes



133
134
135
136
137
138
139
# File 'lib/dyntool.rb', line 133

def AskUser()
	HighLine.track_eof = false
	@answer = ask("Are you sure you want to revert? (yes) ") { |q| q.echo = true } 
	if @answer != "yes"
		exit 2
	end
end

#CloseTheSessionObject

closing dyn session



102
103
104
105
# File 'lib/dyntool.rb', line 102

def CloseTheSession()
	puts "Closing DYN session"
	@session.CloseSession()
end

#CompareGeo(service) ⇒ Object

compare service geo data of current state vs yaml file



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
# File 'lib/dyntool.rb', line 394

def CompareGeo(service)
	@current_data = @session.GetGeoData(service)
	@current_groups = []
	@yaml_groups = []
	@yaml_total_groups = []
	@current_total_groups = []
	@current_data['data']['groups'].each do |group|
		@current_groups << group['name']
	end
	@yaml_groups = @sconf.GetRegions
	@current_total_groups = @current_groups - @yaml_groups
	@yaml_total_groups = @yaml_groups - @current_groups
	if (! @yaml_total_groups.empty?)
		puts "Diffrence in regions list:"
		puts "+Yaml #{@yaml_total_groups}"
	end
	if (! @current_total_groups.empty?)
		puts "Diffrence in regions list:"
		puts "+Current #{@current_total_groups}"
	end
	@current_data['data']['groups'].each do |region|
		@yaml_ips = []
		@current_ips = []
		@yaml_total_ips = []
		@current_total_ips = []
		@name = region['name']
		region['rdata']['a_rdata'].each do |rdata|
			@current_ips << rdata['address']
		end
		@sconf.GetDcsEnabled(@name,service).each do |dc|
			@providers = @sconf.GetDcProviders(dc)
			@providers.each do |provider|
				@sconf.GetIPs(provider).each do |ips|
					ips.each do |ip|
						@yaml_ips << ip
					end
				end
			end
		end
		@current_total_ips = @current_ips - @yaml_ips
		@yaml_total_ips = @yaml_ips - @current_ips
		if ! @yaml_total_ips.empty?
			PrintDiff(@name,@yaml_total_ips,"IP","Yaml")
		end
		if ! @current_total_ips.empty?
			PrintDiff(@name,@current_total_ips,"IP","Current")
		end
		@yaml_countries = []
		@current_countries = []
		@yaml_total_countries = []
		@current_total_countries = []
		region['countries'].each do |country|
			@current_countries << country
		end
		@sconf.GetCountries(@name).each do |country|
			@yaml_countries << country
		end
		@yaml_total_countries = @yaml_countries - @current_countries
		@current_total_countries = @current_countries - @yaml_countries
		if ! @yaml_total_countries.empty?
			PrintDiff(@name,@yaml_total_countries,"Countries","Yaml")
		end
		if ! @current_total_countries.empty?
			PrintDiff(@name,@current_total_countries,"Countries","Current")
		end
		@current_weights = []
		@yaml_weights = []
		region['weight']['a_weight'].each do |weight|
			@current_weights << weight
		end
		@i = 0
		@yaml_ips.each do |ip|
			@yaml_weights << @sconf.GetIpWeight(ip,@name)
		end
		@yaml_weights.each do |weight|
			if weight.to_s != @current_weights[@i].to_s
				puts "##############################################################################"
				puts "There is weight difference in region #{@name}, IP: #{@yaml_ips[@i]}"
				puts "Yaml weight is: #{weight}"
				puts "Current weight is: #{@current_weights[@i]}"
				puts "##############################################################################"
				@i += 1
			end
		end
	end
end

#DisplayUsageObject

display script usage



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

def DisplayUsage
	STDOUT.puts <<-EOF
	Usage:

	* add: Adding a new record *

	   add a fqdn(a) ip 				 				 example: dynamo.rb add a mynode.example.com 11.12.13.14
 	   add cname zone fqdn(a) fqdn(cname) 			    		         example: dynamo.rb add cname mycnamenode.example.com mynode.example.com
	   add txt zone fqdn(a) "some text" 		 				 example: dynamo.rb add txt mynode.example.com "this is some text"
	   add zone zonename								 example: dynamo.rb add zone example2.com
	
	
	* del: Deleting a node or service *

	   del zone node fqdn(node) - node can be a,cname,txt,geonode,etc 	         example: dynamo.rb del node mynode.example.com
	   del service geoservice zone							 example: dynamo.rb del service geo17
	
	
	* change: Changing a record *
	
	   change cname zone cname newfqdn						 example: dynamo.rb change cname mycnamenode.example.com test19.example.com
	
	
	* generate: Generating Geo service from yaml *

	   generate geoservice zone geonode						 example: dynamo.rb generate geo11 test11.example.com
	
	
	* shift: Shifting traffic from dc or from provider on geo service *

	   shift geoservice zone from dc/provider to dc/dcs/provider(in same dc)	 example1: dynamo.rb shift geo11 from dc chidc1 to nydc1
											 example2: dynamo.rb shift geo11 from dc chidc1 to nydc1 ladc1
											 example3: dynamo.rb shift geo15 from provider amc_chi to inap_chi
	
	* revert: Reverting a state of geo service to yaml state *
	
	  revert zone geoservice 							 example: dynamo.rb revert geo15
	
	
	* get: Getting infotmation of geo services, geonodes, cname pointing to geo *

	   get services (get all geo services)						 example: dynamo.rb get services
  	   get node geonode								 example: dynamo.rb get node test1.example.com
	   get cname zone cname								 example: dynamo.rb get cname mycnamenode.example.com
	EOF
	exit(2)
end

#GetArgsDcs(from, sdcs) ⇒ Object

Get dcs list from arguments



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/dyntool.rb', line 246

def GetArgsDcs(from,sdcs)
	@c = 0
	@dcs = []
	@sdcs = sdcs
	@ARGV.each do |arg|
		@c += 1
		if (@c > 6)
			if (! @sdcs.include? "#{arg}")
				abort("The dcs is not in the configuration dcs list")
			else
				@dcs << "#{arg}"
			end
		end
	end
	if @dcs.include? "#{from}"
		abort("destination dc/s cant include source dc")
	end
	if @dcs.empty?
		abort("List of dcs cant be empty")
	end
	return (@dcs)
end

#GetZone(fqdn) ⇒ Object



148
149
150
151
152
# File 'lib/dyntool.rb', line 148

def GetZone(fqdn)
	@fqdn_parts = fqdn.split('.')
	@zone = "#{@fqdn_parts[-2]}" + "." + "#{@fqdn_parts[-1]}"
	return(@zone)
end

#OpenTheSession(customer, user, password) ⇒ Object

Opening Session to dyn using creds from the command line



96
97
98
99
100
# File 'lib/dyntool.rb', line 96

def OpenTheSession(customer,user,password)
	puts "opening a session to DYN"
	@session = Restcall.new()
	@session.OpenSession(customer,user,password)
end

#Parameters(type) ⇒ Object

Routing the validation upon the relevant operation



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dyntool.rb', line 107

def Parameters(type)
	@operate = OperateDyn.new()
	@params = case type
		when "main_operation" then @operations = [ "add", "del", "shift", "generate", "get", "change", "revert" ]
		when "add" then	ValidateAdd()
		when "change" then ValidateChange()
		when "del" then ValidateDel()
		when "shift" then ValidateShift()
		when "generate" then ValidateGenerate()
		when "get" then ValidateGet()
		when "revert" then ValidateRevert()
		else DisplayUsage()
	end
end

#PrintDiff(region, list, name, symbol) ⇒ Object

gets region, list of differencesm, what is different and where and print it out



481
482
483
484
485
486
487
488
# File 'lib/dyntool.rb', line 481

def PrintDiff(region,list,name,symbol)
	puts "Diffrence in #{name} list:"
	puts "Region #{region}:"
	puts "+#{symbol} #{name}:"
	list.each do |item|
		puts "#{item}"
	end
end

#SetArgs=(argv) ⇒ Object



89
90
91
92
93
94
# File 'lib/dyntool.rb', line 89

def SetArgs=( argv )
	@ARGV = argv
	@sconf = LoadConfig.new()
	@sconf.SetConfFile 
	@sconf.GetHandler
end

#ValidateAddObject

Check type of record to add, the zone exist, then upon type, check arguments and set the required add operation



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
# File 'lib/dyntool.rb', line 171

def ValidateAdd
	ValidateType(@ARGV[1])
	case @ARGV[1]
		when "a" then 	  @zone = GetZone(@ARGV[2])
				  ValidateZone(@zone)
				  ValidateFqdn(@zone,@ARGV[2],"false")
			          ValidateIP(@ARGV[3])
			          @operate.SetOperation("AddARecord")
			          @operate.SetSession(@session)
			          @operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
		when "cname" then @zone = GetZone(@ARGV[2]) 
				  ValidateZone(@zone)
		                  ValidateFqdn(@zone,@ARGV[2],"false")
			          @operate.SetOperation("AddCnameRecord")
			          @operate.SetSession(@session)
				  @operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
		when "txt" then   @zone = GetZone(@ARGV[2])
				  ValidateZone(@zone)
				  ValidateFqdn(@zone,@ARGV[2],"true")
			  	  @operate.SetOperation("AddTxtRecord")
				  @operate.SetSession(@session)
				  @operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
		when "zone" then  @operate.SetOperation("AddZone")
				  @operate.SetSession(@session)
				  @operate.SetArgs(@ARGV[2])
	end
end

#ValidateChangeObject



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/dyntool.rb', line 198

def ValidateChange
	ValidateType(@ARGV[1])
	@zone = GetZone(@ARGV[2])
	ValidateZone(@zone)
	case @ARGV[1]
		when "cname" then ValidateFqdn(@zone,@ARGV[2],"true")
                       		  ValidateFqdn(@zone,@ARGV[3],"true")
		   		  @operate.SetOperation("ChangeCnameRecord")
				  @operate.SetSession(@session)
				  @operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
	end
end

#ValidateDC(site) ⇒ Object

validate that site exist in yaml



316
317
318
319
320
321
# File 'lib/dyntool.rb', line 316

def ValidateDC(site)
	if ! @conf.GetDcs.include? site
		puts "The DC does not exist"
		DisplayUsage()
	end
end

#ValidateDelObject

Validating the delete arguments: zone and fqdn exist, in case of service that the service exist



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/dyntool.rb', line 211

def ValidateDel
	case @ARGV[1]
		when "node" then    @zone = GetZone(@ARGV[2])
				    ValidateZone(@zone)
		            	    ValidateFqdn(@zone,@ARGV[2],"true")
		  	            @operate.SetOperation("DeleteNode")
			            @operate.SetSession(@session)
			            @operate.SetArgs(@zone,@ARGV[2])
		when "service" then @services = @session.GetServices()
				    ValidateServiceExist(@services,@ARGV[2])
			            @operate.SetOperation("DeleteService")
			            @operate.SetSession(@session)
			            @operate.SetArgs(@ARGV[2])
		end
end

#ValidateFqdn(zone, node, state) ⇒ Object



303
304
305
306
# File 'lib/dyntool.rb', line 303

def ValidateFqdn(zone,node,state)
	puts "Validating the host: " + node + " in zone: " + zone
	@session.CheckNodes(zone,node,state)
end

#ValidateGenerateObject



283
284
285
286
287
288
289
290
291
# File 'lib/dyntool.rb', line 283

def ValidateGenerate
	@zone = GetZone(@ARGV[2])
	ValidateServiceFormat(@ARGV[1])
	ValidateZone(@zone)
	ValidateFqdn(@zone,@ARGV[2],"false")
	@operate.SetOperation("GenerateGeoNode")
	@operate.SetSession(@session)
	@operate.SetArgs(@ARGV[1],@zone,@ARGV[2])
end

#ValidateGetObject

Validating the get operation, and setting the relevant arguments in OperateDyn object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dyntool.rb', line 155

def ValidateGet()
	case @ARGV[1]
		when "services" then @operate.SetOperation("GetServices")
				     @operate.SetSession(@session)
		when "node" then     @operate.SetOperation("GetNodeService")
				     @operate.SetArgs(@ARGV[2])
				     @operate.SetSession(@session)
		when "cname" then    @zone = GetZone(@ARGV[2])
				     ValidateZone(@zone)
				     ValidateFqdn(@zone,@ARGV[2],"true")	
				     @operate.SetOperation("GetCnameService")
				     @operate.SetArgs(@zone,@ARGV[2])
				     @operate.SetSession(@session)
	end
end

#ValidateIP(ip) ⇒ Object

checking that ip is in a valid format



308
309
310
311
312
313
314
# File 'lib/dyntool.rb', line 308

def ValidateIP(ip)
	puts "Validating the IP address: " + ip
	if (IPAddr.new(ip) rescue nil).nil?
		puts "IP Address is not valid"
		DisplayUsage()
	end
end

#ValidateParams(arg) ⇒ Object

Checking if the parameter is valid, if not return usage



141
142
143
144
145
146
147
# File 'lib/dyntool.rb', line 141

def ValidateParams(arg)
	if ! @operations.include? arg
		DisplayUsage()
	else
	     @valid = arg 
	end
end

#ValidateProvider(srcprovider, dstprovider) ⇒ Object

validate that provider given as argument actually exist in yaml



323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/dyntool.rb', line 323

def ValidateProvider(srcprovider,dstprovider)
	@providers = @sconf.GetProviders
	@dcsrc = srcprovider.split('_')
	@dcdst = dstprovider.split('_')
	if ((! @providers.include? srcprovider) || (! @providers.include? dstprovider))
		abort("The Provider does not exist")
	end
	if (srcprovider == dstprovider)
		abort("Source providers cant be same as destination provider")
	end
	if (@dcsrc[1] != @dcdst[1]) 
		abort("Cant shift traffic between providers in different dcs")
	end
end

#ValidateRevertObject

validating revert to current yml configuration



122
123
124
125
126
127
128
129
130
131
# File 'lib/dyntool.rb', line 122

def ValidateRevert()
	@services = @session.GetServices()
	ValidateServiceExist(@services,@ARGV[1])
	CompareGeo(@ARGV[1])
	AskUser()
	@operate.SetOperation("RevertGeo")
	@operate.SetSession(@session)
	@operate.SetArgs(@ARGV[1])

end

#ValidateServiceExist(services, geo) ⇒ Object

check that service given as argument exist in dyn



277
278
279
280
281
282
# File 'lib/dyntool.rb', line 277

def ValidateServiceExist(services,geo)
	@services = services
	if ! @services.include? "#{geo}"
		abort("The geo services list does not include the geo entered")
	end
end

#ValidateServiceFormat(service) ⇒ Object



292
293
294
295
296
297
298
# File 'lib/dyntool.rb', line 292

def ValidateServiceFormat(service)
	@service = service
	if ! (@service.match /geo[0][1-9]|geo[1-4][0-9]/)
		puts "service name must match geo(01-49) structure"
		DisplayUsage()
	end
end

#ValidateShiftObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/dyntool.rb', line 226

def ValidateShift
	@sdcs = @sconf.GetDcs
	@sproviders = @sconf
	@services = @session.GetServices()
	ValidateServiceExist(@services,@ARGV[1])
	#ValidateZone(@ARGV[2])
	@resource = ValidateShiftArgs(@ARGV[2],@ARGV[5],@ARGV[3])
	case @resource
		when "dc" then @dcs = GetArgsDcs(@ARGV[4],@sdcs)
			@dcs_list = @dcs.join("-")
			@operate.SetOperation("ShiftTraffic")
            		        @operate.SetSession(@session)
              			@operate.SetArgs(@ARGV[1],@ARGV[3],@ARGV[4],@dcs_list)
		when "provider" then ValidateProvider(@ARGV[4],@ARGV[6])
			@operate.SetOperation("ShiftTraffic")
            		        @operate.SetSession(@session)
              			@operate.SetArgs(@ARGV[1],@ARGV[3],@ARGV[4],@ARGV[6])
	end
end

#ValidateShiftArgs(from, to, resource) ⇒ Object

checking arguments for shift traffic



269
270
271
272
273
274
275
# File 'lib/dyntool.rb', line 269

def ValidateShiftArgs(from,to,resource)
	if (("#{from}" != "from") || ("#{to}" != "to") || (("#{resource}" != "dc") && ("#{resource}" != "provider")))
		DisplayUsage()
	else
		return("#{resource}")
	end
end

#ValidateType(type) ⇒ Object

check that record type is in the allow list



338
339
340
341
342
343
344
# File 'lib/dyntool.rb', line 338

def ValidateType(type)
	@types = [ "a", "cname", "txt", "zone" ]
	if ! @types.include? type
		puts "Not a valid record type"
		DisplayUsage()
	end
end

#ValidateZone(zone) ⇒ Object



299
300
301
302
# File 'lib/dyntool.rb', line 299

def ValidateZone(zone)
	puts "Validating zone: " + zone 
	@session.CheckZone(zone)	
end