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

Added features for auto updating and pruning vagrant box images #421

Merged
merged 2 commits into from
Aug 7, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ default `box` value of `bento/ubuntu-16.04`. Alternatively, a box called

Whether to check for box updates (enabled by default).

### <a name="config-box-auto-update"></a> box\_auto\_update

Whether to update box to the latest version prior to vagrant up command

### <a name="config-box-auto-prune"></a> box\_auto\_prune

Whether to prune older versions of the box and only keep the newest version

### <a name="config-box-url"></a> box\_url

A box_url is not required when using the Vagrant Cloud format of
Expand Down
36 changes: 36 additions & 0 deletions lib/kitchen/driver/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Vagrant < Kitchen::Driver::Base

default_config :box_check_update, nil

default_config :box_auto_update, nil

default_config :box_auto_prune, nil

default_config :box_download_insecure, nil

default_config :box_download_ca_cert, nil
Expand Down Expand Up @@ -109,6 +113,8 @@ class Vagrant < Kitchen::Driver::Base
def create(state)
create_vagrantfile
run_pre_create_command
run_box_auto_update
run_box_auto_prune
run_vagrant_up
update_state(state)
instance.transport.connection(state).wait_until_ready
Expand Down Expand Up @@ -172,6 +178,8 @@ def package(state)
def finalize_config!(instance)
super
finalize_vm_hostname!
finalize_box_auto_update!
finalize_box_auto_prune!
finalize_pre_create_command!
finalize_synced_folders!
finalize_ca_cert!
Expand Down Expand Up @@ -299,6 +307,20 @@ def finalize_ca_cert!
end
end

# Create vagrant command to update box to the latest version
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]}"
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 --name #{config[:box]}"
end

# Replaces any `{{vagrant_root}}` tokens in the pre create command.
#
# @api private
Expand Down Expand Up @@ -461,6 +483,20 @@ def run_command(cmd, options = {})
end
# rubocop:enable Metrics/CyclomaticComplexity

# Tell vagrant to update vagrant box to latest version
def run_box_auto_update
if config[:box_auto_update]
run(config[:box_auto_update])
end
end

# Tell vagrant to remove older vagrant boxes
def run_box_auto_prune
if config[:box_auto_prune]
run(config[:box_auto_prune])
end
end

# Runs a local command before `vagrant up` has been called.
#
# @api private
Expand Down