chef-lxc 0.1.3 → 0.3

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: 82d501f875d1abf562a893e4f3b4534fc612e304
4
- data.tar.gz: 8d8d6bb519022d075cd5fcc0cfcd2c0930539ae0
3
+ metadata.gz: 024d4681812669a4f30454c5ab95e29f02494212
4
+ data.tar.gz: cdc9eeb06b91a6c575962994e75d145a38dbb4f8
5
5
  SHA512:
6
- metadata.gz: 94b0b095341452c57c043b25e8c638805dc95696b4b3f6a5e04e870d81354c361f1a2f6bd9c9f4b2251a449152d641caa2c8cb019872df936ec5647847fc7b26
7
- data.tar.gz: 0bd86128c42f08920d6b8c01fd97a41a3b71fd18c89b17bffe8a6dcc479005820cbd1e7df10efc9c49fe58fad2ac4d2dbfb68b3463fb4882c1ac35097c3c9112
6
+ metadata.gz: d5858865ababef7d065a64a23d6c94c98e15e81e6ee29633e1b9a02672188cd1bbe6c7a1d3e2ff89d68a4afdbe8dafc93d57a189bbd0befeb92ffb03e5ce161e
7
+ data.tar.gz: c096e26912c6ae3425e4eb5449204144b53e6eae6733efcb6c8e058065a2c37d8ae4f72b4a4eecfb280bad3f6dcea4d7649740902dc6597aba73a651b6826da2
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ gem 'chef', github: 'chef/chef'
data/Rakefile CHANGED
@@ -7,3 +7,4 @@ RSpec::Core::RakeTask.new(:spec) do |t|
7
7
  a.push('--format progress')
8
8
  end.join(' ')
9
9
  end
10
+ task :default => :spec
data/chef-lxc.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
  spec.bindir = "bin"
21
21
 
22
- spec.add_dependency "chef", ">= 11"
22
+ spec.add_dependency "chef", ">= 12"
23
23
  spec.add_dependency "ruby-lxc"
24
24
  spec.add_dependency "lxc-extra"
25
25
  spec.homepage = 'https://github.com/ranjib/chef-lxc'
@@ -33,7 +33,7 @@ class Chef
33
33
  def upload_data_bag(name, path, opts = {})
34
34
  items = Dir["#{path}/*"]
35
35
  name_args = [name, items].flatten
36
- plugin = knife(Chef::Knife::DataBagFromFile, *name_args)do |config|
36
+ plugin = knife(Chef::Knife::DataBagFromFile, *name_args) do |config|
37
37
  if opts[:encrypted]
38
38
  config[:secret_file] = opts[:secret_file]
39
39
  config[:encrypt] = true
@@ -42,6 +42,35 @@ class Chef
42
42
  end
43
43
  end
44
44
 
45
+ def load_secret(secret_file = nil)
46
+ config = chef_config
47
+ Chef::EncryptedDataBagItem.load_secret(
48
+ secret_file || config[:knife][:secret_file]
49
+ )
50
+ end
51
+
52
+ def update_data_bag_item(data_bag_name, item_name, update_hash, opts = {})
53
+ hash = Chef::DataBagItem.load(data_bag_name, item_name)
54
+ if opts[:encrypted]
55
+ secret = load_secret(opts[:secret_file])
56
+ hash = Chef::EncryptedDataBagItem.load(data_bag_name, item_name, secret).to_hash
57
+ end
58
+ updated_hash = hash.merge(update_hash)
59
+ upload_data_bag_item_from_hash(data_bag_name, updated_hash, opts)
60
+ end
61
+
62
+ # hash must contain an entry of the form 'id' => item_name
63
+ def upload_data_bag_item_from_hash(data_bag_name, hash, opts = {})
64
+ config = chef_config
65
+ if opts[:encrypted]
66
+ secret = load_secret(opts[:secret_file])
67
+ hash = Chef::EncryptedDataBagItem.encrypt_data_bag_item(hash, secret)
68
+ end
69
+ item = Chef::DataBagItem.from_hash(hash)
70
+ item.data_bag(data_bag_name)
71
+ item.save
72
+ end
73
+
45
74
  def create_environment(name, opts ={})
46
75
  e = Chef::Environment.new
47
76
  e.name(name)
@@ -1,5 +1,5 @@
1
1
  class Chef
2
2
  module LXC
3
- VERSION = '0.1.3'
3
+ VERSION = '0.3'
4
4
  end
5
5
  end
@@ -1,6 +1,7 @@
1
1
  require 'lxc'
2
2
  require 'lxc/extra'
3
3
  require 'mixlib/shellout'
4
+ require 'chef/resource_resolver'
4
5
 
5
6
  class Chef
6
7
  module LXCHelper
@@ -2,46 +2,47 @@ require 'chef/lxc'
2
2
  require 'chef_zero/server'
3
3
  require 'tempfile'
4
4
 
5
+ require 'spec_helper'
6
+
5
7
  cookbook_path = File.expand_path('../../../data/cookbooks', __FILE__)
6
- server = ChefZero::Server.new(host: '10.0.3.1', port: 8889)
7
- server.start_background unless server.running?
8
- tempfile = Tempfile.new('chef-lxc')
9
- File.open(tempfile.path, 'w') do |f|
10
- f.write(server.gen_key_pair.first)
11
- end
12
8
 
13
- Chef::LXC.create_fleet('memcache') do |fleet|
14
- # Create base container with chef installed in it
15
- fleet.create_container('base') do |ct|
16
- ct.recipe do
17
- execute 'apt-get update -y'
18
- remote_file '/opt/chef_12.2.1-1_amd64.deb' do
19
- source 'http://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/13.04/x86_64/chef_12.2.1-1_amd64.deb'
20
- end
21
- dpkg_package 'chef' do
22
- source '/opt/chef_12.2.1-1_amd64.deb'
23
- end
24
- directory '/etc/chef'
25
- file '/etc/chef/client.pem' do
26
- content ChefZero::Server.new.gen_key_pair.first
27
- end
28
- file '/etc/chef/client.rb' do
29
- content "chef_server_url 'http://10.0.3.1:8889'\n"
9
+ describe(:fleet_dsl) do
10
+ let(:server) { ChefSpecHelper.new_server }
11
+ let(:fleet) { ChefSpecHelper.create_fleet(server) }
12
+
13
+ after(:each) do
14
+ server.stop if server.running?
15
+ end
16
+
17
+ it 'succeeds' do
18
+ # Upload cookbooks, data bags, create roles
19
+ fleet.upload_cookbooks(cookbook_path)
20
+ fleet.create_role('memcached', 'recipe[memcached]')
21
+
22
+ # Create base container with chef installed in it
23
+ fleet.create_container('base') do |ct|
24
+ ct.recipe do
25
+ execute 'apt-get update -y'
26
+ remote_file '/opt/chef_12.2.1-1_amd64.deb' do
27
+ source 'http://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/13.04/x86_64/chef_12.2.1-1_amd64.deb'
28
+ end
29
+ dpkg_package 'chef' do
30
+ source '/opt/chef_12.2.1-1_amd64.deb'
31
+ end
32
+ directory '/etc/chef'
33
+ file '/etc/chef/client.pem' do
34
+ content ChefZero::Server.new.gen_key_pair.first
35
+ end
36
+ file '/etc/chef/client.rb' do
37
+ content "chef_server_url 'http://10.0.3.1:8889'\n"
38
+ end
30
39
  end
40
+ ct.stop
41
+ end
42
+
43
+ fleet.create_container('memcached', from: 'base') do |ct|
44
+ ct.command!('chef-client -r role[memcached]')
45
+ ct.stop
31
46
  end
32
- end
33
-
34
- # configure chef setting for the new chef server
35
- fleet.chef_config do |config|
36
- config[:client_key] = tempfile.path
37
- config[:node_name] = 'test'
38
- config[:chef_server_url] = 'http://10.0.3.1:8889'
39
- end
40
-
41
- # Upload cookbooks, data bags, create roles
42
- fleet.upload_cookbooks(cookbook_path)
43
- fleet.create_role('memcached', 'recipe[memcached]')
44
- fleet.create_container('memcached', from: 'base') do |ct|
45
- ct.command('chef-client -r role[memcached]')
46
47
  end
47
48
  end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chef::LXC::KnifeHelper do
4
+ before(:each) do
5
+ fleet.chef_config({encrypted_data_bag_secret: 'secret'})
6
+
7
+ allow(File).to receive(:exist?).and_call_original
8
+ allow(File).to receive(:exist?).with('secret').and_return(true)
9
+ allow(IO).to receive(:read).and_call_original
10
+ allow(IO).to receive(:read).with('secret').and_return('secret_val')
11
+ end
12
+
13
+ after(:each) do
14
+ server.stop if server.running?
15
+ end
16
+
17
+ def create_data_bag(data_bag_name, hash, opts = {})
18
+ fleet.create_data_bag(data_bag_name)
19
+ fleet.upload_data_bag_item_from_hash(data_bag_name, hash, opts)
20
+ end
21
+
22
+ let(:server) { ChefSpecHelper.new_server }
23
+ let(:fleet) { ChefSpecHelper.create_fleet(server) }
24
+ let(:data_bag_name) { :data_bag_name }
25
+ let(:item_name) { :item_name }
26
+
27
+ context('#update_data_bag_item') do
28
+ it 'adds new keys to data bag item (encrypted)' do
29
+ opts = {encrypted: true}
30
+ create_data_bag(data_bag_name, {'id' => item_name}, opts)
31
+
32
+ fleet.update_data_bag_item(data_bag_name, item_name, {a: :b}, opts)
33
+
34
+ secrets = Chef::EncryptedDataBagItem.load(data_bag_name, item_name)
35
+ expect(secrets['a']).to eq('b')
36
+ end
37
+
38
+ it 'overwrites old keys in data bag item (encrypted)' do
39
+ opts = {encrypted: true}
40
+ existing_data = {'id' => item_name, a: :c}
41
+ create_data_bag(data_bag_name, existing_data, opts)
42
+
43
+ fleet.update_data_bag_item(data_bag_name, item_name, {a: :b}, opts)
44
+
45
+ secrets = Chef::EncryptedDataBagItem.load(data_bag_name, item_name)
46
+ expect(secrets['a']).to eq('b')
47
+ end
48
+
49
+ it 'adds new keys to data bag item (unencrypted)' do
50
+ opts = {}
51
+ create_data_bag(data_bag_name, {'id' => item_name}, opts)
52
+
53
+ fleet.update_data_bag_item(data_bag_name, item_name, {a: :b}, opts)
54
+
55
+ secrets = Chef::DataBagItem.load(data_bag_name, item_name)
56
+ expect(secrets['a']).to eq('b')
57
+ end
58
+
59
+ it 'overwrites old keys in data bag item (unencrypted)' do
60
+ opts = {}
61
+ existing_data = {'id' => item_name, a: :c}
62
+ create_data_bag(data_bag_name, existing_data, opts)
63
+
64
+ fleet.update_data_bag_item(data_bag_name, item_name, {a: :b}, opts)
65
+
66
+ secrets = Chef::DataBagItem.load(data_bag_name, item_name)
67
+ expect(secrets['a']).to eq('b')
68
+ end
69
+ end
70
+ end
data/spec/spec_helper.rb CHANGED
@@ -19,6 +19,28 @@ module LXCSpecHelper
19
19
  end
20
20
  end
21
21
 
22
+ module ChefSpecHelper
23
+ def self.new_server
24
+ ChefZero::Server.new(host: '10.0.3.1', port: 8889)
25
+ end
26
+
27
+ def self.create_fleet(server)
28
+ server.start_background unless server.running?
29
+ tempfile = Tempfile.new('chef-lxc')
30
+ File.open(tempfile.path, 'w') do |f|
31
+ f.write(server.gen_key_pair.first)
32
+ end
33
+
34
+ Chef::LXC.create_fleet('chef-helper') do |fleet|
35
+ fleet.chef_config do |config|
36
+ config[:client_key] = tempfile.path
37
+ config[:node_name] = 'test'
38
+ config[:chef_server_url] = server.url
39
+ end
40
+ end
41
+ end
42
+ end
43
+
22
44
  RSpec.configure do |config|
23
45
  config.expect_with(:rspec) { |c| c.syntax = :expect }
24
46
  config.filter_run(focus: true)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-lxc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ranjib Dey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-16 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '11'
19
+ version: '12'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '11'
26
+ version: '12'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ruby-lxc
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -148,6 +148,7 @@ files:
148
148
  - lib/chef/resource/lxc.rb
149
149
  - spec/chef/lxc/fleet_dsl.rb
150
150
  - spec/chef/lxc/fleet_spec.rb
151
+ - spec/chef/lxc/knife_helper_spec.rb
151
152
  - spec/chef/recipe_spec.rb
152
153
  - spec/chef/simple_spec.rb
153
154
  - spec/chef/template_spec.rb
@@ -178,13 +179,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
179
  version: '0'
179
180
  requirements: []
180
181
  rubyforge_project:
181
- rubygems_version: 2.2.2
182
+ rubygems_version: 2.4.5
182
183
  signing_key:
183
184
  specification_version: 4
184
185
  summary: LXC bindings for Chef
185
186
  test_files:
186
187
  - spec/chef/lxc/fleet_dsl.rb
187
188
  - spec/chef/lxc/fleet_spec.rb
189
+ - spec/chef/lxc/knife_helper_spec.rb
188
190
  - spec/chef/recipe_spec.rb
189
191
  - spec/chef/simple_spec.rb
190
192
  - spec/chef/template_spec.rb