Class: CemWinSpec::WinExec::WinRMCmd
- 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
Instance Attribute Summary collapse
-
#conn_opts ⇒ Object
readonly
Returns the value of attribute conn_opts.
Attributes inherited from BaseCmd
#env_vars, #puppet_version, #working_dir
Instance Method Summary collapse
- #available? ⇒ Boolean
- #create_dir(path) ⇒ Object
- #delete_file(path) ⇒ Object
- #file_exists?(path) ⇒ Boolean
-
#initialize(conn_opts, working_dir = nil, quiet: false, puppet_version: nil, **env_vars) ⇒ WinRMCmd
constructor
A new instance of WinRMCmd.
-
#run(cmd, *_args, **kwargs) ⇒ Object
Run a command over WinRM.
- #temp_dir ⇒ Object
- #upload(local_path, remote_path) ⇒ Object
Methods inherited from BaseCmd
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_opts ⇒ Object (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
20 21 22 |
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 20 def available? @available end |
#create_dir(path) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 54 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
62 63 64 65 66 67 68 |
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 62 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
70 71 72 |
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 70 def file_exists?(path) file_manager.exists?(path) end |
#run(cmd, *_args, **kwargs) ⇒ Object
Run a command over WinRM
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 32 def run(cmd, *_args, **kwargs) cmd = command(cmd, **kwargs) log_command(cmd) shell = nil output = nil begin shell = conn.shell(:powershell) output = shell.run(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_dir ⇒ Object
74 75 76 |
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 74 def temp_dir file_manager.temp_dir end |
#upload(local_path, remote_path) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/cem_win_spec/win_exec/cmd/winrm_cmd.rb', line 78 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 |