Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vagrant architecture support #491

Merged
merged 8 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ required.

### Vagrant

A Vagrant version of 1.6 or later.
A Vagrant version of 2.4 or later.

## Installation

Expand Down
71 changes: 13 additions & 58 deletions lib/kitchen/driver/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Vagrant < Kitchen::Driver::Base

default_config :box_version, nil

default_config :box_arch, nil

default_config :boot_timeout, nil

default_config :customize, {}
Expand Down Expand Up @@ -125,7 +127,7 @@ def create(state)
# @return [String,nil] the Vagrant box for this Instance
def default_box
if bento_box?(instance.platform.name)
"bento/#{instance.platform.name}#{"-arm64" if RbConfig::CONFIG["host_cpu"].eql?("arm64")}"
"bento/#{instance.platform.name}"
else
instance.platform.name
end
Expand Down Expand Up @@ -224,16 +226,10 @@ def cache_directory

protected

WEBSITE = "https://www.vagrantup.com/downloads.html".freeze
MIN_VER = "1.1.0".freeze
WEBSITE = "https://developer.hashicorp.com/vagrant/install".freeze
MIN_VER = "2.4.0".freeze

class << self

# @return [true,false] whether or not the vagrant-winrm plugin is
# installed
# @api private
attr_accessor :winrm_plugin_passed

# @return [String] the version of Vagrant installed on the workstation
# @api private
attr_accessor :vagrant_version
Expand Down Expand Up @@ -313,14 +309,20 @@ def finalize_ca_cert!
def finalize_box_auto_update!
return if config[:box_auto_update].nil?

config[:box_auto_update] = "vagrant box update #{"--insecure " if config[:box_download_insecure]}--box #{config[:box]} --provider #{config[:provider]}"
cmd = "#{config[:vagrant_binary]} box update --box #{config[:box]}"
cmd += " --architecture #{config[:box_arch]}" if config[:box_arch]
cmd += " --provider #{config[:provider]}" if config[:provider]
cmd += " --insecure" if config[:box_download_insecure]
config[:box_auto_update] = cmd
end

# Create vagrant command to remove older versions of the box
def finalize_box_auto_prune!
return if config[:box_auto_prune].nil?

config[:box_auto_prune] = "vagrant box prune --force --keep-active-boxes --name #{config[:box]} --provider #{config[:provider]}"
cmd = "#{config[:vagrant_binary]} box prune --force --keep-active-boxes --name #{config[:box]}"
cmd += " --provider #{config[:provider]}" if config[:provider]
config[:box_auto_prune] = cmd
end

# Replaces any `{{vagrant_root}}` tokens in the pre create command.
Expand Down Expand Up @@ -390,19 +392,6 @@ def finalize_network!
end
end

# Loads any required third party Ruby libraries or runs any shell out
# commands to prepare the plugin. This method will be called in the
# context of the main thread of execution and so does not necessarily
# have to be thread safe.
#
# @raise [ClientError] if any library loading fails or any of the
# dependency requirements cannot be satisfied
# @api private
def load_needed_dependencies!
super
verify_winrm_plugin if winrm_transport?
end

# Renders the Vagrantfile ERb template.
#
# @return [String] the contents for a Vagrantfile
Expand Down Expand Up @@ -597,46 +586,12 @@ def vagrant_version
" Please download a package from #{WEBSITE}."
end

# Verify that the vagrant-winrm plugin is installed and a suitable
# version of Vagrant is installed
#
# @api private
def verify_winrm_plugin
if Gem::Version.new(vagrant_version) < Gem::Version.new("1.6")
raise UserError, "Detected an old version of Vagrant " \
"(#{vagrant_version}) that cannot support the vagrant-winrm " \
"Vagrant plugin." \
" Please upgrade to version 1.6 or higher from #{WEBSITE}."
end

if Gem::Version.new(vagrant_version) < Gem::Version.new("2.2.0") && !winrm_plugin_installed?
raise UserError, "Vagrant version #{vagrant_version} requires the " \
"vagrant-winrm plugin to properly communicate with this Vagrant VM " \
"over WinRM Transport. Please install this plugin with: " \
"`vagrant plugin install vagrant-winrm' and try again." \
"Alternatively upgrade to Vagrant >= 2.2.0 which does not " \
"require the vagrant-winrm plugin."
end
end

# @return [true,false] whether or not the host is windows
#
# @api private
def windows_host?
RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
end

# @return [true,false] whether or not the vagrant-winrm plugin is
# installed
# @api private
def winrm_plugin_installed?
return true if self.class.winrm_plugin_passed

self.class.winrm_plugin_passed = run_silently(
"#{config[:vagrant_binary]} plugin list", cwd: Dir.pwd
)
.split("\n").find { |line| line =~ /vagrant-winrm\s+/ }
end
end
end
end
70 changes: 3 additions & 67 deletions spec/kitchen/driver/vagrant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def run_command(_cmd, options = {})
before(:each) { stub_const("ENV", env) }

after do
driver_object.class.send(:winrm_plugin_passed=, nil)
driver_object.class.send(:vagrant_version=, nil)
end

Expand Down Expand Up @@ -542,7 +541,7 @@ def run_command(_cmd, options = {})
with_unsupported_vagrant

expect { driver.verify_dependencies }.to raise_error(
Kitchen::UserError, /Please upgrade to version 1.1.0 or higher/
Kitchen::UserError, /Please upgrade to version 2.4.0 or higher/
)
end

Expand All @@ -551,74 +550,11 @@ def run_command(_cmd, options = {})
.with("vagrant --version", any_args).and_raise(Errno::ENOENT)

expect { driver.verify_dependencies }.to raise_error(
Kitchen::UserError, /Vagrant 1.1.0 or higher is not installed/
Kitchen::UserError, /Vagrant 2.4.0 or higher is not installed/
)
end
end

describe "#load_needed_dependencies!" do

describe "with winrm transport" do

before { allow(transport).to receive(:name).and_return("WinRM") }

it "old version of Vagrant raises UserError" do
with_vagrant("1.5.0")

expect { instance }.to raise_error(
Kitchen::Error, /Please upgrade to version 1.6 or higher/
)
end

it "modern vagrant without plugin installed raises UserError" do
with_modern_vagrant
allow(driver_object).to receive(:run_command)
.with("vagrant plugin list", any_args).and_return("nope (1.2.3)")

expect { instance }.to raise_error(
Kitchen::Error, /vagrant plugin install vagrant-winrm/
)
end

it "modern vagrant with plugin installed succeeds" do
with_modern_vagrant
allow(driver_object).to receive(:run_command)
.with("vagrant plugin list", any_args)
.and_return("vagrant-winrm (1.2.3)")

instance
end
end

describe "without winrm transport" do

before { allow(transport).to receive(:name).and_return("Anything") }

it "old version of Vagrant succeeds" do
with_vagrant("1.5.0")

instance
end

it "modern vagrant without plugin installed succeeds" do
with_modern_vagrant
allow(driver_object).to receive(:run_command)
.with("vagrant plugin list", any_args).and_return("nope (1.2.3)")

instance
end

it "modern vagrant with plugin installed succeeds" do
with_modern_vagrant
allow(driver_object).to receive(:run_command)
.with("vagrant plugin list", any_args)
.and_return("vagrant-winrm (1.2.3)")

instance
end
end
end

describe "#create" do

let(:cmd) { driver.create(state) }
Expand Down Expand Up @@ -2139,7 +2075,7 @@ def debug_lines
end

def with_modern_vagrant
with_vagrant("1.7.2")
with_vagrant("2.4.1")
end

def with_unsupported_vagrant
Expand Down
4 changes: 4 additions & 0 deletions templates/Vagrantfile.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Vagrant.configure("2") do |c|
c.vm.box_version = "<%= config[:box_version] %>"
<% end %>

<% if config[:box_arch] %>
c.vm.box_architecture = "<%= config[:box_arch] %>"
<% end %>

<% if !config[:box_check_update].nil? %>
c.vm.box_check_update = <%= config[:box_check_update] %>
<% end %>
Expand Down