Module: Mixlib::ShellOut::Windows

### Home Classes Methods

### In Files

- lib/mixlib/shellout/windows.rb

### Namespace

- MODULE Mixlib::ShellOut::Windows::Utils
- CLASS Mixlib::ShellOut::Windows::ThingThatLooksSortOfLikeAProcessStatus

### Methods

- #run_command
- #validate_options

### Included Modules

- ::Windows::Handle
- ::Windows::Process
- ::Windows::Synchronize

### Files

- LICENSE

### Class/Module Index

Quicksearch

- Mixlib
- Mixlib::ShellOut
- Mixlib::ShellOut::CommandTimeout
- Mixlib::ShellOut::InvalidCommandOption
- Mixlib::ShellOut::ShellCommandFailed
- Mixlib::ShellOut::Unix
- Mixlib::ShellOut::Windows
- Mixlib::ShellOut::Windows::ThingThatLooksSortOfLikeAProcessStatus
- Mixlib::ShellOut::Windows::Utils
- Process
- Process::Constants
- Process::Functions
- Process::Functions::FFI
- Process::Functions::FFI::Library

No matching classes.

# Mixlib::ShellOut::Windows

### Constants

IS_BATCH_FILE
TIME_SLICE

### Public Instance Methods

run_command() click to toggle source

    # File lib/mixlib/shellout/windows.rb, line 50
    def run_command

      #
      # Create pipes to capture stdout and stderr,
      #
      stdout_read, stdout_write = IO.pipe
      stderr_read, stderr_write = IO.pipe
      stdin_read, stdin_write = IO.pipe
      open_streams = [ stdout_read, stderr_read ]

      begin

        #
        # Set cwd, environment, appname, etc.
        #
        app_name, command_line = command_to_run(self.command)
        create_process_args = {
          :app_name => app_name,
          :command_line => command_line,
          :startup_info => {
            :stdout => stdout_write,
            :stderr => stderr_write,
            :stdin => stdin_read
          },
          :environment => inherit_environment.map { |k,v| "#{k}=#{v}" },
          :close_handles => false
        }
        create_process_args[:cwd] = cwd if cwd
        # default to local account database if domain is not specified
        create_process_args[:domain] = domain.nil? ? "." : domain
        create_process_args[:with_logon] = with_logon if with_logon
        create_process_args[:password] = password if password

        #
        # Start the process
        #
        process = Process.create(create_process_args)
        begin
          # Start pushing data into input
          stdin_write << input if input

          # Close pipe to kick things off
          stdin_write.close

          #
          # Wait for the process to finish, consuming output as we go
          #
          start_wait = Time.now
          while true
            wait_status = WaitForSingleObject(process.process_handle, 0)
            case wait_status
            when WAIT_OBJECT_0
              # Get process exit code
              exit_code = [0].pack('l')
              unless GetExitCodeProcess(process.process_handle, exit_code)
                raise get_last_error
              end
              @status = ThingThatLooksSortOfLikeAProcessStatus.new
              @status.exitstatus = exit_code.unpack('l').first

              return self
            when WAIT_TIMEOUT
              # Kill the process
              if (Time.now - start_wait) > timeout
                begin
                  Process.kill(:KILL, process.process_id)
                rescue Errno::EIO
                  logger.warn("Failed to kill timed out process #{process.process_id}") if logger
                end

                raise Mixlib::ShellOut::CommandTimeout, "command timed out:\n#{format_for_exception}"
              end

              consume_output(open_streams, stdout_read, stderr_read)
            else
              raise "Unknown response from WaitForSingleObject(#{process.process_handle}, #{timeout*1000}): #{wait_status}"
            end

          end

        ensure
          CloseHandle(process.thread_handle) if process.thread_handle
          CloseHandle(process.process_handle) if process.process_handle
        end

      ensure
        #
        # Consume all remaining data from the pipes until they are closed
        #
        stdout_write.close
        stderr_write.close

        while consume_output(open_streams, stdout_read, stderr_read)
        end
      end
    end

validate_options(opts) click to toggle source

Option validation that is windows specific

    # File lib/mixlib/shellout/windows.rb, line 39
    def validate_options(opts)
      if opts[:user]
        unless opts[:password]
          raise InvalidCommandOption, "You must supply both a username and password when supplying a user in windows"
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.