lib/beaker/subcommands/subcommand_util.rb in beaker-3.12.0 vs lib/beaker/subcommands/subcommand_util.rb in beaker-3.13.0
- old
+ new
@@ -1,7 +1,9 @@
require 'rake'
require 'stringio'
+require 'yaml/store'
+require 'fileutils'
module Beaker
module Subcommands
# Methods used in execution of Subcommands
# - should we execute a subcommand?
@@ -16,10 +18,12 @@
# - execute the quick start task for the specified hypervisor
# - capture stdout and stderr
module SubcommandUtil
BEAKER_REQUIRE = "require 'beaker/tasks/quick_start'"
HYPERVISORS = ["vagrant", "vmpooler"]
+ CONFIG_DIR = ".beaker"
+ CONFIG_KEYS = [:hypervisor, :provisioned]
# Check if the first argument to the beaker execution is a subcommand
# @return [Boolean] true if argv[0] is "help" or a method defined in the Subcommands class, false otherwise
def self.execute_subcommand?(arg0)
return false if arg0.nil?
@@ -84,28 +88,39 @@
# Execute the quick start task for vmpooler
def self.init_vmpooler()
execute_rake_task("beaker_quickstart:gen_hosts[vmpooler]")
end
- # Print a message to the console and exit with 0
- # @param [String] msg the message to print
- def self.exit_with(msg)
+ # Print a message to the console and exit with specified exit code, defaults to 1
+ # @param [String] msg the message to output
+ # @param [Hash<Object>] options to specify exit code or output stack trace
+ def self.error_with(msg, options={})
puts msg
- exit(0)
+ puts options[:stack_trace] if options[:stack_trace]
+ exit_code = options[:exit_code] ? options[:exit_code] : 1
+ exit(exit_code)
end
# Call the quick start task for the specified hypervisor
- # @param [Array<Object>] options the options we want to query
- def self.init_hypervisor(options)
- case options[:hypervisor]
+ # @param [String] hypervisor the hypervisor we want to query
+ def self.init_hypervisor(hypervisor)
+ case hypervisor
when "vagrant"
init_vagrant
when "vmpooler"
init_vmpooler
end
end
+ # Verify that a valid hypervisor has been specified
+ # @param [String] hypervisor the hypervisor we want to validate
+ def self.verify_init_args(hypervisor)
+ unless HYPERVISORS.include?(hypervisor)
+ error_with("Invalid hypervisor. Currently supported hypervisors are: #{HYPERVISORS.join(', ')}")
+ end
+ end
+
# Execute a task but capture stdout and stderr to a buffer
def self.with_captured_output
begin
old_stdout = $stdout.clone
old_stderr = $stderr.clone
@@ -114,9 +129,45 @@
yield
ensure
$stdout = old_stdout
$stderr = old_stderr
end
+ end
+
+ # Initialise the beaker config
+ def self.init_config()
+ FileUtils.mkdir_p CONFIG_DIR
+ @@store = YAML::Store.new("#{CONFIG_DIR}/config")
+ end
+
+ # Store values from a hash into the beaker config
+ # @param [Hash{Symbol=>String}] config values we want to store
+ def self.store_config(config)
+ @@store.transaction do
+ CONFIG_KEYS.each do |key|
+ @@store[key] = config[key] unless config[key].nil?
+ end
+ end
+ end
+
+ # Delete keys from the beaker configi
+ # @param [Array<Object>] keys the keys we want to delete from the config
+ def self.delete_config(keys)
+ @@store.transaction {
+ keys.each do |key|
+ @@store.delete(key)
+ end
+ }
+ end
+
+ # Reset args and provision nodes
+ # @param [String] hypervisor the hypervisor to use
+ # @param [Array<Object>] options the options to use when provisioning
+ def self.provision(hypervisor, options)
+ reset_argv(["--hosts", "#{CONFIG_DIR}/acceptance/config/default_#{hypervisor}_hosts.yaml", "--validate", options[:validate], "--configure", options[:configure]])
+ beaker = Beaker::CLI.new.parse_options
+ beaker.provision
+ beaker.preserve_hosts_file
end
end
end
end