kitchen-vz 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b13e71aada0ac01d7ee5f9077b7f89b7b18571f
4
+ data.tar.gz: bb56d5a8c0cc50786c35a1ac4f08a18e5a14fd31
5
+ SHA512:
6
+ metadata.gz: 12d852135052285afaed5a80de8e89a1b27e83ba3fba2faa93f0312d1248a12ebfb0e32d6891a6f1f0dfe9cef76fe03d31d43b54248e67cc2d0202847e92c921
7
+ data.tar.gz: 3689b273994d03191a114e6c6a4a7ab3b476a1aeceb36adf2fe49dadb2ee02a9544b96a6cad72ff6ac45b3a7d495486cc19951fedef9ac22614a15c69068fe49
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .kitchen
data/.kitchen.yml ADDED
@@ -0,0 +1,17 @@
1
+ ---
2
+ driver:
3
+ name: vz
4
+
5
+ provisioner:
6
+ name: dummy
7
+
8
+ platforms:
9
+ - name: centos-6.6
10
+ driver:
11
+ socket: 'ssh://cookbook-ci.prls.net'
12
+ network:
13
+ Bridged:
14
+ dhcp: true
15
+
16
+ suites:
17
+ - name: default
data/.rubocop.yml ADDED
@@ -0,0 +1,32 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+
5
+ AlignParameters:
6
+ Enabled: false
7
+ ClassLength:
8
+ Enabled: false
9
+ CyclomaticComplexity:
10
+ Enabled: false
11
+ Documentation:
12
+ Enabled: false
13
+ Encoding:
14
+ Enabled: false
15
+ Style/FileName:
16
+ Enabled: false
17
+ LineLength:
18
+ Enabled: false
19
+ MethodLength:
20
+ Enabled: false
21
+ Metrics/AbcSize:
22
+ Enabled: false
23
+ PerceivedComplexity:
24
+ Enabled: false
25
+ Lint/SpaceBeforeFirstArg:
26
+ Enabled: false
27
+ Style/ClassAndModuleChildren:
28
+ Enabled: false
29
+ Style/FileName:
30
+ Enabled: false
31
+ Style/AccessorMethodName:
32
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - 1.9.2
7
+ - ruby-head
8
+
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ Author:: Pavel Yudin (<pyudin@parallels.com>)
2
+
3
+ Copyright (C) 2016, Pavel Yudin
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # Kitchen::Vz
2
+
3
+ A Virtuozzo driver for Test Kitchen
4
+
5
+ ## Requirements
6
+
7
+ This driver works via ssh or directly executes command. So you need to have ssh access via keys on the server, where container will be started. Also you need to have sudo permissions for your user to run `prlctl` and `vzctl` commands.
8
+
9
+ This driver tested only on Virtuozzo 7.
10
+
11
+ ## Installation and Setup
12
+
13
+ Please read the Test Kitchen [docs](http://kitchen.ci/docs/getting-started/) for more details.
14
+
15
+ Example `.kitchen.local.yml`:
16
+
17
+ ```yaml
18
+ ---
19
+ driver:
20
+ name: vz
21
+
22
+ platforms:
23
+ - name: centos-7
24
+ driver:
25
+ socket: ssh://user@virtuozzo.domain.loc
26
+ suites:
27
+ - name: default
28
+ ```
29
+
30
+ ## Configuration
31
+
32
+ |Attribute|Description|Default value|
33
+ |---------|-----------|-------------|
34
+ |:socket|Connection string to virtuozzo server. Supports ssh uri or `local`. `local` means what `prlctl` and `vzctl` will be started locally.|'local'|
35
+ |:username|User with this name will be created in virtuozzo container. Test kitchen uses this user to connect to the container.|'kitchen'|
36
+ |:arch|Container architecture. This attribute shows how architecture container will be created.|'x86_64'|
37
+ |:network|Hash with network configuration. See section Network configuration.|'Bridged' => {dhcp: true}|
38
+ |:customize|Hash with container settings. It may contain :memory, :disk and :cpus options.|memory: '512M', disk: '10G', cpus: 2|
39
+ |:private_key|Path to private key. This key pair used by the kitchen to login into container.|.kitchen/kitchen_id_rsa|
40
+ |:public_key|Path to public key. This key pair used by the kitchen to login into container.|.kitchen/kitchen_id_rsa.pub|
41
+ |:ostemplate|Virtuozzo template which will be used for container creating.||
42
+ |:use_sudo|It shows will sudo be used or not.|true|
43
+ |:ct_hostname|Container hostname.|It is formed from platform name and suite name.|
44
+
45
+ ### Network configuration
46
+
47
+ Example of network settings:
48
+
49
+ ```ruby
50
+ {
51
+ 'Bridged' => {
52
+ dhcp: true
53
+ },
54
+ 'Host-Only' => {
55
+ ip: '192.168.75.1/24'
56
+ },
57
+ 'PUB' => {
58
+ ip: '10.10.10.20/24'
59
+ gw: '10.10.10.1'
60
+ }
61
+ }
62
+ ```
63
+
64
+ The key of hash is a name of Virtuozzo network. Container interface will be bridged with this network. Value is a hash, which may contains next keys:
65
+ * dhcp - If this parameter is true, then network settings will be get from dhcp server.
66
+ * ip - IP-address which will be set to container. The address must be specified with the mask. Ex: 192.168.5.3/24
67
+ * gw - Gateway which will be set to container.
68
+
69
+ ## Development
70
+
71
+ * Source hosted at [GitHub][repo]
72
+ * Report issues/questions/feature requests on [GitHub Issues][issues]
73
+
74
+ Pull requests are very welcome! Make sure your patches are well tested.
75
+ Ideally create a topic branch for every separate change you make. For
76
+ example:
77
+
78
+ 1. Fork the repo
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
83
+
84
+ ## Authors
85
+
86
+ Created and maintained by [Pavel Yudin][author] (<pyudin@parallels.com>)
87
+
88
+ ## License
89
+
90
+ Apache 2.0 (see [LICENSE][license])
91
+
92
+
93
+ [author]: https://github.com/Kasen
94
+ [issues]: https://github.com/Kasen/kitchen-vz/issues
95
+ [license]: https://github.com/Kasen/kitchen-vz/blob/master/LICENSE
96
+ [repo]: https://github.com/Kasen/kitchen-vz
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+
4
+ desc 'Run Ruby style checks'
5
+ RuboCop::RakeTask.new(:rubocop)
6
+
7
+ desc 'Run all quality tasks'
8
+ task quality: [:rubocop]
9
+
10
+ task default: [:quality]
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kitchen/driver/vz_version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'kitchen-vz'
8
+ spec.version = Kitchen::Driver::VZ_VERSION
9
+ spec.authors = ['Pavel Yudin']
10
+ spec.email = ['pyudin@parallels.com']
11
+ spec.description = 'A Virtuozzo driver for Test Kitchen'
12
+ spec.summary = spec.description
13
+ spec.homepage = 'https://github.com/Kasen/kitchen-vz'
14
+ spec.license = 'Apache 2.0'
15
+
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
+ spec.executables = []
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'test-kitchen', '~> 1.5'
22
+ spec.add_dependency 'net-ssh', '~> 3.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+
27
+ spec.add_development_dependency 'rubocop'
28
+ end
@@ -0,0 +1,205 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Pavel Yudin (<pyudin@parallels.com>)
4
+ #
5
+ # Copyright (C) 2016, Pavel Yudin
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 'securerandom'
21
+ require 'uri'
22
+ require 'net/ssh'
23
+
24
+ module Kitchen
25
+ module Driver
26
+
27
+ # Virtuozzo driver for Kitchen.
28
+ #
29
+ # @author Pavel Yudin <pyudin@parallels.com>
30
+ class Vz < Kitchen::Driver::SSHBase
31
+
32
+ default_config :socket, 'local'
33
+ default_config :username, 'kitchen'
34
+ default_config :private_key, File.join(Dir.pwd, '.kitchen', 'kitchen_id_rsa')
35
+ default_config :public_key, File.join(Dir.pwd, '.kitchen', 'kitchen_id_rsa.pub')
36
+ default_config :network, 'Bridged' => { dhcp: true }
37
+ default_config :use_sudo, true
38
+ default_config :arch, 'x86_64'
39
+ default_config :customize, memory: '512M', disk: '10G', cpus: 2
40
+ default_config :ostemplate, nil
41
+ default_config :ct_hostname do |driver|
42
+ driver.instance.name
43
+ end
44
+
45
+ def create(state)
46
+ state[:ct_id] = SecureRandom.uuid
47
+ generate_keys
48
+ state[:ssh_key] = config[:ssh_key] = config[:private_key]
49
+ create_ct(state)
50
+ set_ct_network(state)
51
+ set_ct_cpu(state)
52
+ set_ct_mem(state)
53
+ set_ct_disk(state)
54
+ run_ct(state)
55
+ create_user(state)
56
+ state[:hostname] = ct_ip(state)
57
+ wait_for_sshd(state[:hostname], nil, keys: [state[:ssh_key]])
58
+ end
59
+
60
+ def destroy(state)
61
+ execute_command("#{prlctl} stop #{state[:ct_id]}") if state[:ct_id]
62
+ execute_command("#{prlctl} destroy #{state[:ct_id]}") if state[:ct_id]
63
+ end
64
+
65
+ private
66
+
67
+ def prlctl
68
+ '/usr/bin/prlctl'
69
+ end
70
+
71
+ def vzctl
72
+ '/usr/sbin/vzctl'
73
+ end
74
+
75
+ def generate_keys
76
+ if !File.exist?(config[:public_key]) || !File.exist?(config[:private_key])
77
+ private_key = OpenSSL::PKey::RSA.new(2048)
78
+ blobbed_key = Base64.encode64(private_key.to_blob)
79
+ public_key = "ssh-rsa #{blobbed_key} kitchen_key"
80
+ File.open(config[:private_key], 'w') do |file|
81
+ file.write(private_key)
82
+ file.chmod(0600)
83
+ end
84
+ File.open(config[:public_key], 'w') do |file|
85
+ file.write(public_key)
86
+ file.chmod(0600)
87
+ end
88
+ end
89
+ end
90
+
91
+ def create_ct(state)
92
+ command_line = "#{vzctl} create #{state[:ct_id]} --hostname #{config[:ct_hostname]} --ostemplate "
93
+ command_line += config[:ostemplate] || "#{platform_major}-#{config[:arch]}"
94
+ execute_command(command_line)
95
+ end
96
+
97
+ def set_ct_network(state)
98
+ iface_number = 0
99
+ config[:network].each do |network, _settings|
100
+ execute_command("#{vzctl} set #{state[:ct_id]} --netif_add eth#{iface_number} --save")
101
+ command_line = "#{vzctl} set #{state[:ct_id]} --network #{network} --ifname eth#{iface_number} "
102
+ command_line += '--dhcp yes ' if config[:network][network][:dhcp]
103
+ command_line += "--ipadd #{config[:network][network][:ip]} " if config[:network][network][:ip]
104
+ command_line += "--gw #{config[:network][network][:gw]} " if config[:network][network][:gw]
105
+ command_line += '--save'
106
+ execute_command(command_line)
107
+ iface_number += 1
108
+ end
109
+ execute_command("#{prlctl} set #{state[:ct_id]} --netfilter full")
110
+ end
111
+
112
+ def set_ct_cpu(state)
113
+ execute_command("#{prlctl} set #{state[:ct_id]} --cpus #{config[:customize][:cpus]}")
114
+ end
115
+
116
+ def set_ct_mem(state)
117
+ execute_command("#{prlctl} set #{state[:ct_id]} --memsize #{config[:customize][:memory]}")
118
+ end
119
+
120
+ def set_ct_disk(state)
121
+ ds = config[:customize][:disk]
122
+ execute_command("#{vzctl} set #{state[:ct_id]} --diskspace #{ds}:#{ds} --save")
123
+ end
124
+
125
+ def run_ct(state)
126
+ execute_command("#{prlctl} start #{state[:ct_id]}")
127
+ end
128
+
129
+ def create_user(state)
130
+ ["useradd #{config[:username]}",
131
+ "mkdir /home/#{config[:username]}/.ssh",
132
+ "chown #{config[:username]}: /home/#{config[:username]}/.ssh",
133
+ "chmod 700 /home/#{config[:username]}/.ssh",
134
+ "echo '#{File.open(config[:public_key]).read}' > /home/#{config[:username]}/.ssh/authorized_keys",
135
+ "chown #{config[:username]}: /home/#{config[:username]}/.ssh/authorized_keys",
136
+ "chmod 600 /home/#{config[:username]}/.ssh/authorized_keys",
137
+ 'mkdir -p /etc/sudoers.d',
138
+ "echo '#{config[:username]} ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers.d/#{config[:username]}",
139
+ "chmod 0440 /etc/sudoers.d/#{config[:username]}"].each do |command|
140
+ execute_command("#{prlctl} exec #{state[:ct_id]} \"#{command}\"")
141
+ end
142
+ end
143
+
144
+ def ct_ip(state)
145
+ ip = nil
146
+ 1..10.times do
147
+ output = execute_command("#{vzctl} exec #{state[:ct_id]} \"/sbin/ip -o -f inet addr show dev eth0\"")
148
+ result = %r{(([0-9]{1,3}\.){3}[0-9]{1,3})\/[0-9]{1,2}}.match(output)
149
+ ip = result[1] if result
150
+ break if ip
151
+ sleep(1)
152
+ end
153
+ raise "Can't detect an IP!" if !ip
154
+ ip
155
+ end
156
+
157
+ def platform_major
158
+ instance.platform.name.split('.')[0]
159
+ end
160
+
161
+ def execute_command(command)
162
+ if config[:socket] == 'local'
163
+ run_command(command)
164
+ else
165
+ command = 'sudo -E ' + command if config[:use_sudo]
166
+ channel = ssh_connection.open_channel do |ch|
167
+ ch.exec(command) do |_ch, _success|
168
+ channel[:data] = ''
169
+ channel[:ext_data] = ''
170
+
171
+ channel.on_data do |_ch, data|
172
+ channel[:data] << data
173
+ end
174
+
175
+ channel.on_extended_data do |_ch, _type, data|
176
+ channel[:ext_data] << data
177
+ end
178
+
179
+ channel.on_request 'exit-status' do |_ch, data|
180
+ if data.read_long.to_i != 0
181
+ raise "SSH command failed with: #{channel[:ext_data]}"
182
+ else
183
+ puts channel[:data] unless channel[:data].empty?
184
+ end
185
+ end
186
+ end
187
+ ch.wait
188
+ end
189
+ channel.wait
190
+ channel[:data]
191
+ end
192
+ end
193
+
194
+ def uri
195
+ uri = URI(config[:socket])
196
+ raise "Invalid URI scheme: #{uri.scheme}. Only 'ssh' is supported." if uri.scheme != 'ssh'
197
+ uri
198
+ end
199
+
200
+ def ssh_connection
201
+ @connection ||= Net::SSH.start(uri.host, uri.user, port: uri.port)
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Pavel Yudin (<pyudin@parallels.com>)
4
+ #
5
+ # Copyright (C) 2016, Pavel Yudin
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
+ module Kitchen
20
+ module Driver
21
+ # Version string for Vz Kitchen driver
22
+ VZ_VERSION = '0.1.1'.freeze
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kitchen-vz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Yudin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: test-kitchen
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-ssh
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A Virtuozzo driver for Test Kitchen
84
+ email:
85
+ - pyudin@parallels.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".kitchen.yml"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - CHANGELOG.md
95
+ - Gemfile
96
+ - LICENSE
97
+ - README.md
98
+ - Rakefile
99
+ - kitchen-vz.gemspec
100
+ - lib/kitchen/driver/vz.rb
101
+ - lib/kitchen/driver/vz_version.rb
102
+ homepage: https://github.com/Kasen/kitchen-vz
103
+ licenses:
104
+ - Apache 2.0
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.14
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A Virtuozzo driver for Test Kitchen
126
+ test_files: []
127
+ has_rdoc: