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



222
223
224
225
226
227
228
# File 'lib/dyntool.rb', line 222

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



182
183
184
185
# File 'lib/dyntool.rb', line 182

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

#CompareGeo(service) ⇒ Object

compare service geo data of current state vs yaml file



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

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



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

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 -m "some comment"
 	   add cname fqdn(cname) fqdn(a) 			    		         example: dynamo.rb add cname mycnamenode.example.com mynode.example.com -m "some comment"
	   add txt fqdn(a) "some text" 		 				         example: dynamo.rb add txt mynode.example.com "this is some text" -m "some comment"
	   add zone zonename								 example: dynamo.rb add zone example2.com -m "some comment"
	
	
	* del: Deleting a node or service *

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

	   generate geoservice geonode						         example: dynamo.rb generate geo11 test11.example.com -m "some comment" 
	
	
	* shift: Shifting traffic from dc or from provider on geo service *

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

	   get services (get all geo services)						 example: dynamo.rb get services -m "some comment"
  	   get node geonode								 example: dynamo.rb get node test1.example.com -m "some comment"
	   get cname cname								 example: dynamo.rb get cname mycnamenode.example.com -m "some comment"
	EOF
	exit(2)
end

#GetArgsDcs(from, sdcs) ⇒ Object

Get dcs list from arguments



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

def GetArgsDcs(from,sdcs)
	@c = 0
	@dcs = []
	@sdcs = sdcs
	@poped = @ARGV.pop(2)
	if ((@poped[0] != "-m") || (! @poped[1].is_a? String))
		usage()
	end
	@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



244
245
246
247
248
# File 'lib/dyntool.rb', line 244

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



176
177
178
179
180
# File 'lib/dyntool.rb', line 176

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/dyntool.rb', line 187

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



583
584
585
586
587
588
589
590
# File 'lib/dyntool.rb', line 583

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

#SendEvent(user, suffix, key, comment) ⇒ Object



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

def SendEvent(user,suffix,key,comment)
	@data = []
	@notify_apps = []
	@flag = "false"
	@args = @ARGV.join(" ")
	@data << ":#{user}"
	@data << ":DNS"
	@data << ":#{comment}:"
	@dcs = @sconf.GetDcs
	@dcs.each do |dc|
		@result = `echo "Key73512383ghtyA1109 #{@data} #{@args}" | nc -w 1 event.#{dc}.#{suffix} 7279 2> /dev/null`
		if @result == "accepted"
			@flag = "true"
			break
		end
	end
	if @flag == "false"
		abort("You are not connected to outbrain network / cant send event to logstash, aborting...")
	end
	@graphite_server = @sconf.GetGraphite
	@notify_apps = @sconf.GetNotifyApps
	@notify_apps.each do |app|
		`curl -H "x-api-key:#{key}" -d "deployment[app_name]=#{app}" -d "deployment[description]=External DNS Change" -d "deployment[changelog]=External DNS Change" -d "deployment[user]=#{user}" https://api.newrelic.com/deployments.xml > /dev/null 2>&1`
	end
	@time = Time.now.to_i
	`echo "events.dns.change 1 #{@time}" | nc #{@graphite_server} 2003`
end

#SetArgs=(argv) ⇒ Object



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

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



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

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  @contacts = @sconf.GetContacts
				  @operate.SetOperation("AddZone")
				  @operate.SetSession(@session)
				  @operate.SetArgs(@ARGV[2],@contacts)
	end
end

#ValidateChangeObject



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/dyntool.rb', line 295

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

#ValidateComment(flag, comment) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/dyntool.rb', line 237

def ValidateComment(flag,comment)
	if flag != "-m" 
		DisplayUsage()
	else
		@comment = comment
	end
end

#ValidateDC(site) ⇒ Object

validate that site exist in yaml



416
417
418
419
420
421
# File 'lib/dyntool.rb', line 416

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



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/dyntool.rb', line 307

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



403
404
405
406
# File 'lib/dyntool.rb', line 403

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

#ValidateGenerateObject



383
384
385
386
387
388
389
390
391
# File 'lib/dyntool.rb', line 383

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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/dyntool.rb', line 251

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



408
409
410
411
412
413
414
# File 'lib/dyntool.rb', line 408

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



230
231
232
233
234
235
236
# File 'lib/dyntool.rb', line 230

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



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/dyntool.rb', line 423

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/dyntool.rb', line 202

def ValidateRevert()
	@services = @session.GetServices()
	ValidateServiceExist(@services,@ARGV[1])
	@operate.SetOperation("RevertGeo")
	@operate.SetSession(@session)
	if (@ARGV[1] == "all") 
		@services.each do |service|
			puts "+++++++++++++ ALL operation: Doing #{service} ++++++++++++++" 
			#CompareGeo(service)
			#AskUser()
		end
		@operate.SetArgs("all")
	else
		CompareGeo(@ARGV[1])
		AskUser()
		@operate.SetArgs(@ARGV[1])
	end

end

#ValidateServiceExist(services, geo) ⇒ Object

check that service given as argument exist in dyn



377
378
379
380
381
382
# File 'lib/dyntool.rb', line 377

def ValidateServiceExist(services,geo)
	@services = services
	if (! @services.include? "#{geo}") && ( "#{geo}" != "all")
		abort("The geo services list does not include the geo entered / you didn't use all paramater")
	end
end

#ValidateServiceFormat(service) ⇒ Object



392
393
394
395
396
397
398
# File 'lib/dyntool.rb', line 392

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



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

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



369
370
371
372
373
374
375
# File 'lib/dyntool.rb', line 369

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



438
439
440
441
442
443
444
# File 'lib/dyntool.rb', line 438

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

#ValidateZone(zone) ⇒ Object



399
400
401
402
# File 'lib/dyntool.rb', line 399

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