Skip to content

Instantly share code, notes, and snippets.

@trlinkin
Last active December 18, 2015 19:49
Show Gist options
  • Save trlinkin/5835463 to your computer and use it in GitHub Desktop.
Save trlinkin/5835463 to your computer and use it in GitHub Desktop.
Simple base box updater for Vagrant base-boxes. Go from a pre 1.1 Virtualbox base-box to one ready for Vagrant 1.1+
#!/usr/bin/env ruby
require 'etc'
require 'FileUtils'
box_dir = "#{Etc.getpwuid.dir}/.vagrant.d/boxes"
printf "Attempting to upgrade your baseboxes in #{box_dir}\n"
Dir.entries(box_dir).each do |box|
next if ['.','..'].include? box
print "Checking \"#{box}\"\n"
# Assume that a 'Vagrantfile' in the root of the base-box dir means it's
# from the pre 1.1+ days. If not found, consider this box converted.
if File.exists? "#{box_dir}/#{box}/Vagrantfile"
print "Moving VM files for \"#{box}\" under 'virtualbox' directory...\n"
Dir.mkdir "#{box_dir}/#{box}/virtualbox" unless Dir.exists? "#{box_dir}/#{box}/virtualbox"
Dir.entries("#{box_dir}/#{box}").each do |file|
# Ignore currently valid provider directories. These are the mainstream providers at the very least.
next if ['.','..','virtualbox','vmwarevm','vmware_fusion','aws'].include? file
print "\t- Moving \"#{box}/#{file}\" to \"#{box}/virtualbox/#{file}\"\n"
FileUtils.mv "#{box_dir}/#{box}/#{file}", "#{box_dir}/#{box}/virtualbox/#{file}"
end
end
# Upgrade the Vagrantfile for virtualbox if still using the old syntax (technically treated as version 1 syntax)
if File.exists? "#{box_dir}/#{box}/virtualbox/Vagrantfile"
vagrantfile = File.read("#{box_dir}/#{box}/virtualbox/Vagrantfile")
# Fancy gsub does our "upgrayedd" - http://goo.gl/3WXu4
if vagrantfile.gsub!(/Vagrant::Config\.run.*$/,'Vagrant.configure("2") do |config|')
print "\t- Upgrading 'virtualbox/Vagrantfile' for \"#{box}\" to configuration verion 2...\n"
File.open("#{box_dir}/#{box}/virtualbox/Vagrantfile",'w').write(vagrantfile)
end
end
# Add a basic metadata.conf if missing. Needed by the new 'virtualbox' provider.
unless File.exists? "#{box_dir}/#{box}/virtualbox/metadata.json"
print "\t- Adding default 'virtualbox/metadata.json' for \"#{box}\"...\n"
File.open("#{box_dir}/#{box}/virtualbox/metadata.json",'w').write('{"provider":"virtualbox"}')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment