vagrant-chefconfig 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 90f78ac71ef42cf38083250b82db72935520ccdb
4
+ data.tar.gz: 34c47dda0b741eead9d5805b86cab6c4ae783fb3
5
+ SHA512:
6
+ metadata.gz: 66fcd4941fdfd87c0d00ef9de9ae52e1e997b8c4e3f34fafe5f8e5a624873f75ef4d5aa8170b976a7bae9418a8a48f0d0687a4439b8ff5da27aeb31e9e36350d
7
+ data.tar.gz: f11577e2131f3214033a5a0777bc71d88a62cd4900f23b76b7e1e9f27c69579f6acb9a4ded5c5c7f09609e8752442431db779b8eb0a942a64d397921a7319a10
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
19
+ .vagrant/
20
+ .vagrant-chefconfig/
21
+ *.sublime-*
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ script: bundle exec rspec spec/
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vagrant-chefconfig.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ # We depend on Vagrant for development, but we don't add it as a
8
+ # gem dependency because we expect to be installed within the
9
+ # Vagrant environment itself using `vagrant plugin`.
10
+ gem "vagrant", :github => 'mitchellh/vagrant'
11
+ end
12
+
13
+ # This group is read by Vagrant for plugin development
14
+ group :plugins do
15
+ gem "vagrant-chefconfig", path: "."
16
+ end
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2014 Shawn Neal
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,58 @@
1
+ # Vagrant-ChefConfig
2
+
3
+ This [Vagrant](http://www.vagrantup.com/) plugin allows you to automatically configure all your Chef client provisioner blocks from your host's knife.rb. Just install the plugin and use the chef-client provisioners in Vagrant. That's it.
4
+
5
+ ## Installation
6
+
7
+ vagrant plugin install vagrant-chefconfig
8
+
9
+ ## Usage
10
+
11
+ In your Vagrantfile use the plugin like so:
12
+ ```ruby
13
+ Vagrant.configure('2') do |config|
14
+ config.vm.box = "precise64"
15
+ config.vm.provision :chef_client do |chef|
16
+ chef.node_name = 'vagrant-mydummytest'
17
+ chef.run_list = [ 'recipe[dummy::fail]' ]
18
+ end
19
+ end
20
+ ```
21
+
22
+ The plugin will automatically configure the following Vagrant chef-client provisioner attributes from your knife.rb.
23
+
24
+ Vagrant chef-client attribute -> knife config attribute
25
+ --------------------------------------------------------
26
+ * `chef_server_url` - same
27
+ * `log_level` - same
28
+ * `validation_key_path` - maps to `validation_key`
29
+ * `validation_client_name` - same
30
+ * `environment` - maps to `vagrant_environment`, this is a non-standard knife config key.
31
+ * `encrypted_data_bag_secret_key_path` - maps to `encrypted_data_bag_secret`
32
+
33
+ Values specified directly in the Vagrantfile override any configured values found in your knife configuration file.
34
+
35
+ ## Optional Configuration
36
+
37
+ By default the plugin will be enabled and the path to the knife.rb uses the [standard Knife configuration](http://docs.opscode.com/config_rb_knife.html) loading mechanism. You can override this behavior using the following optional plugin configuration options:
38
+
39
+ * chefconfig.enabled = false
40
+ * chefconfig.knife_config_path = '/my/nonstandard/path/knife.rb'
41
+
42
+ ## Changelog
43
+
44
+ ### 0.0.1
45
+
46
+ Initial release for Vagrant 1.5
47
+
48
+ # Authors
49
+
50
+ * Shawn Neal (<sneal@sneal.net>)
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant.configure('2') do |config|
5
+ config.vm.box = "opscode_ubuntu-12.04_chef-11.2.0"
6
+ config.vm.provision :chef_client do |chef|
7
+ chef.node_name = 'vagrant_chefconfig_test_vm'
8
+ chef.run_list = [ 'apt' ]
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ begin
2
+ require "vagrant"
3
+ rescue LoadError
4
+ raise "This plugin must run within Vagrant."
5
+ end
6
+
7
+ require 'vagrant-chefconfig/version'
8
+ require 'vagrant-chefconfig/errors'
9
+
10
+ module Vagrant
11
+ module ChefConfig
12
+ require_relative 'vagrant-chefconfig/config'
13
+ require_relative 'vagrant-chefconfig/action'
14
+ require_relative 'vagrant-chefconfig/env'
15
+ end
16
+ end
17
+
18
+ require 'vagrant-chefconfig/plugin'
@@ -0,0 +1,14 @@
1
+ module Vagrant
2
+ module ChefConfig
3
+ module Action
4
+ require_relative 'action/load_chef_config'
5
+
6
+ def self.load_chef_config
7
+ ::Vagrant::Action::Builder.new.tap do |b|
8
+ b.use Vagrant::ChefConfig::Action::LoadChefConfig
9
+ end
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../chef_client_configurator'
2
+
3
+ module Vagrant
4
+ module ChefConfig
5
+ module Action
6
+ class LoadChefConfig
7
+
8
+ def initialize(app, env)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ if env[:machine].config.chefconfig.enabled
14
+ ChefClientConfigurator.new(env).apply_knife_config()
15
+ end
16
+ @app.call(env)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,79 @@
1
+ require 'log4r'
2
+ require 'chef'
3
+ require 'chef/knife'
4
+
5
+ module Vagrant
6
+ module ChefConfig
7
+ class ChefClientConfigurator
8
+
9
+ def initialize(env)
10
+ @logger = Log4r::Logger.new("vagrant_chefconfig::chefclientconfigurator")
11
+ @env = env
12
+ end
13
+
14
+ def apply_knife_config()
15
+ load_knife_config()
16
+
17
+ chef_client_provisioners.each do |chef_client_provisioner|
18
+ chef_config = chef_client_provisioner.config
19
+
20
+ set_if_default(chef_config, :chef_server_url, :chef_server_url)
21
+ set_if_default(chef_config, :log_level, :log_level)
22
+ set_if_default(chef_config, :validation_key_path, :validation_key)
23
+ set_if_default(chef_config, :validation_client_name, :validation_client_name)
24
+ set_if_default(chef_config, :environment, :vagrant_environment)
25
+ set_if_default(chef_config, :encrypted_data_bag_secret_key_path, :encrypted_data_bag_secret)
26
+ end
27
+ end
28
+
29
+
30
+ private
31
+
32
+ def set_if_default(chef_config, config_prop_sym, chef_prop_sym)
33
+ config_val = chef_config.send(config_prop_sym)
34
+
35
+ @logger.debug("Vagrantfile config val #{config_prop_sym} = #{config_val}")
36
+ @logger.debug("Knife config val #{config_prop_sym} = #{Chef::Config[chef_prop_sym]}")
37
+
38
+ if is_default_value(config_prop_sym, config_val)
39
+ @logger.debug("Overwriting '#{config_prop_sym}' in Vagrantfile chef-client " +
40
+ "provisioner with '#{Chef::Config[chef_prop_sym]}'")
41
+ chef_config.send("#{config_prop_sym}=", Chef::Config[chef_prop_sym])
42
+ end
43
+ end
44
+
45
+ def is_default_value(config_prop_sym, config_val)
46
+ case config_prop_sym
47
+ when :validation_client_name
48
+ return config_val == 'chef-validator'
49
+ when :client_key_path
50
+ return config_val == '/etc/chef/client.pem'
51
+ else
52
+ return config_val.nil?
53
+ end
54
+ end
55
+
56
+ def chef_client_provisioners()
57
+ @env[:machine].config.vm.provisioners.select { |prov| prov.name == :chef_client }
58
+ end
59
+
60
+ def load_knife_config()
61
+ knife_config_path = locate_knife_config_file()
62
+ @logger.debug("Using knife config from '#{knife_config_path}'")
63
+ Chef::Config.from_file(knife_config_path)
64
+ end
65
+
66
+ def locate_knife_config_file()
67
+ # If the Vagrantfile directly sets the knife.rb path use it, otherwise let
68
+ # knife find its configuration file
69
+ knife_config_path = plugin_config().knife_config_path
70
+ knife_config_path ? knife_config_path : Chef::Knife.locate_config_file()
71
+ end
72
+
73
+ def plugin_config()
74
+ @env[:machine].config.chefconfig
75
+ end
76
+
77
+ end #ChefClientConfigurator
78
+ end
79
+ end
@@ -0,0 +1,31 @@
1
+ module Vagrant
2
+ module ChefConfig
3
+ class Config < ::Vagrant.plugin('2', :config)
4
+ attr_accessor :enabled
5
+ attr_accessor :knife_config_path
6
+
7
+ def initialize
8
+ super
9
+ @enabled = UNSET_VALUE
10
+ @knife_config_path = UNSET_VALUE
11
+ end
12
+
13
+ def finalize!
14
+ @enabled = true if @enabled == UNSET_VALUE
15
+ @knife_config_path = nil if @knife_config_path == UNSET_VALUE
16
+ end
17
+
18
+ def validate(machine)
19
+ errors =[]
20
+
21
+ if @enabled
22
+ if @knife_config_path && !File.exists?(@knife_config_path)
23
+ errors << "chefconfig.knife_config_path file '#{@knife_config_path}' not found"
24
+ end
25
+ end
26
+
27
+ { "chefconfig" => errors }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module Vagrant
2
+ module ChefConfig
3
+ class Env
4
+ attr_accessor :ui
5
+
6
+ def initialize
7
+ vagrant_version = Gem::Version.new(::Vagrant::VERSION)
8
+ if vagrant_version >= Gem::Version.new("1.5")
9
+ @ui = ::Vagrant::UI::Colored.new
10
+ @ui.opts[:target] = 'ChefConfig'
11
+ elsif vagrant_version >= Gem::Version.new("1.2")
12
+ @ui = ::Vagrant::UI::Colored.new.scope('ChefConfig')
13
+ else
14
+ @ui = ::Vagrant::UI::Colored.new('ChefConfig')
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require 'vagrant/errors'
2
+
3
+ module Vagrant
4
+ module ChefConfig
5
+ module Errors
6
+ class KnifeConfigNotFound < ::Vagrant::Errors::VagrantError
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,59 @@
1
+ module Vagrant
2
+ module ChefConfig
3
+ class Plugin < Vagrant.plugin('2')
4
+ name "vagrant-chefconfig"
5
+ description <<-DESC
6
+ Auto-load knife configuration data on Vagrant startup
7
+ DESC
8
+
9
+
10
+ action_hook(:vagrant_chefconfig_validate) do |hook|
11
+ hook.before(
12
+ ::Vagrant::Action::Builtin::ConfigValidate,
13
+ Vagrant::ChefConfig::Action.load_chef_config)
14
+ end
15
+
16
+ action_hook(:vagrant_chefconfig_provision) do |hook|
17
+ hook.before(
18
+ ::Vagrant::Action::Builtin::Provision,
19
+ Vagrant::ChefConfig::Action.load_chef_config)
20
+ end
21
+
22
+ config("chefconfig") do
23
+ Config
24
+ end
25
+
26
+ # This sets up our log level to be whatever VAGRANT_LOG is.
27
+ def self.setup_logging
28
+ require 'log4r'
29
+
30
+ level = nil
31
+ begin
32
+ level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
33
+ rescue NameError
34
+ # This means that the logging constant wasn't found,
35
+ # which is fine. We just keep `level` as `nil`. But
36
+ # we tell the user.
37
+ level = nil
38
+ end
39
+
40
+ # Some constants, such as "true" resolve to booleans, so the
41
+ # above error checking doesn't catch it. This will check to make
42
+ # sure that the log level is an integer, as Log4r requires.
43
+ level = nil if !level.is_a?(Integer)
44
+
45
+ # Set the logging level on all "vagrant" namespaced
46
+ # logs as long as we have a valid level.
47
+ if level
48
+ logger = Log4r::Logger.new("vagrant_chefconfig")
49
+ logger.outputters = Log4r::Outputter.stderr
50
+ logger.level = level
51
+ logger = nil
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+
59
+ Vagrant::ChefConfig::Plugin.setup_logging()
@@ -0,0 +1,5 @@
1
+ module Vagrant
2
+ module ChefConfig
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-chefconfig/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "vagrant-chefconfig"
8
+ gem.version = Vagrant::ChefConfig::VERSION
9
+ gem.authors = ["Shawn Neal"]
10
+ gem.email = ["sneal@sneal.net"]
11
+ gem.description = %q{Load Chef gem client config from knife.rb}
12
+ gem.summary = %q{Duplicating Chef client configuration is less than ideal, lets reuse our knife.rb configuration data}
13
+ gem.homepage = "https://github.com/sneal/vagrant-chefconfig"
14
+ gem.license = 'Apache2'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency "chef", "~> 11.8.2"
22
+
23
+ gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "rspec"
25
+ gem.add_development_dependency "bundler", ">= 1.3"
26
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-chefconfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shawn Neal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 11.8.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 11.8.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description: Load Chef gem client config from knife.rb
70
+ email:
71
+ - sneal@sneal.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - Vagrantfile
83
+ - lib/vagrant-chefconfig.rb
84
+ - lib/vagrant-chefconfig/action.rb
85
+ - lib/vagrant-chefconfig/action/load_chef_config.rb
86
+ - lib/vagrant-chefconfig/chef_client_configurator.rb
87
+ - lib/vagrant-chefconfig/config.rb
88
+ - lib/vagrant-chefconfig/env.rb
89
+ - lib/vagrant-chefconfig/errors.rb
90
+ - lib/vagrant-chefconfig/plugin.rb
91
+ - lib/vagrant-chefconfig/version.rb
92
+ - vagrant-chefconfig.gemspec
93
+ homepage: https://github.com/sneal/vagrant-chefconfig
94
+ licenses:
95
+ - Apache2
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.0.14
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Duplicating Chef client configuration is less than ideal, lets reuse our
117
+ knife.rb configuration data
118
+ test_files: []