From 875c2edc62fbd76d7ae2e6d2f65960a390e661fc Mon Sep 17 00:00:00 2001 From: Artem Sidorenko Date: Wed, 14 Jun 2017 12:34:52 +0200 Subject: [PATCH] Add config option omnibus_url for chef provisioners This option is useful for internal setups, where own customized omnibus installation script is used (e.g. to get chef from a mirror) --- .../chef/cap/debian/chef_install.rb | 4 ++-- .../chef/cap/freebsd/chef_install.rb | 4 ++-- .../chef/cap/omnios/chef_install.rb | 4 ++-- .../chef/cap/redhat/chef_install.rb | 4 ++-- .../chef/cap/suse/chef_install.rb | 4 ++-- .../chef/cap/windows/chef_install.rb | 4 ++-- plugins/provisioners/chef/config/base.rb | 17 +++++++++++++ plugins/provisioners/chef/installer.rb | 15 ++++++------ plugins/provisioners/chef/omnibus.rb | 10 ++++---- plugins/provisioners/chef/provisioner/base.rb | 1 + .../provisioners/chef/config/base_test.rb | 13 ++++++++++ .../plugins/provisioners/chef/omnibus_test.rb | 24 +++++++++++++------ .../docs/provisioning/chef_common.html.md | 12 +++++++++- 13 files changed, 83 insertions(+), 33 deletions(-) diff --git a/plugins/provisioners/chef/cap/debian/chef_install.rb b/plugins/provisioners/chef/cap/debian/chef_install.rb index e1cf9470d..9e911513d 100644 --- a/plugins/provisioners/chef/cap/debian/chef_install.rb +++ b/plugins/provisioners/chef/cap/debian/chef_install.rb @@ -5,11 +5,11 @@ module VagrantPlugins module Cap module Debian module ChefInstall - def self.chef_install(machine, project, version, channel, options = {}) + def self.chef_install(machine, project, version, channel, omnibus_url, options = {}) machine.communicate.sudo("apt-get update -y -qq") machine.communicate.sudo("apt-get install -y -qq curl") - command = Omnibus.sh_command(project, version, channel, options) + command = Omnibus.sh_command(project, version, channel, omnibus_url, options) machine.communicate.sudo(command) end end diff --git a/plugins/provisioners/chef/cap/freebsd/chef_install.rb b/plugins/provisioners/chef/cap/freebsd/chef_install.rb index d763f44c6..378a5ae39 100644 --- a/plugins/provisioners/chef/cap/freebsd/chef_install.rb +++ b/plugins/provisioners/chef/cap/freebsd/chef_install.rb @@ -5,10 +5,10 @@ module VagrantPlugins module Cap module FreeBSD module ChefInstall - def self.chef_install(machine, project, version, channel, options = {}) + def self.chef_install(machine, project, version, channel, omnibus_url, options = {}) machine.communicate.sudo("pkg install -y -qq curl bash") - command = Omnibus.sh_command(project, version, channel, options) + command = Omnibus.sh_command(project, version, channel, omnibus_url, options) machine.communicate.sudo(command) end end diff --git a/plugins/provisioners/chef/cap/omnios/chef_install.rb b/plugins/provisioners/chef/cap/omnios/chef_install.rb index 034b2ff5a..608f5faf9 100644 --- a/plugins/provisioners/chef/cap/omnios/chef_install.rb +++ b/plugins/provisioners/chef/cap/omnios/chef_install.rb @@ -5,12 +5,12 @@ module VagrantPlugins module Cap module OmniOS module ChefInstall - def self.chef_install(machine, project, version, channel, options = {}) + def self.chef_install(machine, project, version, channel, omnibus_url, options = {}) su = machine.config.solaris.suexec_cmd machine.communicate.execute("#{su} pkg list --no-refresh web/curl > /dev/null 2>&1 || pkg install -q --accept web/curl") - command = Omnibus.sh_command(project, version, channel, options) + command = Omnibus.sh_command(project, version, channel, omnibus_url, options) machine.communicate.execute("#{su} #{command}") end end diff --git a/plugins/provisioners/chef/cap/redhat/chef_install.rb b/plugins/provisioners/chef/cap/redhat/chef_install.rb index 7e72a902e..26c7a034d 100644 --- a/plugins/provisioners/chef/cap/redhat/chef_install.rb +++ b/plugins/provisioners/chef/cap/redhat/chef_install.rb @@ -5,7 +5,7 @@ module VagrantPlugins module Cap module Redhat module ChefInstall - def self.chef_install(machine, project, version, channel, options = {}) + def self.chef_install(machine, project, version, channel, omnibus_url, options = {}) machine.communicate.sudo <<-EOH.gsub(/^ {14}/, '') if command -v dnf; then dnf -y install curl @@ -14,7 +14,7 @@ module VagrantPlugins fi EOH - command = Omnibus.sh_command(project, version, channel, options) + command = Omnibus.sh_command(project, version, channel, omnibus_url, options) machine.communicate.sudo(command) end end diff --git a/plugins/provisioners/chef/cap/suse/chef_install.rb b/plugins/provisioners/chef/cap/suse/chef_install.rb index c6cedcefe..888727099 100644 --- a/plugins/provisioners/chef/cap/suse/chef_install.rb +++ b/plugins/provisioners/chef/cap/suse/chef_install.rb @@ -5,13 +5,13 @@ module VagrantPlugins module Cap module Suse module ChefInstall - def self.chef_install(machine, project, version, channel, options = {}) + def self.chef_install(machine, project, version, channel, omnibus_url, options = {}) unless curl?(machine) machine.communicate.sudo("zypper -n -q update") machine.communicate.sudo("zypper -n -q install curl") end - command = Omnibus.sh_command(project, version, channel, options) + command = Omnibus.sh_command(project, version, channel, omnibus_url, options) machine.communicate.sudo(command) end diff --git a/plugins/provisioners/chef/cap/windows/chef_install.rb b/plugins/provisioners/chef/cap/windows/chef_install.rb index f1f2317a6..7e0a0b42f 100644 --- a/plugins/provisioners/chef/cap/windows/chef_install.rb +++ b/plugins/provisioners/chef/cap/windows/chef_install.rb @@ -5,8 +5,8 @@ module VagrantPlugins module Cap module Windows module ChefInstall - def self.chef_install(machine, project, version, channel, options = {}) - command = Omnibus.ps_command(project, version, channel, options) + def self.chef_install(machine, project, version, channel, omnibus_url, options = {}) + command = Omnibus.ps_command(project, version, channel, omnibus_url, options) machine.communicate.sudo(command) end end diff --git a/plugins/provisioners/chef/config/base.rb b/plugins/provisioners/chef/config/base.rb index 9d033ecf4..74780268a 100644 --- a/plugins/provisioners/chef/config/base.rb +++ b/plugins/provisioners/chef/config/base.rb @@ -56,6 +56,21 @@ module VagrantPlugins # @return [String] attr_accessor :version + # Location of Omnibus installation scripts. + # This URL specifies the location of install.sh/install.ps1 for + # Linux/Unix and Windows respectively. + # + # It defaults to https://omnitruck.chef.io/. The full URL is then: + # - Linux/Unix: https://omnitruck.chef.io/install.sh + # - Windows: https://omnitruck.chef.io/install.ps1 + # + # If you want to have https://example.com/install.sh as Omnibus script + # for your Linux/Unix installations, you should set this option to + # https://example.com + # + # @return [String] + attr_accessor :omnibus_url + # The path where the Chef installer will be downloaded to. Only valid if # install is true or "force". It defaults to nil, which means that the # omnibus installer will choose the destination and you have no control @@ -74,6 +89,7 @@ module VagrantPlugins @log_level = UNSET_VALUE @channel = UNSET_VALUE @version = UNSET_VALUE + @omnibus_url = UNSET_VALUE @installer_download_path = UNSET_VALUE end @@ -85,6 +101,7 @@ module VagrantPlugins @log_level = :info if @log_level == UNSET_VALUE @channel = "stable" if @channel == UNSET_VALUE @version = :latest if @version == UNSET_VALUE + @omnibus_url = 'https://omnitruck.chef.io' if @omnibus_url == UNSET_VALUE @installer_download_path = nil if @installer_download_path == UNSET_VALUE # Make sure the install is a symbol if it's not a boolean diff --git a/plugins/provisioners/chef/installer.rb b/plugins/provisioners/chef/installer.rb index 734136328..11ae7d526 100644 --- a/plugins/provisioners/chef/installer.rb +++ b/plugins/provisioners/chef/installer.rb @@ -2,12 +2,13 @@ module VagrantPlugins module Chef class Installer def initialize(machine, options = {}) - @machine = machine - @product = options.fetch(:product) - @channel = options.fetch(:channel) - @version = options.fetch(:version) - @force = options.fetch(:force) - @options = options.dup + @machine = machine + @product = options.fetch(:product) + @channel = options.fetch(:channel) + @version = options.fetch(:version) + @force = options.fetch(:force) + @omnibus_url = options.fetch(:omnibus_url) + @options = options.dup end # This handles verifying the Chef installation, installing it if it was @@ -29,7 +30,7 @@ module VagrantPlugins @machine.ui.detail(I18n.t("vagrant.chef_installing", version: @version.to_s)) - @machine.guest.capability(:chef_install, @product, @version, @channel, @options) + @machine.guest.capability(:chef_install, @product, @version, @channel, @omnibus_url, @options) if !@machine.guest.capability(:chef_installed, @product, @version) raise Provisioner::Base::ChefError, :install_failed diff --git a/plugins/provisioners/chef/omnibus.rb b/plugins/provisioners/chef/omnibus.rb index edc090718..a971b0962 100644 --- a/plugins/provisioners/chef/omnibus.rb +++ b/plugins/provisioners/chef/omnibus.rb @@ -5,10 +5,8 @@ module VagrantPlugins # https://docs.chef.io/install_omnibus.html # module Omnibus - OMNITRUCK = "https://omnitruck.chef.io".freeze - - def sh_command(project, version, channel, options = {}) - command = "curl -sL #{OMNITRUCK}/install.sh | bash" + def sh_command(project, version, channel, omnibus_url, options = {}) + command = "curl -sL #{omnibus_url}/install.sh | bash" command << " -s -- -P \"#{project}\" -c \"#{channel}\"" if version != :latest @@ -23,8 +21,8 @@ module VagrantPlugins end module_function :sh_command - def ps_command(project, version, channel, options = {}) - command = ". { iwr -useb #{OMNITRUCK}/install.ps1 } | iex; install" + def ps_command(project, version, channel, omnibus_url, options = {}) + command = ". { iwr -useb #{omnibus_url}/install.ps1 } | iex; install" command << " -project '#{project}' -channel '#{channel}'" if version != :latest diff --git a/plugins/provisioners/chef/provisioner/base.rb b/plugins/provisioners/chef/provisioner/base.rb index 8f41e67bb..28c30bb64 100644 --- a/plugins/provisioners/chef/provisioner/base.rb +++ b/plugins/provisioners/chef/provisioner/base.rb @@ -58,6 +58,7 @@ module VagrantPlugins product: config.product, channel: config.channel, version: config.version, + omnibus_url: config.omnibus_url, force: config.install == :force, download_path: config.installer_download_path ) diff --git a/test/unit/plugins/provisioners/chef/config/base_test.rb b/test/unit/plugins/provisioners/chef/config/base_test.rb index a12c76295..52d2a144d 100644 --- a/test/unit/plugins/provisioners/chef/config/base_test.rb +++ b/test/unit/plugins/provisioners/chef/config/base_test.rb @@ -82,4 +82,17 @@ describe VagrantPlugins::Chef::Config::Base do expect(subject.installer_download_path).to be(nil) end end + + describe "#omnibus_url" do + it "defaults to https://omnitruck.chef.io" do + subject.finalize! + expect(subject.omnibus_url).to eq("https://omnitruck.chef.io") + end + + it "makes use of the configured url" do + subject.omnibus_url = "https://omnitruck.example.com" + subject.finalize! + expect(subject.omnibus_url).to eq("https://omnitruck.example.com") + end + end end diff --git a/test/unit/plugins/provisioners/chef/omnibus_test.rb b/test/unit/plugins/provisioners/chef/omnibus_test.rb index 5e8c4aaea..b0c4a9dec 100644 --- a/test/unit/plugins/provisioners/chef/omnibus_test.rb +++ b/test/unit/plugins/provisioners/chef/omnibus_test.rb @@ -5,22 +5,27 @@ require Vagrant.source_root.join("plugins/provisioners/chef/omnibus") describe VagrantPlugins::Chef::Omnibus do describe "#sh_command" do it "includes the project name" do - command = described_class.sh_command("chef", nil, "stable") + command = described_class.sh_command("chef", nil, "stable", "https://omnitruck.chef.io") expect(command).to include %|-P "chef"| end it "includes the channel" do - command = described_class.sh_command("chef", nil, "stable") + command = described_class.sh_command("chef", nil, "stable", "https://omnitruck.chef.io") expect(command).to include %|-c "stable"| end it "includes the version" do - command = described_class.sh_command("chef", "1.2.3", "stable") + command = described_class.sh_command("chef", "1.2.3", "stable", "https://omnitruck.chef.io") expect(command).to include %|-v "1.2.3"| end + it "includes the Omnibus installation URL" do + command = described_class.sh_command("chef", "1.2.3", "stable", "https://omnitruck.chef.io") + expect(command).to include %|https://omnitruck.chef.io/install.sh| + end + it "includes the download path" do - command = described_class.sh_command("chef", "1.2.3", "stable", + command = described_class.sh_command("chef", "1.2.3", "stable", "https://omnitruck.chef.io", download_path: "/some/path", ) expect(command).to include %|-d "/some/path"| @@ -29,18 +34,23 @@ describe VagrantPlugins::Chef::Omnibus do describe "#ps_command" do it "includes the project name" do - command = described_class.ps_command("chef", nil, "stable") + command = described_class.ps_command("chef", nil, "stable", "https://omnitruck.chef.io") expect(command).to include %|-project 'chef'| end it "includes the channel" do - command = described_class.ps_command("chef", nil, "stable") + command = described_class.ps_command("chef", nil, "stable", "https://omnitruck.chef.io") expect(command).to include %|-channel 'stable'| end it "includes the version" do - command = described_class.ps_command("chef", "1.2.3", "stable") + command = described_class.ps_command("chef", "1.2.3", "stable", "https://omnitruck.chef.io") expect(command).to include %|-version '1.2.3'| end + + it "includes the Omnibus installation URL" do + command = described_class.ps_command("chef", "1.2.3", "stable", "https://omnitruck.chef.io") + expect(command).to include %|https://omnitruck.chef.io/install.ps1| + end end end diff --git a/website/source/docs/provisioning/chef_common.html.md b/website/source/docs/provisioning/chef_common.html.md index 435b84067..b0009b810 100644 --- a/website/source/docs/provisioning/chef_common.html.md +++ b/website/source/docs/provisioning/chef_common.html.md @@ -59,12 +59,22 @@ understand their purpose. requested version. If they match, no action is taken. If they do not match, the value specified in this attribute will be installed in favor of the existing version (a message will be displayed). - You can also specify "latest" (default), which will install the latest version of Chef on the system. In this case, Chef will use whatever version is on the system. To force the newest version of Chef to be installed on every provision, set the [`install`](#install) option to "force". +- `omnibus_url` (string) - Location of Omnibus installation scripts. + This URL specifies the location of install.sh/install.ps1 for + Linux/Unix and Windows respectively. + It defaults to https://omnitruck.chef.io. The full URL is in this case: + + - Linux/Unix: https://omnitruck.chef.io/install.sh + - Windows: https://omnitruck.chef.io/install.ps1 + + If you want to have https://example.com/install.sh as Omnibus script + for your Linux/Unix installations, you should set this option to + https://example.com ## Runner Chef Provisioners