Module: Beaker::Subcommands::SubcommandUtil

Defined in:
lib/beaker/subcommands/subcommand_util.rb

Overview

Methods used in execution of Subcommands

  • should we execute a subcommand?

  • reset ARGV

  • execute Beaker

  • update a rakefile to require beaker quickstart tasks

 - initialise a rake application

  • execute a rake task

  • execute the vagrant quickstart task

  • execute the vmpooler quickstart task

  • exit with a specific message

  • execute the quick start task for the specified hypervisor

  • capture stdout and stderr

Constant Summary collapse

BEAKER_REQUIRE =
"require 'beaker/tasks/quick_start'"
HYPERVISORS =
["vagrant", "vmpooler"]
CONFIG_DIR =
".beaker"
CONFIG_KEYS =
[:hypervisor, :provisioned]

Class Method Summary collapse

Class Method Details

.delete_config(keys) ⇒ Object

Delete keys from the beaker configi

Parameters:

  • keys (Array<Object>)

    the keys we want to delete from the config



154
155
156
157
158
159
160
# File 'lib/beaker/subcommands/subcommand_util.rb', line 154

def self.delete_config(keys)
  @@store.transaction {
    keys.each do |key|
      @@store.delete(key)
    end
  }
end

.determine_rake_fileString

Determines what Rakefile to use

Returns:

  • (String)

    the name of the rakefile to use



51
52
53
# File 'lib/beaker/subcommands/subcommand_util.rb', line 51

def self.determine_rake_file()
  rake_app.find_rakefile_location() ? rake_app.find_rakefile_location()[0] : "Rakefile"
end

.error_with(msg, options = {}) ⇒ Object

Print a message to the console and exit with specified exit code, defaults to 1  @param [String] msg the message to output

Parameters:

  • options (Hash<Object>) (defaults to: {})

    to specify exit code or output stack trace



96
97
98
99
100
101
# File 'lib/beaker/subcommands/subcommand_util.rb', line 96

def self.error_with(msg, options={})
  puts msg
  puts options[:stack_trace] if options[:stack_trace]
  exit_code = options[:exit_code] ? options[:exit_code] : 1
  exit(exit_code)
end

.execute_beaker(*args) ⇒ Object

Update ARGV and call Beaker  @param [Array<String>] args the arguments determined by a specific subcommand



44
45
46
47
# File 'lib/beaker/subcommands/subcommand_util.rb', line 44

def self.execute_beaker(*args)
  reset_argv(args)
  Beaker::CLI.new.execute!
end

.execute_rake_task(task) ⇒ Object

Clear ARGV and execute a Rake task  @param [String] task the rake task to execute



78
79
80
81
# File 'lib/beaker/subcommands/subcommand_util.rb', line 78

def self.execute_rake_task(task)
  rake_app.load_rakefile()
  with_captured_output { rake_app.invoke_task(task) }
end

.execute_subcommand?(arg0) ⇒ Boolean

Check if the first argument to the beaker execution is a subcommand

Returns:

  • (Boolean)

    true if argv is “help” or a method defined in the Subcommands class, false otherwise



28
29
30
31
# File 'lib/beaker/subcommands/subcommand_util.rb', line 28

def self.execute_subcommand?(arg0)
  return false if arg0.nil?
  (Beaker::Subcommand.instance_methods(false) << :help).include? arg0.to_sym
end

.init_configObject

Initialise the beaker config



137
138
139
140
# File 'lib/beaker/subcommands/subcommand_util.rb', line 137

def self.init_config()
  FileUtils.mkdir_p CONFIG_DIR
  @@store = YAML::Store.new("#{CONFIG_DIR}/config")
end

.init_hypervisor(hypervisor) ⇒ Object

Call the quick start task for the specified hypervisor

Parameters:

  • hypervisor (String)

    the hypervisor we want to query



105
106
107
108
109
110
111
112
# File 'lib/beaker/subcommands/subcommand_util.rb', line 105

def self.init_hypervisor(hypervisor)
  case hypervisor
  when "vagrant"
    init_vagrant
  when "vmpooler"
    init_vmpooler
  end
end

.init_vagrantObject

Execute the quick start task for vagrant



84
85
86
# File 'lib/beaker/subcommands/subcommand_util.rb', line 84

def self.init_vagrant()
  execute_rake_task("beaker_quickstart:gen_hosts[vagrant]")
end

.init_vmpoolerObject

Execute the quick start task for vmpooler



89
90
91
# File 'lib/beaker/subcommands/subcommand_util.rb', line 89

def self.init_vmpooler()
  execute_rake_task("beaker_quickstart:gen_hosts[vmpooler]")
end

.provision(hypervisor, options) ⇒ Object

Reset args and provision nodes

Parameters:

  • hypervisor (String)

    the hypervisor to use

  • options (Array<Object>)

    the options to use when provisioning



165
166
167
168
169
170
# File 'lib/beaker/subcommands/subcommand_util.rb', line 165

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

.rake_appObject

Initialises a rake application

Returns:

  • (Object)

    a rake application



67
68
69
70
71
72
73
74
# File 'lib/beaker/subcommands/subcommand_util.rb', line 67

def self.rake_app()
  unless @rake_app
    ARGV.clear
    @rake_app = Rake.application
    @rake_app.init
  end
  @rake_app
end

.require_tasksObject

Check for the presence of a Rakefile containing the require of the quick start tasks



57
58
59
60
61
62
63
# File 'lib/beaker/subcommands/subcommand_util.rb', line 57

def self.require_tasks()
  rake_file = determine_rake_file()
  FileUtils.touch(rake_file)
  unless File.readlines(rake_file).grep(/#{BEAKER_REQUIRE}/).any?
    File.open(rake_file, "a+") { |f| f.puts(BEAKER_REQUIRE) }
  end
end

.reset_argv(args) ⇒ Object

Reset ARGV to contain the arguments determined by a specific subcommand  @param [Array<String>] args the arguments determined by a specific subcommand



35
36
37
38
39
40
# File 'lib/beaker/subcommands/subcommand_util.rb', line 35

def self.reset_argv(args)
  ARGV.clear
  args.each do |arg|
    ARGV << arg
  end
end

.store_config(config) ⇒ Object

Store values from a hash into the beaker config

Parameters:

  • config (Hash{Symbol=>String})

    values we want to store



144
145
146
147
148
149
150
# File 'lib/beaker/subcommands/subcommand_util.rb', line 144

def self.store_config(config)
  @@store.transaction do
    CONFIG_KEYS.each do |key|
      @@store[key] = config[key] unless config[key].nil?
    end
  end
end

.verify_init_args(hypervisor) ⇒ Object

 Verify that a valid hypervisor has been specified  @param [String] hypervisor the hypervisor we want to validate



116
117
118
119
120
# File 'lib/beaker/subcommands/subcommand_util.rb', line 116

def self.verify_init_args(hypervisor)
  unless HYPERVISORS.include?(hypervisor)
    error_with("Invalid hypervisor. Currently supported hypervisors are: #{HYPERVISORS.join(', ')}")
  end
end

.with_captured_outputObject

Execute a task but capture stdout and stderr to a buffer



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/beaker/subcommands/subcommand_util.rb', line 123

def self.with_captured_output
  begin
    old_stdout = $stdout.clone
    old_stderr = $stderr.clone
    $stdout = StringIO.new
    $stderr = StringIO.new
    yield
  ensure
    $stdout = old_stdout
    $stderr = old_stderr
  end
end