Updates install checks for Chef provisioners

Signed-off-by: Collin McNeese <cmcneese@chef.io>
This commit is contained in:
Collin McNeese 2021-10-24 11:38:59 -05:00
parent cea854c240
commit 692cb1ae34
No known key found for this signature in database
GPG Key ID: 9FA182748DD18914
8 changed files with 136 additions and 53 deletions

View File

@ -6,11 +6,13 @@ module VagrantPlugins
# Check if Chef is installed at the given version.
# @return [true, false]
def self.chef_installed(machine, product, version)
knife = "/opt/#{product}/bin/knife"
command = "test -x #{knife}"
product_name = product == 'chef-workstation' ? 'chef-workstation' : 'chef'
product_binary = product_name == 'chef-workstation' ? 'chef' : 'chef-client'
test_binary = "/opt/#{product_name}/bin/#{product_binary}"
command = "test -x #{test_binary}"
if version != :latest
command << "&& #{knife} --version | grep '#{version}'"
command << "&& #{test_binary} --version | grep '#{version}'"
end
machine.communicate.test(command, sudo: true)

View File

@ -6,11 +6,13 @@ module VagrantPlugins
# Check if Chef is installed at the given version.
# @return [true, false]
def self.chef_installed(machine, product, version)
knife = "/opt/#{product}/bin/knife"
command = "test -x #{knife}"
product_name = product == 'chef-workstation' ? 'chef-workstation' : 'chef'
product_binary = product_name == 'chef-workstation' ? 'chef' : 'chef-client'
test_binary = "/opt/#{product_name}/bin/#{product_binary}"
command = "test -x #{test_binary}"
if version != :latest
command << "&& #{knife} --version | grep '#{version}'"
command << "&& #{test_binary} --version | grep '#{version}'"
end
machine.communicate.test(command, sudo: true)

View File

@ -2,19 +2,21 @@ module VagrantPlugins
module Chef
module Cap
module OmniOS
module ChefInstalled
module ChefInstalled
# TODO: this is the same code as cap/linux/chef_installed, consider merging
# Check if Chef is installed at the given version.
# @return [true, false]
def self.chef_installed(machine, product, version)
knife = "/opt/#{product}/bin/knife"
command = "test -x #{knife}"
product_name = product == 'chef-workstation' ? 'chef-workstation' : 'chef'
product_binary = product_name == 'chef-workstation' ? 'chef' : 'chef-client'
test_binary = "/opt/#{product_name}/bin/#{product_binary}"
command = "test -x #{test_binary}"
if version != :latest
command << "&& #{knife} --version | grep '#{version}'"
command << "&& #{test_binary} --version | grep '#{version}'"
end
machine.communicate.test(command, sudo: true)
machine.communicate.test(command, sudo: true)
end
end
end

View File

@ -6,10 +6,11 @@ module VagrantPlugins
# Check if Chef is installed at the given version.
# @return [true, false]
def self.chef_installed(machine, product, version)
test_binary = product == 'chef-workstation' ? 'chef' : 'chef-client'
if version != :latest
command = 'if ((&knife --version) -Match "' + version.to_s + '"){ exit 0 } else { exit 1 }'
command = 'if ((&' + test_binary + ' --version) -Match "' + version.to_s + '"){ exit 0 } else { exit 1 }'
else
command = 'if ((&knife --version) -Match "Chef*"){ exit 0 } else { exit 1 }'
command = 'if ((&' + test_binary + ' --version) -Match "Chef*"){ exit 0 } else { exit 1 }'
end
machine.communicate.test(command, sudo: true)
end

View File

@ -23,19 +23,38 @@ describe VagrantPlugins::Chef::Cap::FreeBSD::ChefInstalled do
end
describe "#chef_installed" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef_solo/bin/knife&& /opt/chef_solo/bin/knife --version | grep '15.0.0'" }
describe "when chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef-workstation/bin/chef&& /opt/chef-workstation/bin/chef --version | grep '15.0.0'" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef-workstation", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef-workstation", version)).to be_falsey
end
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
describe "when not chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef/bin/chef-client&& /opt/chef/bin/chef-client --version | grep '15.0.0'" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
end
end
end
end

View File

@ -23,19 +23,38 @@ describe VagrantPlugins::Chef::Cap::Linux::ChefInstalled do
end
describe "#chef_installed" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef_solo/bin/knife&& /opt/chef_solo/bin/knife --version | grep '15.0.0'" }
describe "when chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef-workstation/bin/chef&& /opt/chef-workstation/bin/chef --version | grep '15.0.0'" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef-workstation", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef-workstation", version)).to be_falsey
end
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
describe "when not chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef/bin/chef-client&& /opt/chef/bin/chef-client --version | grep '15.0.0'" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
end
end
end
end

View File

@ -23,19 +23,38 @@ describe VagrantPlugins::Chef::Cap::OmniOS::ChefInstalled do
end
describe "#chef_installed" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef_solo/bin/knife&& /opt/chef_solo/bin/knife --version | grep '15.0.0'" }
describe "when chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef-workstation/bin/chef&& /opt/chef-workstation/bin/chef --version | grep '15.0.0'" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef-workstation", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef-workstation", version)).to be_falsey
end
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
describe "when not chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "test -x /opt/chef/bin/chef-client&& /opt/chef/bin/chef-client --version | grep '15.0.0'" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
end
end
end
end

View File

@ -23,19 +23,38 @@ describe VagrantPlugins::Chef::Cap::Windows::ChefInstalled do
end
describe "#chef_installed" do
let(:version) { "15.0.0" }
let(:command) { "if ((&knife --version) -Match \"15.0.0\"){ exit 0 } else { exit 1 }" }
describe "when chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "if ((&chef --version) -Match \"15.0.0\"){ exit 0 } else { exit 1 }" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef-workstation", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef-workstation", version)).to be_falsey
end
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
describe "when chef-workstation" do
let(:version) { "15.0.0" }
let(:command) { "if ((&chef-client --version) -Match \"15.0.0\"){ exit 0 } else { exit 1 }" }
it "returns true if installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(true)
subject.chef_installed(machine, "chef_solo", version)
end
it "returns false if not installed" do
expect(machine.communicate).to receive(:test).
with(command, sudo: true).and_return(false)
expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey
end
end
end
end