vagrant-butcher 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/README.md +32 -25
- data/Vagrantfile +25 -0
- data/lib/vagrant-butcher/action/cleanup.rb +13 -19
- data/lib/vagrant-butcher/action/copy_guest_key.rb +47 -0
- data/lib/vagrant-butcher/action.rb +4 -4
- data/lib/vagrant-butcher/config.rb +23 -26
- data/lib/vagrant-butcher/env.rb +1 -1
- data/lib/vagrant-butcher/env_helpers.rb +61 -59
- data/lib/vagrant-butcher/errors.rb +1 -1
- data/lib/vagrant-butcher/plugin.rb +4 -4
- data/lib/vagrant-butcher/version.rb +1 -1
- data/lib/vagrant-butcher.rb +5 -1
- data/spec/unit/vagrant_butcher/config_spec.rb +8 -50
- data/vagrant-butcher.gemspec +5 -4
- metadata +20 -18
- data/lib/vagrant-butcher/action/auto_knife.rb +0 -66
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
NGQ4NmRjMTM3YWJhMDgxMDRiODVlMTE0Mzk4MGM5NjhjOWZkZGI3MWQ4ZmIw
|
10
|
-
MGZkNDA3NWE5OTM4NDY5ZjQ0ZWJmMjFmYzRjMjg4ZWJiYTFlNTkxZjFiNzZj
|
11
|
-
OGYyNDQwM2Y5M2ZmN2YwYzA4NGNjYWY0MzA2ZWM3ZTU0ZDMxYWY=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NTQ2NGYyMDg2N2ZlMjM5MTFkMzRmMDk2MzE4NTg5NjBjMGMzNDQ1ZGUyMGQy
|
14
|
-
NTg5YmRkZGI0ZWU2ZTc4YzhhMGI4ZjY0NGM3N2I3MjJiODY1MmIxY2I5OTE4
|
15
|
-
ZDBlZGM3OTAzYmU4MTE0NjIyODhhYjdiNGNlZDE1MDI3NTU0OGY=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca0c243f1bc78d523af45de51eda02479461c5eb
|
4
|
+
data.tar.gz: 7fd4a44a11cb8a079b163ad38485f6828535ddcb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4bbba48df2d30503688bc49232c54e12c79815165f47e0af1566c1e1cf12fa96e792b03e2f6b51ebcd403f53d584f5132fae0980a3c68b2f6dcc39c72fc4e819
|
7
|
+
data.tar.gz: 92fc213d56dc3eaefad1d826ca50a82c23828936b34bec2cf425e713e3a6b2a9400d3fe5ccb627ae248aa7243e7d9f5df85a8cb256169c40bfdb5e181c82de09
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -7,5 +7,5 @@ group :development do
|
|
7
7
|
# We depend on Vagrant for development, but we don't add it as a
|
8
8
|
# gem dependency because we expect to be installed within the
|
9
9
|
# Vagrant environment itself using `vagrant plugin`.
|
10
|
-
gem "vagrant", :
|
10
|
+
gem "vagrant", :github => 'mitchellh/vagrant'
|
11
11
|
end
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ This plugin will automatically get rid of that cruft for you when you destroy th
|
|
10
10
|
|
11
11
|
## <a id="install"></a>Installation
|
12
12
|
|
13
|
-
|
13
|
+
Install this plugin using the Vagrant command line:
|
14
14
|
|
15
15
|
$ vagrant plugin install vagrant-butcher
|
16
16
|
|
@@ -18,43 +18,45 @@ Starting with version 1.0.0, installation is made via Vagrant plugins only:
|
|
18
18
|
|
19
19
|
The plugin is loaded automatically once installed.
|
20
20
|
|
21
|
-
|
21
|
+
## <a id='configuration'></a>Configuration
|
22
|
+
|
23
|
+
For most cases, the plugin shouldn't need any configuration. However, there are a few options that can be set. The options are all set in the `Vagrantfile` in the format:
|
22
24
|
|
23
25
|
```ruby
|
24
26
|
Vagrant.configure("2") do |config|
|
25
|
-
|
26
|
-
config.
|
27
|
-
|
28
|
-
end
|
27
|
+
...
|
28
|
+
config.butcher.<option> = <value>
|
29
|
+
...
|
29
30
|
end
|
30
31
|
```
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
[default] Forcing shutdown of VM...
|
41
|
-
[default] Destroying VM and associated drives...
|
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.
|
33
|
+
Option | Default Value | Purpose
|
34
|
+
-------|---------|--------
|
35
|
+
`enabled` | `true` | Defines whether `node` and `client` should be deleted
|
36
|
+
`guest_key_path` | `'/etc/chef/client.pem'` | Location of the client key in the guest VM
|
37
|
+
`verify_ssl` | `true` | If set to false, does not verify Chef's host key
|
38
|
+
`proxy` | `nil` | Inform the URL of a proxy server between your host and the Chef Server
|
39
|
+
`client_name` | Guest's node name | Inform a client name to override the plugin's default behaviour
|
40
|
+
`client_key` | Guest's client key | Point to a local `.pem` key file that matches the `client_name`
|
50
41
|
|
51
42
|
## <a id='caveats'></a>Caveats
|
52
43
|
|
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
|
54
44
|
* 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.
|
45
|
+
* It doesn't work with windows guests. If this is your case, either stick to version 1.x or (better) file bug reports with the errors you get.
|
46
|
+
* The default `.` -> `/vagrant` shared folder should be mounted.
|
47
|
+
* `verify_ssl` is enabled by default. You might want to disable that if, for example, you run your own Chef server with a self-signed cert. Check [here](#configuration) to see how.
|
55
48
|
|
56
49
|
## Changelog
|
57
50
|
|
51
|
+
### 2.0.0
|
52
|
+
|
53
|
+
* No more option to point to `knife.rb`. Data is retrieved from the `Vagrantfile`'s `chef_client` provisioner
|
54
|
+
* `chef` is no longer a requirement (no more `json` conflicts)
|
55
|
+
* [Configuration](#configuration) items were added to point to custom client name and key
|
56
|
+
* It's possible to disable the plugin by setting the `enabled` flag to `false` in the Vagrantfile.
|
57
|
+
|
58
|
+
See [Configuration](#configuration) for all possible customisations.
|
59
|
+
|
58
60
|
### 1.1.0
|
59
61
|
|
60
62
|
* :auto was added as an optional value for knife_config_file
|
@@ -74,6 +76,11 @@ This option has the following two limitations:
|
|
74
76
|
|
75
77
|
* 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
78
|
|
79
|
+
# Authors
|
80
|
+
|
81
|
+
* Cassiano Leal (<cassianoleal@gmail.com>)
|
82
|
+
* Daniel Searles ([https://github.com/squaresurf]())
|
83
|
+
|
77
84
|
## Contributing
|
78
85
|
|
79
86
|
1. Fork it
|
data/Vagrantfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- mode: ruby -*-
|
2
|
+
# vi: set ft=ruby :
|
3
|
+
|
4
|
+
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
|
5
|
+
VAGRANTFILE_API_VERSION = "2"
|
6
|
+
|
7
|
+
Vagrant.require_plugin "vagrant-butcher"
|
8
|
+
|
9
|
+
orgname = ENV['CHEF_ORGNAME']
|
10
|
+
|
11
|
+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
|
12
|
+
config.vm.box = "precise64"
|
13
|
+
# config.vm.network :private_network, ip: "192.168.33.10"
|
14
|
+
# config.vm.network :public_network
|
15
|
+
|
16
|
+
config.butcher.verify_ssl = true
|
17
|
+
|
18
|
+
config.vm.provision :chef_client do |chef|
|
19
|
+
chef.node_name = 'vagrant_butcher_test_vm'
|
20
|
+
chef.chef_server_url = "https://api.opscode.com/organizations/#{orgname}"
|
21
|
+
chef.validation_key_path = "~/.chef/#{orgname}-validator.pem"
|
22
|
+
chef.validation_client_name = "#{orgname}-validator"
|
23
|
+
# chef.run_list = [ 'recipe[dummy::fail]' ]
|
24
|
+
end
|
25
|
+
end
|
@@ -1,7 +1,3 @@
|
|
1
|
-
require 'chef/config'
|
2
|
-
require 'chef/rest'
|
3
|
-
require 'chef/api_client'
|
4
|
-
|
5
1
|
module Vagrant
|
6
2
|
module Butcher
|
7
3
|
module Action
|
@@ -15,39 +11,37 @@ module Vagrant
|
|
15
11
|
|
16
12
|
def delete_resource(resource, env)
|
17
13
|
begin
|
18
|
-
|
14
|
+
@conn.send(resource.to_sym).delete(victim(env))
|
19
15
|
env[:butcher].ui.success "Chef #{resource} '#{victim(env)}' successfully butchered from the server..."
|
20
16
|
rescue Exception => e
|
21
|
-
env[:butcher].ui.warn "Could not
|
17
|
+
env[:butcher].ui.warn "Could not butcher #{resource} #{victim(env)}: #{e.message}"
|
22
18
|
@delete_all_success = false
|
23
19
|
end
|
24
20
|
end
|
25
21
|
|
26
|
-
def
|
22
|
+
def cleanup_cache_dir(env)
|
27
23
|
if @delete_all_success
|
28
|
-
File.delete(
|
29
|
-
File.delete(auto_knife_key_path(env))
|
24
|
+
File.delete(host_key_path(env))
|
30
25
|
begin
|
31
26
|
Dir.delete(cache_dir(env))
|
32
27
|
rescue
|
33
28
|
# The dir wasn't empty.
|
34
29
|
end
|
30
|
+
else
|
31
|
+
env[:butcher].ui.warn "Client and/or node not butchered from the Chef Server. Client key was left at #{host_key_path(env)}"
|
35
32
|
end
|
36
33
|
end
|
37
34
|
|
38
35
|
def call(env)
|
39
|
-
|
40
|
-
begin
|
41
|
-
::Chef::Config.from_file knife_config_file(env)
|
42
|
-
rescue Errno::ENOENT => e
|
43
|
-
raise ::Vagrant::Butcher::VagrantWrapperError.new(e)
|
44
|
-
end
|
45
|
-
|
46
|
-
%w(node client).each { |resource| delete_resource(resource, env) }
|
36
|
+
setup_connection(env)
|
47
37
|
|
48
|
-
|
49
|
-
|
38
|
+
if butcher_config(env).enabled
|
39
|
+
if chef_client?(env)
|
40
|
+
%w(node client).each { |resource| delete_resource(resource, env) }
|
41
|
+
cleanup_cache_dir(env)
|
50
42
|
end
|
43
|
+
else
|
44
|
+
env[:butcher].ui.warn "Vagrant::Butcher disabled, not cleaning up Chef server!"
|
51
45
|
end
|
52
46
|
|
53
47
|
@app.call(env)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Vagrant
|
2
|
+
module Butcher
|
3
|
+
module Action
|
4
|
+
class CopyGuestKey
|
5
|
+
include ::Vagrant::Butcher::EnvHelpers
|
6
|
+
|
7
|
+
def initialize(app, env)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_cache_dir(env)
|
12
|
+
unless File.exists?(cache_dir(env))
|
13
|
+
env[:butcher].ui.info "Creating #{cache_dir(env)}"
|
14
|
+
Dir.mkdir(cache_dir(env))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_key_from_guest(env)
|
19
|
+
create_cache_dir(env)
|
20
|
+
|
21
|
+
begin
|
22
|
+
env[:machine].communicate.execute "cp #{guest_key_path(env)} #{guest_cache_key_path(env)}", :sudo => true
|
23
|
+
env[:butcher].ui.info "Copied #{guest_key_path(env)} to #{guest_cache_key_path(env)}"
|
24
|
+
rescue Exception => e
|
25
|
+
env[:butcher].ui.error "Failed to copy #{guest_key_path(env)} to #{guest_cache_key_path(env)}"
|
26
|
+
env[:butcher].ui.error e
|
27
|
+
return false
|
28
|
+
end
|
29
|
+
|
30
|
+
return true
|
31
|
+
end
|
32
|
+
|
33
|
+
def call(env)
|
34
|
+
begin
|
35
|
+
@app.call(env)
|
36
|
+
ensure
|
37
|
+
if chef_client?(env)
|
38
|
+
unless guest_cache_dir(env) && copy_key_from_guest(env)
|
39
|
+
env[:butcher].ui.error "Failed to copy Chef client key from the guest."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -2,8 +2,8 @@ module Vagrant
|
|
2
2
|
module Butcher
|
3
3
|
module Action
|
4
4
|
autoload :Cleanup, 'vagrant-butcher/action/cleanup'
|
5
|
-
autoload :
|
6
|
-
|
5
|
+
autoload :CopyGuestKey, 'vagrant-butcher/action/copy_guest_key'
|
6
|
+
|
7
7
|
def self.cleanup
|
8
8
|
::Vagrant::Action::Builder.new.tap do |b|
|
9
9
|
b.use setup
|
@@ -11,10 +11,10 @@ module Vagrant
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
def self.
|
14
|
+
def self.copy_guest_key
|
15
15
|
::Vagrant::Action::Builder.new.tap do |b|
|
16
16
|
b.use setup
|
17
|
-
b.use Vagrant::Butcher::Action::
|
17
|
+
b.use Vagrant::Butcher::Action::CopyGuestKey
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -1,46 +1,43 @@
|
|
1
1
|
module Vagrant
|
2
2
|
module Butcher
|
3
3
|
class Config < ::Vagrant.plugin('2', :config)
|
4
|
+
attr_accessor :enabled
|
4
5
|
attr_accessor :guest_key_path
|
5
6
|
attr_accessor :cache_dir
|
6
|
-
attr_accessor :
|
7
|
-
attr_accessor :
|
8
|
-
|
7
|
+
attr_accessor :verify_ssl
|
8
|
+
attr_accessor :retries
|
9
|
+
attr_accessor :retry_interval
|
10
|
+
attr_accessor :proxy
|
11
|
+
attr_accessor :client_name
|
12
|
+
attr_accessor :client_key
|
13
|
+
|
9
14
|
def initialize
|
10
15
|
super
|
16
|
+
@enabled = UNSET_VALUE
|
11
17
|
@guest_key_path = UNSET_VALUE
|
12
18
|
@cache_dir = UNSET_VALUE
|
13
|
-
@
|
14
|
-
@
|
19
|
+
@verify_ssl = UNSET_VALUE
|
20
|
+
@retries = UNSET_VALUE
|
21
|
+
@retry_interval = UNSET_VALUE
|
22
|
+
@proxy = UNSET_VALUE
|
23
|
+
@client_name = UNSET_VALUE
|
24
|
+
@client_key = UNSET_VALUE
|
15
25
|
end
|
16
26
|
|
17
27
|
def cache_dir=(value)
|
18
28
|
@cache_dir = File.expand_path(value)
|
19
29
|
end
|
20
30
|
|
21
|
-
def knife_config_file=(value)
|
22
|
-
unless value == :auto
|
23
|
-
value = File.expand_path(value)
|
24
|
-
end
|
25
|
-
|
26
|
-
@knife_config_file = value
|
27
|
-
end
|
28
|
-
|
29
|
-
def validate(machine)
|
30
|
-
errors = []
|
31
|
-
|
32
|
-
if @knife_config_file != :auto && !File.exists?(@knife_config_file)
|
33
|
-
errors << "Knife configuration not found at #{@knife_config_file}."
|
34
|
-
end
|
35
|
-
|
36
|
-
{ "butcher configuration" => errors }
|
37
|
-
end
|
38
|
-
|
39
31
|
def finalize!
|
32
|
+
@enabled = true if @enabled == UNSET_VALUE
|
40
33
|
@guest_key_path = '/etc/chef/client.pem' if @guest_key_path == UNSET_VALUE
|
41
|
-
@cache_dir = File.expand_path ".vagrant
|
42
|
-
@
|
43
|
-
@
|
34
|
+
@cache_dir = File.expand_path ".vagrant/butcher" if @cache_dir == UNSET_VALUE
|
35
|
+
@verify_ssl = true if @verify_ssl == UNSET_VALUE
|
36
|
+
@retries = 0 if @retries == UNSET_VALUE
|
37
|
+
@retry_interval = 0 if @retry_interval == UNSET_VALUE
|
38
|
+
@proxy = nil if @proxy == UNSET_VALUE
|
39
|
+
@client_name = nil if @client_name == UNSET_VALUE
|
40
|
+
@client_key = nil if @client_key == UNSET_VALUE
|
44
41
|
end
|
45
42
|
end
|
46
43
|
end
|
data/lib/vagrant-butcher/env.rb
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
require 'ridley'
|
2
|
+
|
3
|
+
# Silence celluloid warnings and errors: https://github.com/RiotGames/ridley/issues/220
|
4
|
+
::Ridley::Logging.logger.level = Logger.const_get 'FATAL'
|
5
|
+
|
1
6
|
module Vagrant
|
2
7
|
module Butcher
|
3
8
|
module EnvHelpers
|
@@ -5,8 +10,27 @@ module Vagrant
|
|
5
10
|
@vm_config ||= env[:machine].config.vm
|
6
11
|
end
|
7
12
|
|
8
|
-
def
|
9
|
-
|
13
|
+
def setup_connection(env)
|
14
|
+
unless @conn
|
15
|
+
begin
|
16
|
+
@conn = ::Ridley.new(
|
17
|
+
server_url: chef_provisioner(env).chef_server_url,
|
18
|
+
client_name: client_name(env),
|
19
|
+
client_key: client_key(env),
|
20
|
+
ssl: {
|
21
|
+
verify: butcher_config(env).verify_ssl
|
22
|
+
},
|
23
|
+
retries: butcher_config(env).retries,
|
24
|
+
retry_interval: butcher_config(env).retry_interval,
|
25
|
+
proxy: butcher_config(env).proxy
|
26
|
+
)
|
27
|
+
rescue Ridley::Errors::ClientKeyFileNotFoundOrInvalid
|
28
|
+
env[:butcher].ui.error "Chef client key not found at #{host_key_path(env)}"
|
29
|
+
rescue Exception => e
|
30
|
+
env[:butcher].ui.error "Could not connect to Chef Server: #{e}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@conn
|
10
34
|
end
|
11
35
|
|
12
36
|
def chef_provisioner(env)
|
@@ -19,88 +43,66 @@ module Vagrant
|
|
19
43
|
vm_config(env).provisioners.select { |p| p.name == :chef_client }.any?
|
20
44
|
end
|
21
45
|
|
22
|
-
def auto_knife?(env)
|
23
|
-
butcher_config(env).knife_config_file == :auto
|
24
|
-
end
|
25
|
-
|
26
46
|
def butcher_config(env)
|
27
47
|
@butcher_config ||= env[:machine].config.butcher
|
28
48
|
end
|
29
49
|
|
30
50
|
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
51
|
@cache_dir ||= butcher_config(env).cache_dir
|
36
52
|
end
|
37
53
|
|
38
|
-
def
|
39
|
-
unless @
|
40
|
-
|
41
|
-
env
|
42
|
-
end
|
54
|
+
def cache_dir_mapping(env)
|
55
|
+
unless @cache_dir_mapping
|
56
|
+
# Grab all enabled synced_folders
|
57
|
+
synced_folders = vm_config(env).synced_folders.values.find_all { |f| !f[:disabled] }
|
43
58
|
|
44
|
-
|
45
|
-
|
59
|
+
# Expand the hostpath of each folder
|
60
|
+
synced_folders.each { |f| f[:hostpath] = File.expand_path(f[:hostpath]) }
|
46
61
|
|
47
|
-
|
48
|
-
|
49
|
-
@
|
50
|
-
env[:butcher].ui.info "guest key path is set to '#{@guest_key_path}'"
|
62
|
+
# Select the folder wherein the cache_dir is contained
|
63
|
+
cache_dir_mappings = synced_folders.select { |f| cache_dir(env) =~ /^#{f[:hostpath]}/ }
|
64
|
+
@cache_dir_mapping = cache_dir_mappings.first if cache_dir_mappings.any?
|
51
65
|
end
|
52
|
-
|
53
|
-
@guest_key_path
|
66
|
+
@cache_dir_mapping
|
54
67
|
end
|
55
68
|
|
56
|
-
def
|
57
|
-
@
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
69
|
+
def guest_cache_dir(env)
|
70
|
+
unless @guest_cache_dir
|
71
|
+
if cache_dir_mapping(env)
|
72
|
+
# Return the path to the cache dir inside the VM
|
73
|
+
@guest_cache_dir = cache_dir(env).gsub(cache_dir_mapping(env)[:hostpath], cache_dir_mapping(env)[:guestpath])
|
74
|
+
env[:butcher].ui.info "Guest cache dir at #{@guest_cache_dir}"
|
67
75
|
else
|
68
|
-
|
76
|
+
@guest_cache_dir = false
|
77
|
+
env[:butcher].ui.error "We couldn't find a synced folder to access the cache dir on the guest."
|
78
|
+
env[:butcher].ui.error "Did you disable the /vagrant folder or set a butcher.cache_path that isn't shared with the guest?"
|
69
79
|
end
|
70
|
-
|
71
|
-
env[:butcher].ui.info "knife.rb location set to '#{file}'"
|
72
|
-
|
73
|
-
@knife_config_file = file
|
74
80
|
end
|
75
|
-
|
76
|
-
@knife_config_file
|
81
|
+
@guest_cache_dir
|
77
82
|
end
|
78
83
|
|
79
|
-
def
|
80
|
-
|
81
|
-
|
82
|
-
env[:butcher].ui.info "auto client key name is #{@auto_knife_key}"
|
83
|
-
end
|
84
|
+
def guest_key_path(env)
|
85
|
+
@guest_key_path ||= butcher_config(env).guest_key_path
|
86
|
+
end
|
84
87
|
|
85
|
-
|
88
|
+
def guest_key_filename(env)
|
89
|
+
@guest_key ||= "#{env[:machine].name}-client.pem"
|
86
90
|
end
|
87
91
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
env[:butcher].ui.info "auto client key path is #{@auto_knife_key_path}"
|
92
|
-
end
|
92
|
+
def host_key_path(env)
|
93
|
+
@host_key_path ||= "#{cache_dir(env)}/#{guest_key_filename(env)}"
|
94
|
+
end
|
93
95
|
|
94
|
-
|
96
|
+
def guest_cache_key_path(env)
|
97
|
+
@guest_cache_key_path ||= "#{guest_cache_dir(env)}/#{guest_key_filename(env)}"
|
95
98
|
end
|
96
99
|
|
97
|
-
def
|
98
|
-
|
99
|
-
|
100
|
-
env[:butcher].ui.info "auto client guest key path is #{@auto_knife_guest_key_path}"
|
101
|
-
end
|
100
|
+
def client_key(env)
|
101
|
+
@client_key ||= butcher_config(env).client_key || host_key_path(env)
|
102
|
+
end
|
102
103
|
|
103
|
-
|
104
|
+
def client_name(env)
|
105
|
+
@client_name ||= butcher_config(env).client_name || victim(env)
|
104
106
|
end
|
105
107
|
|
106
108
|
def victim(env)
|
@@ -4,7 +4,7 @@ module Vagrant
|
|
4
4
|
module Butcher
|
5
5
|
# This is a copy/adaptation of the VagrantWrapperError class of berkshelf-vagrant
|
6
6
|
# The original can be found on https://github.com/RiotGames/berkshelf-vagrant
|
7
|
-
|
7
|
+
|
8
8
|
# @author Jamie Winsor <reset@riotgames.com>
|
9
9
|
#
|
10
10
|
# A wrapper for a BerkshelfError for Vagrant. All Berkshelf exceptions should be
|
@@ -9,13 +9,13 @@ module Vagrant
|
|
9
9
|
class << self
|
10
10
|
def provision(hook)
|
11
11
|
# This should be at the end so that it can copy the chef client pem.
|
12
|
-
hook.
|
12
|
+
hook.before(::Vagrant::Action::Builtin::Provision, Vagrant::Butcher::Action.copy_guest_key)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
action_hook(:
|
17
|
-
action_hook(:
|
18
|
-
action_hook(:
|
16
|
+
action_hook(:vagrant_butcher_copy_guest_key, :machine_action_up, &method(:provision))
|
17
|
+
action_hook(:vagrant_butcher_copy_guest_key, :machine_action_reload, &method(:provision))
|
18
|
+
action_hook(:vagrant_butcher_copy_guest_key, :machine_action_provision, &method(:provision))
|
19
19
|
|
20
20
|
action_hook(:vagrant_butcher_cleanup, :machine_action_destroy) do |hook|
|
21
21
|
hook.after(::Vagrant::Action::Builtin::ConfigValidate, Vagrant::Butcher::Action.cleanup)
|
data/lib/vagrant-butcher.rb
CHANGED
@@ -2,68 +2,26 @@ require 'spec_helper.rb'
|
|
2
2
|
|
3
3
|
describe Vagrant::Butcher::Config do
|
4
4
|
subject { described_class.new }
|
5
|
-
|
6
|
-
it "
|
7
|
-
subject.
|
5
|
+
|
6
|
+
it "does not have the option to set knife.rb path" do
|
7
|
+
subject.should_not respond_to(:knife_config_file)
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
it "has the option to set guest chef client pem path" do
|
11
11
|
subject.should respond_to(:guest_key_path)
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
it "has the option to set cache dir path" do
|
15
15
|
subject.should respond_to(:cache_dir)
|
16
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
|
-
|
22
|
-
it "sets knife.rb default path" do
|
23
|
-
subject.finalize!.should eql(File.expand_path("#{ENV['HOME']}/.chef/knife.rb"))
|
24
|
-
end
|
25
|
-
|
17
|
+
|
26
18
|
it "sets guest chef client pem default path" do
|
27
19
|
subject.finalize!
|
28
20
|
subject.guest_key_path.should eql('/etc/chef/client.pem')
|
29
21
|
end
|
30
|
-
|
22
|
+
|
31
23
|
it "sets cache dir default path" do
|
32
24
|
subject.finalize!
|
33
|
-
subject.cache_dir.should eql(File.expand_path(".vagrant
|
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
|
-
|
41
|
-
describe "#validate" do
|
42
|
-
let(:env) { double('env') }
|
43
|
-
let(:config) { double('config', butcher: subject) }
|
44
|
-
let(:machine) { double('machine', config: config, env: env) }
|
45
|
-
let(:result) { subject.validate(machine) }
|
46
|
-
|
47
|
-
context "when validations pass" do
|
48
|
-
before(:each) do
|
49
|
-
File.should_receive(:exists?).with(subject.knife_config_file).and_return(true)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "contains an empty Array for the 'butcher configuration' key" do
|
53
|
-
result["butcher configuration"].should be_a(Array)
|
54
|
-
result["butcher configuration"].should be_empty
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context "when validations fail" do
|
59
|
-
before(:each) do
|
60
|
-
File.should_receive(:exists?).with(subject.knife_config_file).and_return(false)
|
61
|
-
end
|
62
|
-
|
63
|
-
it "contains an empty Array for the 'butcher configuration' key" do
|
64
|
-
result["butcher configuration"].should be_a(Array)
|
65
|
-
result["butcher configuration"].should_not be_empty
|
66
|
-
end
|
67
|
-
end
|
25
|
+
subject.cache_dir.should eql(File.expand_path(".vagrant/butcher"))
|
68
26
|
end
|
69
27
|
end
|
data/vagrant-butcher.gemspec
CHANGED
@@ -11,15 +11,16 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.description = %q{Delete Chef client and node when destroying Vagrant VM}
|
12
12
|
gem.summary = %q{When a Vagrant VM that was spun up using Chef-Client is destroyed, it leaves behind a client and a node on the Chef server. What butcher does is to clean up those during the destroy operation.}
|
13
13
|
gem.homepage = "https://github.com/cassianoleal/vagrant-butcher"
|
14
|
+
gem.license = 'MIT'
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
19
|
gem.require_paths = ["lib"]
|
19
|
-
|
20
|
-
gem.add_dependency "
|
21
|
-
|
20
|
+
|
21
|
+
gem.add_dependency "ridley", ">= 1.5.3"
|
22
|
+
|
22
23
|
gem.add_development_dependency "rspec"
|
23
24
|
gem.add_development_dependency "pry-debugger"
|
24
|
-
gem.add_development_dependency "bundler", "
|
25
|
+
gem.add_development_dependency "bundler", ">= 1.3"
|
25
26
|
end
|
metadata
CHANGED
@@ -1,69 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-butcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cassiano Leal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: ridley
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.5.3
|
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:
|
26
|
+
version: 1.5.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: pry-debugger
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '1.3'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.3'
|
69
69
|
description: Delete Chef client and node when destroying Vagrant VM
|
@@ -79,10 +79,11 @@ files:
|
|
79
79
|
- LICENSE.txt
|
80
80
|
- README.md
|
81
81
|
- Rakefile
|
82
|
+
- Vagrantfile
|
82
83
|
- lib/vagrant-butcher.rb
|
83
84
|
- lib/vagrant-butcher/action.rb
|
84
|
-
- lib/vagrant-butcher/action/auto_knife.rb
|
85
85
|
- lib/vagrant-butcher/action/cleanup.rb
|
86
|
+
- lib/vagrant-butcher/action/copy_guest_key.rb
|
86
87
|
- lib/vagrant-butcher/config.rb
|
87
88
|
- lib/vagrant-butcher/env.rb
|
88
89
|
- lib/vagrant-butcher/env_helpers.rb
|
@@ -95,7 +96,8 @@ files:
|
|
95
96
|
- spec/unit/vagrant_butcher_spec.rb
|
96
97
|
- vagrant-butcher.gemspec
|
97
98
|
homepage: https://github.com/cassianoleal/vagrant-butcher
|
98
|
-
licenses:
|
99
|
+
licenses:
|
100
|
+
- MIT
|
99
101
|
metadata: {}
|
100
102
|
post_install_message:
|
101
103
|
rdoc_options: []
|
@@ -103,17 +105,17 @@ require_paths:
|
|
103
105
|
- lib
|
104
106
|
required_ruby_version: !ruby/object:Gem::Requirement
|
105
107
|
requirements:
|
106
|
-
- -
|
108
|
+
- - '>='
|
107
109
|
- !ruby/object:Gem::Version
|
108
110
|
version: '0'
|
109
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
112
|
requirements:
|
111
|
-
- -
|
113
|
+
- - '>='
|
112
114
|
- !ruby/object:Gem::Version
|
113
115
|
version: '0'
|
114
116
|
requirements: []
|
115
117
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.0.
|
118
|
+
rubygems_version: 2.0.3
|
117
119
|
signing_key:
|
118
120
|
specification_version: 4
|
119
121
|
summary: When a Vagrant VM that was spun up using Chef-Client is destroyed, it leaves
|
@@ -1,66 +0,0 @@
|
|
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
|