gogetit 0.20.2 → 0.21.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
  SHA256:
3
- metadata.gz: 4639acfdbff32bdcaf38e6b7300572aa51191fbaaaa44f4b4c1d37a549c83183
4
- data.tar.gz: 8323c80277bd0cd70d319a1830859d48662afa83bcf575813349153d48dee8e6
3
+ metadata.gz: b3c995acc5f536c6cd8b860e0c025d5da14d55a5b00cba8fae71cba612a81aa0
4
+ data.tar.gz: 512981119248e95559725188ce2203ba85b520e224d4948691ebbb30ce6f39f1
5
5
  SHA512:
6
- metadata.gz: 0d660f035aaf0a9886dac34a75c5b66ed386828d195ce0f44a0a392eb47f9ece4b546ea66897aed59ab7ebf01ded5a23862b8b6aad9cc3f160ef6244f3383649
7
- data.tar.gz: e2a26f58a34c7dfe821895f58a0c3ccbb1d060b0090b8e90cf0578f0d5b10289a58427ea8e69f607796f6b46e7933704ccf4d7afa0a5313fbe560ea1a4f0523c
6
+ metadata.gz: 76757d10c32ed818bbff5749259ba0031b0025d2bad170dcab8d79396c3a09a259ddc9f7254ea4a5e3dcee48b702c591a8b4401f4401d6d60b34d3a3aa5a4866
7
+ data.tar.gz: 9a56506d5b228f248105f456306bb342728ce4f966a50c7cfb8f4310692c8216a08287cb794137dad9c89e7ada8c6159fb2b49b840af86cd1c3b5ff2dfb8306d
@@ -261,5 +261,90 @@ module Gogetit
261
261
  end
262
262
  end
263
263
  end
264
+
265
+ def generate_cloud_init_config(options, config, user_data = {})
266
+ logger.info("Calling <#{__method__.to_s}>")
267
+
268
+ # apt
269
+ user_data['apt'] = {}
270
+ # preserve source list for a while
271
+ user_data['apt']['preserve_sources_list'] = true
272
+
273
+ if options['no-maas']
274
+ # When there is no MAAS, containers should be able to resolve
275
+ # their name with hosts file.
276
+ user_data['manage_etc_hosts'] = true
277
+ end
278
+
279
+ # To add truested root CA certificates
280
+ # https://cloudinit.readthedocs.io/en/latest/topics/examples.html
281
+ # #configure-an-instances-trusted-ca-certificates
282
+ #
283
+ if config[:cloud_init] && config[:cloud_init][:ca_certs]
284
+ user_data['ca-certs'] = {}
285
+ certs = []
286
+
287
+ config[:cloud_init][:ca_certs].each do |ca|
288
+ content = get_http_content(ca)
289
+ certs.push(
290
+ /^-----BEGIN CERTIFICATE-----.*-/m.match(content).to_s
291
+ ) if content
292
+ end
293
+
294
+ user_data['ca-certs'] = { 'trusted' => certs }
295
+ end
296
+
297
+ # To get CA public key to be used for SSH authentication
298
+ # https://cloudinit.readthedocs.io/en/latest/topics/examples.html
299
+ # #writing-out-arbitrary-files
300
+ if config[:cloud_init] && config[:cloud_init][:ssh_ca_public_key]
301
+ user_data['write_files'] = []
302
+ content = get_http_content(config[:cloud_init][:ssh_ca_public_key][:key_url])
303
+ if content
304
+ file = {
305
+ 'content' => content.chop!,
306
+ 'path' => config[:cloud_init][:ssh_ca_public_key][:key_path],
307
+ 'owner' => config[:cloud_init][:ssh_ca_public_key][:owner],
308
+ 'permissions' => config[:cloud_init][:ssh_ca_public_key][:permissions]
309
+ }
310
+ user_data['write_files'].push(file)
311
+ user_data['bootcmd'] = []
312
+ user_data['bootcmd'].push(
313
+ "cloud-init-per once ssh-ca-pub-key \
314
+ echo \"TrustedUserCAKeys #{file['path']}\" >> /etc/ssh/sshd_config"
315
+ )
316
+ end
317
+
318
+ if config[:cloud_init][:ssh_ca_public_key][:revocation_url]
319
+ content = get_http_content(config[:cloud_init][:ssh_ca_public_key][:revocation_url])
320
+ if content
321
+ user_data['bootcmd'].push(
322
+ "cloud-init-per once download-key-revocation-list \
323
+ curl -o #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]} \
324
+ #{config[:cloud_init][:ssh_ca_public_key][:revocation_url]}"
325
+ )
326
+ user_data['bootcmd'].push(
327
+ "cloud-init-per once ssh-user-key-revocation-list \
328
+ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\" \
329
+ >> /etc/ssh/sshd_config"
330
+ )
331
+ end
332
+ end
333
+ end
334
+
335
+ # To add users
336
+ # https://cloudinit.readthedocs.io/en/latest/topics/examples.html
337
+ # #including-users-and-groups
338
+ if config[:cloud_init] && config[:cloud_init][:users]
339
+ user_data['users'] = []
340
+ user_data['users'].push('default')
341
+
342
+ config[:cloud_init][:users].each do |user|
343
+ user_data['users'].push(Hashie.stringify_keys user)
344
+ end
345
+ end
346
+
347
+ return user_data
348
+ end
264
349
  end
265
350
  end
@@ -1,3 +1,3 @@
1
1
  module Gogetit
2
- VERSION = "0.20.2"
2
+ VERSION = "0.21.0"
3
3
  end
@@ -3,6 +3,7 @@ require 'securerandom'
3
3
  require 'oga'
4
4
  require 'rexml/document'
5
5
  require 'gogetit/util'
6
+ require 'yaml'
6
7
 
7
8
  module Gogetit
8
9
  class GogetLibvirt
@@ -249,8 +250,22 @@ module Gogetit
249
250
  distro = options[:distro]
250
251
  end
251
252
 
252
- maas.conn.request(:post, ['machines', system_id], \
253
- {'op' => 'deploy', 'distro_series' => distro})
253
+ require 'base64'
254
+ user_data = Base64.encode64(
255
+ "#cloud-config\n" +
256
+ YAML.dump(generate_cloud_init_config(options, config))[4..-1]
257
+ )
258
+
259
+ maas.conn.request(
260
+ :post,
261
+ ['machines', system_id],
262
+ {
263
+ 'op' => 'deploy',
264
+ 'distro_series' => distro,
265
+ 'user_data' => user_data
266
+ }
267
+ )
268
+
254
269
  maas.wait_until_state(system_id, 'Deployed')
255
270
 
256
271
  fqdn = name + '.' + maas.get_domain
@@ -46,157 +46,76 @@ module Gogetit
46
46
  end
47
47
 
48
48
  # to generate 'user.user-data'
49
- def generate_user_data(args, options)
49
+ def generate_user_data(lxd_params, options)
50
50
  logger.info("Calling <#{__method__.to_s}>")
51
51
 
52
- args[:config] = {}
52
+ lxd_params[:config] = {}
53
53
 
54
54
  if options['no-maas']
55
- args[:config][:"user.user-data"] = {}
55
+ lxd_params[:config][:"user.user-data"] = {}
56
56
  else
57
57
  sshkeys = maas.get_sshkeys
58
58
  pkg_repos = maas.get_package_repos
59
59
 
60
- args[:config][:'user.user-data'] = { 'ssh_authorized_keys' => [] }
60
+ lxd_params[:config][:'user.user-data'] = { 'ssh_authorized_keys' => [] }
61
61
 
62
62
  sshkeys.each do |key|
63
- args[:config][:'user.user-data']['ssh_authorized_keys'].push(key['key'])
63
+ lxd_params[:config][:'user.user-data']['ssh_authorized_keys'].push(key['key'])
64
64
  end
65
65
 
66
66
  pkg_repos.each do |repo|
67
67
  if repo['name'] == 'main_archive'
68
- args[:config][:'user.user-data']['apt_mirror'] = repo['url']
68
+ lxd_params[:config][:'user.user-data']['apt_mirror'] = repo['url']
69
69
  end
70
70
  end
71
71
 
72
- args[:config][:"user.user-data"]['source_image_alias'] = args[:alias]
73
- args[:config][:"user.user-data"]['maas'] = true
72
+ lxd_params[:config][:"user.user-data"]['source_image_alias'] = lxd_params[:alias]
73
+ lxd_params[:config][:"user.user-data"]['maas'] = true
74
74
  end
75
75
 
76
76
  if options['maas-on-lxc']
77
- args[:config][:"security.privileged"] = "true"
77
+ lxd_params[:config][:"security.privileged"] = "true"
78
78
  end
79
79
 
80
80
  if options['lxd-in-lxd']
81
- args[:config][:"security.nesting"] = "true"
81
+ lxd_params[:config][:"security.nesting"] = "true"
82
82
  end
83
83
 
84
- args[:config][:"user.user-data"]['gogetit'] = true
84
+ lxd_params[:config][:"user.user-data"]['gogetit'] = true
85
85
 
86
86
  # To disable to update apt database on first boot
87
87
  # so chef client can keep doing its job.
88
- args[:config][:'user.user-data']['package_update'] = false
89
- args[:config][:'user.user-data']['package_upgrade'] = false
88
+ lxd_params[:config][:'user.user-data']['package_update'] = false
89
+ lxd_params[:config][:'user.user-data']['package_upgrade'] = false
90
90
 
91
- generate_cloud_init_config(options, config, args)
92
-
93
- args[:config][:"user.user-data"] = \
94
- "#cloud-config\n" + YAML.dump(args[:config][:"user.user-data"])[4..-1]
95
-
96
- return args
97
- end
98
-
99
- def generate_cloud_init_config(options, config, args)
100
- logger.info("Calling <#{__method__.to_s}>")
101
-
102
- # apt
103
- args[:config][:'user.user-data']['apt'] = {}
104
- # preserve source list for a while
105
- args[:config][:'user.user-data']['apt']['preserve_sources_list'] = true
106
-
107
- if options['no-maas']
108
- # When there is no MAAS, containers should be able to resolve
109
- # their name with hosts file.
110
- args[:config][:'user.user-data']['manage_etc_hosts'] = true
111
- end
112
-
113
- # To add truested root CA certificates
114
- # https://cloudinit.readthedocs.io/en/latest/topics/examples.html
115
- # #configure-an-instances-trusted-ca-certificates
116
- #
117
- if config[:cloud_init] && config[:cloud_init][:ca_certs]
118
- args[:config][:'user.user-data']['ca-certs'] = {}
119
- certs = []
120
-
121
- config[:cloud_init][:ca_certs].each do |ca|
122
- content = get_http_content(ca)
123
- certs.push(
124
- /^-----BEGIN CERTIFICATE-----.*-/m.match(content).to_s
125
- ) if content
126
- end
127
-
128
- args[:config][:'user.user-data']['ca-certs'] = { 'trusted' => certs }
129
- end
130
-
131
- # To get CA public key to be used for SSH authentication
132
- # https://cloudinit.readthedocs.io/en/latest/topics/examples.html
133
- # #writing-out-arbitrary-files
134
- if config[:cloud_init] && config[:cloud_init][:ssh_ca_public_key]
135
- args[:config][:'user.user-data']['write_files'] = []
136
- content = get_http_content(config[:cloud_init][:ssh_ca_public_key][:key_url])
137
- if content
138
- file = {
139
- 'content' => content.chop!,
140
- 'path' => config[:cloud_init][:ssh_ca_public_key][:key_path],
141
- 'owner' => config[:cloud_init][:ssh_ca_public_key][:owner],
142
- 'permissions' => config[:cloud_init][:ssh_ca_public_key][:permissions]
143
- }
144
- args[:config][:'user.user-data']['write_files'].push(file)
145
- args[:config][:'user.user-data']['bootcmd'] = []
146
- args[:config][:'user.user-data']['bootcmd'].push(
147
- "cloud-init-per once ssh-ca-pub-key \
148
- echo \"TrustedUserCAKeys #{file['path']}\" >> /etc/ssh/sshd_config"
149
- )
150
- end
151
-
152
- if config[:cloud_init][:ssh_ca_public_key][:revocation_url]
153
- content = get_http_content(config[:cloud_init][:ssh_ca_public_key][:revocation_url])
154
- if content
155
- args[:config][:'user.user-data']['bootcmd'].push(
156
- "cloud-init-per once download-key-revocation-list \
157
- curl -o #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]} \
158
- #{config[:cloud_init][:ssh_ca_public_key][:revocation_url]}"
159
- )
160
- args[:config][:'user.user-data']['bootcmd'].push(
161
- "cloud-init-per once ssh-user-key-revocation-list \
162
- echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\" \
163
- >> /etc/ssh/sshd_config"
164
- )
165
- end
166
- end
167
- end
168
-
169
- # To add users
170
- # https://cloudinit.readthedocs.io/en/latest/topics/examples.html
171
- # #including-users-and-groups
172
- if config[:cloud_init] && config[:cloud_init][:users]
173
- args[:config][:'user.user-data']['users'] = []
174
- args[:config][:'user.user-data']['users'].push('default')
91
+ lxd_params[:config][:'user.user-data'] = generate_cloud_init_config(
92
+ options,
93
+ config,
94
+ lxd_params[:config][:'user.user-data']
95
+ )
175
96
 
176
- config[:cloud_init][:users].each do |user|
177
- args[:config][:'user.user-data']['users'].push(Hashie.stringify_keys user)
178
- end
179
- end
97
+ lxd_params[:config][:"user.user-data"] = \
98
+ "#cloud-config\n" + YAML.dump(lxd_params[:config][:"user.user-data"])[4..-1]
180
99
 
181
- return args
100
+ return lxd_params
182
101
  end
183
102
 
184
- def generate_network_config(args, options)
103
+ def generate_network_config(lxd_params, options)
185
104
  logger.info("Calling <#{__method__.to_s}>")
186
105
 
187
106
  if options['no-maas']
188
- args[:config][:'user.network-config'] = \
107
+ lxd_params[:config][:'user.network-config'] = \
189
108
  YAML.load_file(options['file'])['network']
190
109
 
191
110
  # physical device will be the gate device
192
- args[:config][:"user.network-config"]['config'].each do |iface|
111
+ lxd_params[:config][:"user.network-config"]['config'].each do |iface|
193
112
  if iface['type'] == "physical"
194
113
  options['ip_to_access'] = iface['subnets'][0]['address'].split('/')[0]
195
114
  end
196
115
  end
197
116
 
198
- args[:config][:"user.network-config"] = \
199
- YAML.dump(args[:config][:"user.network-config"])[4..-1]
117
+ lxd_params[:config][:"user.network-config"] = \
118
+ YAML.dump(lxd_params[:config][:"user.network-config"])[4..-1]
200
119
 
201
120
  elsif options['ipaddresses']
202
121
  options[:ifaces] = check_ip_available(options['ipaddresses'], maas)
@@ -205,7 +124,7 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
205
124
  abort("There is no gateway specified for the gateway network.") \
206
125
  unless options[:ifaces][0]['gateway_ip']
207
126
 
208
- args[:config][:'user.network-config'] = {
127
+ lxd_params[:config][:'user.network-config'] = {
209
128
  'version' => 1,
210
129
  'config' => [
211
130
  {
@@ -266,26 +185,26 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
266
185
  end
267
186
  end
268
187
 
269
- args[:config][:'user.network-config']['config'].push(iface_conf)
188
+ lxd_params[:config][:'user.network-config']['config'].push(iface_conf)
270
189
  end
271
190
 
272
- args[:config][:"user.network-config"] = \
273
- YAML.dump(args[:config][:"user.network-config"])[4..-1]
191
+ lxd_params[:config][:"user.network-config"] = \
192
+ YAML.dump(lxd_params[:config][:"user.network-config"])[4..-1]
274
193
  end
275
194
 
276
- return args
195
+ return lxd_params
277
196
  end
278
197
 
279
198
  # To configure devices
280
- def generate_devices(args, options)
199
+ def generate_devices(lxd_params, options)
281
200
  logger.info("Calling <#{__method__.to_s}>")
282
- args[:devices] = {}
201
+ lxd_params[:devices] = {}
283
202
 
284
203
  if options['no-maas']
285
- args[:devices] = YAML.load_file(options['file'])['devices']
204
+ lxd_params[:devices] = YAML.load_file(options['file'])['devices']
286
205
 
287
206
  # Now, LXD API can handle integer as a value of a map
288
- args[:devices].each do |k, v|
207
+ lxd_params[:devices].each do |k, v|
289
208
  v.each do |kk, vv|
290
209
  if vv.is_a? Integer
291
210
  v[kk] = vv.to_s
@@ -293,13 +212,13 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
293
212
  end
294
213
  end
295
214
 
296
- args[:devices] = (Hashie.symbolize_keys args[:devices])
215
+ lxd_params[:devices] = (Hashie.symbolize_keys lxd_params[:devices])
297
216
 
298
217
  elsif options['ipaddresses']
299
218
  options[:ifaces].each_with_index do |iface,index|
300
219
  if index == 0
301
220
  if iface['vlan']['name'] == 'untagged' # or vid == 0
302
- args[:devices][:"eth#{index}"] = {
221
+ lxd_params[:devices][:"eth#{index}"] = {
303
222
  mtu: iface['vlan']['mtu'].to_s, #This must be string
304
223
  name: "eth#{index}",
305
224
  nictype: 'bridged',
@@ -307,7 +226,7 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
307
226
  type: 'nic'
308
227
  }
309
228
  elsif iface['vlan']['name'] != 'untagged' # or vid != 0
310
- args[:devices][:"eth#{index}"] = {
229
+ lxd_params[:devices][:"eth#{index}"] = {
311
230
  mtu: iface['vlan']['mtu'].to_s, #This must be string
312
231
  name: "eth#{index}",
313
232
  nictype: 'bridged',
@@ -319,7 +238,7 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
319
238
  # it does not need to generate more devices
320
239
  # since it will configure the IPs with tagged VLANs.
321
240
  elsif options[:ifaces][0]['vlan']['name'] != 'untagged'
322
- args[:devices][:"eth#{index}"] = {
241
+ lxd_params[:devices][:"eth#{index}"] = {
323
242
  mtu: iface['vlan']['mtu'].to_s, #This must be string
324
243
  name: "eth#{index}",
325
244
  nictype: 'bridged',
@@ -346,8 +265,8 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
346
265
  end
347
266
  end
348
267
 
349
- args[:devices] = {}
350
- args[:devices][:"eth0"] = {
268
+ lxd_params[:devices] = {}
269
+ lxd_params[:devices][:"eth0"] = {
351
270
  mtu: root_bridge_mtu.to_s, #This must be string
352
271
  name: 'eth0',
353
272
  nictype: 'bridged',
@@ -360,13 +279,13 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
360
279
  # https://docs.maas.io/2.4/en/installconfig-lxd-install
361
280
  for i in 0..7
362
281
  i = i.to_s
363
- args[:devices]["loop" + i] = {}
364
- args[:devices]["loop" + i]["path"] = "/dev/loop" + i
365
- args[:devices]["loop" + i]["type"] = "unix-block"
282
+ lxd_params[:devices]["loop" + i] = {}
283
+ lxd_params[:devices]["loop" + i]["path"] = "/dev/loop" + i
284
+ lxd_params[:devices]["loop" + i]["type"] = "unix-block"
366
285
  end
367
286
  end
368
287
 
369
- return args
288
+ return lxd_params
370
289
  end
371
290
 
372
291
  def reserve_ips(name, options, container)
@@ -417,24 +336,24 @@ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\
417
336
  abort("Domain #{name}.#{maas.get_domain} already exists!") \
418
337
  if maas.domain_name_exists?(name) unless options['no-maas']
419
338
 
420
- args = {}
339
+ lxd_params = {}
421
340
 
422
341
  if options['alias'].nil? or options['alias'].empty?
423
- args[:alias] = config[:lxd][:default_alias]
342
+ lxd_params[:alias] = config[:lxd][:default_alias]
424
343
  else
425
- args[:alias] = options['alias']
344
+ lxd_params[:alias] = options['alias']
426
345
  end
427
346
 
428
- args = generate_user_data(args, options)
429
- args = generate_network_config(args, options)
430
- args = generate_devices(args, options)
347
+ lxd_params = generate_user_data(lxd_params, options)
348
+ lxd_params = generate_network_config(lxd_params, options)
349
+ lxd_params = generate_devices(lxd_params, options)
431
350
 
432
- args[:sync] ||= true
351
+ lxd_params[:sync] ||= true
433
352
 
434
- conn.create_container(name, args)
353
+ conn.create_container(name, lxd_params)
435
354
  container = conn.container(name)
436
355
 
437
- container.devices = args[:devices].merge!(container.devices.to_hash)
356
+ container.devices = lxd_params[:devices].merge!(container.devices.to_hash)
438
357
 
439
358
  # https://github.com/jeffshantz/hyperkit/blob/master/lib/hyperkit/client/containers.rb#L240
440
359
  # Adding configurations that are necessary for shipping MAAS on lxc
@@ -471,7 +390,7 @@ lxc.cgroup.devices.allow = b 7:* rwm"
471
390
  default_user = config[:default][:user]
472
391
  end
473
392
 
474
- args[:default_user] = default_user
393
+ lxd_params[:default_user] = default_user
475
394
 
476
395
  wait_until_available(ip_or_fqdn, default_user)
477
396
  logger.info("#{name} has been created.")
@@ -482,19 +401,19 @@ lxc.cgroup.devices.allow = b 7:* rwm"
482
401
  puts "ssh #{default_user}@#{name}"
483
402
  end
484
403
 
485
- { result: true, info: args }
404
+ { result: true, info: lxd_params }
486
405
  end
487
406
 
488
- def destroy(name, args = {})
407
+ def destroy(name, lxd_params = {})
489
408
  logger.info("Calling <#{__method__.to_s}>")
490
409
 
491
410
  container = conn.container(name)
492
- args[:sync] ||= true
411
+ lxd_params[:sync] ||= true
493
412
 
494
413
  info = container.to_hash
495
414
 
496
415
  if get_state(name) == 'Running'
497
- conn.stop_container(name, args)
416
+ conn.stop_container(name, lxd_params)
498
417
  end
499
418
 
500
419
  wait_until_state(name, 'Stopped')
@@ -524,7 +443,7 @@ lxc.cgroup.devices.allow = b 7:* rwm"
524
443
  maas.delete_dns_record(name)
525
444
  end
526
445
 
527
- conn.delete_container(name, args)
446
+ conn.delete_container(name, lxd_params)
528
447
 
529
448
  # When multiple static IPs were reserved, it will not delete anything
530
449
  # since they are deleted when releasing the IPs above.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gogetit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.2
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Draper