Class: CemWinSpec::WinExec::WinRMCmd

Inherits:
BaseCmd
  • Object
show all
Defined in:
lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb

Overview

Class for executing PowerShell commands over WinRM

Constant Summary

Constants inherited from BaseCmd

BaseCmd::COMMAND_SEPARATOR, BaseCmd::PUPPET_VER_TO_RUBY_VER, BaseCmd::TOOL_DIR_BY_RUBY_VER

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Attributes inherited from BaseCmd

#env_vars, #puppet_version, #working_dir

Instance Method Summary collapse

Methods inherited from BaseCmd

#command, #ruby_version

Methods included from Logging

#current_log_format, current_log_format, current_log_level, #current_log_level, included, log_setup!, #log_setup!, logger, #logger, new_log_formatter, #new_log_formatter, new_log_level, #new_log_level

Constructor Details

#initialize(conn_opts, working_dir = nil, quiet: false, puppet_version: nil, **env_vars) ⇒ WinRMCmd

Returns a new instance of WinRMCmd.



13
14
15
16
17
18
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 13

def initialize(conn_opts, working_dir = nil, quiet: false, puppet_version: nil, **env_vars)
  @conn_opts = conn_opts
  @quiet = quiet
  @conn = new_conn(@conn_opts.to_h)
  super(working_dir, puppet_version: puppet_version, **env_vars)
end

Instance Attribute Details

#conn_optsObject (readonly)

Returns the value of attribute conn_opts.



11
12
13
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 11

def conn_opts
  @conn_opts
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 20

def available?
  @available
end

#create_dir(path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 46

def create_dir(path)
  result = file_manager.create_dir(path)
  raise "Failed to create directory #{path}" unless result

  logger.info "Created directory #{path}"
  result
end

#delete_file(path) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 54

def delete_file(path)
  result = file_manager.delete(path)
  raise "Failed to delete file #{path}" unless result

  logger.info "Deleted file #{path}"
  result
end

#file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 62

def file_exists?(path)
  file_manager.exists?(path)
end

#run(cmd, *_args, **_kwargs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 24

def run(cmd, *_args, **_kwargs)
  cmd = cmd.join('; ') if cmd.is_a?(Array)
  log_command(cmd)
  shell = nil
  output = nil
  begin
    shell = conn.shell(:powershell)
    output = shell.run(command(cmd)) do |stdout, stderr|
      logger << stdout if stdout
      logger << stderr if stderr
    end
  rescue WinRM::WinRMAuthorizationError => e
    @available = false
    raise e
  ensure
    shell&.close
  end
  raise 'Something went wrong, no output from command' if output.nil?

  output
end

#temp_dirObject



66
67
68
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 66

def temp_dir
  file_manager.temp_dir
end

#upload(local_path, remote_path) ⇒ Object



70
71
72
73
74
75
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 70

def upload(local_path, remote_path)
  file_manager.upload(local_path, remote_path) do |bytes_copied, total_bytes, lpath, rpath|
    logger.debug "Copied #{bytes_copied} of #{total_bytes} bytes from #{lpath} to #{rpath}"
  end
  logger.info "Uploaded #{local_path} to #{remote_path}"
end