Sha256: 20a2d053f94a172430085154a99a3d65f44e5b157858b972916937ae0c463779

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'optparse'
require 'vagrant/util/safe_puts'

module VagrantPlugins
  module VagrantWinRM
    class WinRM < Vagrant.plugin('2', :command)
      def self.synopsis
        'connects to machine via WinRM'
      end

      def execute
        options = {}

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

          o.on('-c', '--command COMMAND', 'Execute a WinRM command directly') do |c|
            options[:command] = Array.new if options[:command].nil?
            options[:command].push c
          end

          o.on('--plugin-version', 'Print the version of the plugin and exit') do
            options[:version] = true
          end
        end

        # Parse the options and return if we don't have any target.
        argv = parse_options(opts)
        return unless argv

        if options[:version]
          require "#{VagrantPlugins::VagrantWinRM.source_root}/lib/version"
          safe_puts "Vagrant-winrm plugin #{VERSION}"
          return 0
        end

        return 0 unless options[:command]

        # Execute the actual WinRM command
        with_target_vms(argv, single_target: true) do |vm|

          raise Errors::ConfigurationError, { :communicator => vm.config.vm.communicator } if vm.config.vm.communicator != :winrm

          exit_code = 0
          @logger.debug("Executing a batch of #{options[:command].length} on remote machine")

          options[:command].each do |c|
            @logger.debug("Executing command: #{c}")
            c.gsub! '"', '\"' if c.include? '`' or c.include? '$(' # Powershell is so strange sometimes!
            exit_code |= vm.communicate.execute(c) do |type, data|
              $stdout.print data if type == :stdout
              $stderr.print data if type == :stderr
            end
          end
          return exit_code
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-winrm-0.0.2 lib/vagrant-winrm/commands/winrm.rb