Module: Veewee::Provider::Core::Helper::Winrm

Defined in:
lib/veewee/provider/core/helper/winrm.rb

Instance Method Summary collapse

Instance Method Details

#new_session(host, options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/veewee/provider/core/helper/winrm.rb', line 98

def new_session(host,options)
  opts = winrm_options.merge(options)
  # create a session
  begin
    endpoint = "http://#{host}:#{opts[:port]}/wsman"
    client = ::WinRM::WinRMWebService.new(endpoint, :plaintext, opts)
    if opts[:operation_timeout]
      client.set_timeout(opts[:operation_timeout])
    end
  rescue ::WinRM::WinRMAuthorizationError => error
    raise ::WinRM::WinRMAuthorizationError.new("#{error.message}@#{host}")
  end
  client
end

#when_winrm_login_works(ip = "127.0.0.1", options = {}, &block) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/veewee/provider/core/helper/winrm.rb', line 40

def (ip="127.0.0.1", options = {}, &block)

  #options=winrm_options.merge(options.merge({:operation_timeout => 5}))
  options=winrm_options.merge(options)
  @login_works ||= {}
  begin
    Timeout::timeout(options[:timeout]) do
      if @login_works[ip]
        block.call(ip);
      else
        env.ui.info  "Waiting for winrm login on #{ip} with user #{options[:user]} to windows on port => #{options[:port]} to work, timeout=#{options[:timeout]} sec"
        until @connected do
          begin
            sleep 1
            env.ui.info ".",{:new_line => false}
            next if not winrm_up?(ip, options)
            winrm_execute(ip,"hostname",options.merge({:progress => nil}))
            @login_works[ip]=true
            env.ui.info "\n"
            block.call(ip);
            env.ui.info ""
            sleep 1
            @connected = true
            return true
          rescue Exception => e
            puts e.inspect
            puts e.message
            puts e.backtrace.inspect
            sleep 5
          end
        end
      end
    end
  rescue Timeout::Error
    env.ui.error "Winrm timeout #{options[:timeout]} sec has been reached."
    exit -1
  rescue WinRM::WinRMHTTPTransportError => e
    if e.message =~ /401/
      env.ui.error "Unable to authenticate as '#{options[:user]}' with password '#{options[:pass]}'"
    else
      raise e
    end
  end
  return false
end

#winrm_execute(host, command, options) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/veewee/provider/core/helper/winrm.rb', line 113

def winrm_execute(host,command, options)

  options = winrm_options.merge( # global default
                                { # function defaults
                                  :exitcode => "0",
                                  :progress => "on"
                                }.merge(
                                  options # calling override
                                ))
                                stdout=""
                                stderr=""
                                status=-99999

                                @session ||= new_session(host, options)

                                env.ui.info "Executing winrm command: #{command}" if options[:progress]

                                @remote_id ||= @session.open_shell
                                command_id = @session.run_command(@remote_id, command)
                                output = @session.get_command_output(@remote_id, command_id) do |out,error|
                                  if out
                                    stdout += out
                                    env.ui.info out,{:new_line => false} if options[:progress]
                                  end
                                  if error
                                    stderr += error
                                    env.ui.info error,{:new_line => false} if options[:progress]
                                  end
                                end
                                status = output[:exitcode]
                                @session.close_shell(@remote_id)
                                @remote_id = nil
                                # env.ui.info "EXITCODE: #{status}" if status > 0
                                # @session.unbind
                                # FIXME: May want to look for a list of possible exitcodes
                                if (status.to_s != options[:exitcode] )
                                  if (options[:exitcode]=="*")
                                    #its a test so we don't need to worry
                                  else
                                    raise "Exitcode was not what we expected"
                                  end

                                end

                                return Veewee::Provider::Core::Helper::WinrmResult.new(stdout,stderr,status)

end

#winrm_transfer_file(host, filename, destination = '.', options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/veewee/provider/core/helper/winrm.rb', line 87

def winrm_transfer_file(host,filename,destination = '.' , options = {})
  options = winrm_options.merge(options.merge({:paranoid => false }))
  # when_winrm_login_works

  env.ui.info "FIXME: Transferring #{filename} to #{destination} "
  # print these out while uploading:
  env.ui.info ".",{:new_line => false}
  # t::SSH.start( host,options[:user],options )
  env.ui.info ""
end

#winrm_up?(ip, options) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/veewee/provider/core/helper/winrm.rb', line 25

def winrm_up?(ip,options)
  begin
    if not @winrm_up
      @httpcli = HTTPClient.new(:agent_name => 'Ruby WinRM Client')
      @httpcli.receive_timeout = 10
      @httpcli.set_auth(nil, options[:user], options[:pass])
      @httpcli.get("http://#{ip}:#{options[:port]}/wsman")
      @winrm_up = true
    end
  rescue HTTPClient::ReceiveTimeoutError,HTTPClient::ConnectTimeoutError
    @winrm_up = false
  end
end