class VagrantPlugins::Registration::Action::Register

This registers the guest if the guest plugin supports it

Public Class Methods

new(app, env) click to toggle source
# File lib/vagrant-registration/action/register.rb, line 8
def initialize(app, env)
  @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)
  @app.call(env)

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

  if capabilities_provided?(guest) && manager_installed?(guest) && !config.skip
    env[:ui].info("Registering box with vagrant-registration...")

    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) unless config.skip
  end

  @logger.debug("Registration is skipped due to the configuration") if config.skip
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 45
def capabilities_provided?(guest)
  if guest.capability?(:registration_register) && guest.capability?(:registration_manager_installed)
    true
  else
    @logger.debug("Registration is skipped due to the missing guest capability")
    false
  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 86
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 65
def credentials_required(machine)
  if machine.guest.capability?(:registration_credentials)
    machine.guest.capability(:registration_credentials)
  else
    []
  end
end
manager_installed?(guest) click to toggle source

Check if selected registration manager is installed

# File lib/vagrant-registration/action/register.rb, line 55
def manager_installed?(guest)
  if guest.capability(:registration_manager_installed)
    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 100
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_s}=".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 74
def secrets(machine)
  if machine.guest.capability?(:registration_secrets)
    machine.guest.capability(:registration_secrets)
  else
    []
  end
end