Module: VagrantBolt::Util::Machine

Defined in:
lib/vagrant-bolt/util/machine.rb

Class Method Summary collapse

Class Method Details

.machine_by_name(env, name) ⇒ Object?

Get the running machine object by the machine name

Parameters:

  • env (Object)

    The enviornment to look in

  • name (String, Symbol)

    The name of the machine in the environment

Returns:

  • (Object, nil)

    The object or nil if it is not found



67
68
69
70
71
72
# File 'lib/vagrant-bolt/util/machine.rb', line 67

def self.machine_by_name(env, name)
  vm = env.active_machines.find { |m| m[0] == name.to_sym }
  env.machine(*vm) unless vm.nil?
rescue Vagrant::Errors::MachineNotFound
  nil
end

.machines_in_environment(env) ⇒ Array<Object>

Generate a list of active machines in the environment

Parameters:

  • env (Object)

    The Environment

Returns:

  • (Array<Object>)


36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-bolt/util/machine.rb', line 36

def self.machines_in_environment(env)
  env.active_machines.map { |vm|
    begin
      env.machine(*vm)
    rescue Vagrant::Errors::MachineNotFound
      nil
    end
  }.compact
end

.run_command(command, localui) ⇒ Object

Run a command locally with an execute

Parameters:

  • command (String)

    The command to run

  • localui (Object)

    The UI object to write to



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vagrant-bolt/util/machine.rb', line 10

def self.run_command(command, localui)
  localui.info(
    I18n.t('vagrant-bolt.provisioner.bolt.info.running_bolt',
           command: command),
  )

  # TODO: Update this so it works on windows platforms
  Vagrant::Util::Subprocess.execute(
    'bash',
    '-c',
    command,
    notify: [:stdout, :stderr],
    env: { PATH: ENV["VAGRANT_OLD_ENV_PATH"] },
  ) do |io_name, data|
    case io_name
    when :stdout
      localui.info data
    when :stderr
      localui.warn data
    end
  end
end

.running?(machine) ⇒ Boolean

Return if the guest is running by checking the communicator

Parameters:

  • machine (Object)

    The machine

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/vagrant-bolt/util/machine.rb', line 56

def self.running?(machine)
  # Taken from https://github.com/oscar-stack/vagrant-hosts/blob/master/lib/vagrant-hosts/provisioner/hosts.rb
  machine.communicate.ready?
rescue
  false
end

.windows?(machine) ⇒ Boolean

Return if the guest is windows. This only works for online machines

Parameters:

  • machine (Object)

    The machine

Returns:

  • (Boolean)


49
50
51
# File 'lib/vagrant-bolt/util/machine.rb', line 49

def self.windows?(machine)
  machine.config.vm.communicator == :winrm
end