Class: VagrantBolt::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-bolt/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



8
9
10
# File 'lib/vagrant-bolt/command.rb', line 8

def self.synopsis
  "Calls the bolt executable with the given options"
end

Instance Method Details

#executeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant-bolt/command.rb', line 12

def execute
  options = {}
  options[:update] = false

  parser = OptionParser.new do |o|
    o.banner = "Usage: vagrant bolt <options> [bolt options]"
    o.separator ""
    o.separator "Options:"
    o.separator ""

    o.on("-u", "--[no-]updateinventory", "Update the inventory file (defaults to false)") do |u|
      options[:update] = u
    end
  end

  # This is a hack. We are passing everything to bolt, but still allow for bolt options.
  # We just remove them from @argv here and use the rest.
  # This allows for not having to define a seperator as long as there are no argument colisions
  return if @argv.empty?

  args = @argv.dup
  begin
    parser.parse!(args)
  rescue OptionParser::InvalidOption
    retry
  end
  bolt_args = @argv - ['-u', '--updateinventory', '--no-updateinventory']

  VagrantBolt::Util::Bolt.update_inventory_file(@env) if options[:update]

  execute_bolt_command(bolt_args)
end

#execute_bolt_command(args) ⇒ Object

Run a bolt command with the inventory path, and project

Parameters:

  • args (Array<String>)

    An array containing the bolt arguments



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-bolt/command.rb', line 47

def execute_bolt_command(args)
  bolt_exe = @env.vagrantfile.config.bolt.bolt_exe
  project = VagrantBolt::Util::Config.relative_path(@env.vagrantfile.config.bolt.project, @env.root_path)
  inventoryfile = VagrantBolt::Util::Bolt.inventory_file(@env)

  quoted_args = args.flatten.compact.map { |a| "'#{a}'" }
  command = [
    bolt_exe,
    quoted_args,
    '--project',
    project,
  ]
  command << ['--inventoryfile', "\'#{inventoryfile}\'"] if File.exist?(inventoryfile)
  VagrantBolt::Util::Machine.run_command(command.flatten.join(' '), @env.ui)
end