iblox 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/iblox +152 -40
  3. data/lib/iblox.rb +81 -5
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 678b86b0e1c03bb31eb8ffcd9367b8e7af450ca0
4
- data.tar.gz: 35dbd2fb53c1b7ace8a96cf8ff18564bbf241598
3
+ metadata.gz: 36ea496cb3bfc27dba8fc1cdeacc0d2d1490d1d3
4
+ data.tar.gz: ba43ecca3274f223750e3c391faeb17ad3815583
5
5
  SHA512:
6
- metadata.gz: af467ec41f2b56d39e0329f28cbd008f0116bb1d90b77c828c76bcacfe9a3473fb8e119984b4198bb2df84b40abc5f0077fd7c23f66324ce9a95694652da3d43
7
- data.tar.gz: a35ef0aac5f869c6c3189dda3899a3a5220e092adcbb1198e259d425b4d5106cb10eb3ccef20ea579906bd3eb218a953e81625ec141985aa39d6b1d18bccb4cd
6
+ metadata.gz: 870d2ec8098b69d2fb132d8496013603e971b35389a2880d3ee317034440462cb35c6811632844f0ebc2c4049dd71b72708165d76afa0259e92138132f2c50b0
7
+ data.tar.gz: 87f025a668f38821fb1b390e114e63fd1a37fd14a2e9a5925eec8cdc92120839733ea3d5c05c1b08db3e64567912673c69ffcff244a89e05be4ecc8a0e60c406
data/bin/iblox CHANGED
@@ -2,15 +2,15 @@
2
2
  require 'infoblox'
3
3
  require 'pp'
4
4
  require 'yaml'
5
+ require 'json'
5
6
  require 'ipaddr'
6
7
  require 'optparse'
7
- require 'blox'
8
+ require 'iblox'
8
9
 
9
- ENV['WAPI_VERSION']='2.0'
10
10
  options = Hash.new
11
11
 
12
12
  OptionParser.new do |opts|
13
- opts.banner = "Usage infoblox <Facility (dns|dhcp)> <action>(add|update|delete) [options]"
13
+ opts.banner = "Usage iblox <Facility (dns|dhcp)> <action>(add|update|delete) [options]"
14
14
  opts.on("-c", "--config /path/to/infoblox.yaml",String, "use custom config path (~/.infoblox.yaml, /etc/infoblox.yaml)") do |config |
15
15
  options.merge!(Hash[YAML::load(open(config)).map { |k, v| [k.to_sym, v] }])
16
16
  options[:config] = config
@@ -36,10 +36,21 @@ OptionParser.new do |opts|
36
36
  opts.on("-p", "--new-mac 00:05:67:45:32:02",String,"New Mac Address to use") do |new_mac|
37
37
  options[:new_mac] = new_mac
38
38
  end
39
+ opts.on("-a", "--api 2.0",String,"WAPI Version") do |api|
40
+ options[:wapi_version] = api
41
+ end
42
+ opts.on("-r", "--range","Use a DHCP range when finding addresses") do |range|
43
+ options[:range] = range
44
+ end
39
45
 
40
46
  opts.on("-v","--verbose", "Be verbose") do |v|
41
47
  options[:verbose] = v
42
48
  end
49
+
50
+ opts.on("-b", "--batch /path/to/batch_file",String,"Turn on Batch mode") do |batch|
51
+ options[:batch] = batch
52
+ end
53
+
43
54
  opts.parse!
44
55
  end
45
56
 
@@ -69,62 +80,97 @@ else
69
80
  end
70
81
  end
71
82
 
72
- #dns and dhc have different requirements for options and functions.
83
+ #dns and dhcp have different requirements for options and functions.
73
84
 
74
85
  case options[:facility]
75
86
  when "dns"
76
- if options[:ip] == nil or options[:fqdn] == nil
77
- raise "Both IP and FQDN are required for DNS actions"
87
+ #are we doing batch mode?
88
+ if options[:batch] != nil
89
+ batch_data=Iblox.batch_dns_load(options[:batch])
90
+ else
91
+ if options[:ip] == nil or options[:fqdn] == nil
92
+ raise "Both IP and FQDN are required for DNS actions"
93
+ end
94
+ #lets load batch_data with our one item so we can just "loop" that in either case
95
+ batch_data=[]
96
+ single_item={}
97
+ single_item[:fqdn] = options[:fqdn]
98
+ single_item[:ip] = options[:ip]
99
+ batch_data.push(single_item)
78
100
  end
79
- connection=Iblox.connect(options[:username],options[:password],options[:host])
80
- if Iblox.ipv4check(options[:ip])
81
- case options[:action]
82
- when "add"
83
- #Be smarter here, see if we exist, and if so don't #FAIL, just say so.
84
- if !Iblox.dns_exists(options[:fqdn],options[:ip],options[:verbose],connection)
85
- if options[:verbose] ==true
86
- puts "record doesn't exist, adding"
101
+ pp batch_data
102
+ #connect once
103
+ connection=Iblox.connect(options[:username],options[:password],options[:host],options[:wapi_version])
104
+ batch_data.each do |item|
105
+ if Iblox.ipv4check(item[:ip])
106
+ case options[:action]
107
+ when "add"
108
+ #Be smarter here, see if we exist, and if so don't #FAIL, just say so.
109
+ if !Iblox.dns_exists(item[:fqdn],item[:ip],options[:verbose],connection)
110
+ if options[:verbose] ==true
111
+ puts "record doesn't exist, adding"
112
+ end
113
+ Iblox.dns_add(item[:fqdn],item[:ip],options[:verbose],connection)
114
+ else
115
+ if options[:verbose] ==true
116
+ puts "record exists! not adding"
117
+ end
87
118
  end
88
- Iblox.dns_add(options[:fqdn],options[:ip],options[:verbose],connection)
89
- else
90
- if options[:verbose] ==true
91
- puts "record exists! not adding"
119
+ when "update"
120
+ #if we're in batch ode, bail out, cause...well i haven't written that yet.
121
+ if options[:batch] != nil
122
+ raise "Update in Batch mode not supported yet"
123
+ else
124
+ #check we got the new values.
125
+ if ((options[:new_ip] != nil) or (options[:new_fqdn] != nil))
126
+ Iblox.dns_update(item[:fqdn],item[:ip],options[:new_fqdn],options[:new_ip],options[:verbose],connection)
127
+ else
128
+ raise "No new IP or FQDN provided for update action"
129
+ end
92
130
  end
131
+ when "delete"
132
+ Iblox.dns_delete(item[:fqdn],item[:ip],options[:verbose],connection)
93
133
  end
94
- when "update"
95
- #check we got the new values.
96
- if ((options[:new_ip] != nil) or (options[:new_fqdn] != nil))
97
- Iblox.dns_update(options[:fqdn],options[:ip],options[:new_fqdn],options[:new_ip],options[:verbose],connection)
98
- else
99
- raise "No new IP or FQDN provided for update action"
100
- end
101
- when "delete"
102
- Iblox.dns_delete(options[:fqdn],options[:ip],options[:verbose],connection)
103
134
  end
104
135
  end
105
136
 
106
137
  when "dhcp"
107
- if (options[:ip] == nil and options[:network] == nil) or options[:fqdn] == nil or options[:mac] ==nil
108
- raise "IP or Network, and FQDN and MAC are required for DNS actions"
138
+ #are we doing batch mode?
139
+ if options[:batch] != nil
140
+ batch_data=Iblox.batch_dhcp_load(options[:batch])
141
+ else
142
+ if (options[:ip] == nil and options[:network] == nil) or options[:fqdn] == nil or options[:mac] ==nil
143
+ raise "IP or Network, and FQDN and MAC are required for DHCP actions"
144
+ end
145
+ #lets load batch_data with our one item so we can just "loop" that in either case
146
+ batch_data=[]
147
+ single_item={}
148
+ single_item[:fqdn] = options[:fqdn]
149
+ single_item[:ip] = options[:ip]
150
+ single_item[:mac] = options[:mac]
151
+ single_item[:network] = options[:network]
152
+ batch_data.push(single_item)
109
153
  end
110
- connection=Iblox.connect(options[:username],options[:password],options[:host])
154
+ #connect once
155
+ connection=Iblox.connect(options[:username],options[:password],options[:host],options[:wapi_version])
111
156
  if options[:verbose] ==true
112
157
  puts "Connected to Infoblox"
113
158
  end
159
+ batch_data.each do |item|
114
160
  case options[:action]
115
161
  when "add"
116
162
  #if we have an ip, check for existing reservation
117
163
  # if not check get next avail
118
- if (options[:ip] == nil and options[:network] != nil)
164
+ if (item[:ip] == nil and item[:network] != nil)
119
165
  #get net avail ip
120
- options[:ip]=Iblox.dhcp_next(options[:network],options[:verbose],connection)
166
+ item[:ip]=Iblox.dhcp_next(item[:network],options[:verbose],connection,options[:range])
121
167
  end
122
- if Iblox.ipv4check(options[:ip])
123
- if !Iblox.dhcp_exists(options[:mac],options[:verbose],connection)
168
+ if Iblox.ipv4check(item[:ip])
169
+ if !Iblox.dhcp_exists(item[:mac],options[:verbose],connection)
124
170
  if options[:verbose] ==true
125
171
  puts "record doesn't exist, adding"
126
172
  end
127
- Iblox.dhcp_add(options[:fqdn],options[:ip],options[:mac],options[:verbose],connection)
173
+ Iblox.dhcp_add(item[:fqdn],item[:ip],item[:mac],options[:verbose],connection)
128
174
  else
129
175
  if options[:verbose] ==true
130
176
  puts "record exists! not adding"
@@ -132,13 +178,79 @@ when "dhcp"
132
178
  end
133
179
  end
134
180
  when "update"
135
- if ((options[:new_ip] != nil) or (options[:new_fqdn] != nil) or (options[:new_mac] != nil ))
136
- Iblox.dhcp_update(options[:fqdn],options[:ip],options[:mac],options[:new_fqdn],options[:new_ip],options[:new_mac],options[:verbose],connection)
181
+ if options[:batch] != nil
182
+ raise "Update in Batch mode not supported yet"
137
183
  else
138
- raise "No new IP, MAC, od FQDN provided for update action"
184
+ if ((options[:new_ip] != nil) or (options[:new_fqdn] != nil) or (options[:new_mac] != nil ))
185
+ Iblox.dhcp_update(options[:fqdn],options[:ip],options[:mac],options[:new_fqdn],options[:new_ip],options[:new_mac],options[:verbose],connection)
186
+ else
187
+ raise "No new IP, MAC, od FQDN provided for update action"
188
+ end
139
189
  end
140
190
  when "delete"
141
- Iblox.dhcp_delete(options[:fqdn],options[:ip],options[:mac],options[:verbose],connection)
191
+ Iblox.dhcp_delete(item[:fqdn],item[:ip],item[:mac],options[:verbose],connection)
142
192
  end
143
- #when "both"
144
193
  end
194
+ when "both"
195
+ #do both operations
196
+ #are we doing batch mode?
197
+ if options[:batch] != nil
198
+ #We can use the dhcp loader here since it loads all the info we need
199
+ batch_data=Iblox.batch_dhcp_load(options[:batch])
200
+ else
201
+ if (options[:ip] == nil and options[:network] == nil) or options[:fqdn] == nil or options[:mac] ==nil
202
+ raise "IP or Network, and FQDN and MAC are required for DHCP+DNS actions"
203
+ end
204
+ #lets load batch_data with our one item so we can just "loop" that in either case
205
+ batch_data=[]
206
+ single_item={}
207
+ single_item[:fqdn] = options[:fqdn]
208
+ single_item[:ip] = options[:ip]
209
+ single_item[:mac] = options[:mac]
210
+ single_item[:network] = options[:network]
211
+ batch_data.push(single_item)
212
+ end
213
+ #connect once
214
+ connection=Iblox.connect(options[:username],options[:password],options[:host],options[:wapi_version])
215
+ if options[:verbose] ==true
216
+ puts "Connected to Infoblox"
217
+ end
218
+ batch_data.each do |item|
219
+ case options[:action]
220
+ when "add"
221
+ #if we have an ip, check for existing reservation
222
+ # if not check get next avail
223
+ if (item[:ip] == nil and item[:network] != nil)
224
+ #get net avail ip
225
+ item[:ip]=Iblox.dhcp_next(item[:network],options[:verbose],connection,options[:range])
226
+ end
227
+ if Iblox.ipv4check(item[:ip])
228
+ if !Iblox.dhcp_exists(item[:mac],options[:verbose],connection)
229
+ if options[:verbose] ==true
230
+ puts "record doesn't exist, adding"
231
+ end
232
+ Iblox.dhcp_add(item[:fqdn],item[:ip],item[:mac],options[:verbose],connection)
233
+ if !Iblox.dns_exists(item[:fqdn],item[:ip],options[:verbose],connection)
234
+ if options[:verbose] ==true
235
+ puts "record doesn't exist, adding"
236
+ end
237
+ Iblox.dns_add(item[:fqdn],item[:ip],options[:verbose],connection)
238
+ else
239
+ if options[:verbose] ==true
240
+ puts "DNS record exists! not adding"
241
+ end
242
+ end
243
+ else
244
+ if options[:verbose] ==true
245
+ puts "DHCP record exists! not adding"
246
+ end
247
+ end
248
+ end
249
+ when "update"
250
+ raise "update not supported for BOTH mode right now"
251
+ when "delete"
252
+ Iblox.dhcp_delete(item[:fqdn],item[:ip],item[:mac],options[:verbose],connection)
253
+ Iblox.dns_delete(item[:fqdn],item[:ip],options[:verbose],connection)
254
+ end
255
+ end
256
+ end
data/lib/iblox.rb CHANGED
@@ -14,11 +14,83 @@ def self.config_find()
14
14
  end
15
15
 
16
16
  #Open infoblox connection object
17
- def self.connect (username, password, host)
17
+ def self.connect (username, password, host, wapi_version)
18
+ ::Infoblox.wapi_version = "#{wapi_version}"
18
19
  connection = Infoblox::Connection.new(username: "#{username}",password: "#{password}", host: "#{host}")
19
20
  return connection
20
21
  end
21
22
 
23
+ def self.batch_dns_load(batch_file)
24
+ #First, see if we're using .csv, .yaml/.yml, or .json
25
+ ext=batch_file.split('.')[-1]
26
+ if ext.match(/(csv|yaml|yml|json)/)
27
+ #make sure it exists
28
+ if (File.exist? File.expand_path batch_file)
29
+ batch_data=[]
30
+ case ext
31
+ when "csv"
32
+ batch_data=[]
33
+ f=File.open(batch_file)
34
+ f.each_line do|line|
35
+ entry = {}
36
+ entry[:fqdn] = line.split(',')[0].chomp
37
+ entry[:ip] = line.split(',')[1].chomp
38
+ batch_data.push(entry)
39
+ end
40
+ when "yaml","yml"
41
+ batch_data=YAML::load(File.open(batch_file))
42
+ when "json"
43
+ batch_data=JSON.parse(File.read(batch_file),{:symbolize_names => true})
44
+ else
45
+ raise ("Batch filetype #{ext} not supported")
46
+ end
47
+ return batch_data
48
+ else
49
+ raise "Batch file does not exist"
50
+ end
51
+ else
52
+ raise ("Batch filetype #{ext} not supported")
53
+ end
54
+ end
55
+ def self.batch_dhcp_load(batch_file)
56
+ ext=batch_file.split('.')[-1]
57
+ if ext.match(/(csv|yaml|yml|json)/)
58
+ #make sure it exists
59
+ if (File.exist? File.expand_path batch_file)
60
+ batch_data=[]
61
+ case ext
62
+ when "csv"
63
+ batch_data=[]
64
+ f=File.open(batch_file)
65
+ f.each_line do|line|
66
+ entry = {}
67
+ entry[:fqdn] = line.split(',')[0].chomp
68
+ #figure out if this entry is an ip or CIDR
69
+ if line.split(',')[1].chomp.match('/')
70
+ puts "CIDR"
71
+ entry[:network] = line.split(',')[1].chomp
72
+ else
73
+ puts "IP"
74
+ entry[:ip] = line.split(',')[1].chomp
75
+ end
76
+ entry[:mac] = line.split(',')[2].chomp
77
+ batch_data.push(entry)
78
+ end
79
+ when "yaml","yml"
80
+ batch_data=YAML::load(File.open(batch_file))
81
+ when "json"
82
+ batch_data=JSON.parse(File.read(batch_file),{:symbolize_names => true})
83
+ else
84
+ raise ("Batch filetype #{ext} not supported")
85
+ end
86
+ return batch_data
87
+ else
88
+ raise "Batch file does not exist"
89
+ end
90
+ else
91
+ raise ("Batch filetype #{ext} not supported")
92
+ end
93
+ end
22
94
  #Check we got an IPv4 address
23
95
  def self.ipv4check (ip)
24
96
  ipaddr1 = IPAddr.new "#{ip}"
@@ -108,13 +180,17 @@ def self.dhcp_exists(mac,verbose,connection)
108
180
  return true
109
181
  end
110
182
  end
111
- def self.dhcp_next(network,verbose,connection)
183
+ def self.dhcp_next(network,verbose,connection,range)
112
184
  if verbose == true
113
185
  puts "Getting next available IP address for network #{network}"
114
186
  end
115
- #net = Infoblox::Network.find(connection, network: network).first
116
- range = Infoblox::Range.find(connection, network: network).first
117
- ip = range.next_available_ip[0]
187
+ if range == true
188
+ range = Infoblox::Range.find(connection, network: network).first
189
+ ip = range.next_available_ip[0]
190
+ else
191
+ net = Infoblox::Network.find(connection, network: network).first
192
+ ip = net.next_available_ip[0]
193
+ end
118
194
  return ip
119
195
  end
120
196
  def self.dhcp_add(fqdn,ip,mac,verbose,connection)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iblox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Nicholson
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.0'
33
+ version: 0.5.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.0'
40
+ version: 0.5.3
41
41
  description: A simple Infoblox CLI wrapper
42
42
  email: matthew.a.nicholson@gmail.com
43
43
  executables: