Class: Bolt::Transport::LocalWindows
- Defined in:
- lib/bolt/transport/local_windows.rb
Constant Summary
Constants inherited from Base
Base::ENVIRONMENT_METHODS, Base::STDIN_METHODS
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #connected?(_targets) ⇒ Boolean
- #copy_file(source, destination) ⇒ Object
- #default_input_method(executable) ⇒ Object
- #execute(*command, options) ⇒ Object
- #provided_features ⇒ Object
- #run_command(target, command, options = {}) ⇒ Object
- #run_script(target, script, arguments, options = {}) ⇒ Object
- #run_task(target, task, arguments, options = {}) ⇒ Object
- #upload(target, source, destination, options = {}) ⇒ Object
Methods inherited from Base
#assert_batch_size_one, #batch_command, #batch_connected?, #batch_script, #batch_task, #batch_upload, #batches, default_options, #envify_params, filter_options, #initialize, #select_implementation, #select_interpreter, #unwrap_sensitive_args, #with_events
Constructor Details
This class inherits a constructor from Bolt::Transport::Base
Class Method Details
.options ⇒ Object
15 16 17 |
# File 'lib/bolt/transport/local_windows.rb', line 15 def self. %w[tmpdir interpreters run-as run-as-command sudo-password] end |
.validate(options) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/bolt/transport/local_windows.rb', line 28 def self.validate() logger = Logging.logger[self] if ['sudo-password'] || ['run-as'] || ['run-as-command'] || [:run_as] logger.warn("run-as is not supported for Windows hosts using the local transport") end end |
Instance Method Details
#connected?(_targets) ⇒ Boolean
199 200 201 |
# File 'lib/bolt/transport/local_windows.rb', line 199 def connected?(_targets) true end |
#copy_file(source, destination) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/bolt/transport/local_windows.rb', line 49 def copy_file(source, destination) logger.debug { "Uploading #{source}, to #{destination}" } FileUtils.cp_r(source, destination, remove_destination: true) rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |
#default_input_method(executable) ⇒ Object
23 24 25 26 |
# File 'lib/bolt/transport/local_windows.rb', line 23 def default_input_method(executable) input_method ||= Powershell.powershell_file?(executable) ? 'powershell' : 'both' input_method end |
#execute(*command, options) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bolt/transport/local_windows.rb', line 66 def execute(*command, ) # Interpreter can be an array or string. It will be appended to the command array. command.unshift([:interpreter]).flatten! if [:interpreter] command = [[:env]] + command if [:env] if [:stdin] stdout, stderr, rc = Open3.capture3(*command, stdin_data: [:stdin], chdir: [:dir]) else stdout, stderr, rc = Open3.capture3(*command, chdir: [:dir]) end result_output = Bolt::Node::Output.new result_output.stdout << stdout unless stdout.nil? result_output.stderr << stderr unless stderr.nil? result_output.exit_code = rc.exitstatus result_output end |
#provided_features ⇒ Object
19 20 21 |
# File 'lib/bolt/transport/local_windows.rb', line 19 def provided_features ['powershell'] end |
#run_command(target, command, options = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bolt/transport/local_windows.rb', line 90 def run_command(target, command, = {}) self.class.validate() in_tmpdir(target.['tmpdir']) do |dir| output = execute(command, dir: dir) Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code, 'command', command) end end |
#run_script(target, script, arguments, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bolt/transport/local_windows.rb', line 102 def run_script(target, script, arguments, = {}) self.class.validate() with_tmpscript(File.absolute_path(script), target.['tmpdir']) do |file, dir| logger.debug "Running '#{file}' with #{arguments.to_json}" # unpack any Sensitive data AFTER we log arguments = unwrap_sensitive_args(arguments) if Powershell.powershell_file?(file) command = Powershell.run_script(arguments, file) interpreter = ['powershell.exe', *Powershell.ps_args] output = execute(command, dir: dir, interpreter: interpreter) else path, args = *Powershell.process_from_extension(file) args += Powershell.escape_arguments(arguments) command = args.unshift(path).join(' ') output = execute(command, dir: dir) end Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code, 'script', script) end end |
#run_task(target, task, arguments, options = {}) ⇒ Object
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/bolt/transport/local_windows.rb', line 127 def run_task(target, task, arguments, = {}) self.class.validate() implementation = select_implementation(target, task) executable = implementation['path'] input_method = implementation['input_method'] extra_files = implementation['files'] in_tmpdir(target.['tmpdir']) do |dir| if extra_files.empty? script = File.join(dir, File.basename(executable)) else arguments['_installdir'] = dir script_dest = File.join(dir, task.tasks_dir) FileUtils.mkdir_p([script_dest] + extra_files.map { |file| File.join(dir, File.dirname(file['name'])) }) script = File.join(script_dest, File.basename(executable)) extra_files.each do |file| dest = File.join(dir, file['name']) copy_file(file['path'], dest) File.chmod(0o750, dest) end end copy_file(executable, script) File.chmod(0o750, script) interpreter = select_interpreter(script, target.['interpreters']) interpreter_debug = interpreter ? " using '#{interpreter}' interpreter" : nil # log the arguments with sensitive data redacted, do NOT log unwrapped_arguments logger.debug("Running '#{script}' with #{arguments.to_json}#{interpreter_debug}") unwrapped_arguments = unwrap_sensitive_args(arguments) stdin = STDIN_METHODS.include?(input_method) ? JSON.dump(unwrapped_arguments) : nil if ENVIRONMENT_METHODS.include?(input_method) environment_params = envify_params(unwrapped_arguments).each_with_object([]) do |(arg, val), list| list << Powershell.set_env(arg, val) end environment_params = environment_params.join("\n") + "\n" else environment_params = "" end if Powershell.powershell_file?(script) && stdin.nil? command = Powershell.run_ps_task(arguments, script, input_method) command = environment_params + Powershell.shell_init + command interpreter ||= ['powershell.exe', *Powershell.ps_args] output = if input_method == 'powershell' execute(command, dir: dir, interpreter: interpreter) else execute(command, dir: dir, stdin: stdin, interpreter: interpreter) end end unless output if interpreter env = ENVIRONMENT_METHODS.include?(input_method) ? envify_params(unwrapped_arguments) : nil output = execute(script, stdin: stdin, env: env, dir: dir, interpreter: interpreter) else path, args = *Powershell.process_from_extension(script) command = args.unshift(path).join(' ') command = environment_params + Powershell.shell_init + command output = execute(command, dir: dir, stdin: stdin, interpreter: 'powershell.exe') end end Bolt::Result.for_task(target, output.stdout.string, output.stderr.string, output.exit_code, task.name) end end |
#upload(target, source, destination, options = {}) ⇒ Object
84 85 86 87 88 |
# File 'lib/bolt/transport/local_windows.rb', line 84 def upload(target, source, destination, = {}) self.class.validate() copy_file(source, destination) Bolt::Result.for_upload(target, source, destination) end |