kitchen-lxd 0.1.1 → 0.2.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: 66958532b083530043a7897ffca23ef14d1322a5
4
- data.tar.gz: 42c74aec7000ea99bc219fe850a719f718198005
3
+ metadata.gz: bb8e8006b752908610c409c5d138e712fc11472c
4
+ data.tar.gz: a62b71d6d1504c16479809006124444918fb901e
5
5
  SHA512:
6
- metadata.gz: 20ee77c76546f2cf7782951e42bc0cff1d7f15448403970967a915c13aa229281ed6ec2803d1b74ca3fea2aa926e9275007afebe759f340979b4dd15f9fd3232
7
- data.tar.gz: 2e3a72ccb0c16b6ae775b6bf479e4973689d2f65502025b16d40ef1d76005a520acad5167d2453326004f32b1e30af65d230179d5e118a4efcfc520eb6348397
6
+ metadata.gz: f0e9d5a7a1fee40803833b21ddd7c4070abb9ad39fc3a152f3c834c94045e1e61f62c2ad0c3b1922877a05cf5028193dd466be668a010d73d82f7bf389a1d60b
7
+ data.tar.gz: d46312416e606f2ac76c0d57a2f49f2f5301d85a87cb5bb6307379e40868b1e664fb15dd737699fdab9bcc36ee605dba95292632c55a87b96a4453b4e5ddf8ea
data/CHANGELOG.md CHANGED
@@ -1,7 +1,15 @@
1
- ## 0.1.1
1
+ ## 0.2.0 (2017-10-11)
2
+
3
+ - Add LXD transport.
4
+ - Remove ssh transport support.
5
+ - Allow specifying network bridge to attach to.
6
+ - Allow specifying path to lxc executable.
7
+ - Support downloading image from remote, if it's not available locally.
8
+
9
+ ## 0.1.1 (2017-02-08)
2
10
 
3
11
  - Support Lxd version 2.8.
4
12
 
5
- ## 0.1.0
13
+ ## 0.1.0 (2017-02-07)
6
14
 
7
- - Initial release
15
+ - Initial release.
data/README.md CHANGED
@@ -1,21 +1,33 @@
1
- # <a name="title"></a> Kitchen::Lxd
1
+ # Kitchen::Lxd
2
2
 
3
3
  [![Build Status](https://travis-ci.org/zeroturnaround/kitchen-lxd.svg?branch=master)](https://travis-ci.org/zeroturnaround/kitchen-lxd)
4
4
 
5
- A Test Kitchen Driver for Lxd.
5
+ - [Requirements](#requirements)
6
+ - [Lxd](#lxd)
7
+ - [Installation and Setup](#installation-and-setup)
8
+ - [Configuration](#configuration)
9
+ - [Driver](#driver)
10
+ - [Transport](#transport)
11
+ - [Development](#development)
12
+ - [Authors](#authors)
13
+ - [License](#license)
6
14
 
7
- ## <a name="requirements"></a> Requirements
15
+ A Test Kitchen Driver (with Transport) for Lxd.
16
+
17
+ ## Requirements
8
18
 
9
19
  ### Lxd
10
20
 
11
- Lxd version of 2.3 (the one where "lxc network" commands were introduced) or higher is required for
12
- this driver which means that a native package must be installed on the system running Test Kitchen.
13
- You must also prepare a container to be used by Test Kitchen. For that make sure there is ssh daemon
14
- installed and starting on boot. ( On Ubuntu it can be done by simply installing 'openssh-server'
15
- package. ) You can also install Chef Client there, if you do not want Test Kitchen to install it
16
- every time the container is started.
21
+ Lxd version of 2.3 (the one where "lxc network" commands were introduced) or higher is required
22
+ for this driver which means that a native package must be installed on the system running Test
23
+ Kitchen.
24
+
25
+ You do not have to prepare any container image specifically, like downloading it or installing ssh
26
+ server. The driver will download container image automatically from the provided remote server,
27
+ if it's not available locally. Also you can use `lxd` transport instead of default `ssh`. Which
28
+ means files will be uploaded to container using `lxc file push` command.
17
29
 
18
- ## <a name="installation"></a> Installation and Setup
30
+ ## Installation and Setup
19
31
 
20
32
  Install using command line:
21
33
 
@@ -23,74 +35,43 @@ Install using command line:
23
35
  gem install kitchen-lxd
24
36
  ```
25
37
 
26
- ## <a name="config"></a> Configuration
27
-
28
- Available options:
29
-
30
- - image
31
- - container
32
- - require_chef_omnibus
33
-
34
- ### image
38
+ ## Configuration
35
39
 
36
- Define from which lxd image the container will be created.
40
+ Example config file may look like this:
37
41
 
38
42
  ```yaml
39
43
  ---
40
44
  driver:
41
- image: ubuntu/xenial/amd64
42
- ```
43
-
44
- The default is value of `platform name`.
45
-
46
- ```yaml
47
- ---
48
- platforms:
49
- - name: kitchen-xenial64
50
- ```
51
-
52
- In this case: 'kitchen-xenial64', expecting to have an lxd image with this name locally.
53
-
54
- ### container
45
+ name: lxd
46
+ binary: lxc # this is default
47
+ remote: images # this is default
48
+ network: lxdbr0 # this is default
55
49
 
56
- Created container name.
57
-
58
- ```yaml
59
- ---
60
- driver:
61
- container_name: my_name
50
+ transport:
51
+ name: lxd
62
52
  ```
63
53
 
64
- The default is value of `kitchen instance name`.
54
+ Default values can be omitted, so the minimal config file looks like this:
65
55
 
66
56
  ```yaml
67
57
  ---
68
- platforms:
69
- - name: kitchen-xenial64
58
+ driver: lxd
70
59
 
71
- suites:
72
- - name: webserver
60
+ transport: lxd
73
61
  ```
74
62
 
75
- In this case: 'webserver-kitchen-xenial64'.
76
-
77
- ### <a name="config-require-chef-omnibus"></a> require\_chef\_omnibus
78
-
79
- Determines whether or not a Chef [Omnibus package][chef_omnibus_dl] will be
80
- installed. There are several different behaviors available:
63
+ ### Driver
81
64
 
82
- - `true` - the latest release will be installed. Subsequent converges
83
- will skip re-installing if chef is present.
84
- - `latest` - the latest release will be installed. Subsequent converges
85
- will always re-install even if chef is present.
86
- - `<VERSION_STRING>` (ex: `10.24.0`) - the desired version string will
87
- be passed the the install.sh script. Subsequent converges will skip if
88
- the installed version and the desired version match.
89
- - `false` or `nil` - no chef is installed.
65
+ Available options:
90
66
 
91
- The default value is unset, or `nil`.
67
+ Name | Description | Type | Default
68
+ -----|-------------|------|--------
69
+ binary | Path to lxc executable | String | `lxc`
70
+ remote | Remote LXD server to download image from, if it does not exist locally | String | `images`
71
+ network | Network bridge to attach to container | String | `lxdbr0`
72
+ wait_until_ready | Wait for the network to come up | Boolean | `true`
92
73
 
93
- ## <a name="development"></a> Development
74
+ ## Development
94
75
 
95
76
  - Source hosted at [GitHub][repo]
96
77
  - Report issues/questions/feature requests on [GitHub Issues][issues]
@@ -105,18 +86,15 @@ example:
105
86
  4. Push to the branch (`git push origin my-new-feature`)
106
87
  5. Create new Pull Request
107
88
 
108
- ## <a name="authors"></a> Authors
89
+ ## Authors
109
90
 
110
- Created and maintained by [ZeroTurnaround][author].
91
+ Created and maintained by [Juri Timošin][author].
111
92
 
112
- ## <a name="license"></a> License
93
+ ## License
113
94
 
114
95
  Apache 2.0 (see [LICENSE][license])
115
96
 
116
-
117
- [author]: https://github.com/zeroturnaround
118
- [issues]: https://github.com/zeroturnaround/kitchen-lxd/issues
119
- [license]: https://github.com/zeroturnaround/kitchen-lxd/blob/master/LICENSE
120
- [repo]: https://github.com/zeroturnaround/kitchen-lxd
121
- [driver_usage]: http://docs.kitchen-ci.org/drivers/usage
122
- [chef_omnibus_dl]: http://www.chef.io/chef/install/
97
+ [author]: https://github.com/DracoAter
98
+ [issues]: https://github.com/DracoAter/kitchen-lxd/issues
99
+ [license]: https://github.com/DracoAter/kitchen-lxd/blob/master/LICENSE
100
+ [repo]: https://github.com/DracoAter/kitchen-lxd
@@ -8,7 +8,7 @@
8
8
  # you may not use this file except in compliance with the License.
9
9
  # You may obtain a copy of the License at
10
10
  #
11
- # http://www.apache.org/licenses/LICENSE-2.0
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
12
  #
13
13
  # Unless required by applicable law or agreed to in writing, software
14
14
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -25,21 +25,31 @@ module Kitchen
25
25
  #
26
26
  # @author Juri Timošin <draco.ater@gmail.com>
27
27
  class Lxd < Kitchen::Driver::Base
28
+ MIN_LXD_VERSION = '2.3'
29
+
28
30
  kitchen_driver_api_version 2
29
31
 
30
- default_config( :image ){|driver| driver.instance.platform.name }
31
- default_config( :container ){|driver| driver.instance.name }
32
+ default_config :binary, 'lxc'
33
+ default_config :remote, 'images'
34
+ default_config :network, 'lxdbr0'
35
+ default_config :wait_until_ready, true
36
+ default_config :fix_chef_install, false
32
37
 
33
- attr_accessor :container
38
+ default_config :image do |driver|
39
+ driver.instance.platform.name
40
+ end
41
+
42
+ default_config :container do |driver|
43
+ driver.instance.name
44
+ end
34
45
 
35
46
  def create( state )
36
47
  container.init
37
- container.attach_network 'lxdbr0'
48
+ container.attach_network config[:network] if config[:network]
38
49
  container.start
39
- container.prepare_ssh
40
50
 
41
- state[:hostname] = container.wait_for_ipv4
42
- instance.transport.connection( state ).wait_until_ready
51
+ state[:hostname] = instance.transport.connection( state ).wait_until_ready if config[:wait_until_ready]
52
+ container.fix_chef_install( instance.platform.name ) if config[:fix_chef_install]
43
53
  end
44
54
 
45
55
  def destroy( state )
@@ -48,10 +58,17 @@ module Kitchen
48
58
  container.destroy
49
59
  end
50
60
 
51
- private
61
+ def verify_dependencies
62
+ return
63
+ version = run_command( "#{config[:binary]} --version" ).strip
64
+ if Gem::Version.new( version ) < Gem::Version.new( MIN_LXD_VERSION )
65
+ raise UserError, "Detected old version of Lxd (#{version}), please upgrade to version "\
66
+ "#{MIN_LXD_VERSION} or higher."
67
+ end
68
+ end
52
69
 
53
70
  def container
54
- @container = Lxd::Container.new( config[:container], config[:image], logger ) if @container.nil?
71
+ @container = Lxd::Container.new( logger, config ) if @container.nil?
55
72
  @container
56
73
  end
57
74
  end
@@ -8,7 +8,7 @@
8
8
  # you may not use this file except in compliance with the License.
9
9
  # You may obtain a copy of the License at
10
10
  #
11
- # http://www.apache.org/licenses/LICENSE-2.0
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
12
  #
13
13
  # Unless required by applicable law or agreed to in writing, software
14
14
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,77 +29,99 @@ module Kitchen
29
29
  attr_reader :logger
30
30
  attr_reader :state
31
31
 
32
- def initialize( name, image, logger )
33
- @name = name
34
- @image = image
32
+ def initialize( logger, opts )
35
33
  @logger = logger
36
- update_state
34
+ @name = opts[:container]
35
+ @image = opts[:image]
36
+ @remote = opts[:remote]
37
+ @binary = opts[:binary]
37
38
  end
38
39
 
39
40
  def init
40
- unless created?
41
- run_command "lxc init #@image #@name"
42
- update_state
43
- end
41
+ return if created?
42
+ download_image unless image_exists?
43
+ run_command "#@binary init #@image #@name"
44
44
  end
45
45
 
46
46
  def attach_network( network )
47
- unless device_attached? network
48
- run_command "lxc network attach #{network} #@name"
49
- update_state
50
- end
47
+ return if device_attached? network
48
+ run_command "#@binary network attach #{network} #@name"
51
49
  end
52
50
 
53
51
  def start
54
- unless running?
55
- run_command "lxc start #@name"
56
- update_state
57
- end
58
- end
59
-
60
- def prepare_ssh
61
- run_command "lxc exec #@name mkdir -- -p /root/.ssh"
62
- run_command "lxc file push ~/.ssh/id_rsa.pub #@name/root/.ssh/authorized_keys"
63
- run_command "lxc exec #@name chown -- root:root /root/.ssh/authorized_keys"
52
+ return if running?
53
+ run_command "#@binary start #@name"
64
54
  end
65
55
 
66
56
  def destroy
67
- if created?
68
- run_command "lxc delete #@name --force"
69
- update_state
70
- end
57
+ return unless created?
58
+ run_command "#@binary delete #@name --force"
71
59
  end
72
60
 
73
- def wait_for_ipv4
61
+ def wait_until_ready
74
62
  info 'Wait for network to become ready.'
75
63
  9.times do
76
- update_state
77
- s = @state['state'].nil? ? @state['State'] : @state['state']
78
- inet = s['network']['eth0']['addresses'].detect do |i|
79
- i['family'] == 'inet'
64
+ s = fetch_state[:state].nil? ? @state[:State] : @state[:state]
65
+ inet = s[:network][:eth0][:addresses].detect do |i|
66
+ i[:family] == 'inet'
80
67
  end
81
- return inet['address'] if inet
68
+ return inet[:address] if inet
82
69
  sleep 1 unless defined?( Minitest )
83
70
  end
84
71
  nil
85
72
  end
86
73
 
74
+ def download_image
75
+ run_command "#@binary image copy --copy-aliases #@remote:#@image local:"
76
+ end
77
+
78
+ def execute( command )
79
+ return if command.nil?
80
+ run_command "#@binary exec #@name -- #{command}"
81
+ end
82
+
83
+ def login_command
84
+ LoginCommand.new( "#@binary exec #@name -- bash", {} )
85
+ end
86
+
87
+ def upload( locals, remote )
88
+ locals.each do |local|
89
+ run_command "#@binary file push -rp #{local} #@name/#{remote}"
90
+ end
91
+ end
92
+
93
+ def fix_chef_install( platform )
94
+ case platform
95
+ when /ubuntu/, /debian/
96
+ execute "apt install -y wget"
97
+ when /rhel/, /centos/
98
+ execute 'yum install -y sudo wget'
99
+ end
100
+ end
87
101
  private
88
102
 
89
- def update_state
90
- @state = JSON.parse( run_command "lxc list #@name --format json" ).first
103
+ def image_exists?
104
+ !JSON.parse(
105
+ run_command( "#@binary image list #@image --format json" ), symbolize_names: true
106
+ ).empty?
107
+ end
108
+
109
+ def fetch_state
110
+ @state = JSON.parse(
111
+ run_command( "#@binary list #@name --format json" ), symbolize_names: true
112
+ ).first
91
113
  end
92
114
 
93
115
  def running?
94
- @state['status'] == 'Running'
116
+ fetch_state[:status] == 'Running'
95
117
  end
96
118
 
97
119
  def created?
98
- !@state.nil?
120
+ !fetch_state.nil?
99
121
  end
100
122
 
101
123
  def device_attached?( network )
102
- @state['devices'] and @state['devices'][network.to_s]
124
+ fetch_state[:devices] and @state[:devices][network.to_sym]
103
125
  end
104
126
  end
105
127
  end
@@ -8,7 +8,7 @@
8
8
  # you may not use this file except in compliance with the License.
9
9
  # You may obtain a copy of the License at
10
10
  #
11
- # http://www.apache.org/licenses/LICENSE-2.0
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
12
  #
13
13
  # Unless required by applicable law or agreed to in writing, software
14
14
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,9 +16,13 @@
16
16
  # See the License for the specific language governing permissions and
17
17
  # limitations under the License.
18
18
 
19
+ require 'kitchen'
20
+
19
21
  module Kitchen
20
22
  module Driver
21
- # Version string for Lxd Kitchen driver
22
- LXD_VERSION = '0.1.1'
23
+ class Lxd < Kitchen::Driver::Base
24
+ # Version string for Lxd Kitchen driver
25
+ VERSION = '0.2.0'
26
+ end
23
27
  end
24
28
  end
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Juri Timošin (<draco.ater@gmail.com>)
4
+ #
5
+ # Copyright (C) 2017, Juri Timošin
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'kitchen'
20
+ require 'forwardable'
21
+
22
+ module Kitchen
23
+ module Transport
24
+ # Transport for Lxd driver for Kitchen.
25
+ #
26
+ # @author Juri Timošin <draco.ater@gmail.com>
27
+ class Lxd < Base
28
+ kitchen_transport_api_version 2
29
+
30
+ default_config :container do |transport|
31
+ transport.instance.driver.container
32
+ end
33
+
34
+ def connection( state, &block )
35
+ @connection = Connection.new( config.to_hash.merge( state ), &block )
36
+ end
37
+
38
+ class Connection < Base::Connection
39
+ extend Forwardable
40
+
41
+ def initialize( opts )
42
+ @container = opts[:container]
43
+ super( opts )
44
+ end
45
+
46
+ def_delegators :@container, :execute, :login_command, :upload, :wait_until_ready
47
+ end
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-lxd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juri Timošin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-30 00:00:00.000000000 Z
11
+ date: 2017-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-kitchen
@@ -97,7 +97,6 @@ dependencies:
97
97
  description: Kitchen::Driver::Lxd - an Lxd driver for Test Kitchen.
98
98
  email:
99
99
  - draco.ater@gmail.com
100
- - juri.timoshin@zeroturnaround.com
101
100
  executables: []
102
101
  extensions: []
103
102
  extra_rdoc_files: []
@@ -108,7 +107,8 @@ files:
108
107
  - lib/kitchen/driver/lxd.rb
109
108
  - lib/kitchen/driver/lxd/container.rb
110
109
  - lib/kitchen/driver/version.rb
111
- homepage: https://github.com/zeroturnaround/kitchen-lxd
110
+ - lib/kitchen/transport/lxd.rb
111
+ homepage: https://github.com/DracoAter/kitchen-lxd
112
112
  licenses:
113
113
  - Apache-2.0
114
114
  metadata: {}
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  version: '0'
129
129
  requirements: []
130
130
  rubyforge_project:
131
- rubygems_version: 2.5.1
131
+ rubygems_version: 2.5.2
132
132
  signing_key:
133
133
  specification_version: 4
134
134
  summary: An Lxd driver for Test Kitchen.