From 66ff86fda563320bdcdf57796fa9db5bef81e66f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 8 Apr 2013 15:30:41 -0700 Subject: [PATCH] Auto-detect policy server IP for CFEngine --- lib/vagrant/errors.rb | 4 ++++ plugins/guests/linux/cap/read_ip_address.rb | 17 +++++++++++++++++ plugins/guests/linux/plugin.rb | 5 +++++ plugins/provisioners/cfengine/config.rb | 4 +++- plugins/provisioners/cfengine/provisioner.rb | 5 +++++ templates/locales/en.yml | 7 +++++++ 6 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 plugins/guests/linux/cap/read_ip_address.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index b5c90713b..1fbfd4c3f 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -139,6 +139,10 @@ module Vagrant error_key(:cfengine_bootstrap_failed) end + class CFEngineCantAutodetectIP < VagrantError + error_key(:cfengine_cant_autodetect_ip) + end + class CFEngineInstallFailed < VagrantError error_key(:cfengine_install_failed) end diff --git a/plugins/guests/linux/cap/read_ip_address.rb b/plugins/guests/linux/cap/read_ip_address.rb new file mode 100644 index 000000000..0eaffccbf --- /dev/null +++ b/plugins/guests/linux/cap/read_ip_address.rb @@ -0,0 +1,17 @@ +module VagrantPlugins + module GuestLinux + module Cap + class ReadIPAddress + def self.read_ip_address(machine) + command = "ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'" + result = "" + machine.communicate.execute(command) do |type, data| + result << data if type == :stdout + end + + result.chomp + end + end + end + end +end diff --git a/plugins/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index f9531bfa9..c3529faad 100644 --- a/plugins/guests/linux/plugin.rb +++ b/plugins/guests/linux/plugin.rb @@ -30,6 +30,11 @@ module VagrantPlugins require_relative "cap/mount_virtualbox_shared_folder" Cap::MountVirtualBoxSharedFolder end + + guest_capability("linux", "read_ip_address") do + require_relative "cap/read_ip_address" + Cap::ReadIPAddress + end end end end diff --git a/plugins/provisioners/cfengine/config.rb b/plugins/provisioners/cfengine/config.rb index cb218e3a9..9ecff81dd 100644 --- a/plugins/provisioners/cfengine/config.rb +++ b/plugins/provisioners/cfengine/config.rb @@ -90,7 +90,9 @@ module VagrantPlugins errors << I18n.t("vagrant.cfengine_config.invalid_mode") if !valid_modes.include?(@mode) if @mode == :bootstrap - errors << I18n.t("vagrant.cfengine_config.policy_server_address") if !@policy_server_address + if !@policy_server_address && !@am_policy_hub + errors << I18n.t("vagrant.cfengine_config.policy_server_address") + end end if @classes && !@classes.is_a?(Array) diff --git a/plugins/provisioners/cfengine/provisioner.rb b/plugins/provisioners/cfengine/provisioner.rb index 53b13b793..614289734 100644 --- a/plugins/provisioners/cfengine/provisioner.rb +++ b/plugins/provisioners/cfengine/provisioner.rb @@ -79,6 +79,11 @@ module VagrantPlugins # Needs bootstrap, let's determine the parameters policy_server_address = @config.policy_server_address + if !policy_server_address + policy_server_address = @machine.guest.capability(:read_ip_address) + raise Vagrant::Errors::CFEngineCantAutodetectIP if !policy_server_address + @machine.ui.info(I18n.t("vagrant.cfengine_detected_ip", address: policy_server_address)) + end @machine.ui.info(I18n.t("vagrant.cfengine_bootstrapping", policy_server: policy_server_address)) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 25201b633..f6015ce41 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -4,10 +4,17 @@ en: Bootstrapping CFEngine with policy server: %{policy_server}... cfengine_bootstrapping_policy_hub: |- Performing additional bootstrap for policy hubs... + cfengine_cant_autodetect_ip: |- + Vagrant was unable to automatically detect the IP address of the + running machine to use as the policy server address. Please specify + the policy server address manually, or verify that the networks are + configured properly internally. 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_detected_ip: |- + Detected policy server IP address: %{address}... cfengine_installing: |- Installing CFEngine onto machine... cfengine_installing_files_path: |-