Class: Bwrap::Execution::Execute

Inherits:
Object
  • Object
show all
Includes:
Output
Defined in:
lib/bwrap/execution/execute.rb

Overview

Note:

This is kind of pseudo-internal API. Hopefully there won’t be breaking changes. But please use Bwrap::Execution instead of relying functionality of this class, outside of Execute.prepend_rootcmd.

Methods that performs actual execution logic.

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Output

debug?, debug_output, error_output, handle_output_options, info_output, quiet?, trace?, trace_output, verb_output, verbose?, warn_output

Class Attribute Details

.dry_runObject

Dry run flag from parent Execution module.



22
23
24
# File 'lib/bwrap/execution/execute.rb', line 22

def dry_run
  @dry_run
end

.wObject (readonly)

Can be used to access pipe where ‘Kernel#spawn` should write its data.



19
20
21
# File 'lib/bwrap/execution/execute.rb', line 19

def w
  @w
end

Class Method Details

.clean_variablesObject

Removes data in class instance variables after execution has completed either successfully or with an error.



96
97
98
99
100
# File 'lib/bwrap/execution/execute.rb', line 96

def self.clean_variables
  @w = nil
  @r = nil
  @pipe_w = nil
end

.finish_execution(log:, wait:, direct_output:) ⇒ Object

Note:

It makes sense for caller to just return if wait has been set and not check output.

Returns output from command or nil if execution should stop here.

Returns:

  • output from command or nil if execution should stop here.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bwrap/execution/execute.rb', line 82

def self.finish_execution log:, wait:, direct_output:
  @w = @pipe_w if direct_output
  @w.close
  unless wait
    @r.close
    return # With wait == false, execute() stops here.
  end
  output = buffer_exec_output @r, log
  @r.close

  output
end

.format_command(command, rootcmd: nil, config: nil) ⇒ Object

Returns formatted command.

Parameters:

  • rootcmd (Array|Symbol|nil) (defaults to: nil)

    Flags used to construct bubblewrap environment

  • config (Bwrap::Config|nil) (defaults to: nil)

    Configuration used to launch the command inside bubblewrap

Returns:

  • formatted command



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bwrap/execution/execute.rb', line 28

def self.format_command command, rootcmd: nil, config: nil
  # Flatten the command if required, so nils can be converted in more of cases.
  # Flattenization is also done in executions, but they also take in account
  # for example rootcmd, so they probably should be done in addition to this one.
  if command.respond_to? :flatten!
    command.flatten!
  end

  prepare_for_execution command

  if config
    bwrap = Bwrap::Bwrap.new config
    bwrap.build_bwrap_arguments command
  elsif rootcmd
    prepend_rootcmd command, rootcmd: rootcmd
  else
    command
  end
end

.handle_execution_fail(fail:, error:, output:, command:) ⇒ Object

Checks whether execution failed and acts accordingly.

Raises:

  • (exception)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bwrap/execution/execute.rb', line 65

def self.handle_execution_fail fail:, error:, output:, command:
  return unless fail and !execution_success?

  if error == :show and !output.empty?
    Bwrap::Output.warn_output "Command failed with output:\n“#{output}"
  end

  exception = Bwrap::Execution::ExecutionFailed.new "Command execution failed",
                                                    command: command,
                                                    output: output

  raise exception, exception.message, caller
end

.open_pipes(direct_output) ⇒ Object

Opens pipes for command output handling.



49
50
51
52
53
54
55
# File 'lib/bwrap/execution/execute.rb', line 49

def self.open_pipes direct_output
  @r, @w = IO.pipe
  return unless direct_output

  @pipe_w = @w
  @w = $stdout
end

.prepend_rootcmd(command, rootcmd:) ⇒ Object

Stub to instruct implementation in subclass.

Raises:

  • (NotImplementedError)


103
104
105
106
# File 'lib/bwrap/execution/execute.rb', line 103

def self.prepend_rootcmd command, rootcmd:
  raise NotImplementedError, "If rootcmd execution is necessary, monkey patch Bwrap::Execution::Execute " \
                             "to add “self.prepend_rootcmd(command, rootcmd:)” method."
end

.process_output(output:) ⇒ Object

Converts output to be UTF-8.



58
59
60
61
62
# File 'lib/bwrap/execution/execute.rb', line 58

def self.process_output output:
  # read_nonblock() uses read(), which always reads as ASCII-8BIT:
  #   In the case of an integer length, the resulting string is always in ASCII-8BIT encoding.
  output.force_encoding("UTF-8").strip
end