lib/testlab/providers/vagrant.rb in testlab-1.15.1 vs lib/testlab/providers/vagrant.rb in testlab-1.16.0
- old
+ new
@@ -34,10 +34,12 @@
@config = (config || Hash.new)
@ui = (ui || TestLab.ui)
# ensure our vagrant key is there
@config[:vagrant] ||= Hash.new
+
+ @command = ZTK::Command.new(:ui => @ui, :silence => true, :ignore_exit_status => true, :timeout => 3600)
end
################################################################################
# Create the Vagrant instance
@@ -73,10 +75,72 @@
true
end
################################################################################
+ # Export the Vagrant-controlled VM
+ def export(filename=nil)
+ tempfile = Tempfile.new('export')
+ temppath = tempfile.path.dup
+ tempfile.unlink
+ File.exists?(temppath) or FileUtils.mkdir_p(temppath)
+
+ labfile_source = File.join(@config[:vagrant][:file], 'Labfile')
+ labfile_destination = File.join(temppath, 'Labfile')
+
+ image_name = "lab.ova"
+ image_location = File.join(temppath, image_name)
+
+ export_destination = File.join(@config[:vagrant][:file], "#{self.instance_id}.lab")
+
+ self.down
+ self.vboxmanage_cli(%W(export #{self.instance_id} --output #{image_location}))
+ FileUtils.cp(labfile_source, labfile_destination)
+
+ Dir.chdir(temppath) do
+ @command.exec(%(tar cvf #{export_destination} *))
+ end
+
+ FileUtils.rm_rf(temppath)
+ end
+
+ # Import the Vagrant-controlled VM
+ def import(filename=nil)
+ filename = (filename || "#{self.instance_id}.lab")
+
+ tempfile = Tempfile.new('export')
+ temppath = tempfile.path.dup
+ tempfile.unlink
+ File.exists?(temppath) or FileUtils.mkdir_p(temppath)
+
+ id_filename = File.join(@config[:vagrant][:file], ".vagrant", "machines", self.instance_id, "virtualbox", "id")
+ FileUtils.mkdir_p(File.dirname(id_filename))
+
+ labfile_source = File.join(temppath, 'Labfile')
+ labfile_destination = File.join(@config[:vagrant][:file], 'Labfile')
+
+ image_name = "lab.ova"
+ image_location = File.join(temppath, image_name)
+
+ FileUtils.cp(filename, temppath)
+ Dir.chdir(temppath) do
+ @command.exec(%(tar xvf #{filename}))
+ end
+
+ self.destroy
+ self.vboxmanage_cli(%W(import #{image_location} --vsys 0 --vmname #{self.instance_id} --vsys 0 --cpus #{self.cpus} --vsys 0 --memory #{self.memory}))
+ uuid = self.vboxmanage_cli(%W(showvminfo #{self.instance_id} | grep UUID | head -1 | cut -f 2 -d ':')).output.strip
+
+ @command.exec(%(echo '#{uuid}' > #{id_filename}))
+
+ FileUtils.cp(labfile_source, labfile_destination)
+
+ FileUtils.rm_rf(temppath)
+ end
+
+################################################################################
+
# Reload Vagrant-controlled VM
def reload
self.down
self.up
@@ -117,11 +181,11 @@
# START CORE CONFIG
####################
def instance_id
- (@config[:vagrant][:id] || "testlab-#{ENV['USER']}".downcase)
+ (@config[:vagrant][:id] || "#{File.basename(@config[:vagrant][:file])}-#{TestLab.hostname}".downcase)
end
def user
(@config[:vagrant][:user] || "vagrant")
end
@@ -181,17 +245,37 @@
command = TestLab.build_command_line("vagrant", *args)
@ui.logger.debug { "command == #{command.inspect}" }
render_vagrantfile
- result = ZTK::Command.new(:ui => @ui, :silence => true, :ignore_exit_status => true).exec(command)
+ result = @command.exec(command)
if result.exit_code != 0
@ui.stderr.puts
@ui.stderr.puts
@ui.stderr.puts(result.output)
raise VagrantError, "Vagrant failed to execute!"
+ end
+
+ result
+ end
+
+ def vboxmanage_cli(*args)
+ @ui.logger.debug { "args == #{args.inspect}" }
+
+ command = TestLab.build_command_line("VBoxManage", *args)
+ @ui.logger.debug { "command == #{command.inspect}" }
+
+ render_vagrantfile
+ result = @command.exec(command)
+
+ if result.exit_code != 0
+ @ui.stderr.puts
+ @ui.stderr.puts
+ @ui.stderr.puts(result.output)
+
+ raise VagrantError, "VBoxManage failed to execute!"
end
result
end