vagrant-butcher 1.0.1 → 1.1.0

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Nzg1MWJkZjQ2ZGY4MGRlY2I2NmQyZDJlZmVlM2VmNmYyYTY0ZmE5NQ==
5
+ data.tar.gz: !binary |-
6
+ Nzc3OTE1ZWQyMmVlYzZiZTQxNmYzNzU4YTM3MGMyODk2YmYyMzM2OQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YWViMWQ3YTM5NjE1MDBhYzhiMmJmNGNlMDg5ZWIzYzFmMmY4NDlmNmY2Nzdm
10
+ YWVmNzA0ODM0ZjA2NDYwMDlkNWZlMDBjYmZjMWI2OGEyODMxMGRkOWE5NWM1
11
+ ZGFkZTU5ZGEyOTVlNWYzYTlhM2M4M2NkNzgxZGU4ZTJmMTU5MTE=
12
+ data.tar.gz: !binary |-
13
+ YjE1Njk5MjZhMmFiMmY1ZWI2MDQ2MzlkMDBkZDRhNDU1OWIxYmJjYzhmNzMy
14
+ ZGY4YTcwZmViMjJiZWU2MmZiYzIyNjlmYjI2ZTBhMmEyZGZkMjQzMWEwM2Yz
15
+ NzE3NTNkMDkyMTMxYjllNmYzMTg5NDY3MjYyNzZhNWIxYzAzY2E=
data/README.md CHANGED
@@ -8,19 +8,6 @@ If you're using Vagrant with the Chef-Client provisioner it creates a client and
8
8
 
9
9
  This plugin will automatically get rid of that cruft for you when you destroy the VM.
10
10
 
11
- ## Changelog
12
-
13
- ### 1.0.0
14
-
15
- * Support for Vagrant 1.x (it's been tested on 1.4, but should work on previous minor releases) -- if you're using a pre-1.0 Vagrant release, stick to vagrant-butcher 0.0.3.
16
- * [Configuration](#usage) change.
17
- * [Installation](#install) via `vagrant plugin` only.
18
- * Provider-independent. _[Read more](#caveats)_
19
-
20
- ### 0.0.3
21
-
22
- * Uses chef.node_name if set. Otherwise, fall back to vm.host_name (as before), or vm.box. -- _Kudos to [pikesley](https://github.com/pikesley)_.
23
-
24
11
  ## <a id="install"></a>Installation
25
12
 
26
13
  Starting with version 1.0.0, installation is made via Vagrant plugins only:
@@ -33,12 +20,14 @@ The plugin is loaded automatically once installed.
33
20
 
34
21
  By default, the gem looks for the Chef server settings on `$HOME/.chef/knife.rb`. This can be overridden by setting:
35
22
 
36
- Vagrant.configure("2") do |config|
37
- config.butcher.knife_config_file = '/path/to/knife.rb'
38
- config.vm.provision :chef_client do |chef|
39
- # Chef Client provisioner configuration
40
- end
41
- end
23
+ ```ruby
24
+ Vagrant.configure("2") do |config|
25
+ config.butcher.knife_config_file = '/path/to/knife.rb'
26
+ config.vm.provision :chef_client do |chef|
27
+ # Chef Client provisioner configuration
28
+ end
29
+ end
30
+ ```
42
31
 
43
32
  _Note that beginning with 1.0, the configuration is done outside of the `chef_client` provisioner._
44
33
 
@@ -51,11 +40,40 @@ This is the output of the plugin when it runs successfully:
51
40
  [default] Forcing shutdown of VM...
52
41
  [default] Destroying VM and associated drives...
53
42
 
43
+ ### <a id='auto_knife'></a>Auto Knife
44
+
45
+ Starting with version 1.1.0 you can set the knife_config_file to `:auto` if you'd like it to automatically create a knife.rb for that instance from the chef provisioner values.
46
+
47
+ This option has the following two limitations:
48
+ - It doesn't work with windows guests.
49
+ - The default `.` -> `/vagrant` shared folder should be mounted.
50
+
54
51
  ## <a id='caveats'></a>Caveats
55
52
 
56
53
  * Version 1.0 has only been tested with Vagrant 1.1+. If you're using an older version, it's probably best to stick to 0.0.3
57
54
  * So far this has only been tested and confirmed to run with the VirtualBox and Rackspace provisioners. It should work with others, but if you run into issues please file a bug.
58
55
 
56
+ ## Changelog
57
+
58
+ ### 1.1.0
59
+
60
+ * :auto was added as an optional value for knife_config_file
61
+
62
+ ### 1.0.1
63
+
64
+ * Support for Vagrant 1.2
65
+
66
+ ### 1.0.0
67
+
68
+ * Support for Vagrant 1.x (it's been tested on 1.1.4, but should work on previous minor releases) -- if you're using a pre-1.0 Vagrant release, stick to vagrant-butcher 0.0.3.
69
+ * [Configuration](#usage) change.
70
+ * [Installation](#install) via `vagrant plugin` only.
71
+ * Provider-independent. _[Read more](#caveats)_
72
+
73
+ ### 0.0.3
74
+
75
+ * Uses chef.node_name if set. Otherwise, fall back to vm.host_name (as before), or vm.box. -- _Kudos to [pikesley](https://github.com/pikesley)_.
76
+
59
77
  ## Contributing
60
78
 
61
79
  1. Fork it
@@ -2,6 +2,7 @@ module Vagrant
2
2
  module Butcher
3
3
  module Action
4
4
  autoload :Cleanup, 'vagrant-butcher/action/cleanup'
5
+ autoload :AutoKnife, 'vagrant-butcher/action/auto_knife'
5
6
 
6
7
  def self.cleanup
7
8
  ::Vagrant::Action::Builder.new.tap do |b|
@@ -10,6 +11,13 @@ module Vagrant
10
11
  end
11
12
  end
12
13
 
14
+ def self.auto_knife
15
+ ::Vagrant::Action::Builder.new.tap do |b|
16
+ b.use setup
17
+ b.use Vagrant::Butcher::Action::AutoKnife
18
+ end
19
+ end
20
+
13
21
  def self.setup
14
22
  @setup ||= ::Vagrant::Action::Builder.new.tap do |b|
15
23
  b.use ::Vagrant::Action::Builtin::EnvSet, butcher: Vagrant::Butcher::Env.new
@@ -0,0 +1,66 @@
1
+ require 'chef/config'
2
+ require 'chef/rest'
3
+ require 'chef/api_client'
4
+
5
+ module Vagrant
6
+ module Butcher
7
+ module Action
8
+ class AutoKnife
9
+ include ::Vagrant::Butcher::EnvHelpers
10
+
11
+ def initialize(app, env)
12
+ @app = app
13
+ end
14
+
15
+ def victim(env)
16
+ @victim ||= chef_provisioner(env).node_name || vm_config(env).hostname || vm_config(env).box
17
+ end
18
+
19
+ def auto_create_knife(env)
20
+ folders = vm_config(env).synced_folders
21
+ if !folders.has_key?("/vagrant") || folders["/vagrant"][:disabled] == TRUE
22
+ env[:butcher].ui.warn "/vagrant folder not set to be mounted."
23
+ end
24
+
25
+ unless File.exists?(cache_dir(env))
26
+ env[:butcher].ui.info "Creating #{cache_dir(env)}"
27
+ Dir.mkdir(cache_dir(env))
28
+ end
29
+
30
+ begin
31
+ env[:machine].communicate.execute "cp #{guest_key_path(env)} #{auto_knife_guest_key_path(env)}", :sudo => true
32
+ rescue Exception => e
33
+ env[:butcher].ui.error "Failed to copy #{guest_key_path(env)} to #{auto_knife_key_path(env)}"
34
+ env[:butcher].ui.error e
35
+ return false
36
+ end
37
+
38
+ env[:butcher].ui.info "Copied #{guest_key_path(env)} to #{auto_knife_key_path(env)}"
39
+
40
+ env[:butcher].ui.info "Creating #{auto_knife_config_file(env)}"
41
+
42
+ knife_rb = <<-END.gsub(/^ */, '')
43
+ log_level :info
44
+ log_location STDOUT
45
+ client_key '#{auto_knife_key_path(env)}'
46
+ node_name '#{victim(env)}'
47
+ END
48
+
49
+ File.new(auto_knife_config_file(env), 'w+').write(knife_rb)
50
+
51
+ return true
52
+ end
53
+
54
+ def call(env)
55
+ if chef_client?(env) && auto_knife?(env) && !File.exists?(auto_knife_config_file(env))
56
+ unless auto_create_knife(env)
57
+ env[:butcher].ui.error "Failed to auto create knife.rb."
58
+ end
59
+ end
60
+
61
+ @app.call(env)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -10,10 +10,7 @@ module Vagrant
10
10
 
11
11
  def initialize(app, env)
12
12
  @app = app
13
- end
14
-
15
- def victim(env)
16
- @victim ||= chef_provisioner(env).node_name || vm_config(env).hostname || vm_config(env).box
13
+ @delete_all_success = true
17
14
  end
18
15
 
19
16
  def delete_resource(resource, env)
@@ -22,11 +19,25 @@ module Vagrant
22
19
  env[:butcher].ui.success "Chef #{resource} '#{victim(env)}' successfully butchered from the server..."
23
20
  rescue Exception => e
24
21
  env[:butcher].ui.warn "Could not remove #{resource} #{victim(env)}: #{e.message}"
22
+ @delete_all_success = false
23
+ end
24
+ end
25
+
26
+ def delete_auto_knife(env)
27
+ env[:butcher].ui.info 'hwsee'
28
+ if @delete_all_success
29
+ File.delete(auto_knife_config_file(env))
30
+ File.delete(auto_knife_key_path(env))
31
+ begin
32
+ Dir.delete(cache_dir(env))
33
+ rescue
34
+ # The dir wasn't empty.
35
+ end
25
36
  end
26
37
  end
27
38
 
28
39
  def call(env)
29
- if chef_client?(env)
40
+ if chef_client?(env) && knife_config_file(env)
30
41
  begin
31
42
  ::Chef::Config.from_file knife_config_file(env)
32
43
  rescue Errno::ENOENT => e
@@ -34,6 +45,10 @@ module Vagrant
34
45
  end
35
46
 
36
47
  %w(node client).each { |resource| delete_resource(resource, env) }
48
+
49
+ if auto_knife?(env)
50
+ delete_auto_knife(env)
51
+ end
37
52
  end
38
53
 
39
54
  @app.call(env)
@@ -1,25 +1,45 @@
1
1
  module Vagrant
2
2
  module Butcher
3
3
  class Config < ::Vagrant.plugin('2', :config)
4
+ attr_accessor :guest_key_path
5
+ attr_accessor :cache_dir
6
+ attr_accessor :guest_cache_dir
4
7
  attr_accessor :knife_config_file
5
8
 
6
9
  def initialize
7
10
  super
11
+ @guest_key_path = UNSET_VALUE
12
+ @cache_dir = UNSET_VALUE
13
+ @guest_cache_dir = UNSET_VALUE
8
14
  @knife_config_file = UNSET_VALUE
9
15
  end
10
-
16
+
17
+ def cache_dir=(value)
18
+ @cache_dir = File.expand_path(value)
19
+ end
20
+
11
21
  def knife_config_file=(value)
12
- @knife_config_file = File.expand_path(value)
22
+ unless value == :auto
23
+ value = File.expand_path(value)
24
+ end
25
+
26
+ @knife_config_file = value
13
27
  end
14
28
 
15
29
  def validate(machine)
16
30
  errors = []
17
- errors << "Knife configuration not found at #{@knife_config_file}." if !File.exists?(@knife_config_file)
31
+
32
+ if @knife_config_file != :auto && !File.exists?(@knife_config_file)
33
+ errors << "Knife configuration not found at #{@knife_config_file}."
34
+ end
18
35
 
19
36
  { "butcher configuration" => errors }
20
37
  end
21
38
 
22
39
  def finalize!
40
+ @guest_key_path = '/etc/chef/client.pem' if @guest_key_path == UNSET_VALUE
41
+ @cache_dir = File.expand_path ".vagrant-butcher" if @cache_dir == UNSET_VALUE
42
+ @guest_cache_dir = "/vagrant/" + File.basename(@cache_dir) if @guest_cache_dir == UNSET_VALUE
23
43
  @knife_config_file = File.expand_path "#{ENV['HOME']}/.chef/knife.rb" if @knife_config_file == UNSET_VALUE
24
44
  end
25
45
  end
@@ -19,12 +19,92 @@ module Vagrant
19
19
  vm_config(env).provisioners.select { |p| p.name == :chef_client }.any?
20
20
  end
21
21
 
22
+ def auto_knife?(env)
23
+ butcher_config(env).knife_config_file == :auto
24
+ end
25
+
26
+ def butcher_config(env)
27
+ @butcher_config ||= env[:machine].config.butcher
28
+ end
29
+
30
+ def cache_dir(env)
31
+ unless @cache_dir
32
+ env[:butcher].ui.info "cache dir is set to '#{butcher_config(env).cache_dir}'"
33
+ end
34
+
35
+ @cache_dir ||= butcher_config(env).cache_dir
36
+ end
37
+
38
+ def guest_cache_dir(env)
39
+ unless @guest_cache_dir
40
+ @guest_cache_dir = butcher_config(env).guest_cache_dir
41
+ env[:butcher].ui.info "guest cache dir is set to '#{@guest_cache_dir}'"
42
+ end
43
+
44
+ @guest_cache_dir
45
+ end
46
+
47
+ def guest_key_path(env)
48
+ unless @guest_key_path
49
+ @guest_key_path = butcher_config(env).guest_key_path
50
+ env[:butcher].ui.info "guest key path is set to '#{@guest_key_path}'"
51
+ end
52
+
53
+ @guest_key_path
54
+ end
55
+
56
+ def auto_knife_config_file(env)
57
+ @auto_knife_config_file ||= "#{cache_dir(env)}/#{env[:machine].name}-knife.rb"
58
+ end
59
+
22
60
  def knife_config_file(env)
23
- # Make sure that the default is set
24
- env[:machine].config.butcher.finalize!
61
+ unless @knife_config_file
62
+ if auto_knife?(env)
63
+ file = "#{cache_dir(env)}/#{env[:machine].name}-knife.rb"
64
+ unless File.exists?(file)
65
+ file = false
66
+ end
67
+ else
68
+ file = butcher_config(env).knife_config_file
69
+ end
70
+
71
+ env[:butcher].ui.info "knife.rb location set to '#{file}'"
72
+
73
+ @knife_config_file = file
74
+ end
75
+
76
+ @knife_config_file
77
+ end
78
+
79
+ def auto_knife_key(env)
80
+ unless @auto_knife_key
81
+ @auto_knife_key = "#{env[:machine].name}-client.pem"
82
+ env[:butcher].ui.info "auto client key name is #{@auto_knife_key}"
83
+ end
84
+
85
+ @auto_knife_key
86
+ end
87
+
88
+ def auto_knife_key_path(env)
89
+ unless @auto_knife_key_path
90
+ @auto_knife_key_path = "#{cache_dir(env)}/#{auto_knife_key(env)}"
91
+ env[:butcher].ui.info "auto client key path is #{@auto_knife_key_path}"
92
+ end
93
+
94
+ @auto_knife_key_path
95
+ end
96
+
97
+ def auto_knife_guest_key_path(env)
98
+ unless @auto_knife_guest_key_path
99
+ @auto_knife_guest_key_path = "#{guest_cache_dir(env)}/#{auto_knife_key(env)}"
100
+ env[:butcher].ui.info "auto client guest key path is #{@auto_knife_guest_key_path}"
101
+ end
102
+
103
+ @auto_knife_guest_key_path
104
+ end
25
105
 
26
- env[:butcher].ui.info "knife.rb location set to '#{env[:machine].config.butcher.knife_config_file}'"
27
- env[:machine].config.butcher.knife_config_file
106
+ def victim(env)
107
+ @victim ||= chef_provisioner(env).node_name || vm_config(env).hostname || vm_config(env).box
28
108
  end
29
109
  end
30
110
  end
@@ -5,11 +5,22 @@ module Vagrant
5
5
  description <<-DESC
6
6
  Delete node and client from the Chef server when destroying the VM.
7
7
  DESC
8
-
8
+
9
+ class << self
10
+ def provision(hook)
11
+ # This should be at the end so that it can copy the chef client pem.
12
+ hook.append(::Vagrant::Butcher::Action.auto_knife)
13
+ end
14
+ end
15
+
16
+ action_hook(:vagrant_butcher_auto_knife, :machine_action_up, &method(:provision))
17
+ action_hook(:vagrant_butcher_auto_knife, :machine_action_reload, &method(:provision))
18
+ action_hook(:vagrant_butcher_auto_knife, :machine_action_provision, &method(:provision))
19
+
9
20
  action_hook(:vagrant_butcher_cleanup, :machine_action_destroy) do |hook|
10
21
  hook.after(::Vagrant::Action::Builtin::ConfigValidate, Vagrant::Butcher::Action.cleanup)
11
22
  end
12
-
23
+
13
24
  config("butcher") do
14
25
  Config
15
26
  end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Butcher
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -7,10 +7,37 @@ describe Vagrant::Butcher::Config do
7
7
  subject.should respond_to(:knife_config_file)
8
8
  end
9
9
 
10
+ it "has the option to set guest chef client pem path" do
11
+ subject.should respond_to(:guest_key_path)
12
+ end
13
+
14
+ it "has the option to set cache dir path" do
15
+ subject.should respond_to(:cache_dir)
16
+ end
17
+
18
+ it "has the option to set guest cache dir path" do
19
+ subject.should respond_to(:guest_cache_dir)
20
+ end
21
+
10
22
  it "sets knife.rb default path" do
11
23
  subject.finalize!.should eql(File.expand_path("#{ENV['HOME']}/.chef/knife.rb"))
12
24
  end
13
25
 
26
+ it "sets guest chef client pem default path" do
27
+ subject.finalize!
28
+ subject.guest_key_path.should eql('/etc/chef/client.pem')
29
+ end
30
+
31
+ it "sets cache dir default path" do
32
+ subject.finalize!
33
+ subject.cache_dir.should eql(File.expand_path(".vagrant-butcher"))
34
+ end
35
+
36
+ it "sets guest cache dir default path" do
37
+ subject.finalize!
38
+ subject.guest_cache_dir.should eql("/vagrant/" + File.basename(subject.cache_dir))
39
+ end
40
+
14
41
  describe "#validate" do
15
42
  let(:env) { double('env') }
16
43
  let(:config) { double('config', butcher: subject) }
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-butcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
5
- prerelease:
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Cassiano Leal
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-04-16 00:00:00.000000000 Z
11
+ date: 2013-10-15 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: chef
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: pry-debugger
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: bundler
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -90,6 +81,7 @@ files:
90
81
  - Rakefile
91
82
  - lib/vagrant-butcher.rb
92
83
  - lib/vagrant-butcher/action.rb
84
+ - lib/vagrant-butcher/action/auto_knife.rb
93
85
  - lib/vagrant-butcher/action/cleanup.rb
94
86
  - lib/vagrant-butcher/config.rb
95
87
  - lib/vagrant-butcher/env.rb
@@ -104,27 +96,26 @@ files:
104
96
  - vagrant-butcher.gemspec
105
97
  homepage: https://github.com/cassianoleal/vagrant-butcher
106
98
  licenses: []
99
+ metadata: {}
107
100
  post_install_message:
108
101
  rdoc_options: []
109
102
  require_paths:
110
103
  - lib
111
104
  required_ruby_version: !ruby/object:Gem::Requirement
112
- none: false
113
105
  requirements:
114
106
  - - ! '>='
115
107
  - !ruby/object:Gem::Version
116
108
  version: '0'
117
109
  required_rubygems_version: !ruby/object:Gem::Requirement
118
- none: false
119
110
  requirements:
120
111
  - - ! '>='
121
112
  - !ruby/object:Gem::Version
122
113
  version: '0'
123
114
  requirements: []
124
115
  rubyforge_project:
125
- rubygems_version: 1.8.23
116
+ rubygems_version: 2.0.6
126
117
  signing_key:
127
- specification_version: 3
118
+ specification_version: 4
128
119
  summary: When a Vagrant VM that was spun up using Chef-Client is destroyed, it leaves
129
120
  behind a client and a node on the Chef server. What butcher does is to clean up
130
121
  those during the destroy operation.