kitchen-lxd_cli 2.0.0 → 2.0.1

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: ad7b7dd864e7373d8f549d39ffc1ddbb58f3d929
4
- data.tar.gz: 1ca5ed8845e1ed386320d5f228a7f22cd1bf38da
3
+ metadata.gz: a122089b7742dd6200e4989317330712e0d1900a
4
+ data.tar.gz: ce6ef71bb797a9cd96874382b647644b4809a0f9
5
5
  SHA512:
6
- metadata.gz: 67c2f7b009f1f3941674d9c15e08437cc18e7361309e90dfbc70941dcd54dc97f0f53e0489eb7a8cc13511ea013c04821164f9c6a2924e98364f225eab3547ed
7
- data.tar.gz: 482187b180da0b2412be0e233fb4bbc4d8b7644a9c34da4cdee76e1d5d366eb011b666397c7d1ea9057fd31a9c806012f90c21eac535aad718dace56361d7d5e
6
+ metadata.gz: 94f664ec976a9c59913e1c9f8b29cb1bf025a0ef959562000f2ef67e91d7b1231f3ec6f2b635f333bed2f32b66da732dc6aa26cd55a828c870703411ed428799
7
+ data.tar.gz: 02987d6fb2ad4ee40cb4ad6b5081bcad24a759a39f08580c2d9a914e0968e4242c4f459daee514c2c12bcf1fd576c8df3230713ae372f50e4138e4859a000952
data/README.md CHANGED
@@ -65,7 +65,6 @@ driver:
65
65
  # lxd_proxy_update: true
66
66
  # lxd_proxy_path: "~/.lxd_proxy"
67
67
  # lxd_proxy_github_url: "-b development --single-branch https://github.com/bradenwright/cookbook-lxd_polipo
68
- # enable_wait_for_ssh_login: true
69
68
  mount:
70
69
  rails_mongodb_app:
71
70
  local_path: "/mylocalpath"
@@ -79,6 +78,7 @@ driver:
79
78
  use_publish_image: true
80
79
  publish_image_before_destroy: true
81
80
  publish_image_overwrite: true
81
+ lxd_unique_name: true
82
82
  enable_wait_for_ssh_login: false
83
83
  username: kitchen-user
84
84
 
@@ -160,6 +160,7 @@ Current config options:
160
160
  * lxd_proxy_update
161
161
  * lxd_proxy_path
162
162
  * lxd_proxy_github_url
163
+ * lxd_unique_name
163
164
 
164
165
  ### public_key_path
165
166
 
@@ -357,6 +358,10 @@ Default is https://github.com/bradenwright/cookbook-lxd_polipo basically if can
357
358
  lxd_proxy_github_url: "-b development --single-branch https://github.com/bradenwright/cookbook-lxd_polipo"
358
359
  ```
359
360
 
361
+ ### lxd_unique_name
362
+
363
+ Default is true. If true a file is written to .kitchen/<instance_name>.lxd with the unique name. This file is used to identify the lxd instance. If you don't want this feature, set to false
364
+
360
365
  ### enable_wait_for_ssh_login
361
366
 
362
367
  Default is false. If set to true container will loop until it can login to ssh, this ensures that ssh is ready before kitchen moves on to converging. It's false by default b/c it slows things down, and I only seemed to need it after using publishing option. Specifically if I didn't remove /root/.ssh, if I did remove /root/.ssh before publishing, then the sleep/time to setup ssh_public_key seemed to wait long enough that kitchen converge was not timing out.
@@ -30,29 +30,36 @@ module Kitchen
30
30
  kitchen_driver_api_version 2
31
31
 
32
32
  default_config :public_key_path do
33
+ pub_key = \
33
34
  [
34
35
  File.expand_path('~/.ssh/id_rsa.pub'),
35
36
  File.expand_path('~/.ssh/id_dsa.pub'),
36
37
  File.expand_path('~/.ssh/identity.pub'),
37
38
  File.expand_path('~/.ssh/id_ecdsa.pub')
38
39
  ].find { |path| File.exist?(path) }
40
+
41
+ raise 'Public key could not be found in the public_key_path provided. Please update the kitchen-lxd_cli config public_key_path in kitchen yaml or create a ssh key pair (e.g. `ssh-keygen -t rsa`)' unless pub_key
42
+
43
+ pub_key
39
44
  end
40
45
  default_config :never_destroy, false
41
46
  default_config :lxd_proxy_path, "#{ENV['HOME']}/.lxd_proxy"
42
47
  default_config :lxd_proxy_update, false
43
- default_config :username, "root"
48
+ default_config :username, 'root'
49
+ default_config :lxd_unique_name, true
44
50
 
45
- required_config :public_key_path
51
+ @@instance_name
46
52
 
47
53
  def create(state)
54
+ @@instance_name = get_or_create_unique_instance_name
48
55
  install_proxy if config[:lxd_proxy_install] && config[:lxd_proxy_install] == true
49
56
 
50
57
  unless exists?
51
58
  image_name = create_image_if_missing
52
59
  profile_args = setup_profile_args if config[:profile]
53
60
  config_args = setup_config_args
54
- info("Initializing container #{instance.name}")
55
- run_lxc_command("init #{image_name} #{instance.name} #{profile_args} #{config_args}")
61
+ info("Initializing container #{@@instance_name}")
62
+ run_lxc_command("init #{image_name} #{@@instance_name} #{profile_args} #{config_args}")
56
63
  end
57
64
 
58
65
  config_and_start_container unless running?
@@ -62,24 +69,26 @@ module Kitchen
62
69
  state[:username] = config[:username]
63
70
  setup_ssh_access
64
71
  wait_for_ssh_login(lxc_ip) if config[:enable_wait_for_ssh_login] == "true"
65
- IO.popen("lxc exec #{instance.name} bash", "r+") do |p|
72
+ IO.popen("lxc exec #{@@instance_name} bash", "r+") do |p|
66
73
  p.puts("if [ ! -d '#{config[:verifier_path]}' ]; then mkdir -p #{config[:verifier_path]}; fi")
67
74
  p.puts("if [ ! -L '/tmp/verifier' ]; then ln -s #{config[:verifier_path]} /tmp/verifier; fi")
68
75
  end if config[:verifier_path] && config[:verifier_path].length > 0
69
76
  end
70
77
 
71
78
  def destroy(state)
79
+ @@instance_name = get_or_create_unique_instance_name
72
80
  if exists?
73
81
  if running?
74
- info("Stopping container #{instance.name}")
75
- run_lxc_command("stop #{instance.name}")
82
+ info("Stopping container #{@@instance_name}")
83
+ run_lxc_command("stop #{@@instance_name} --force")
76
84
  end
77
85
 
78
86
  publish_image if config[:publish_image_before_destroy]
79
87
 
80
- unless config[:never_destroy]
81
- info("Deleting container #{instance.name}")
82
- run_lxc_command("delete #{instance.name}") unless config[:never_destroy] && config[:never_destroy] == true
88
+ unless config[:never_destroy] && config[:never_destroy] == true
89
+ info("Deleting container #{@@instance_name}")
90
+ run_lxc_command("delete #{@@instance_name} --force")
91
+ File.delete(".kitchen/#{instance.name}.lxd_unique_name") if File.exist?(".kitchen/#{instance.name}.lxd_unique_name")
83
92
  end
84
93
  end
85
94
  state.delete(:hostname)
@@ -87,24 +96,34 @@ module Kitchen
87
96
  end
88
97
 
89
98
  private
99
+ def get_or_create_unique_instance_name
100
+ if config[:lxd_unique_name] == false
101
+ instance_name = instance.name
102
+ else
103
+ File.write(".kitchen/#{instance.name}.lxd", "#{instance.name}-#{Time.now.to_i}") unless File.exist?(".kitchen/#{instance.name}.lxd")
104
+ instance_name = File.read(".kitchen/#{instance.name}.lxd")
105
+ end
106
+ instance_name
107
+ end
108
+
90
109
  def exists?
91
- `lxc info #{instance.name} > /dev/null 2>&1`
110
+ `lxc info #{@@instance_name} > /dev/null 2>&1`
92
111
  if $?.to_i == 0
93
- debug("Container #{instance.name} exists")
112
+ debug("Container #{@@instance_name} exists")
94
113
  return true
95
114
  else
96
- debug("Container #{instance.name} doesn't exist")
115
+ debug("Container #{@@instance_name} doesn't exist")
97
116
  return false
98
117
  end
99
118
  end
100
119
 
101
120
  def running?
102
- status = `lxc info #{instance.name}`.match(/Status: ([a-zA-Z]+)[\n]/).captures[0].upcase
121
+ status = `lxc info #{@@instance_name}`.match(/Status: ([a-zA-Z]+)[\n]/).captures[0].upcase
103
122
  if status == "RUNNING"
104
- debug("Container #{instance.name} is running")
123
+ debug("Container #{@@instance_name} is running")
105
124
  return true
106
125
  else
107
- debug("Container #{instance.name} isn't running")
126
+ debug("Container #{@@instance_name} isn't running")
108
127
  return false
109
128
  end
110
129
  end
@@ -121,7 +140,6 @@ module Kitchen
121
140
  image_release ||= image[:release_num]
122
141
  debug("Ran command: lxc image copy #{image_os}:#{image_release} local: --alias #{image_name}")
123
142
  IO.popen("lxc image copy #{image_os}:#{image_release} local: --alias #{image_name}", "w") { |pipe| puts pipe.gets rescue nil }
124
- # IO.popen("lxd-images import #{image_os} #{image_release} --alias #{image_name}", "w") { |pipe| puts pipe.gets rescue nil }
125
143
  end
126
144
 
127
145
  return image_name
@@ -157,11 +175,11 @@ module Kitchen
157
175
  info("Deleting existing image #{publish_image_name}, so image of same name can be published")
158
176
  run_lxc_command("image delete #{publish_image_name}")
159
177
  else
160
- raise "Image #{publish_image_name} already exists! If you wish to overwrite it set publish_image_overwrite: true in kitchen.yml"
178
+ raise "Image #{publish_image_name} already exists! If you wish to overwrite it set publish_image_overwrite: true in kitchen yaml"
161
179
  end
162
180
  end
163
181
  info("Publishing image #{publish_image_name}")
164
- run_lxc_command("publish #{instance.name} --alias #{publish_image_name}")
182
+ run_lxc_command("publish #{@@instance_name} --alias #{publish_image_name}")
165
183
  end
166
184
 
167
185
  def get_image_name
@@ -200,14 +218,14 @@ module Kitchen
200
218
 
201
219
  if config[:ipv4]
202
220
  IO.popen("bash", "r+") do |p|
203
- p.puts("echo -e \"lxc.network.0.ipv4 = #{config[:ipv4]}\nlxc.network.0.ipv4.gateway = #{config[:ip_gateway]}\n\" | lxc config set #{instance.name} raw.lxc -")
221
+ p.puts("echo -e \"lxc.network.0.ipv4 = #{config[:ipv4]}\nlxc.network.0.ipv4.gateway = #{config[:ip_gateway]}\n\" | lxc config set #{@@instance_name} raw.lxc -")
204
222
  p.puts("exit")
205
223
  end
206
- arg_disable_dhcp = "&& lxc exec #{instance.name} -- sed -i 's/dhcp/manual/g' /etc/network/interfaces.d/eth0.cfg"
224
+ arg_disable_dhcp = "&& lxc exec #{@@instance_name} -- sed -i 's/dhcp/manual/g' /etc/network/interfaces.d/eth0.cfg"
207
225
  end
208
226
 
209
- info("Starting container #{instance.name}")
210
- run_lxc_command("start #{instance.name} #{arg_disable_dhcp}")
227
+ info("Starting container #{@@instance_name}")
228
+ run_lxc_command("start #{@@instance_name} #{arg_disable_dhcp}")
211
229
  setup_mount_bindings if config[:mount].class == Hash
212
230
  end
213
231
 
@@ -252,13 +270,13 @@ module Kitchen
252
270
  debug("Source path for the #{mount_name} doesn't exist, creating #{host_path}")
253
271
  FileUtils.mkdir_p(host_path)
254
272
  end
255
- run_lxc_command("config device add #{instance.name} #{mount_name} disk source=#{host_path} path=#{mount_binding[:container_path]}")
273
+ run_lxc_command("config device add #{@@instance_name} #{mount_name} disk source=#{host_path} path=#{mount_binding[:container_path]}")
256
274
  end
257
275
  end if config[:mount].class == Hash
258
276
  end
259
277
 
260
278
  def configure_dns
261
- IO.popen("lxc exec #{instance.name} bash", "r+") do |p|
279
+ IO.popen("lxc exec #{@@instance_name} bash", "r+") do |p|
262
280
  dns_servers = ""
263
281
  config[:dns_servers].each do |dns_server|
264
282
  dns_servers += "nameserver #{dns_server}\n"
@@ -273,7 +291,7 @@ module Kitchen
273
291
  end if config[:ipv4] && dns_servers.length == 0
274
292
 
275
293
  if dns_servers.length > 0
276
- if system "lxc exec #{instance.name} -- test -e /etc/redhat-release"
294
+ if system "lxc exec #{@@instance_name} -- test -e /etc/redhat-release"
277
295
  wait_for_path("/etc/resolv.conf")
278
296
  debug("Setting up the following dns servers via /etc/resolv.conf:")
279
297
  debug(dns_servers.gsub("\n", ' '))
@@ -304,13 +322,13 @@ module Kitchen
304
322
  end
305
323
 
306
324
  def setup_ssh_access
307
- info("Setting up public key #{config[:public_key_path]} on #{instance.name}")
325
+ info("Setting up public key #{config[:public_key_path]} on #{@@instance_name}")
308
326
  unless config[:username] == "root"
309
327
  create_ssh_user
310
- info("Checking /home/#{config[:username]}/.ssh on #{instance.name}")
328
+ info("Checking /home/#{config[:username]}/.ssh on #{@@instance_name}")
311
329
  wait_for_path("/home/#{config[:username]}/.ssh")
312
330
  else
313
- info("Check /#{config[:username]}/.ssh on #{instance.name}")
331
+ info("Check /#{config[:username]}/.ssh on #{@@instance_name}")
314
332
  wait_for_path("/#{config[:username]}/.ssh")
315
333
  end
316
334
 
@@ -322,20 +340,20 @@ module Kitchen
322
340
  home_path = '/'
323
341
  end
324
342
  authorized_keys_path = "#{home_path}#{config[:username]}/.ssh/authorized_keys"
325
- `lxc file push #{config[:public_key_path]} #{instance.name}#{authorized_keys_path} 2> /dev/null && lxc exec #{instance.name} -- chown #{config[:username]}:#{config[:username]} #{authorized_keys_path}`
343
+ `lxc file push #{config[:public_key_path]} #{@@instance_name}#{authorized_keys_path} 2> /dev/null && lxc exec #{@@instance_name} -- chown #{config[:username]}:#{config[:username]} #{authorized_keys_path}`
326
344
  break if $?.to_i == 0
327
345
  sleep 0.3
328
346
  end while true
329
347
 
330
- debug("Finished Copying public key from #{config[:public_key_path]} to #{instance.name}")
348
+ debug("Finished Copying public key from #{config[:public_key_path]} to #{@@instance_name}")
331
349
  end
332
350
 
333
351
  def create_ssh_user
334
- info("Create user #{config[:username]} on #{instance.name}")
335
- `lxc exec #{instance.name} -- useradd -m -G sudo #{config[:username]} -s /bin/bash`
336
- `lxc exec #{instance.name} -- mkdir /home/#{config[:username]}/.ssh`
337
- `lxc exec #{instance.name} -- chown #{config[:username]}:#{config[:username]} /home/#{config[:username]}/.ssh`
338
- `lxc exec #{instance.name} -- sh -c "echo '#{config[:username]} ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"`
352
+ info("Create user #{config[:username]} on #{@@instance_name}")
353
+ `lxc exec #{@@instance_name} -- useradd -m -G sudo #{config[:username]} -s /bin/bash`
354
+ `lxc exec #{@@instance_name} -- mkdir /home/#{config[:username]}/.ssh`
355
+ `lxc exec #{@@instance_name} -- chown #{config[:username]}:#{config[:username]} /home/#{config[:username]}/.ssh`
356
+ `lxc exec #{@@instance_name} -- sh -c "echo '#{config[:username]} ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"`
339
357
  end
340
358
 
341
359
  def install_proxy
@@ -381,7 +399,7 @@ module Kitchen
381
399
  def wait_for_ip_address
382
400
  info("Waiting for network to become ready")
383
401
  begin
384
- lxc_info = `lxc info #{instance.name}`.match(/eth0:\tinet\t(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
402
+ lxc_info = `lxc info #{@@instance_name}`.match(/eth0:\tinet\t(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
385
403
  debug("Still waiting for IP Address...")
386
404
  lxc_ip = lxc_info.captures[0].to_s if lxc_info && lxc_info.captures
387
405
  break if lxc_ip && lxc_ip.length > 7
@@ -394,7 +412,7 @@ module Kitchen
394
412
  def wait_for_path(path)
395
413
  begin
396
414
  debug("Waiting for #{path} to become available...")
397
- run_lxc_command("exec #{instance.name} -- ls #{path} > /dev/null 2>&1")
415
+ run_lxc_command("exec #{@@instance_name} -- ls #{path} > /dev/null 2>&1")
398
416
  break if $?.to_i == 0
399
417
  sleep 0.3
400
418
  end while true
@@ -430,7 +448,7 @@ module Kitchen
430
448
  def configure_ip_via_lxc_restart
431
449
  debug("Configuring new ip address on eth0")
432
450
 
433
- IO.popen("lxc exec #{instance.name} bash", "r+") do |p|
451
+ IO.popen("lxc exec #{@@instance_name} bash", "r+") do |p|
434
452
  p.puts('echo -e "#############################################" > /etc/network/interfaces.d/eth0.cfg')
435
453
  p.puts('echo -e "# DO NOT EDIT CONTROLLED BY KITCHEN-LXC_CLI #" >> /etc/network/interfaces.d/eth0.cfg')
436
454
  p.puts('echo -e "#############################################" >> /etc/network/interfaces.d/eth0.cfg')
@@ -452,13 +470,13 @@ module Kitchen
452
470
  end
453
471
  p.puts("exit")
454
472
  end
455
- debug("Finished configuring new ip address, restarting #{instance.name} for settings to take effect")
473
+ debug("Finished configuring new ip address, restarting #{@@instance_name} for settings to take effect")
456
474
  debug_note_about_configuring_ip
457
475
  wait_for_ip_address
458
476
  sleep 3 # Was hanging more often than not whenever I lowered the sleep
459
- debug("Restarting #{instance.name}")
460
- run_lxc_command("restart #{instance.name}")
461
- debug("Finished restarting #{instance.name} ip address should be configured")
477
+ debug("Restarting #{@@instance_name}")
478
+ run_lxc_command("restart #{@@instance_name}")
479
+ debug("Finished restarting #{@@instance_name} ip address should be configured")
462
480
  end
463
481
  =end
464
482
 
@@ -21,6 +21,6 @@ module Kitchen
21
21
  module Driver
22
22
 
23
23
  # Version string for LxdCli Kitchen driver
24
- LXD_CLI_VERSION = "2.0.0"
24
+ LXD_CLI_VERSION = "2.0.1"
25
25
  end
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-lxd_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braden Wright
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-04 00:00:00.000000000 Z
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-kitchen