class VagrantPlugins::Registration::Action::Register

This registers the guest if the guest plugin supports it

Public Class Methods

new(app, _) click to toggle source
# File lib/vagrant-registration/action/register.rb, line 8
def initialize(app, _)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_registration::action::register')
end

Public Instance Methods

call(env) click to toggle source
# File lib/vagrant-registration/action/register.rb, line 13
def call(env)
  # Vbguest plugin (if present) is called next. Therefore registration
  # needs to be done first. This does not work with the default
  # 'action_register' hook.
  @app.call(env) unless Plugin.vbguest_plugin?

  # Configuration from Vagrantfile
  config = env[:machine].config.registration
  machine = env[:machine]
  guest = env[:machine].guest

  if should_register?(machine, env[:ui])
    env[:ui].info('Registering box with vagrant-registration...')
    check_configuration_options(machine, env[:ui])

    unless credentials_provided? machine
      @logger.debug('Credentials for registration not provided')

      # Offer to register ATM or skip
      register_now = env[:ui].ask('Would you like to register the system now (default: yes)? [y|n]')

      if register_now == 'n'
        config.skip = true
      else
        config = register_on_screen(machine, env[:ui])
      end
    end
    guest.capability(:registration_register, env[:ui]) unless config.skip
  end

  @logger.debug('Registration is skipped due to the configuration') if config.skip

  @app.call(env) if Plugin.vbguest_plugin?
end

Private Instance Methods

capabilities_provided?(guest) click to toggle source

Check if registration capabilities are available

# File lib/vagrant-registration/action/register.rb, line 84
def capabilities_provided?(guest)
  if guest.capability?(:registration_register) &&
     guest.capability?(:registration_manager_installed) &&
     guest.capability?(:registration_registered?)
    true
  else
    @logger.debug('Registration is skipped due to the missing guest capability')
    false
  end
end
check_configuration_options(machine, ui) click to toggle source

Issues warning if an unsupported option is used and displays a list of supported options

# File lib/vagrant-registration/action/register.rb, line 60
def check_configuration_options(machine, ui)
  manager = machine.guest.capability(:registration_manager).to_s
  available_options = machine.guest.capability(:registration_options)
  options = machine.config.registration.conf.each_pair.map { |pair| pair[0] }

  if unsupported_options_provided?(manager, available_options, options, ui)
    ui.warn("WARNING: #{manager} supports only the following options:"                      "\nWARNING: #{available_options.join(', ')}")
  end
end
credentials_provided?(machine) click to toggle source

Check if required credentials has been provided in Vagrantfile

Checks if at least one of the registration options is able to register.

# File lib/vagrant-registration/action/register.rb, line 127
def credentials_provided?(machine)
  provided = true
  credentials_required(machine).each do |registration_option|
    provided = true
    registration_option.each do |value|
      provided = false unless machine.config.registration.send value
    end
    break if provided
  end
  provided ? true : false
end
credentials_required(machine) click to toggle source

Fetch required credentials for selected manager

# File lib/vagrant-registration/action/register.rb, line 106
def credentials_required(machine)
  if machine.guest.capability?(:registration_credentials)
    machine.guest.capability(:registration_credentials)
  else
    []
  end
end
manager_installed?(guest, ui) click to toggle source

Check if selected registration manager is installed

# File lib/vagrant-registration/action/register.rb, line 96
def manager_installed?(guest, ui)
  if guest.capability(:registration_manager_installed, ui)
    true
  else
    @logger.debug('Registration manager not found on guest')
    false
  end
end
register_on_screen(machine, ui) click to toggle source

Ask user on required credentials and return them, skip options that are provided by Vagrantfile

# File lib/vagrant-registration/action/register.rb, line 141
def register_on_screen(machine, ui)
  credentials_required(machine)[0].each do |option|
    unless machine.config.registration.send(option)
      echo = !(secrets(machine).include? option)
      response = ui.ask("#{option}: ", echo: echo)
      machine.config.registration.send("#{option}=".to_sym, response)
    end
  end
  machine.config.registration
end
secrets(machine) click to toggle source

Secret options for selected manager

# File lib/vagrant-registration/action/register.rb, line 115
def secrets(machine)
  if machine.guest.capability?(:registration_secrets)
    machine.guest.capability(:registration_secrets)
  else
    []
  end
end
should_register?(machine, ui) click to toggle source

Shall we register the box?

# File lib/vagrant-registration/action/register.rb, line 51
def should_register?(machine, ui)
  !machine.config.registration.skip &&
  capabilities_provided?(machine.guest) &&
  manager_installed?(machine.guest, ui) &&
  !machine.guest.capability(:registration_registered?)
end
unsupported_options_provided?(manager, available_options, options, ui) click to toggle source

Return true if there are any unsupported options

# File lib/vagrant-registration/action/register.rb, line 72
def unsupported_options_provided?(manager, available_options, options, ui)
  warned = false
  options.each do |option|
    unless available_options.include? option
      ui.warn("WARNING: #{option} option is not supported for #{manager}")
      warned = true
    end
  end
  warned
end