File: command.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (69 lines) | stat: -rw-r--r-- 1,833 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'optparse'

require "vagrant/util/safe_puts"

module VagrantPlugins
  module CommandWinRM
    class Command < Vagrant.plugin("2", :command)
      include Vagrant::Util::SafePuts

      def self.synopsis
        "executes commands on a machine via WinRM"
      end

      def execute
        options = {
          command: [],
          shell: :powershell
        }

        opts = OptionParser.new do |o|
          o.banner = "Usage: vagrant winrm [options] [name|id]"
          o.separator ""
          o.separator "Options:"
          o.separator ""

          o.on("-c", "--command COMMAND", "Execute a WinRM command directly") do |c|
            options[:command] << c
          end

          o.on("-e", "--elevated", "Run with elevated credentials") do |e|
            options[:elevated] = true
          end

          o.on("-s", "--shell SHELL", [:powershell, :cmd], "Use specified shell (powershell, cmd)") do |s|
            options[:shell] = s
          end
        end

        argv = parse_options(opts)
        return if !argv

        with_target_vms(argv, single_target: true) do |machine|
          if machine.config.vm.communicator != :winrm
            raise Vagrant::Errors::WinRMInvalidCommunicator
          end

          opts = {
            shell: options[:shell],
            elevated: !!options[:elevated]
          }

          options[:command].each do |cmd|
            begin
              machine.communicate.execute(cmd, opts) do |type, data|
                io = type == :stderr ? $stderr : $stdout
                safe_puts(data, io: io, printer: :print)
              end
            rescue VagrantPlugins::CommunicatorWinRM::Errors::WinRMBadExitStatus
              return 1
            end
          end
        end

        # Success, exit status 0
        0
      end
    end
  end
end