dyntool 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dyntool.rb +125 -88
- metadata +3 -3
data/lib/dyntool.rb
CHANGED
@@ -5,10 +5,12 @@ require 'uri'
|
|
5
5
|
require 'json'
|
6
6
|
require 'ipaddr'
|
7
7
|
require 'highline/import'
|
8
|
+
require 'yaml'
|
8
9
|
### class for loading and retrieving configuration from dyn.yml
|
9
10
|
class LoadConfig
|
10
|
-
def SetConfFile
|
11
|
-
@
|
11
|
+
def SetConfFile
|
12
|
+
@pwd = Dir.pwd
|
13
|
+
@confFile = "#{@pwd}/dyn.yml"
|
12
14
|
end
|
13
15
|
def GetHandler
|
14
16
|
@conf = YAML.load_file(@confFile)
|
@@ -34,8 +36,8 @@ class LoadConfig
|
|
34
36
|
def GetCountries(region)
|
35
37
|
@countries = @conf['regions'][region]
|
36
38
|
end
|
37
|
-
def GetDcsEnabled(region)
|
38
|
-
@dcs = @conf[
|
39
|
+
def GetDcsEnabled(region,service)
|
40
|
+
@dcs = @conf[service][region]
|
39
41
|
end
|
40
42
|
def GetIpLabel(ip)
|
41
43
|
@iplabel = @conf['ips'][ip]['label']
|
@@ -70,7 +72,7 @@ class Parameters
|
|
70
72
|
def SetArgs=( argv )
|
71
73
|
@ARGV = argv
|
72
74
|
@sconf = LoadConfig.new()
|
73
|
-
@sconf.SetConfFile
|
75
|
+
@sconf.SetConfFile #= "dyn.yml"
|
74
76
|
@sconf.GetHandler
|
75
77
|
end
|
76
78
|
# Opening Session to dyn using creds from the command line
|
@@ -96,18 +98,18 @@ class Parameters
|
|
96
98
|
when "generate" then ValidateGenerate()
|
97
99
|
when "get" then ValidateGet()
|
98
100
|
when "revert" then ValidateRevert()
|
101
|
+
else DisplayUsage()
|
99
102
|
end
|
100
103
|
end
|
101
104
|
# validating revert to current yml configuration
|
102
105
|
def ValidateRevert()
|
103
106
|
@services = @session.GetServices()
|
104
|
-
ValidateServiceExist(@services,@ARGV[
|
105
|
-
CompareGeo(@ARGV[
|
107
|
+
ValidateServiceExist(@services,@ARGV[1])
|
108
|
+
CompareGeo(@ARGV[1])
|
106
109
|
AskUser()
|
107
|
-
ValidateZone(@ARGV[1])
|
108
110
|
@operate.SetOperation("RevertGeo")
|
109
111
|
@operate.SetSession(@session)
|
110
|
-
@operate.SetArgs(@ARGV[1]
|
112
|
+
@operate.SetArgs(@ARGV[1])
|
111
113
|
|
112
114
|
end
|
113
115
|
# Get approval from user to revert/shift after reviewing the changes
|
@@ -126,6 +128,12 @@ class Parameters
|
|
126
128
|
@valid = arg
|
127
129
|
end
|
128
130
|
end
|
131
|
+
def GetZone(fqdn)
|
132
|
+
@fqdn_parts = fqdn.split('.')
|
133
|
+
@zone = "#{@fqdn_parts[-2]}" + "." + "#{@fqdn_parts[-1]}"
|
134
|
+
return(@zone)
|
135
|
+
end
|
136
|
+
|
129
137
|
# Validating the get operation, and setting the relevant arguments in OperateDyn object
|
130
138
|
def ValidateGet()
|
131
139
|
case @ARGV[1]
|
@@ -134,59 +142,69 @@ class Parameters
|
|
134
142
|
when "node" then @operate.SetOperation("GetNodeService")
|
135
143
|
@operate.SetArgs(@ARGV[2])
|
136
144
|
@operate.SetSession(@session)
|
137
|
-
when "cname" then
|
138
|
-
|
145
|
+
when "cname" then @zone = GetZone(@ARGV[2])
|
146
|
+
ValidateZone(@zone)
|
147
|
+
ValidateFqdn(@zone,@ARGV[2],"true")
|
139
148
|
@operate.SetOperation("GetCnameService")
|
140
|
-
@operate.SetArgs(@ARGV[2]
|
149
|
+
@operate.SetArgs(@zone,@ARGV[2])
|
141
150
|
@operate.SetSession(@session)
|
142
151
|
end
|
143
152
|
end
|
144
153
|
# Check type of record to add, the zone exist, then upon type, check arguments and set the required add operation
|
145
154
|
def ValidateAdd
|
146
155
|
ValidateType(@ARGV[1])
|
147
|
-
ValidateZone(@ARGV[2])
|
148
156
|
case @ARGV[1]
|
149
|
-
when "a" then
|
150
|
-
|
157
|
+
when "a" then @zone = GetZone(@ARGV[2])
|
158
|
+
ValidateZone(@zone)
|
159
|
+
ValidateFqdn(@zone,@ARGV[2],"false")
|
160
|
+
ValidateIP(@ARGV[3])
|
151
161
|
@operate.SetOperation("AddARecord")
|
152
162
|
@operate.SetSession(@session)
|
153
|
-
@operate.SetArgs(@ARGV[2],@ARGV[3]
|
154
|
-
when "cname" then
|
155
|
-
|
163
|
+
@operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
|
164
|
+
when "cname" then @zone = GetZone(@ARGV[2])
|
165
|
+
ValidateZone(@zone)
|
166
|
+
ValidateFqdn(@zone,@ARGV[2],"false")
|
156
167
|
@operate.SetOperation("AddCnameRecord")
|
157
168
|
@operate.SetSession(@session)
|
158
|
-
@operate.SetArgs(@ARGV[2],@ARGV[3]
|
159
|
-
when "txt" then
|
169
|
+
@operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
|
170
|
+
when "txt" then @zone = GetZone(@ARGV[2])
|
171
|
+
ValidateZone(@zone)
|
172
|
+
ValidateFqdn(@zone,@ARGV[2],"true")
|
160
173
|
@operate.SetOperation("AddTxtRecord")
|
161
174
|
@operate.SetSession(@session)
|
162
|
-
@operate.SetArgs(@ARGV[2],@ARGV[3]
|
175
|
+
@operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
|
176
|
+
when "zone" then @operate.SetOperation("AddZone")
|
177
|
+
@operate.SetSession(@session)
|
178
|
+
@operate.SetArgs(@ARGV[2])
|
163
179
|
end
|
164
180
|
end
|
165
181
|
def ValidateChange
|
166
182
|
ValidateType(@ARGV[1])
|
167
|
-
|
183
|
+
@zone = GetZone(@ARGV[2])
|
184
|
+
ValidateZone(@zone)
|
168
185
|
case @ARGV[1]
|
169
|
-
when "cname" then ValidateFqdn(@ARGV[2]
|
170
|
-
ValidateFqdn(@
|
186
|
+
when "cname" then ValidateFqdn(@zone,@ARGV[2],"true")
|
187
|
+
ValidateFqdn(@zone,@ARGV[3],"true")
|
171
188
|
@operate.SetOperation("ChangeCnameRecord")
|
172
189
|
@operate.SetSession(@session)
|
173
|
-
@operate.SetArgs(@ARGV[2],@ARGV[3]
|
190
|
+
@operate.SetArgs(@zone,@ARGV[2],@ARGV[3])
|
174
191
|
end
|
175
192
|
end
|
176
193
|
# Validating the delete arguments: zone and fqdn exist, in case of service that the service exist
|
177
194
|
def ValidateDel
|
178
195
|
case @ARGV[1]
|
179
|
-
when "node" then
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
196
|
+
when "node" then @zone = GetZone(@ARGV[2])
|
197
|
+
ValidateZone(@zone)
|
198
|
+
ValidateFqdn(@zone,@ARGV[2],"true")
|
199
|
+
@operate.SetOperation("DeleteNode")
|
200
|
+
@operate.SetSession(@session)
|
201
|
+
@operate.SetArgs(@zone,@ARGV[2])
|
202
|
+
when "service" then #ValidateZone(@ARGV[3])
|
185
203
|
@services = @session.GetServices()
|
186
204
|
ValidateServiceExist(@services,@ARGV[2])
|
187
205
|
@operate.SetOperation("DeleteService")
|
188
206
|
@operate.SetSession(@session)
|
189
|
-
@operate.SetArgs(@ARGV[2]
|
207
|
+
@operate.SetArgs(@ARGV[2])
|
190
208
|
end
|
191
209
|
end
|
192
210
|
def ValidateShift
|
@@ -194,18 +212,18 @@ class Parameters
|
|
194
212
|
@sproviders = @sconf
|
195
213
|
@services = @session.GetServices()
|
196
214
|
ValidateServiceExist(@services,@ARGV[1])
|
197
|
-
ValidateZone(@ARGV[2])
|
198
|
-
@resource = ValidateShiftArgs(@ARGV[
|
215
|
+
#ValidateZone(@ARGV[2])
|
216
|
+
@resource = ValidateShiftArgs(@ARGV[2],@ARGV[5],@ARGV[3])
|
199
217
|
case @resource
|
200
|
-
when "dc" then @dcs = GetArgsDcs(@ARGV[
|
218
|
+
when "dc" then @dcs = GetArgsDcs(@ARGV[4],@sdcs)
|
201
219
|
@dcs_list = @dcs.join("-")
|
202
220
|
@operate.SetOperation("ShiftTraffic")
|
203
221
|
@operate.SetSession(@session)
|
204
|
-
@operate.SetArgs(@ARGV[1],@ARGV[
|
205
|
-
when "provider" then ValidateProvider(@ARGV[
|
222
|
+
@operate.SetArgs(@ARGV[1],@ARGV[3],@ARGV[4],@dcs_list)
|
223
|
+
when "provider" then ValidateProvider(@ARGV[4],@ARGV[6])
|
206
224
|
@operate.SetOperation("ShiftTraffic")
|
207
225
|
@operate.SetSession(@session)
|
208
|
-
@operate.SetArgs(@ARGV[1],@ARGV[
|
226
|
+
@operate.SetArgs(@ARGV[1],@ARGV[3],@ARGV[4],@ARGV[6])
|
209
227
|
end
|
210
228
|
end
|
211
229
|
# Get dcs list from arguments
|
@@ -215,7 +233,7 @@ class Parameters
|
|
215
233
|
@sdcs = sdcs
|
216
234
|
@ARGV.each do |arg|
|
217
235
|
@c += 1
|
218
|
-
if (@c >
|
236
|
+
if (@c > 6)
|
219
237
|
if (! @sdcs.include? "#{arg}")
|
220
238
|
abort("The dcs is not in the configuration dcs list")
|
221
239
|
else
|
@@ -247,12 +265,13 @@ class Parameters
|
|
247
265
|
end
|
248
266
|
end
|
249
267
|
def ValidateGenerate
|
268
|
+
@zone = GetZone(@ARGV[2])
|
250
269
|
ValidateServiceFormat(@ARGV[1])
|
251
|
-
ValidateZone(@
|
252
|
-
ValidateFqdn(@ARGV[2]
|
270
|
+
ValidateZone(@zone)
|
271
|
+
ValidateFqdn(@zone,@ARGV[2],"false")
|
253
272
|
@operate.SetOperation("GenerateGeoNode")
|
254
273
|
@operate.SetSession(@session)
|
255
|
-
@operate.SetArgs(@ARGV[1],@ARGV[2]
|
274
|
+
@operate.SetArgs(@ARGV[1],@zone,@ARGV[2])
|
256
275
|
end
|
257
276
|
def ValidateServiceFormat(service)
|
258
277
|
@service = service
|
@@ -301,7 +320,7 @@ class Parameters
|
|
301
320
|
end
|
302
321
|
# check that record type is in the allow list
|
303
322
|
def ValidateType(type)
|
304
|
-
@types = [ "a", "cname", "txt" ]
|
323
|
+
@types = [ "a", "cname", "txt", "zone" ]
|
305
324
|
if ! @types.include? type
|
306
325
|
puts "Not a valid record type"
|
307
326
|
DisplayUsage()
|
@@ -309,39 +328,49 @@ class Parameters
|
|
309
328
|
end
|
310
329
|
# display script usage
|
311
330
|
def DisplayUsage
|
312
|
-
puts
|
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
|
-
|
331
|
+
STDOUT.puts <<-EOF
|
332
|
+
Usage:
|
333
|
+
|
334
|
+
* add: Adding a new record *
|
335
|
+
|
336
|
+
add a fqdn(a) ip example: dynamo.rb add a mynode.example.com 11.12.13.14
|
337
|
+
add cname zone fqdn(a) fqdn(cname) example: dynamo.rb add cname mycnamenode.example.com mynode.example.com
|
338
|
+
add txt zone fqdn(a) "some text" example: dynamo.rb add txt mynode.example.com "this is some text"
|
339
|
+
|
340
|
+
|
341
|
+
* del: Deleting a node or service *
|
342
|
+
|
343
|
+
del zone node fqdn(node) - node can be a,cname,txt,geonode,etc example: dynamo.rb del node mynode.example.com
|
344
|
+
del service geoservice zone example: dynamo.rb del service geo17
|
345
|
+
|
346
|
+
|
347
|
+
* change: Changing a record *
|
348
|
+
|
349
|
+
change cname zone cname newfqdn example: dynamo.rb change cname mycnamenode.example.com test19.example.com
|
350
|
+
|
351
|
+
|
352
|
+
* generate: Generating Geo service from yaml *
|
353
|
+
|
354
|
+
generate geoservice zone geonode example: dynamo.rb generate geo11 test11.example.com
|
355
|
+
|
356
|
+
|
357
|
+
* shift: Shifting traffic from dc or from provider on geo service *
|
358
|
+
|
359
|
+
shift geoservice zone from dc/provider to dc/dcs/provider(in same dc) example1: dynamo.rb shift geo11 from dc chidc1 to nydc1
|
360
|
+
example2: dynamo.rb shift geo11 from dc chidc1 to nydc1 ladc1
|
361
|
+
example3: dynamo.rb shift geo15 from provider amc_chi to inap_chi
|
362
|
+
|
363
|
+
* revert: Reverting a state of geo service to yaml state *
|
364
|
+
|
365
|
+
revert zone geoservice example: dynamo.rb revert geo15
|
366
|
+
|
367
|
+
|
368
|
+
* get: Getting infotmation of geo services, geonodes, cname pointing to geo *
|
369
|
+
|
370
|
+
get services (get all geo services) example: dynamo.rb get services
|
371
|
+
get node geonode example: dynamo.rb get node test1.example.com
|
372
|
+
get cname zone cname example: dynamo.rb get cname mycnamenode.example.com
|
373
|
+
EOF
|
345
374
|
exit(2)
|
346
375
|
end
|
347
376
|
# compare service geo data of current state vs yaml file
|
@@ -374,7 +403,7 @@ class Parameters
|
|
374
403
|
region['rdata']['a_rdata'].each do |rdata|
|
375
404
|
@current_ips << rdata['address']
|
376
405
|
end
|
377
|
-
@sconf.GetDcsEnabled(@name).each do |dc|
|
406
|
+
@sconf.GetDcsEnabled(@name,service).each do |dc|
|
378
407
|
@providers = @sconf.GetDcProviders(dc)
|
379
408
|
@providers.each do |provider|
|
380
409
|
@sconf.GetIPs(provider).each do |ips|
|
@@ -458,13 +487,14 @@ class OperateDyn
|
|
458
487
|
def RunOP
|
459
488
|
case(@@op)
|
460
489
|
when "AddARecord" then @@session.AddARecord(@@args[0],@@args[1],@@args[2])
|
490
|
+
when "AddZone" then @@session.CreateZone(@@args[0])
|
461
491
|
when "DeleteNode" then @@session.DeleteNode(@@args[0],@@args[1])
|
462
|
-
when "DeleteService" then @@session.DeleteService(@@args[0]
|
492
|
+
when "DeleteService" then @@session.DeleteService(@@args[0])
|
463
493
|
when "AddCnameRecord" then @@session.AddCnameRecord(@@args[0],@@args[1],@@args[2],"post")
|
464
494
|
when "AddTxtRecord" then @@session.AddTxtRecord(@@args[0],@@args[1],@@args[2])
|
465
495
|
when "ChangeCnameRecord" then @@session.AddCnameRecord(@@args[0],@@args[1],@@args[2],"put")
|
466
496
|
when "GenerateGeoNode" then @record_data = BuildService(@@args[0],"","","")
|
467
|
-
@@session.GenerateGeoService(@@args[0]
|
497
|
+
@@session.GenerateGeoService(@@args[0],@record_data,"create")
|
468
498
|
@@session.GenerateGeoNode(@@args[0],@@args[1],@@args[2])
|
469
499
|
when "GetServices" then @services = @@session.GetServices()
|
470
500
|
puts "Here is the list of Geo services:"
|
@@ -475,9 +505,9 @@ class OperateDyn
|
|
475
505
|
@geonode = @@session.GetCnameService(@@args[1],@@args[0])
|
476
506
|
GetGeoNodeService(@services,@geonode)
|
477
507
|
when "ShiftTraffic" then @record_data = BuildService(@@args[0],@@args[1],@@args[2],@@args[3])
|
478
|
-
@@session.GenerateGeoService(@@args[0]
|
508
|
+
@@session.GenerateGeoService(@@args[0],@record_data,"update")
|
479
509
|
when "RevertGeo" then @record_data = BuildService(@@args[0],"","","")
|
480
|
-
@@session.GenerateGeoService(@@args[
|
510
|
+
@@session.GenerateGeoService(@@args[0],@record_data,"update")
|
481
511
|
end
|
482
512
|
end
|
483
513
|
def GetOP
|
@@ -497,7 +527,7 @@ class OperateDyn
|
|
497
527
|
def BuildService(service,flag,shiftfrom,shifto)
|
498
528
|
puts "Generating the geo service data"
|
499
529
|
@@conf = LoadConfig.new()
|
500
|
-
@@conf.SetConfFile
|
530
|
+
@@conf.SetConfFile #= "dyn.yml"
|
501
531
|
@@conf.GetHandler
|
502
532
|
@regions = @@conf.GetRegions
|
503
533
|
@dcs = @@conf.GetDcs
|
@@ -512,7 +542,7 @@ class OperateDyn
|
|
512
542
|
@labels = []
|
513
543
|
@weights = []
|
514
544
|
@countries = @@conf.GetCountries(region)
|
515
|
-
@dcs = @@conf.GetDcsEnabled(region)
|
545
|
+
@dcs = @@conf.GetDcsEnabled(region,service)
|
516
546
|
if (flag == "dc")
|
517
547
|
if @dcs.include? "#{shiftfrom}"
|
518
548
|
@dcs = @dcs - @shiftfrom
|
@@ -633,7 +663,7 @@ class Restcall
|
|
633
663
|
PublishDyn(zone)
|
634
664
|
end
|
635
665
|
# Generating or modifying Geo service
|
636
|
-
def GenerateGeoService(service,
|
666
|
+
def GenerateGeoService(service,record_data,flag)
|
637
667
|
@record_data = record_data
|
638
668
|
@url = URI.parse("https://api2.dynect.net/REST/Geo/#{service}/")
|
639
669
|
if flag == "create"
|
@@ -643,7 +673,7 @@ class Restcall
|
|
643
673
|
puts "Changing geo service"
|
644
674
|
PutDyn()
|
645
675
|
end
|
646
|
-
PublishDyn(zone)
|
676
|
+
#PublishDyn(zone)
|
647
677
|
end
|
648
678
|
# Generating geo node and attaching to service
|
649
679
|
def GenerateGeoNode(service,zone,geonode)
|
@@ -659,9 +689,11 @@ class Restcall
|
|
659
689
|
@record_data = { :rdata => { :cname => "#{arecord}" }, :ttl => "0" }
|
660
690
|
if (flag == "post")
|
661
691
|
puts "Adding cname record"
|
692
|
+
puts "arecord #{arecord}, cname: #{cname}"
|
662
693
|
PostDyn()
|
663
694
|
elsif (flag == "put")
|
664
695
|
puts "Changing cname record"
|
696
|
+
puts "arecord #{arecord}, cname: #{cname}"
|
665
697
|
PutDyn()
|
666
698
|
end
|
667
699
|
PublishDyn(zone)
|
@@ -680,14 +712,13 @@ class Restcall
|
|
680
712
|
PublishDyn(zone)
|
681
713
|
end
|
682
714
|
# deleting service
|
683
|
-
def DeleteService(service
|
715
|
+
def DeleteService(service)
|
684
716
|
@url = URI.parse("https://api2.dynect.net/REST/Geo/#{service}/")
|
685
717
|
DeleteDyn()
|
686
|
-
PublishDyn(zone)
|
718
|
+
#PublishDyn(zone)
|
687
719
|
end
|
688
720
|
# get method to dyn
|
689
721
|
def GetDyn()
|
690
|
-
puts "Getting data from dyn"
|
691
722
|
@headers = { "Content-Type" => 'application/json', 'Auth-Token' => @auth_token }
|
692
723
|
@resp, @data = @http.get(@url.path, @headers)
|
693
724
|
@result = JSON.parse(@data)
|
@@ -761,6 +792,12 @@ class Restcall
|
|
761
792
|
@geonode = @result['data']['rdata']['cname'].chomp('.')
|
762
793
|
return(@geonode)
|
763
794
|
end
|
795
|
+
def CreateZone(zone)
|
796
|
+
@url = URI.parse("https://api2.dynect.net/REST/Zone/#{zone}/")
|
797
|
+
@record_data = { :rname => "ops@outbrain.com", :ttl => "3600" }
|
798
|
+
PostDyn()
|
799
|
+
PublishDyn(zone)
|
800
|
+
end
|
764
801
|
def PrintNodes(nodes)
|
765
802
|
nodes.each do |node|
|
766
803
|
puts node['fqdn']
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ariel Moskovich
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2013-07
|
17
|
+
date: 2013-09-07 00:00:00 +03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|