diff --git a/lib/vagrant-hiera.rb b/lib/vagrant-hiera.rb index 643bcaf..e765db5 100644 --- a/lib/vagrant-hiera.rb +++ b/lib/vagrant-hiera.rb @@ -1,12 +1,6 @@ require 'vagrant' require 'vagrant-hiera/middleware/prepare' require 'vagrant-hiera/middleware/setup' -require 'vagrant-hiera/config' - - -Vagrant.config_keys.register(:hiera) { VagrantHiera::Config } - -Vagrant.actions[:start].insert_after Vagrant::Action::VM::ShareFolders, VagrantHiera::Middleware::Prepare -Vagrant.actions[:start].use VagrantHiera::Middleware::Setup +require 'vagrant-hiera/plugin' I18n.load_path << File.expand_path("../../locales/en.yml", __FILE__) diff --git a/lib/vagrant-hiera/config.rb b/lib/vagrant-hiera/config.rb index 88afcc5..31c119a 100644 --- a/lib/vagrant-hiera/config.rb +++ b/lib/vagrant-hiera/config.rb @@ -1,6 +1,7 @@ +require 'vagrant' module VagrantHiera - class Config < Vagrant::Config::Base + class Config < Vagrant.plugin(2, :config) attr_accessor :config_path attr_accessor :guest_config_path attr_accessor :config_file @@ -13,24 +14,33 @@ class Config < Vagrant::Config::Base attr_accessor :hiera_puppet_version attr_accessor :hiera_version + #name "vagrant-hiera" + #def initialize + # @widgets = UNSET_VALUE + #end + + #def finalize! + # @widgets = 0 if @widgets == UNSET_VALUE + #end + def guest_config_path - @guest_config_path.nil? ? (@guest_config_path = '/tmp/vagrant-hiera/config') : @guest_config_path + @guest_config_path ||= '/tmp/vagrant-hiera/config' end def guest_data_path - @guest_data_path.nil? ? (@guest_data_path = '/tmp/vagrant-hiera/data') : @guest_data_path + @guest_data_path ||= '/tmp/vagrant-hiera/data' end def puppet_apt_source - @puppet_apt_source.nil? ? (@puppet_apt_source = 'deb http://apt.puppetlabs.com/ stable main') : @puppet_apt_source + @puppet_apt_source ||= 'deb http://apt.puppetlabs.com/ stable main' end def apt_opts - @apt_opts.nil? ? (@apt_opts = '-y --force-yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"') : @apt_opts + @apt_opts ||= '-y --force-yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"' end def puppet_version - @puppet_version.nil? ? (@puppet_version = '3.0.0-1puppetlabs1') : @puppet_version + @puppet_version ||= '3.0.0-1puppetlabs1' end def hiera_puppet_version @@ -38,7 +48,7 @@ def hiera_puppet_version end def hiera_version - @hiera_version.nil? ? (@hiera_version = '1.0.0-1puppetlabs2') : @hiera_version + @hiera_version ||= '1.0.0-1puppetlabs2' end def config_path @@ -57,19 +67,20 @@ def install_puppet_heira? puppet_version.to_i < 3 end - def validate(env, errors) + def validate(machine) return unless set? + errors = _detected_errors - errors.add("Config path can not be empty.") if config_path.nil? - errors.add("Config file can not be empty.") if config_file.nil? - errors.add("Data path can not be empty.") if data_path.nil? - errors.add("Puppet version path can not be empty.") if puppet_version.nil? - errors.add("Puppet apt source can not be empty.") if puppet_apt_source.nil? - errors.add("Hiera puppet version can not be empty if puppet_version < 3.0.") if install_puppet_heira? && hiera_puppet_version.nil? - errors.add("Hiera version can not be empty.") if hiera_version.nil? + errors << "Config path can not be empty." if config_path.nil? + errors << "Config file can not be empty." if config_file.nil? + errors << "Data path can not be empty." if data_path.nil? + errors << "Puppet version path can not be empty." if puppet_version.nil? + errors << "Puppet apt source can not be empty." if puppet_apt_source.nil? + errors << "Hiera puppet version can not be empty if puppet_version < 3.0." if install_puppet_heira? && hiera_puppet_version.nil? + errors << "Hiera version can not be empty." if hiera_version.nil? config = File.join("#{config_path}", "#{config_file}") - errors.add("Config file not found at '#{config}'.") unless File.exists?(config) - errors.add("Data directory not found at '#{data_path}'.") unless File.exists?("#{data_path}") + errors << "Config file not found at '#{config}'." unless File.exists?(config) + errors << "Data directory not found at '#{data_path}'." unless File.exists?("#{data_path}") end end end diff --git a/lib/vagrant-hiera/middleware/prepare.rb b/lib/vagrant-hiera/middleware/prepare.rb index 8938172..20cc430 100644 --- a/lib/vagrant-hiera/middleware/prepare.rb +++ b/lib/vagrant-hiera/middleware/prepare.rb @@ -9,7 +9,7 @@ def initialize(app, env) def call(env) @env = env - if @env[:vm].config.hiera.set? + if @env[:machine].config.hiera.set? create_shared_folders end @app.call(env) @@ -17,9 +17,9 @@ def call(env) def create_shared_folders @env[:ui].info I18n.t('vagrant.plugins.hiera.middleware.prepare.shared_folders') - folders = [{:name => 'vagrant-hiera-config', :hostpath => @env[:vm].config.hiera.config_path}, - {:name => 'vagrant-hiera-data', :hostpath => @env[:vm].config.hiera.data_path}] - @env[:vm].driver.share_folders(folders) + folders = [{:name => 'vagrant-hiera-config', :hostpath => @env[:machine].config.hiera.config_path}, + {:name => 'vagrant-hiera-data', :hostpath => @env[:machine].config.hiera.data_path}] + @env[:machine].provider.driver.share_folders(folders) end end diff --git a/lib/vagrant-hiera/middleware/setup.rb b/lib/vagrant-hiera/middleware/setup.rb index 1d5c8f5..f932b80 100644 --- a/lib/vagrant-hiera/middleware/setup.rb +++ b/lib/vagrant-hiera/middleware/setup.rb @@ -8,25 +8,25 @@ def initialize(app, env) @app = app @env = env - @guest_config_path = @env[:vm].config.hiera.guest_config_path - @guest_data_path = @env[:vm].config.hiera.guest_data_path - @config_path = @env[:vm].config.hiera.config_path - @data_path = @env[:vm].config.hiera.data_path - @config_file = @env[:vm].config.hiera.config_file - @puppet_version = @env[:vm].config.hiera.puppet_version - @puppet_apt_source = @env[:vm].config.hiera.puppet_apt_source - @apt_opts = @env[:vm].config.hiera.apt_opts - @hiera_puppet_version = @env[:vm].config.hiera.hiera_puppet_version - @hiera_version = @env[:vm].config.hiera.hiera_version + @guest_config_path = @env[:machine].config.hiera.guest_config_path + @guest_data_path = @env[:machine].config.hiera.guest_data_path + @config_path = @env[:machine].config.hiera.config_path + @data_path = @env[:machine].config.hiera.data_path + @config_file = @env[:machine].config.hiera.config_file + @puppet_version = @env[:machine].config.hiera.puppet_version + @puppet_apt_source = @env[:machine].config.hiera.puppet_apt_source + @apt_opts = @env[:machine].config.hiera.apt_opts + @hiera_puppet_version = @env[:machine].config.hiera.hiera_puppet_version + @hiera_version = @env[:machine].config.hiera.hiera_version end def call(env) @env = env - if @env[:vm].config.hiera.set? + if @env[:machine].config.hiera.set? add_apt_repo unless apt_repo_set? install_puppet unless puppet_installed? install_hiera unless hiera_installed? - if @env[:vm].config.hiera.install_puppet_heira? + if @env[:machine].config.hiera.install_puppet_heira? install_hiera_puppet unless hiera_puppet_installed? end create_shared_folders @@ -37,69 +37,69 @@ def call(env) def apt_repo_set? cmd = "test -f #{APT_SOURCE_FILE} && grep '#{@puppet_apt_source}' #{APT_SOURCE_FILE}" - setup = ( @env[:vm].channel.execute(cmd, :error_check => false) == 0 ) + setup = ( @env[:machine].channel.execute(cmd, :error_check => false) == 0 ) @env[:ui].success I18n.t('vagrant.plugins.hiera.middleware.setup.apt_repo_set') if setup setup end def add_apt_repo @env[:ui].warn I18n.t('vagrant.plugins.hiera.middleware.setup.add_apt_repo') - @env[:vm].channel.sudo("wget http://apt.puppetlabs.com/puppetlabs-release-stable.deb") - @env[:vm].channel.sudo("dpkg -i puppetlabs-release-stable.deb") - @env[:vm].channel.sudo("echo '#{@puppet_apt_source}' >> #{APT_SOURCE_FILE}") - @env[:vm].channel.sudo("apt-get update") + @env[:machine].channel.sudo("wget http://apt.puppetlabs.com/puppetlabs-release-stable.deb") + @env[:machine].channel.sudo("dpkg -i puppetlabs-release-stable.deb") + @env[:machine].channel.sudo("echo '#{@puppet_apt_source}' >> #{APT_SOURCE_FILE}") + @env[:machine].channel.sudo("apt-get update") end def hiera_installed? - installed = ( @env[:vm].channel.execute("dpkg -l | grep puppet | grep #{@puppet_version}", :error_check => false) == 0 ) + installed = ( @env[:machine].channel.execute("dpkg -l | grep puppet | grep #{@puppet_version}", :error_check => false) == 0 ) @env[:ui].success I18n.t('vagrant.plugins.hiera.middleware.setup.hiera_installed') if installed installed end def install_hiera @env[:ui].warn I18n.t('vagrant.plugins.hiera.middleware.setup.install_hiera') - @env[:vm].channel.sudo("apt-get #{@apt_opts} install hiera=#{@hiera_version}") + @env[:machine].channel.sudo("apt-get #{@apt_opts} install hiera=#{@hiera_version}") end def puppet_installed? - installed = ( @env[:vm].channel.execute("dpkg -l | grep puppet | grep #{@puppet_version}", :error_check => false) == 0 ) + installed = ( @env[:machine].channel.execute("dpkg -l | grep puppet | grep #{@puppet_version}", :error_check => false) == 0 ) @env[:ui].success I18n.t('vagrant.plugins.hiera.middleware.setup.puppet_installed') if installed installed end def install_puppet @env[:ui].warn I18n.t('vagrant.plugins.hiera.middleware.setup.install_puppet') - @env[:vm].channel.sudo("apt-get #{@apt_opts} install puppet-common=#{@puppet_version}") - @env[:vm].channel.sudo("apt-get #{@apt_opts} install puppet=#{@puppet_version}") + @env[:machine].channel.sudo("apt-get #{@apt_opts} install puppet-common=#{@puppet_version}") + @env[:machine].channel.sudo("apt-get #{@apt_opts} install puppet=#{@puppet_version}") end def hiera_puppet_installed? - installed = ( @env[:vm].channel.execute("dpkg -l | grep hiera-puppet | grep #{@hiera_puppet_version}", :error_check => false) == 0 ) + installed = ( @env[:machine].channel.execute("dpkg -l | grep hiera-puppet | grep #{@hiera_puppet_version}", :error_check => false) == 0 ) @env[:ui].success I18n.t('vagrant.plugins.hiera.middleware.setup.hiera_puppet_installed') if installed installed end def install_hiera_puppet @env[:ui].warn I18n.t('vagrant.plugins.hiera.middleware.setup.install_hiera_puppet') - @env[:vm].channel.sudo("apt-get #{@apt_opts} install hiera-puppet=#{@hiera_puppet_version}") + @env[:machine].channel.sudo("apt-get #{@apt_opts} install hiera-puppet=#{@hiera_puppet_version}") end def create_shared_folders @env[:ui].info I18n.t('vagrant.plugins.hiera.middleware.setup.shared_folders') data = {} - data[:owner] ||= @env[:vm].config.ssh.username - data[:group] ||= @env[:vm].config.ssh.username - @env[:vm].guest.mount_shared_folder('vagrant-hiera-config', @guest_config_path, data) - @env[:vm].guest.mount_shared_folder('vagrant-hiera-data', @guest_data_path, data) + data[:owner] ||= @env[:machine].config.ssh.username + data[:group] ||= @env[:machine].config.ssh.username + @env[:machine].guest.mount_shared_folder('vagrant-hiera-config', @guest_config_path, data) + @env[:machine].guest.mount_shared_folder('vagrant-hiera-data', @guest_data_path, data) end def create_symlink_to_hiera_config @env[:ui].info I18n.t('vagrant.plugins.hiera.middleware.setup.installing_hiera_config') - @env[:vm].channel.sudo("mkdir -p /etc/puppet") + @env[:machine].channel.sudo("mkdir -p /etc/puppet") # This is where I think this file will end up once the official puppet v3 release is out - @env[:vm].channel.sudo("ln -fs #{@guest_config_path}/#{@config_file} /etc/hiera.yaml") + @env[:machine].channel.sudo("ln -fs #{@guest_config_path}/#{@config_file} /etc/hiera.yaml") # But this is where it looks for it now - @env[:vm].channel.sudo("ln -fs #{@guest_config_path}/#{@config_file} /etc/puppet/hiera.yaml") + @env[:machine].channel.sudo("ln -fs #{@guest_config_path}/#{@config_file} /etc/puppet/hiera.yaml") end end end diff --git a/lib/vagrant-hiera/plugin.rb b/lib/vagrant-hiera/plugin.rb new file mode 100644 index 0000000..29248c9 --- /dev/null +++ b/lib/vagrant-hiera/plugin.rb @@ -0,0 +1,20 @@ +require 'vagrant' +require 'vagrant-hiera/config' + +module VagrantHiera + class Plugin < Vagrant.plugin("2") + name "vagrant-hiera" + + config 'hiera' do + VagrantHiera::Config + end + + action_hook 'vagrant-hiera' do |hook| + hook.after Vagrant::Action::Builtin::NFS, VagrantHiera::Middleware::Prepare + end + + ::Vagrant::Action::Builder.new.tap do |b| + b.use VagrantHiera::Middleware::Setup + end + end +end