From 9599d8a45daeac71d7aadf5b0d0c82e378565db1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 8 Apr 2013 13:05:53 -0700 Subject: [PATCH] Starting work on CFEngine, installs on Debian --- lib/vagrant/errors.rb | 8 +++++ .../cfengine/cap/debian/cfengine_install.rb | 19 ++++++++++ .../cfengine/cap/linux/cfengine_installed.rb | 14 ++++++++ plugins/provisioners/cfengine/config.rb | 36 +++++++++++++++++++ plugins/provisioners/cfengine/plugin.rb | 32 +++++++++++++++++ plugins/provisioners/cfengine/provisioner.rb | 35 ++++++++++++++++++ templates/locales/en.yml | 17 +++++++++ 7 files changed, 161 insertions(+) create mode 100644 plugins/provisioners/cfengine/cap/debian/cfengine_install.rb create mode 100644 plugins/provisioners/cfengine/cap/linux/cfengine_installed.rb create mode 100644 plugins/provisioners/cfengine/config.rb create mode 100644 plugins/provisioners/cfengine/plugin.rb create mode 100644 plugins/provisioners/cfengine/provisioner.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index fa5cd4131..e0fd363d7 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -135,6 +135,14 @@ module Vagrant error_key(:failed, "vagrant.actions.box.verify") end + class CFEngineInstallFailed < VagrantError + error_key(:cfengine_install_failed) + end + + class CFEngineNotInstalled < VagrantError + error_key(:cfengine_not_installed) + end + class CLIInvalidUsage < VagrantError error_key(:cli_invalid_usage) end diff --git a/plugins/provisioners/cfengine/cap/debian/cfengine_install.rb b/plugins/provisioners/cfengine/cap/debian/cfengine_install.rb new file mode 100644 index 000000000..f57522abf --- /dev/null +++ b/plugins/provisioners/cfengine/cap/debian/cfengine_install.rb @@ -0,0 +1,19 @@ +module VagrantPlugins + module CFEngine + module Cap + module Debian + module CFEngineInstall + def self.cfengine_install(machine, config) + machine.communicate.tap do |comm| + comm.sudo("mkdir -p #{File.dirname(config.deb_repo_file)} && /bin/echo #{config.deb_repo_line} > #{config.deb_repo_file}") + comm.sudo("GPGFILE=`tempfile`; wget -O $GPGFILE #{config.repo_gpg_key_url} && apt-key add $GPGFILE; rm -f $GPGFILE") + + comm.sudo("apt-get update") + comm.sudo("apt-get install -y cfengine-community") + end + end + end + end + end + end +end diff --git a/plugins/provisioners/cfengine/cap/linux/cfengine_installed.rb b/plugins/provisioners/cfengine/cap/linux/cfengine_installed.rb new file mode 100644 index 000000000..002df4af3 --- /dev/null +++ b/plugins/provisioners/cfengine/cap/linux/cfengine_installed.rb @@ -0,0 +1,14 @@ +module VagrantPlugins + module CFEngine + module Cap + module Linux + module CFEngineInstalled + def self.cfengine_installed(machine) + machine.communicate.test( + "test -d /var/cfengine && test -x /var/cfengine/bin/cf-agent", sudo: true) + end + end + end + end + end +end diff --git a/plugins/provisioners/cfengine/config.rb b/plugins/provisioners/cfengine/config.rb new file mode 100644 index 000000000..6257f1bca --- /dev/null +++ b/plugins/provisioners/cfengine/config.rb @@ -0,0 +1,36 @@ +require "vagrant" + +module VagrantPlugins + module CFEngine + class Config < Vagrant.plugin("2", :config) + attr_accessor :install + attr_accessor :deb_repo_file + attr_accessor :deb_repo_line + attr_accessor :repo_gpg_key_url + + def initialize + @deb_repo_file = UNSET_VALUE + @deb_repo_line = UNSET_VALUE + @install = UNSET_VALUE + @repo_gpg_key_url = UNSET_VALUE + end + + def finalize! + if @deb_repo_file == UNSET_VALUE + @deb_repo_file = "/etc/apt/sources.list.d/cfengine-community.list" + end + + if @deb_repo_line == UNSET_VALUE + @deb_repo_line = "deb http://cfengine.com/pub/apt $(lsb_release -cs) main" + end + + @install = true if @install == UNSET_VALUE + @install = @install.to_sym if @install.respond_to?(:to_sym) + + if @repo_gpg_key_url == UNSET_VALUE + @repo_gpg_key_url = "http://cfengine.com/pub/gpg.key" + end + end + end + end +end diff --git a/plugins/provisioners/cfengine/plugin.rb b/plugins/provisioners/cfengine/plugin.rb new file mode 100644 index 000000000..8e5a21609 --- /dev/null +++ b/plugins/provisioners/cfengine/plugin.rb @@ -0,0 +1,32 @@ +require "vagrant" + +module VagrantPlugins + module CFEngine + class Plugin < Vagrant.plugin("2") + name "CFEngine Provisioner" + description <<-DESC + Provisions machines with CFEngine. + DESC + + config(:cfengine, :provisioner) do + require_relative "config" + Config + end + + guest_capability("debian", "cfengine_install") do + require_relative "cap/debian/cfengine_install" + Cap::Debian::CFEngineInstall + end + + guest_capability("linux", "cfengine_installed") do + require_relative "cap/linux/cfengine_installed" + Cap::Linux::CFEngineInstalled + end + + provisioner(:cfengine) do + require_relative "provisioner" + Provisioner + end + end + end +end diff --git a/plugins/provisioners/cfengine/provisioner.rb b/plugins/provisioners/cfengine/provisioner.rb new file mode 100644 index 000000000..00e683b7d --- /dev/null +++ b/plugins/provisioners/cfengine/provisioner.rb @@ -0,0 +1,35 @@ +require "vagrant" + +module VagrantPlugins + module CFEngine + class Provisioner < Vagrant.plugin("2", :provisioner) + def provision + handle_cfengine_installation + end + + protected + + # This handles verifying the CFEngine installation, installing it + # if it was requested, and so on. This method will raise exceptions + # if things are wrong. + def handle_cfengine_installation + if !@machine.guest.capability?(:cfengine_installed) + @machine.ui.warn(I18n.t("vagrant.cfengine_cant_detect")) + return + end + + installed = @machine.guest.capability(:cfengine_installed) + if !installed || @config.install == :force + raise Vagrant::Errors::CFEngineNotInstalled if !@config.install + + @machine.ui.info(I18n.t("vagrant.cfengine_installing")) + @machine.guest.capability(:cfengine_install, @config) + + if !@machine.guest.capability(:cfengine_installed) + raise Vagrant::Errors::CFEngineInstallFailed + end + end + end + end + end +end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 7038c5786..4bcfbe15b 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1,5 +1,12 @@ en: vagrant: + cfengine_cant_detect: |- + Vagrant doesn't support detecting whether CFEngine is installed + for the guest OS running in the machine. Vagrant will assume it is + installed and attempt to continue. + cfengine_installing: |- + Installing CFEngine onto machine... + general: batch_unexpected_error: |- An unexpected error ocurred when executing the action on the @@ -99,6 +106,16 @@ en: The box '%{name}' is still stored on disk in the Vagrant 1.0.x format. This box must be upgraded in order to work properly with this version of Vagrant. + cfengine_install_failed: |- + After installing CFEngine, Vagrant still can't detect a proper + CFEngine installation. Please verify that CFEngine was properly + installed, as the installation may have failed. + cfengine_not_installed: |- + CFEngine appears to not be installed on the guest machine. Vagrant + can attempt to install CFEngine for you if you set the "install" + setting to "true", but this setting was not set. Please install + CFEngine on the guest machine or enable the "install" setting for + Vagrant to attempt to install it for you. cli_invalid_options: |- An invalid option was specified. The help for this command is available below.