From fccd4e0b33896f5d514a49d15fc20994e2dfc132 Mon Sep 17 00:00:00 2001 From: thomashaw Date: Fri, 13 Oct 2017 13:48:08 +0100 Subject: [PATCH] Only destroy broken VMs --- lib/helpers/gem_exec.rb | 5 +++-- secgen.rb | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/helpers/gem_exec.rb b/lib/helpers/gem_exec.rb index 9a98f3c32..2c3a73c14 100644 --- a/lib/helpers/gem_exec.rb +++ b/lib/helpers/gem_exec.rb @@ -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 diff --git a/secgen.rb b/secgen.rb index 213d1bae0..6a05cc05e 100644 --- a/secgen.rb +++ b/secgen.rb @@ -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.'