Class: PDK::CLI::Exec::Command
- Inherits:
-
Object
- Object
- PDK::CLI::Exec::Command
- Defined in:
- lib/pdk/cli/exec/command.rb
Direct Known Subclasses
Constant Summary collapse
- TEMPFILE_MODE =
File::RDWR | File::BINARY | File::CREAT | File::TRUNC
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#context ⇒ Object
Returns the value of attribute context.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#exec_group ⇒ Object
writeonly
Sets the attribute exec_group.
-
#spinner ⇒ TTY::Spinner?
readonly
private
The spinner for this command.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #add_spinner(message, opts = {}) ⇒ Object
-
#execute! ⇒ Hash[Symbol => Object]
The result from executing the command :stdout => String : The result of STDOUT :stderr => String : The result of STDERR :exit_code => Integer : The exit code from the command :duration => Float : Number seconds it took to execute.
-
#initialize(*argv) ⇒ Command
constructor
A new instance of Command.
- #register_spinner(spinner, opts = {}) ⇒ Object
- #update_environment(additional_env) ⇒ Object
Constructor Details
#initialize(*argv) ⇒ Command
Returns a new instance of Command.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pdk/cli/exec/command.rb', line 21 def initialize(*argv) require 'childprocess' require 'tempfile' @argv = argv @process = ChildProcess.build(*@argv) # https://github.com/puppetlabs/pdk/issues/1083: # When @process.leader is set, childprocess will set the CREATE_BREAKAWAY_FROM_JOB # and JOB_OBJECT_LIMIT_BREAKAWAY_OK flags in the Win32 API calls. This will cause # issues on systems > Windows 7 / Server 2008, if the JOB_OBJECT_LIMIT_BREAKAWAY_OK # flag is set and the Task Scheduler is trying to kick off a job, it can sometimes # result in ACCESS_DENIED being returned by the Win32 API, depending on the permission # levels / user account this user. # The resolution for pdk/issues/1083 is to ensure @process.leader is not set. # This will potentially cause issues on older Windows systems, in which case we may # need to revisit and conditionally set this param depending on what OS we're on # @process.leader = true @stdout = Tempfile.new('stdout', mode: TEMPFILE_MODE).tap { |io| io.sync = true } @stderr = Tempfile.new('stderr', mode: TEMPFILE_MODE).tap { |io| io.sync = true } @process.io.stdout = @stdout @process.io.stderr = @stderr # Default to running things in the system context. @context = :system # Extra environment vars to add to base set. @environment = {} # Register the ExecGroup when running in parallel @exec_group = nil end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
7 8 9 |
# File 'lib/pdk/cli/exec/command.rb', line 7 def argv @argv end |
#context ⇒ Object
Returns the value of attribute context.
7 8 9 |
# File 'lib/pdk/cli/exec/command.rb', line 7 def context @context end |
#environment ⇒ Object
Returns the value of attribute environment.
8 9 10 |
# File 'lib/pdk/cli/exec/command.rb', line 8 def environment @environment end |
#exec_group=(value) ⇒ Object (writeonly)
Sets the attribute exec_group
9 10 11 |
# File 'lib/pdk/cli/exec/command.rb', line 9 def exec_group=(value) @exec_group = value end |
#spinner ⇒ TTY::Spinner? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The spinner for this command. This should only be used for testing
17 18 19 |
# File 'lib/pdk/cli/exec/command.rb', line 17 def spinner @spinner end |
#timeout ⇒ Object
Returns the value of attribute timeout.
8 9 10 |
# File 'lib/pdk/cli/exec/command.rb', line 8 def timeout @timeout end |
Instance Method Details
#add_spinner(message, opts = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pdk/cli/exec/command.rb', line 73 def add_spinner(, opts = {}) require 'pdk/cli/util' return unless PDK::CLI::Util.interactive? @success_message = opts.delete(:success) @failure_message = opts.delete(:failure) require 'pdk/cli/util/spinner' @spinner = TTY::Spinner.new("[:spinner] #{}", opts.merge(PDK::CLI::Util.spinner_opts_for_platform)) end |
#execute! ⇒ Hash[Symbol => Object]
Returns The result from executing the command :stdout => String : The result of STDOUT :stderr => String : The result of STDERR :exit_code => Integer : The exit code from the command :duration => Float : Number seconds it took to execute.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 |
# File 'lib/pdk/cli/exec/command.rb', line 95 def execute! # Start spinning if configured. @spinner&.auto_spin # Set env for child process resolved_env_for_command.each { |k, v| @process.environment[k] = v } if [:module, :pwd].include?(context) require 'pdk/util' mod_root = PDK::Util.module_root unless mod_root @spinner&.error raise PDK::CLI::FatalError, 'Current working directory is not part of a module. (No metadata.json was found.)' end if Dir.pwd == mod_root || context == :pwd run_process_in_clean_env! else Dir.chdir(mod_root) do run_process_in_clean_env! end end else run_process! end # Stop spinning when done (if configured). stop_spinner @stdout.rewind @stderr.rewind process_data = { stdout: @stdout.read, stderr: @stderr.read, exit_code: @process.exit_code, duration: @duration } PDK.logger.debug format('STDOUT: %{output}', output: process_data[:stdout].empty? ? 'N/A' : "\n#{process_data[:stdout]}") PDK.logger.debug format('STDERR: %{output}', output: process_data[:stderr].empty? ? 'N/A' : "\n#{process_data[:stderr]}") process_data ensure @stdout.close @stderr.close end |
#register_spinner(spinner, opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pdk/cli/exec/command.rb', line 62 def register_spinner(spinner, opts = {}) require 'pdk/cli/util' return unless PDK::CLI::Util.interactive? @success_message = opts.delete(:success) @failure_message = opts.delete(:failure) @spinner = spinner end |
#update_environment(additional_env) ⇒ Object
86 87 88 |
# File 'lib/pdk/cli/exec/command.rb', line 86 def update_environment(additional_env) @environment.merge!(additional_env) end |