Only destroy broken VMs

This commit is contained in:
thomashaw
2017-10-13 13:48:08 +01:00
parent 6b87f6ca31
commit fccd4e0b33
2 changed files with 20 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
require 'rubygems'
require 'open3'
class GemExec
@@ -36,8 +37,8 @@ class GemExec
end
Dir.chdir(working_dir)
system "#{gem_path} #{arguments}"
stdout, stderr, exit_code = Open3.capture3("#{gem_path} #{arguments}")
{:stdout => stdout, :stderr => stderr, :exit_code => exit_code}
end
end

View File

@@ -137,7 +137,8 @@ def build_vms(project_dir, options)
successful_creation = false
while retry_count and !successful_creation
if GemExec.exe('vagrant', project_dir, "#{command} #{system}")
vagrant_output = GemExec.exe('vagrant', project_dir, "#{command} #{system}")
if !vagrant_output[:exit_code] # zero exit code (success)
Print.info 'VMs created.'
successful_creation = true
if options[:shutdown]
@@ -149,13 +150,22 @@ def build_vms(project_dir, options)
end
else
if retry_count > 0
# Identify which VMs failed
stderr = vagrant_output[:stderr]
failed_vm = stderr[/\n==>(.*?): An error occurred/, 1]
destroy = 'destroy'
if failed_vm
destroy += " #{failed_vm}"
end
Print.err 'Error creating VMs, destroying VMs and retrying...'
if GemExec.exe('vagrant', project_dir, 'destroy')
Print.info 'VMs destroyed'
else
Print.err 'Failed to destroy VMs. Exiting.'
exit 1
end
destroy_output = GemExec.exe('vagrant', project_dir, destroy)
if !destroy_output[:exit_code]
Print.info "vagrant #{destroy} completed successfully."
else
Print.err 'Failed to destroy VMs. Exiting.'
exit 1
end
sleep(10)
else
Print.err 'Error creating VMs, exiting SecGen.'