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



178
179
180
181
182
183
184
# File 'lib/dyntool.rb', line 178

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



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

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

#CompareGeo(service) ⇒ Object

compare service geo data of current state vs yaml file



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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
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
# File 'lib/dyntool.rb', line 439

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



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

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

	   generate geoservice geonode						         example: dynamo.rb generate geo11 test11.example.com
	
	
	* 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
											 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 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 cname								 example: dynamo.rb get cname mycnamenode.example.com
	EOF
	exit(2)
end

#GetArgsDcs(from, sdcs) ⇒ Object

Get dcs list from arguments



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/dyntool.rb', line 291

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



193
194
195
196
197
# File 'lib/dyntool.rb', line 193

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



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

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dyntool.rb', line 152

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



526
527
528
529
530
531
532
533
# File 'lib/dyntool.rb', line 526

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) ⇒ Object



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

def SendEvent(user,suffix,key)
	@data = []
	@notify_apps = []	
	@flag = "false"
	@ARGV.each do |arg|
		@data << "#{arg} "
	end
	@data << " #{user}"
	@data << " DNS"
	@dcs = @sconf.GetDcs
	@dcs.each do |dc|
		@result = `echo "Key73512383ghtyA1109 #{@data} " | 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



106
107
108
109
110
111
# File 'lib/dyntool.rb', line 106

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



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

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



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/dyntool.rb', line 244

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

#ValidateDC(site) ⇒ Object

validate that site exist in yaml



361
362
363
364
365
366
# File 'lib/dyntool.rb', line 361

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/dyntool.rb', line 256

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



348
349
350
351
# File 'lib/dyntool.rb', line 348

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

#ValidateGenerateObject



328
329
330
331
332
333
334
335
336
# File 'lib/dyntool.rb', line 328

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/dyntool.rb', line 200

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



353
354
355
356
357
358
359
# File 'lib/dyntool.rb', line 353

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



186
187
188
189
190
191
192
# File 'lib/dyntool.rb', line 186

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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/dyntool.rb', line 368

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



167
168
169
170
171
172
173
174
175
176
# File 'lib/dyntool.rb', line 167

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



322
323
324
325
326
327
# File 'lib/dyntool.rb', line 322

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



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

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/dyntool.rb', line 271

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



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

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



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

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

#ValidateZone(zone) ⇒ Object



344
345
346
347
# File 'lib/dyntool.rb', line 344

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