vagrant-butcher 2.0.0 → 2.1.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: ca0c243f1bc78d523af45de51eda02479461c5eb
4
- data.tar.gz: 7fd4a44a11cb8a079b163ad38485f6828535ddcb
3
+ metadata.gz: ca1d827124b21623cd41daacdb1a72e4dcd7cda6
4
+ data.tar.gz: d9799e2328a5f3d723fe906570d2fbc474dfa9a6
5
5
  SHA512:
6
- metadata.gz: 4bbba48df2d30503688bc49232c54e12c79815165f47e0af1566c1e1cf12fa96e792b03e2f6b51ebcd403f53d584f5132fae0980a3c68b2f6dcc39c72fc4e819
7
- data.tar.gz: 92fc213d56dc3eaefad1d826ca50a82c23828936b34bec2cf425e713e3a6b2a9400d3fe5ccb627ae248aa7243e7d9f5df85a8cb256169c40bfdb5e181c82de09
6
+ metadata.gz: 956282406a98f53cbf15022e0eec08419abaa0079f1fb5f379af90ce8d206c7a94d37f65f679cc61bc94719e4e56260d7f39dcfcfa7233eccdf4de9f68291a63
7
+ data.tar.gz: f02c1defd5d1763ff3c8703e3947bec9d6618739df4ee27a1df601940677e9ca48dd2ebb78475454bf963e5c59105efb3149609ea6d7d8f952e1309e75794b74
data/.gitignore CHANGED
@@ -18,3 +18,4 @@ tmp
18
18
  .idea/
19
19
  .vagrant/
20
20
  .vagrant-butcher/
21
+ *.sublime-*
@@ -11,7 +11,7 @@ module Vagrant
11
11
  autoload :Action, 'vagrant-butcher/action'
12
12
  autoload :Config, 'vagrant-butcher/config'
13
13
  autoload :Env, 'vagrant-butcher/env'
14
- autoload :EnvHelpers, 'vagrant-butcher/env_helpers'
14
+ autoload :Helpers, 'vagrant-butcher/helpers'
15
15
  end
16
16
  end
17
17
 
@@ -2,50 +2,23 @@ module Vagrant
2
2
  module Butcher
3
3
  module Action
4
4
  class Cleanup
5
- include ::Vagrant::Butcher::EnvHelpers
5
+ include ::Vagrant::Butcher::Helpers::Action
6
+ include ::Vagrant::Butcher::Helpers::Connection
6
7
 
7
8
  def initialize(app, env)
8
9
  @app = app
9
- @delete_all_success = true
10
- end
11
-
12
- def delete_resource(resource, env)
13
- begin
14
- @conn.send(resource.to_sym).delete(victim(env))
15
- env[:butcher].ui.success "Chef #{resource} '#{victim(env)}' successfully butchered from the server..."
16
- rescue Exception => e
17
- env[:butcher].ui.warn "Could not butcher #{resource} #{victim(env)}: #{e.message}"
18
- @delete_all_success = false
19
- end
20
- end
21
-
22
- def cleanup_cache_dir(env)
23
- if @delete_all_success
24
- File.delete(host_key_path(env))
25
- begin
26
- Dir.delete(cache_dir(env))
27
- rescue
28
- # The dir wasn't empty.
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)}"
32
- end
33
10
  end
34
11
 
35
12
  def call(env)
36
- setup_connection(env)
37
-
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)
42
- end
13
+ if butcher_config(machine(env)).enabled
14
+ cleanup(env)
43
15
  else
44
- env[:butcher].ui.warn "Vagrant::Butcher disabled, not cleaning up Chef server!"
16
+ ui(env).warn "Vagrant::Butcher disabled, not cleaning up Chef server!"
45
17
  end
46
18
 
47
19
  @app.call(env)
48
20
  end
21
+
49
22
  end
50
23
  end
51
24
  end
@@ -2,45 +2,21 @@ module Vagrant
2
2
  module Butcher
3
3
  module Action
4
4
  class CopyGuestKey
5
- include ::Vagrant::Butcher::EnvHelpers
5
+ include ::Vagrant::Butcher::Helpers::Action
6
+ include ::Vagrant::Butcher::Helpers::KeyFiles
6
7
 
7
8
  def initialize(app, env)
8
9
  @app = app
9
10
  end
10
11
 
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
12
  def call(env)
34
13
  begin
35
14
  @app.call(env)
36
15
  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
16
+ copy_guest_key(env) if chef_client?(env)
42
17
  end
43
18
  end
19
+
44
20
  end
45
21
  end
46
22
  end
@@ -2,37 +2,12 @@ require 'vagrant/errors'
2
2
 
3
3
  module Vagrant
4
4
  module Butcher
5
- # This is a copy/adaptation of the VagrantWrapperError class of berkshelf-vagrant
6
- # The original can be found on https://github.com/RiotGames/berkshelf-vagrant
7
-
8
- # @author Jamie Winsor <reset@riotgames.com>
9
- #
10
- # A wrapper for a BerkshelfError for Vagrant. All Berkshelf exceptions should be
11
- # wrapped in this proxy object so they are properly handled when Vagrant encounters
12
- # an exception.
13
- #
14
- # @example wrapping an error encountered within the Vagrant plugin
15
- # rescue BerkshelfError => e
16
- # VagrantWrapperError.new(e)
17
- # end
18
- class VagrantWrapperError < ::Vagrant::Errors::VagrantError
19
- # @param [BerkshelfError]
20
- attr_reader :original
21
-
22
- # @param [BerkshelfError] original
23
- def initialize(original)
24
- @original = original
5
+ module Errors
6
+ class KeyCopyFailure < ::Vagrant::Errors::VagrantError
25
7
  end
26
8
 
27
- def to_s
28
- "#{original.class}: #{original.to_s}"
9
+ class NoSyncedFolder < ::Vagrant::Errors::VagrantError
29
10
  end
30
-
31
- private
32
-
33
- def method_missing(fun, *args, &block)
34
- original.send(fun, *args, &block)
35
- end
36
11
  end
37
12
  end
38
13
  end
@@ -0,0 +1,10 @@
1
+ module Vagrant
2
+ module Butcher
3
+ module Helpers
4
+ autoload :Config, 'vagrant-butcher/helpers/config'
5
+ autoload :Action, 'vagrant-butcher/helpers/action'
6
+ autoload :KeyFiles, 'vagrant-butcher/helpers/key_files'
7
+ autoload :Connection, 'vagrant-butcher/helpers/connection'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ module Vagrant
2
+ module Butcher
3
+ module Helpers
4
+ module Action
5
+ include Config
6
+
7
+ def ui(env)
8
+ @ui ||= env[:butcher].ui
9
+ end
10
+
11
+ def machine(env)
12
+ @machine ||= env[:machine]
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ module Vagrant
2
+ module Butcher
3
+ module Helpers
4
+ module Config
5
+
6
+ def vm_config(env)
7
+ @vm_config ||= machine(env).config.vm
8
+ end
9
+
10
+ def butcher_config(env)
11
+ @butcher_config ||= machine(env).config.butcher
12
+ end
13
+
14
+ def chef_provisioner(env)
15
+ @chef_provisioner ||= vm_config(env).provisioners.find do |p|
16
+ p.config.is_a? VagrantPlugins::Chef::Config::ChefClient
17
+ end.config
18
+ end
19
+
20
+ def chef_client?(env)
21
+ vm_config(env).provisioners.select { |p| p.name == :chef_client }.any?
22
+ end
23
+
24
+ def client_name(env)
25
+ @client_name ||= butcher_config(env).client_name || victim(env)
26
+ end
27
+
28
+ def victim(env)
29
+ @victim ||= chef_provisioner(env).node_name || vm_config(env).hostname || vm_config(env).box
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,57 @@
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
+
6
+ module Vagrant
7
+ module Butcher
8
+ module Helpers
9
+ module Connection
10
+ include ::Vagrant::Butcher::Helpers::KeyFiles
11
+
12
+ def setup_connection(env)
13
+ unless @conn
14
+ begin
15
+ @conn = ::Ridley.new(
16
+ server_url: chef_provisioner(env).chef_server_url,
17
+ client_name: client_name(env),
18
+ client_key: client_key(env),
19
+ ssl: {
20
+ verify: butcher_config(env).verify_ssl
21
+ },
22
+ retries: butcher_config(env).retries,
23
+ retry_interval: butcher_config(env).retry_interval,
24
+ proxy: butcher_config(env).proxy
25
+ )
26
+ rescue Ridley::Errors::ClientKeyFileNotFoundOrInvalid
27
+ ui(env).error "Chef client key not found at #{client_key(env)}"
28
+ rescue Exception => e
29
+ ui(env).error "Could not connect to Chef Server: #{e}"
30
+ end
31
+ end
32
+ @conn
33
+ end
34
+
35
+ def delete_resource(resource, env)
36
+ begin
37
+ @conn.send(resource.to_sym).delete(victim(env))
38
+ ui(env).success "Chef #{resource} '#{victim(env)}' successfully butchered from the server..."
39
+ rescue Exception => e
40
+ ui(env).warn "Could not butcher #{resource} #{victim(env)}: #{e.message}"
41
+ @failed_deletions ||= []
42
+ @failed_deletions << resource
43
+ end
44
+ end
45
+
46
+ def cleanup(env)
47
+ setup_connection(env)
48
+ if chef_client?(env)
49
+ %w(node client).each { |resource| delete_resource(resource, env) }
50
+ cleanup_cache_dir(env)
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,101 @@
1
+ module Vagrant
2
+ module Butcher
3
+ module Helpers
4
+ module KeyFiles
5
+
6
+ def cache_dir(env)
7
+ @cache_dir ||= butcher_config(env).cache_dir
8
+ end
9
+
10
+ def cache_dir_mapping(env)
11
+ unless @cache_dir_mapping
12
+ # Grab all enabled synced_folders
13
+ synced_folders = vm_config(env).synced_folders.values.find_all { |f| !f[:disabled] }
14
+
15
+ # Expand the hostpath of each folder
16
+ synced_folders.each { |f| f[:hostpath] = File.expand_path(f[:hostpath]) }
17
+
18
+ # Select the folder wherein the cache_dir is contained
19
+ cache_dir_mappings = synced_folders.select { |f| cache_dir(env) =~ /^#{f[:hostpath]}/ }
20
+ @cache_dir_mapping = cache_dir_mappings.first if cache_dir_mappings.any?
21
+ end
22
+ @cache_dir_mapping
23
+ end
24
+
25
+ def guest_cache_dir(env)
26
+ unless @guest_cache_dir
27
+ if cache_dir_mapping(env)
28
+ # Return the path to the cache dir inside the VM
29
+ @guest_cache_dir = cache_dir(env).gsub(cache_dir_mapping(env)[:hostpath], cache_dir_mapping(env)[:guestpath])
30
+ ui(env).info "Guest cache dir at #{@guest_cache_dir}"
31
+ else
32
+ ui(env).error "We couldn't find a synced folder to access the cache dir on the guest."
33
+ ui(env).error "Did you disable the /vagrant folder or set a butcher.cache_path that isn't shared with the guest?"
34
+ raise ::Vagrant::Butcher::Errors::NoSyncedFolder
35
+ end
36
+ end
37
+ @guest_cache_dir
38
+ end
39
+
40
+ def guest_key_path(env)
41
+ @guest_key_path ||= butcher_config(env).guest_key_path
42
+ end
43
+
44
+ def guest_key_filename(env)
45
+ @guest_key ||= "#{env[:machine].name}-client.pem"
46
+ end
47
+
48
+ def guest_cache_key_path(env)
49
+ @guest_cache_key_path ||= "#{guest_cache_dir(env)}/#{guest_key_filename(env)}"
50
+ end
51
+
52
+ def client_key(env)
53
+ @client_key ||= butcher_config(env).client_key || "#{cache_dir(env)}/#{guest_key_filename(env)}"
54
+ end
55
+
56
+ def create_cache_dir(env)
57
+ unless File.exists?(cache_dir(env))
58
+ ui(env).info "Creating #{cache_dir(env)}"
59
+ Dir.mkdir(cache_dir(env))
60
+ end
61
+ end
62
+
63
+ def copy_key_file(env)
64
+ create_cache_dir(env)
65
+
66
+ begin
67
+ machine(env).communicate.execute "mkdir -p #{guest_cache_key_path(env)} ; cp #{guest_key_path(env)} #{guest_cache_key_path(env)}", :sudo => true
68
+ ui(env).info "Copied #{guest_key_path(env)} to #{guest_cache_key_path(env)}"
69
+ rescue Exception => e
70
+ ui(env).error "Failed to copy #{guest_key_path(env)} to #{guest_cache_key_path(env)}"
71
+ ui(env).error e
72
+ raise ::Vagrant::Butcher::Errors::KeyCopyFailure
73
+ end
74
+ end
75
+
76
+ def cleanup_cache_dir(env)
77
+ unless @failed_deletions
78
+ File.delete(client_key(env))
79
+ begin
80
+ Dir.delete(cache_dir(env))
81
+ rescue Errno::ENOTEMPTY
82
+ # The dir wasn't empty.
83
+ end
84
+ else
85
+ ui(env).warn "#{@failed_deletions} not butchered from the Chef Server. Client key was left at #{client_key(env)}"
86
+ end
87
+ end
88
+
89
+ def copy_guest_key(env)
90
+ begin
91
+ guest_cache_dir(env)
92
+ copy_key_file(env)
93
+ rescue ::Vagrant::Errors::VagrantError => e
94
+ ui(env).error "Failed to copy Chef client key from the guest: #{e.class}"
95
+ end
96
+ end
97
+
98
+ end
99
+ end
100
+ end
101
+ end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Butcher
3
- VERSION = "2.0.0"
3
+ VERSION = "2.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-butcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.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-12-05 00:00:00.000000000 Z
11
+ date: 2013-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ridley
@@ -86,8 +86,12 @@ files:
86
86
  - lib/vagrant-butcher/action/copy_guest_key.rb
87
87
  - lib/vagrant-butcher/config.rb
88
88
  - lib/vagrant-butcher/env.rb
89
- - lib/vagrant-butcher/env_helpers.rb
90
89
  - lib/vagrant-butcher/errors.rb
90
+ - lib/vagrant-butcher/helpers.rb
91
+ - lib/vagrant-butcher/helpers/action.rb
92
+ - lib/vagrant-butcher/helpers/config.rb
93
+ - lib/vagrant-butcher/helpers/connection.rb
94
+ - lib/vagrant-butcher/helpers/key_files.rb
91
95
  - lib/vagrant-butcher/plugin.rb
92
96
  - lib/vagrant-butcher/version.rb
93
97
  - spec/spec_helper.rb
@@ -1,113 +0,0 @@
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
-
6
- module Vagrant
7
- module Butcher
8
- module EnvHelpers
9
- def vm_config(env)
10
- @vm_config ||= env[:machine].config.vm
11
- end
12
-
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
34
- end
35
-
36
- def chef_provisioner(env)
37
- @chef_provisioner ||= vm_config(env).provisioners.find do |p|
38
- p.config.is_a? VagrantPlugins::Chef::Config::ChefClient
39
- end.config
40
- end
41
-
42
- def chef_client?(env)
43
- vm_config(env).provisioners.select { |p| p.name == :chef_client }.any?
44
- end
45
-
46
- def butcher_config(env)
47
- @butcher_config ||= env[:machine].config.butcher
48
- end
49
-
50
- def cache_dir(env)
51
- @cache_dir ||= butcher_config(env).cache_dir
52
- end
53
-
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] }
58
-
59
- # Expand the hostpath of each folder
60
- synced_folders.each { |f| f[:hostpath] = File.expand_path(f[:hostpath]) }
61
-
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?
65
- end
66
- @cache_dir_mapping
67
- end
68
-
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}"
75
- else
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?"
79
- end
80
- end
81
- @guest_cache_dir
82
- end
83
-
84
- def guest_key_path(env)
85
- @guest_key_path ||= butcher_config(env).guest_key_path
86
- end
87
-
88
- def guest_key_filename(env)
89
- @guest_key ||= "#{env[:machine].name}-client.pem"
90
- end
91
-
92
- def host_key_path(env)
93
- @host_key_path ||= "#{cache_dir(env)}/#{guest_key_filename(env)}"
94
- end
95
-
96
- def guest_cache_key_path(env)
97
- @guest_cache_key_path ||= "#{guest_cache_dir(env)}/#{guest_key_filename(env)}"
98
- end
99
-
100
- def client_key(env)
101
- @client_key ||= butcher_config(env).client_key || host_key_path(env)
102
- end
103
-
104
- def client_name(env)
105
- @client_name ||= butcher_config(env).client_name || victim(env)
106
- end
107
-
108
- def victim(env)
109
- @victim ||= chef_provisioner(env).node_name || vm_config(env).hostname || vm_config(env).box
110
- end
111
- end
112
- end
113
- end