vagrant-berkshelf 3.0.1 → 4.0.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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +36 -17
  3. data/.travis.yml +6 -4
  4. data/CONTRIBUTING.md +4 -4
  5. data/Gemfile +2 -2
  6. data/{LICENSE.txt → LICENSE} +1 -1
  7. data/README.md +67 -24
  8. data/Rakefile +14 -0
  9. data/lib/vagrant-berkshelf.rb +17 -1
  10. data/lib/vagrant-berkshelf/action/base.rb +48 -0
  11. data/lib/vagrant-berkshelf/action/check.rb +44 -0
  12. data/lib/vagrant-berkshelf/action/clean.rb +29 -0
  13. data/lib/vagrant-berkshelf/action/install.rb +35 -0
  14. data/lib/vagrant-berkshelf/action/load.rb +59 -0
  15. data/lib/vagrant-berkshelf/action/share.rb +38 -0
  16. data/lib/vagrant-berkshelf/action/upload.rb +99 -0
  17. data/lib/vagrant-berkshelf/config.rb +87 -0
  18. data/lib/vagrant-berkshelf/env.rb +14 -0
  19. data/lib/vagrant-berkshelf/errors.rb +79 -0
  20. data/lib/vagrant-berkshelf/helpers.rb +180 -0
  21. data/lib/vagrant-berkshelf/plugin.rb +40 -0
  22. data/lib/vagrant-berkshelf/version.rb +5 -0
  23. data/spec/spec_helper.rb +1 -4
  24. data/spec/unit/vagrant-berkshelf/config_spec.rb +119 -0
  25. data/vagrant-berkshelf.gemspec +26 -14
  26. metadata +35 -52
  27. data/Thorfile +0 -59
  28. data/integration/Berksfile +0 -3
  29. data/integration/Gemfile +0 -10
  30. data/integration/Vagrantfile +0 -19
  31. data/lib/berkshelf/vagrant.rb +0 -68
  32. data/lib/berkshelf/vagrant/action.rb +0 -64
  33. data/lib/berkshelf/vagrant/action/clean.rb +0 -27
  34. data/lib/berkshelf/vagrant/action/configure_chef.rb +0 -27
  35. data/lib/berkshelf/vagrant/action/install.rb +0 -69
  36. data/lib/berkshelf/vagrant/action/load_shelf.rb +0 -52
  37. data/lib/berkshelf/vagrant/action/upload.rb +0 -53
  38. data/lib/berkshelf/vagrant/berks_config.rb +0 -48
  39. data/lib/berkshelf/vagrant/chef_config.rb +0 -88
  40. data/lib/berkshelf/vagrant/config.rb +0 -113
  41. data/lib/berkshelf/vagrant/env.rb +0 -16
  42. data/lib/berkshelf/vagrant/env_helpers.rb +0 -160
  43. data/lib/berkshelf/vagrant/errors.rb +0 -71
  44. data/lib/berkshelf/vagrant/plugin.rb +0 -41
  45. data/lib/berkshelf/vagrant/version.rb +0 -5
  46. data/spec/unit/berkshelf/vagrant/config_spec.rb +0 -97
  47. data/spec/unit/berkshelf/vagrant/errors_spec.rb +0 -12
  48. data/spec/unit/berkshelf/vagrant_spec.rb +0 -31
@@ -1,88 +0,0 @@
1
- require 'vagrant/util/hash_with_indifferent_access'
2
- require 'pathname'
3
-
4
- module Berkshelf::Vagrant
5
- class ChefConfig < ::Vagrant::Util::HashWithIndifferentAccess
6
- class << self
7
- # Return the most sensible path to the Chef configuration file. This can
8
- # be configured by setting a value for the 'CHEF_CONFIG' environment
9
- # variable.
10
- #
11
- # @return [String, nil]
12
- def location
13
- possibles = []
14
-
15
- possibles << ENV['CHEF_CONFIG'] if ENV['CHEF_CONFIG']
16
- possibles << File.join(ENV['KNIFE_HOME'], 'knife.rb') if ENV['KNIFE_HOME']
17
- possibles << File.join(Dir.pwd, 'knife.rb')
18
-
19
- # Ascending search for .chef directory siblings
20
- Pathname.new(Dir.pwd).ascend do |file|
21
- sibling_chef = File.join(file, '.chef')
22
- possibles << File.join(sibling_chef, 'knife.rb')
23
- end
24
-
25
- possibles << File.join(ENV['HOME'], '.chef', 'knife.rb') if ENV['HOME']
26
- possibles.compact!
27
-
28
- location = possibles.find { |loc| File.exists?(File.expand_path(loc)) }
29
-
30
- File.expand_path(location) unless location.nil?
31
- end
32
-
33
- def instance
34
- @instance ||= from_file(location)
35
- end
36
-
37
- def from_file(file = nil)
38
- file = file.nil? ? location : file
39
- if !file.nil? && File.exist?(file) && File.readable?(file)
40
- contents = File.read(file)
41
- return parse(contents, file)
42
- end
43
-
44
- new
45
- end
46
-
47
- def parse(contents, path = nil)
48
- new(contents, path)
49
- end
50
- end
51
-
52
- def initialize(contents = "", path = "")
53
- instance_eval(contents, path)
54
- end
55
-
56
- def chef_server_url(value = nil)
57
- set_or_return(:chef_server_url, value)
58
- end
59
-
60
- def client_key(value = nil)
61
- set_or_return(:client_key, value)
62
- end
63
-
64
- def node_name(value = nil)
65
- set_or_return(:node_name, value)
66
- end
67
-
68
- def method_missing(m, *args, &block)
69
- Proxy.new
70
- end
71
-
72
- private
73
-
74
- def set_or_return(key, value)
75
- if value.nil?
76
- return self[key]
77
- else
78
- self[key] = value
79
- end
80
- end
81
-
82
- class Proxy
83
- def method_missing(m, *args, &block)
84
- self
85
- end
86
- end
87
- end
88
- end
@@ -1,113 +0,0 @@
1
- require 'vagrant/util/hash_with_indifferent_access'
2
-
3
- module Berkshelf
4
- module Vagrant
5
- class Config < ::Vagrant.plugin("2", :config)
6
- # @return [String]
7
- # path to the Berksfile to use with Vagrant
8
- attr_reader :berksfile_path
9
-
10
- # @return [Boolean]
11
- # disable of use Berks in Vagrant
12
- attr_accessor :enabled
13
-
14
- # @return [Array<Symbol>]
15
- # only cookbooks in these groups will be installed and copied to
16
- # Vagrant's shelf
17
- attr_accessor :only
18
-
19
- # @return [Array<Symbol>]
20
- # cookbooks in all other groups except for these will be installed
21
- # and copied to Vagrant's shelf
22
- attr_accessor :except
23
-
24
- # @return [String]
25
- # the Chef node name (client name) to use to authenticate with the remote
26
- # chef server to upload cookbooks when using the chef client provisioner
27
- attr_accessor :node_name
28
-
29
- # @return [String]
30
- # a filepath to a chef client key to use to authenticate with the remote
31
- # chef server to upload cookbooks when using the chef client provisioner
32
- attr_accessor :client_key
33
-
34
- def initialize
35
- super
36
-
37
- @berksfile_path = UNSET_VALUE
38
- @except = Array.new
39
- @only = Array.new
40
- @node_name = BerksConfig.instance.chef[:node_name]
41
- @client_key = BerksConfig.instance.chef[:client_key]
42
- @enabled = UNSET_VALUE
43
- end
44
-
45
- def finalize!
46
- @berksfile_path = File.join(Dir.pwd, "Berksfile") if @berksfile_path == UNSET_VALUE
47
- @enabled = File.exist?(@berksfile_path) if @enabled == UNSET_VALUE
48
- end
49
-
50
- # @param [String] value
51
- def berksfile_path=(value)
52
- @berksfile_path = value
53
- end
54
-
55
- # @param [String] value
56
- def client_key=(value)
57
- @client_key = value
58
- end
59
-
60
- def to_hash
61
- ::Vagrant::Util::HashWithIndifferentAccess.new(instance_variables_hash)
62
- end
63
-
64
- def validate(machine)
65
- errors = Array.new
66
- unless @berksfile_path.nil?
67
- @berksfile_path = File.expand_path(@berksfile_path, machine.env.root_path.to_s)
68
- end
69
- unless @client_key.nil?
70
- @client_key = File.expand_path(@client_key, machine.env.root_path.to_s)
71
- end
72
-
73
- unless [TrueClass, FalseClass].include?(enabled.class)
74
- errors << "A value for berkshelf.enabled can be true or false."
75
- end
76
-
77
- if enabled
78
- if machine.config.berkshelf.berksfile_path.nil?
79
- errors << "berkshelf.berksfile_path cannot be nil."
80
- end
81
-
82
- unless File.exist?(machine.config.berkshelf.berksfile_path)
83
- errors << "No Berksfile was found at #{machine.config.berkshelf.berksfile_path}."
84
- end
85
-
86
- if !except.empty? && !only.empty?
87
- errors << "A value for berkshelf.empty and berkshelf.only cannot both be defined."
88
- end
89
-
90
- if global_provisioners(machine).any? { |prov| prov.name == :chef_client }
91
- if machine.config.berkshelf.node_name.nil?
92
- errors << "A configuration must be set node_name when using the chef_client provisioner." +
93
- " Edit your berkshelf configuration and add a value for chef.node_name."
94
- end
95
-
96
- if machine.config.berkshelf.client_key.nil?
97
- errors << "A configuration must be set for client_key when using the chef_client provisioner." +
98
- " Edit your berkshelf configuration and add a value for chef.client_key."
99
- end
100
- end
101
- end
102
-
103
- {"berkshelf configuration" => errors}
104
- end
105
-
106
- private
107
-
108
- def global_provisioners(machine)
109
- machine.env.vagrantfile.config.vm.provisioners
110
- end
111
- end
112
- end
113
- end
@@ -1,16 +0,0 @@
1
- module Berkshelf
2
- module Vagrant
3
- # Environment data to build up and persist through the middleware chain
4
- class Env
5
- # @return [Vagrant::UI::Colored]
6
- attr_accessor :ui
7
- # @return [String]
8
- attr_accessor :shelf
9
-
10
- def initialize
11
- @ui = ::Vagrant::UI::Colored.new
12
- @ui.opts[:target] = 'Berkshelf'
13
- end
14
- end
15
- end
16
- end
@@ -1,160 +0,0 @@
1
- require 'buff/shell_out'
2
- require 'json'
3
- require 'vagrant/util/which'
4
-
5
- module Berkshelf
6
- module Vagrant
7
- # A module of common helper functions that can be mixed into Berkshelf::Vagrant actions
8
- module EnvHelpers
9
- BERKS_CONSTRAINT = ">= 3.0.0"
10
-
11
- include Buff::ShellOut
12
- include ::Vagrant::Util
13
-
14
- # Execute a berkshelf command with the given arguments and flags.
15
- #
16
- # @overload berks(command, args)
17
- # @param [String] berks CLI command to run
18
- # @param [Object] any number of arguments to pass to CLI
19
- # @overload berks(command, args, options)
20
- # @param [String] berks CLI command to run
21
- # @param [Object] any number of arguments to pass to CLI
22
- # @param [Hash] options to convert to flags for the CLI
23
- #
24
- # @return [String]
25
- # output of the command
26
- #
27
- # @raise [UnsupportedBerksVersion]
28
- # version of Berks installed does not satisfy the application's constraint
29
- # @raise [BerksNotFound]
30
- # berks command is not found in the user's path
31
- # @raise [BerksError]
32
- # CLI command failed
33
- def berks(command, *args)
34
- if defined?(Bundler)
35
- Bundler.with_clean_env { run_berks(command, *args) }
36
- else
37
- run_berks(command, *args)
38
- end
39
- end
40
-
41
- def berksfile_path(env)
42
- env[:machine].env.vagrantfile.config.berkshelf.berksfile_path
43
- end
44
-
45
- # A file to persist vagrant-berkshelf specific information in between
46
- # Vagrant runs.
47
- #
48
- # @return [String]
49
- def cache_file(env)
50
- File.expand_path(File.join('.vagrant', 'machines', env[:machine].name.to_s, 'berkshelf'), env[:root_path].to_s)
51
- end
52
-
53
- # Filter all of the provisioners of the given vagrant environment with the given name
54
- #
55
- # @param [Symbol] name
56
- # name of provisioner to filter
57
- # @param [Vagrant::Environment, Hash] env
58
- # environment to inspect
59
- #
60
- # @return [Array]
61
- def provisioners(name, env)
62
- env[:machine].config.vm.provisioners.select { |prov| prov.name == name }
63
- end
64
-
65
- # Determine if the given vagrant environment contains a chef_solo provisioner
66
- #
67
- # @param [Vagrant::Environment] env
68
- #
69
- # @return [Boolean]
70
- def chef_solo?(env)
71
- provisioners(:chef_solo, env).any?
72
- end
73
-
74
- # Determine if the given vagrant environment contains a chef_client provisioner
75
- #
76
- # @param [Vagrant::Environment] env
77
- #
78
- # @return [Boolean]
79
- def chef_client?(env)
80
- provisioners(:chef_client, env).any?
81
- end
82
-
83
- # Determine if the Berkshelf plugin should be run for the given environment
84
- #
85
- # @param [Vagrant::Environment] env
86
- #
87
- # @return [Boolean]
88
- def berkshelf_enabled?(env)
89
- env[:machine].config.berkshelf.enabled
90
- end
91
-
92
- # Determine if --no-provision was specified
93
- #
94
- # @param [Vagrant::Environment] env
95
- #
96
- # @return [Boolean]
97
- def provision_disabled?(env)
98
- env.has_key?(:provision_enabled) && !env[:provision_enabled]
99
- end
100
-
101
- private
102
-
103
- def berks_version_check!
104
- if (exec = Which.which("berks")).nil?
105
- raise BerksNotFound
106
- end
107
-
108
- unless (response = shell_out("#{exec} version -F json")).success?
109
- raise "Couldn't determine Berks version: #{response.inspect}"
110
- end
111
-
112
- begin
113
- version = Gem::Version.new(JSON.parse(response.stdout)["version"])
114
- Gem::Requirement.new(BERKS_CONSTRAINT).satisfied_by?(version)
115
- exec
116
- rescue => ex
117
- raise UnsupportedBerksVersion.new(exec, BERKS_CONSTRAINT, version)
118
- end
119
- end
120
-
121
- def options_to_flags(opts)
122
- opts.map do |key, value|
123
- if value.is_a?(TrueClass)
124
- "--#{key_to_flag(key)}"
125
- next
126
- end
127
-
128
- if value.is_a?(FalseClass)
129
- "--no-#{key_to_flag(key)}"
130
- next
131
- end
132
-
133
- if value.is_a?(Array)
134
- "--#{key_to_flag(key)}=#{value.join(" ")}"
135
- next
136
- end
137
-
138
- "--#{key_to_flag(key)}=#{value}"
139
- end.join(" ")
140
- end
141
-
142
- def run_berks(command, *args)
143
- exec = berks_version_check!
144
- options = args.last.is_a?(Hash) ? args.pop : Hash.new
145
- arguments = args.join(" ")
146
- flags = options_to_flags(options)
147
-
148
- command = "#{exec} #{command} #{arguments} #{flags}"
149
- unless (response = shell_out(command)).success?
150
- raise BerksError.new("Berks command Failed: #{command}, reason: #{response.stderr}")
151
- end
152
- response.stdout
153
- end
154
-
155
- def key_to_flag(key)
156
- "#{key.to_s.gsub("_", "-")}"
157
- end
158
- end
159
- end
160
- end
@@ -1,71 +0,0 @@
1
- require 'vagrant/errors'
2
-
3
- module Berkshelf
4
- # A wrapper for a BerkshelfError for Vagrant. All Berkshelf exceptions should be
5
- # wrapped in this proxy object so they are properly handled when Vagrant encounters
6
- # an exception.
7
- #
8
- # @example wrapping an error encountered within the Vagrant plugin
9
- # rescue BerkshelfError => e
10
- # VagrantWrapperError.new(e)
11
- # end
12
- class VagrantWrapperError < ::Vagrant::Errors::VagrantError
13
- # @param [BerkshelfError]
14
- attr_reader :original
15
-
16
- # @param [BerkshelfError] original
17
- def initialize(original)
18
- @original = original
19
- end
20
-
21
- def to_s
22
- "#{original.class}: #{original.to_s}"
23
- end
24
-
25
- private
26
-
27
- def method_missing(fun, *args, &block)
28
- original.send(fun, *args, &block)
29
- end
30
- end
31
-
32
- class BerksError < ::Vagrant::Errors::VagrantError
33
- attr_reader :error_message
34
-
35
- def initialize(message)
36
- @error_message = message
37
- super
38
- end
39
- end
40
-
41
- class BerksNotFound < ::Vagrant::Errors::VagrantError
42
- def error_message
43
- "Berks not found. Download the ChefDK from http://downloads.getchef.com/chef-dk and add it to your $PATH."
44
- end
45
- end
46
-
47
- class UnsupportedBerksVersion < ::Vagrant::Errors::VagrantError
48
- def initialize(bin, constraint, version)
49
- @bin = bin
50
- @constraint = constraint
51
- @version = version
52
- super
53
- end
54
-
55
- def error_message
56
- "Unsupported Berkshelf version at: #{@bin}. Requires #{@constraint} and got #{@version}." +
57
- " Download the latest version of the ChefDK from http://downloads.getchef.com/chef-dk and add it to your $PATH."
58
- end
59
- end
60
-
61
- class UnsupportedVagrantVersion < ::Vagrant::Errors::VagrantError
62
- def initialize(constraint)
63
- @constraint = constraint
64
- super
65
- end
66
-
67
- def error_message
68
- "vagrant-berkshelf requires Vagrant #{@constraint}."
69
- end
70
- end
71
- end