class Beaker::Subcommand

Constants

SubcommandUtil

Attributes

cli[R]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/beaker/subcommand.rb, line 11
def initialize(*args)
  super
  FileUtils.mkdir_p(SubcommandUtil::CONFIG_DIR)
  FileUtils.touch(SubcommandUtil::SUBCOMMAND_OPTIONS) unless SubcommandUtil::SUBCOMMAND_OPTIONS.exist?
  FileUtils.touch(SubcommandUtil::SUBCOMMAND_STATE) unless SubcommandUtil::SUBCOMMAND_STATE.exist?
  @cli = Beaker::CLI.new
end

Public Instance Methods

destroy() click to toggle source
# File lib/beaker/subcommand.rb, line 211
def destroy
  if options[:help]
    invoke :help, [], ["destroy"]
    return
  end

  state = YAML::Store.new(SubcommandUtil::SUBCOMMAND_STATE)
  SubcommandUtil.error_with('Please provision an environment') unless state.transaction { state['provisioned'] }

  @cli.parse_options
  @cli.options[:provision] = false
  @cli.initialize_network_manager
  @cli.network_manager.cleanup

  state.transaction do
    state.delete('provisioned')
  end
end
exec(resource = nil) click to toggle source
# File lib/beaker/subcommand.rb, line 152
def exec(resource = nil)
  if options[:help]
    invoke :help, [], ["exec"]
    return
  end

  @cli.parse_options
  @cli.initialize_network_manager

  if !resource
    @cli.execute!
    return
  end

  beaker_suites = %i[pre_suite tests post_suite pre_cleanup]
  resources = resource.split(',')
  paths = resources.map { |r| Pathname(r) }
  if paths.all?(&:exist?)
    # If we determine the resource is a valid file resource, then we empty
    # all the suites and run that file resource in the tests suite. In the
    # future, when we have the ability to have custom suites, we should change
    # this to run in a custom suite. You know, in the future.
    beaker_suites.each do |suite|
      @cli.options[suite] = []
    end
    @cli.options[:tests] = paths.map do |path|
      if path.directory?
        Dir.glob("#{path}/**/*.rb")
      else
        path.to_s
      end
    end.flatten
  elsif resources.all? { |r| /^(pre-suite|tests|post-suite|pre-cleanup)$/.match?(r) }
    # The regex match here is loose so that users can supply multiple suites,
    # such as `beaker exec pre-suite,tests`.
    beaker_suites.each do |suite|
      @cli.options[suite] = [] unless resource.tr('-', '_').match(suite.to_s)
    end
  else
    raise ArgumentError, "Unable to parse #{resource} with beaker exec"
  end

  @cli.execute!

  return unless options['preserve-state']

  @cli.logger.notify 'updating HOSTS key in subcommand_options'
  hosts = SubcommandUtil.sanitize_options_for_save(@cli.combined_instance_and_options_hosts)
  options_storage = YAML::Store.new(SubcommandUtil::SUBCOMMAND_OPTIONS)
  options_storage.transaction do
    options_storage['HOSTS'] = hosts
  end
end
init() click to toggle source
# File lib/beaker/subcommand.rb, line 78
def init
  if options[:help]
    invoke :help, [], ["init"]
    return
  end

  @cli.parse_options

  options_to_write = SubcommandUtil.sanitize_options_for_save(@cli.configured_options)

  @cli.logger.notify 'Writing configured options to disk'
  File.write(SubcommandUtil::SUBCOMMAND_OPTIONS, options_to_write.to_yaml)
  @cli.logger.notify "Options written to #{SubcommandUtil::SUBCOMMAND_OPTIONS}"

  state = YAML::Store.new(SubcommandUtil::SUBCOMMAND_STATE)
  state.transaction do
    state['provisioned'] = false
  end
end
provision() click to toggle source
# File lib/beaker/subcommand.rb, line 105
def provision
  if options[:help]
    invoke :help, [], ["provision"]
    return
  end

  state = YAML::Store.new(SubcommandUtil::SUBCOMMAND_STATE)
  SubcommandUtil.error_with('Provisioned SUTs detected. Please destroy and reprovision.') if state.transaction { state['provisioned'] }

  @cli.parse_options
  @cli.provision

  # Sanitize the hosts
  cleaned_hosts = SubcommandUtil.sanitize_options_for_save(@cli.combined_instance_and_options_hosts)

  # Update each host provisioned with a flag indicating that it no longer needs
  # provisioning
  cleaned_hosts.each do |_host, host_hash|
    host_hash['provision'] = false
  end

  # should we only update the options here with the new host? Or update the settings
  # with whatever new flags may have been provided with provision?
  options_storage = YAML::Store.new(SubcommandUtil::SUBCOMMAND_OPTIONS)
  options_storage.transaction do
    @cli.logger.notify 'updating HOSTS key in subcommand_options'
    options_storage['HOSTS'] = cleaned_hosts
    options_storage['hosts_preserved_yaml_file'] = @cli.options[:hosts_preserved_yaml_file]
  end

  @cli.preserve_hosts_file

  state.transaction do
    state['provisioned'] = true
  end
end