Class: VagrantPlugins::Exec::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



5
6
7
# File 'lib/vagrant-exec/command.rb', line 5

def self.synopsis
  'executes commands in virtual machine'
end

Instance Method Details

#executeObject



9
10
11
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
44
45
46
47
48
49
# File 'lib/vagrant-exec/command.rb', line 9

def execute
  cmd, cmd_args = parse_args
  cmd && cmd_args or return nil

  # Execute the actual SSH
  with_target_vms(nil, single_target: true) do |vm|
    settings = vm.config.exec._parsed_commands
    passed_command, constructed_command = cmd.dup, ''

    # directory is applied only once in the beginning
    settings.reverse.each do |command|
      if command_matches?(command[:cmd], passed_command) && !directory_added?
        constructed_command << add_directory(command[:opts][:directory])
      end
    end

    # apply environment variables
    settings.each do |command|
      if command_matches?(command[:cmd], passed_command)
        constructed_command << add_env(command[:opts][:env])
      end
    end

    # apply prepend in the end
    settings.each do |command|
      if command_matches?(command[:cmd], passed_command)
        constructed_command << add_prepend(command[:opts][:prepend])
      end
    end

    constructed_command << passed_command
    constructed_command << ' ' << cmd_args.join(' ') if cmd_args.any?

    @logger.info("Executing single command on remote machine: #{constructed_command}")
    ssh_opts = { extra_args: ['-q'] } # make it quiet
    env = vm.action(:ssh_run, ssh_run_command: constructed_command, ssh_opts: ssh_opts)

    status = env[:ssh_run_exit_status] || 0
    return status
  end
end