gogetit 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d5861321df7c324e2ed483329663114406a308b
4
- data.tar.gz: 996bd6ae6cf0f34a38fb36f91abff1d94d06b34b
3
+ metadata.gz: c7acf6c55331c1df671dfcd70ce82bb06f548e46
4
+ data.tar.gz: fc059c38b8f2ed01d4355a057acd546899c0c59e
5
5
  SHA512:
6
- metadata.gz: 61507178c9a1ad0aedb2d969a5714f8d9c3197e75fd016aaa974a51f6d860e9e2d65bbdd45680ce72e48600db02dde295a47d626dafc902d482cda99b4795aeb
7
- data.tar.gz: e517a9a7000d81f192a7e456e572933408d3a843536e3e7e7d8fb0f98c25323a517080632d71306be4b6ed6650c5f6aa1639b0e0091f66cdd7fa83df16c7f3cf
6
+ metadata.gz: 6ff6a476cc22611ee2fb8b0932248e01081fc7abc22b779f25c0023409521eabda46e88c2af79c50a957c503572ff88de74ec364d052f97c64047d3e70379555
7
+ data.tar.gz: 8e215e4f3c62a433c8bbda2284d5962da6f36cb76c5f796ae69c17d371d5618451cd36cd8cfc73347a9f703cdb533e72adc3b3ad742fd01c0ccec049966afddd
data/README.md CHANGED
@@ -90,7 +90,7 @@ Clone and then execute followings:
90
90
  $ gem install bundle
91
91
  $ bundle
92
92
 
93
- Questions and pull requests are always welcome!
93
+ Questions, pull requests, advices and suggestions are always welcome!
94
94
 
95
95
  ## License
96
96
 
@@ -41,12 +41,11 @@ module Gogetit
41
41
 
42
42
  # post-tasks
43
43
  if options[:chef]
44
- knife_bootstrap(name, type, Gogetit.config)
45
- update_vault(Gogetit.config)
44
+ knife_bootstrap(name, type, Gogetit.config, Gogetit.logger)
45
+ update_vault(Gogetit.config, Gogetit.logger)
46
46
  end
47
47
  Gogetit.config[:default][:user] ||= ENV['USER']
48
48
  puts "ssh #{Gogetit.config[:default][:user]}@#{name}"
49
- print "ssh #{Gogetit.config[:default][:user]}@#{name}"
50
49
  end
51
50
 
52
51
  desc 'destroy NAME', 'Destroy either a container or KVM domain.'
@@ -65,8 +64,8 @@ module Gogetit
65
64
  end
66
65
  # post-tasks
67
66
  if options[:chef]
68
- knife_remove(name) if options[:chef]
69
- update_vault(Gogetit.config)
67
+ knife_remove(name, Gogetit.logger) if options[:chef]
68
+ update_vault(Gogetit.config, Gogetit.logger)
70
69
  end
71
70
  end
72
71
 
@@ -41,6 +41,11 @@ module Gogetit
41
41
  return true if dnsresource_exists?(name) or machine_exists?(name)
42
42
  end
43
43
 
44
+ def get_subnets
45
+ logger.info("Calling <#{__method__.to_s}>")
46
+ return conn.request(:get, ['subnets'])
47
+ end
48
+
44
49
  def ip_reserved?(addresses)
45
50
  logger.info("Calling <#{__method__.to_s}>")
46
51
  ips = Set.new
@@ -210,5 +215,15 @@ module Gogetit
210
215
  logger.info("Calling <#{__method__.to_s}>")
211
216
  conn.request(:put, ['machines', system_id], { 'hostname' => hostname })
212
217
  end
218
+
219
+ def get_sshkeys
220
+ logger.info("Calling <#{__method__.to_s}>")
221
+ conn.request(:get, ['account', 'prefs', 'sshkeys'])
222
+ end
223
+
224
+ def get_package_repos
225
+ logger.info("Calling <#{__method__.to_s}>")
226
+ conn.request(:get, ['package-repositories'])
227
+ end
213
228
  end
214
229
  end
@@ -4,7 +4,8 @@ require 'active_support/core_ext/hash'
4
4
 
5
5
  module Gogetit
6
6
  module Util
7
- def knife_bootstrap(name, type, config)
7
+ def knife_bootstrap(name, type, config, logger)
8
+ logger.info("Calling <#{__method__.to_s}>")
8
9
  if find_executable 'knife'
9
10
  if system('knife ssl check')
10
11
  install_cmd = "curl \
@@ -22,7 +23,8 @@ module Gogetit
22
23
  end
23
24
  end
24
25
 
25
- def update_vault(config)
26
+ def update_vault(config, logger)
27
+ logger.info("Calling <#{__method__.to_s}>")
26
28
  # It assumes the data_bags directory is under the root directory of Chef Repo
27
29
  data_bags_dir = "#{config[:chef][:chef_repo_root]}/data_bags"
28
30
  (Dir.entries("#{data_bags_dir}") - ['.', '..']).each do |bag|
@@ -42,7 +44,8 @@ module Gogetit
42
44
  end
43
45
  end
44
46
 
45
- def knife_remove(name)
47
+ def knife_remove(name, logger)
48
+ logger.info("Calling <#{__method__.to_s}>")
46
49
  if find_executable 'knife'
47
50
  if system('knife ssl check')
48
51
  puts "Deleting node #{name}.."
@@ -91,6 +94,7 @@ module Gogetit
91
94
  end
92
95
 
93
96
  def wait_until_available(fqdn, logger)
97
+ logger.info("Calling <#{__method__.to_s}>")
94
98
  until ping_available?(fqdn, logger)
95
99
  logger.info("Calling <#{__method__.to_s}> for ping to be ready..")
96
100
  sleep 3
@@ -1,3 +1,3 @@
1
1
  module Gogetit
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -120,6 +120,7 @@ module Gogetit
120
120
 
121
121
  # It assumes you only have a physical interfaces.
122
122
  interfaces = maas.interfaces([system_id])
123
+
123
124
  maas.interfaces(
124
125
  [system_id, interfaces[0]['id']],
125
126
  {
@@ -128,23 +129,21 @@ module Gogetit
128
129
  }
129
130
  )
130
131
 
131
- maas.interfaces(
132
- [system_id, interfaces[0]['id']],
133
- {
134
- 'op' => 'link_subnet',
135
- 'mode' => 'STATIC',
136
- 'subnet' => ifaces[0]['id'],
137
- 'ip_address' => ifaces[0]['ip'],
138
- 'default_gateway' => 'True',
139
- 'force' => 'False'
140
- }
141
- )
132
+ # VLAN configuration
133
+ ifaces.each_with_index do |iface,index|
142
134
 
143
- if domain[:ifaces].length > 1
144
- ifaces.shift
135
+ if index == 0
136
+ params = {
137
+ 'op' => 'link_subnet',
138
+ 'mode' => 'STATIC',
139
+ 'subnet' => ifaces[0]['id'],
140
+ 'ip_address' => ifaces[0]['ip'],
141
+ 'default_gateway' => 'True',
142
+ 'force' => 'False'
143
+ }
144
+ maas.interfaces([system_id, interfaces[0]['id']], params)
145
145
 
146
- # VLAN configuration
147
- ifaces.each_with_index do |iface,index|
146
+ elsif index > 0
148
147
  params = {
149
148
  'op' => 'create_vlan',
150
149
  'vlan' => iface['vlan']['id'],
@@ -153,19 +152,19 @@ module Gogetit
153
152
  maas.interfaces([system_id], params)
154
153
 
155
154
  interfaces = maas.interfaces([system_id])
156
- interfaces.shift
157
-
158
- maas.interfaces([system_id, interfaces[index]['id']],
159
- {
160
- 'op' => 'link_subnet',
161
- 'mode' => 'STATIC',
162
- 'subnet' => ifaces[index]['id'],
163
- 'ip_address' => ifaces[index]['ip'],
164
- 'default_gateway' => 'False',
165
- 'force' => 'False'
166
- }
167
- )
155
+
156
+ params = {
157
+ 'op' => 'link_subnet',
158
+ 'mode' => 'STATIC',
159
+ 'subnet' => ifaces[index]['id'],
160
+ 'ip_address' => ifaces[index]['ip'],
161
+ 'default_gateway' => 'False',
162
+ 'force' => 'False'
163
+ }
164
+
165
+ maas.interfaces([system_id, interfaces[index]['id']], params)
168
166
  end
167
+
169
168
  end
170
169
 
171
170
  elsif options[:vlans]
@@ -42,8 +42,8 @@ module Gogetit
42
42
  end
43
43
  end
44
44
 
45
- def generate_args(options)
46
- args = {}
45
+ def generate_args(args, options)
46
+ logger.info("Calling <#{__method__.to_s}>")
47
47
  args[:devices] = {}
48
48
 
49
49
  ifaces = check_ip_available(options['ipaddresses'], maas, logger)
@@ -52,16 +52,14 @@ module Gogetit
52
52
  abort("There is no gateway specified for the gateway network.") \
53
53
  unless ifaces[0]['gateway_ip']
54
54
  args[:ifaces] = ifaces
55
- args[:config] = {
56
- 'user.network-config': {
57
- 'version' => 1,
58
- 'config' => [
59
- {
60
- 'type' => 'nameserver',
61
- 'address' => ifaces[0]['dns_servers'][0]
62
- }
63
- ]
64
- }
55
+ args[:config][:'user.network-config'] = {
56
+ 'version' => 1,
57
+ 'config' => [
58
+ {
59
+ 'type' => 'nameserver',
60
+ 'address' => ifaces[0]['dns_servers'][0]
61
+ }
62
+ ]
65
63
  }
66
64
 
67
65
  ifaces.each_with_index do |iface,index|
@@ -157,18 +155,67 @@ module Gogetit
157
155
  return args
158
156
  end
159
157
 
158
+ def generate_common_args
159
+ logger.info("Calling <#{__method__.to_s}>")
160
+ args = {}
161
+ sshkeys = maas.get_sshkeys
162
+ pkg_repos = maas.get_package_repos
163
+
164
+ args[:config] = {
165
+ 'user.user-data': { 'ssh_authorized_keys' => [] }
166
+ }
167
+
168
+ sshkeys.each do |key|
169
+ args[:config][:'user.user-data']['ssh_authorized_keys'].push(key['key'])
170
+ end
171
+
172
+ pkg_repos.each do |repo|
173
+ if repo['name'] == 'main_archive'
174
+ args[:config][:'user.user-data']['apt_mirror'] = repo['url']
175
+ end
176
+ end
177
+
178
+ args[:config][:"user.user-data"] = \
179
+ YAML.dump(args[:config][:"user.user-data"])[4..-1]
180
+ return args
181
+ end
182
+
160
183
  def create(name, options = {})
161
184
  logger.info("Calling <#{__method__.to_s}>")
162
185
  abort("Container or Hostname #{name} already exists!") \
163
186
  if container_exists?(name) or maas.domain_name_exists?(name)
164
187
 
165
- args = {}
188
+ args = generate_common_args
189
+
166
190
  if options['ipaddresses']
167
- args = generate_args(options)
191
+ args = generate_args(args, options)
168
192
  elsif options[:vlans]
169
193
  #check_vlan_available(options[:vlans])
170
194
  else
171
- args[:profiles] ||= config[:lxd][:profiles]
195
+ abort("native_bridge #{config[:default][:native_bridge]} does not exist.") \
196
+ unless conn.networks.include? config[:default][:native_bridge]
197
+
198
+ native_bridge_mtu = nil
199
+ # It assumes you only use one fabric as of now,
200
+ # since there might be more fabrics with each untagged vlans on them,
201
+ # which might make finding exact mtu fail as following process.
202
+ default_fabric = 'fabric-0'
203
+
204
+ maas.get_subnets.each do |subnet|
205
+ if subnet['vlan']['name'] == 'untagged' and subnet['vlan']['fabric'] == default_fabric
206
+ native_bridge_mtu = subnet['vlan']['mtu']
207
+ break
208
+ end
209
+ end
210
+
211
+ args[:devices] = {}
212
+ args[:devices][:"eth0"] = {
213
+ mtu: native_bridge_mtu.to_s, #This must be string
214
+ name: 'eth0',
215
+ nictype: 'bridged',
216
+ parent: config[:default][:native_bridge],
217
+ type: 'nic'
218
+ }
172
219
  end
173
220
 
174
221
  args[:alias] ||= config[:lxd][:default_alias]
@@ -177,12 +224,12 @@ module Gogetit
177
224
  conn.create_container(name, args)
178
225
  container = conn.container(name)
179
226
 
180
- if options['vlans'] or options['ipaddresses']
181
- container.devices = args[:devices].merge!(container.devices.to_hash)
182
- conn.update_container(name, container)
183
- # Fetch container object again
184
- container = conn.container(name)
227
+ container.devices = args[:devices].merge!(container.devices.to_hash)
228
+ conn.update_container(name, container)
229
+ # Fetch container object again
230
+ container = conn.container(name)
185
231
 
232
+ if options['vlans'] or options['ipaddresses']
186
233
  # Generate params to reserve IPs
187
234
  args[:ifaces].each_with_index do |iface,index|
188
235
  if index == 0
@@ -193,8 +240,10 @@ module Gogetit
193
240
  'mac' => container[:expanded_config][:"volatile.eth#{index}.hwaddr"]
194
241
  }
195
242
  elsif index > 0
196
- # if dot, '.', is used as a conjunction instead of '-', it fails ocuring '404 not found'.
197
- # if under score, '_', is used as a conjunction instead of '-', it breaks MAAS DNS somehow..
243
+ # if dot, '.', is used as a conjunction instead of '-',
244
+ # it fails ocuring '404 not found'.
245
+ # if under score, '_', is used as a conjunction instead of '-',
246
+ # it breaks MAAS DNS somehow..
198
247
  if args[:ifaces][0]['vlan']['name'] == 'untagged'
199
248
  params = {
200
249
  'subnet' => iface['cidr'],
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gogetit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Draper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-07 00:00:00.000000000 Z
11
+ date: 2017-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler