Class: Bwrap::Execution::Exec

Inherits:
Object
  • Object
show all
Defined in:
lib/bwrap/execution/exec.rb

Overview

Actually performs the execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Exec

Returns a new instance of Exec.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bwrap/execution/exec.rb', line 23

def initialize command
  @command = command

  @fail = true
  @wait = true
  @log = true
  @direct_output = false
  @env = nil
  @clear_env = false
  @error = nil
  @config = nil
  @rootcmd = nil
end

Instance Attribute Details

#clear_env=(value) ⇒ Object (writeonly)

Sets the attribute clear_env



10
11
12
# File 'lib/bwrap/execution/exec.rb', line 10

def clear_env=(value)
  @clear_env = value
end

#config=(value) ⇒ Object (writeonly)

Sets the attribute config



11
12
13
# File 'lib/bwrap/execution/exec.rb', line 11

def config=(value)
  @config = value
end

#direct_output=(value) ⇒ Object (writeonly)

Sets the attribute direct_output



12
13
14
# File 'lib/bwrap/execution/exec.rb', line 12

def direct_output=(value)
  @direct_output = value
end

#dry_run=(value) ⇒ Object (writeonly)



8
9
10
# File 'lib/bwrap/execution/exec.rb', line 8

def dry_run=(value)
  @dry_run = value
end

#env=(value) ⇒ Object (writeonly)

Sets the attribute env



13
14
15
# File 'lib/bwrap/execution/exec.rb', line 13

def env=(value)
  @env = value
end

#error=(value) ⇒ Object (writeonly)

Sets the attribute error



14
15
16
# File 'lib/bwrap/execution/exec.rb', line 14

def error=(value)
  @error = value
end

#fail=(value) ⇒ Object (writeonly)

Sets the attribute fail



15
16
17
# File 'lib/bwrap/execution/exec.rb', line 15

def fail=(value)
  @fail = value
end

#last_statusObject (readonly)

Returns the value of attribute last_status.



20
21
22
# File 'lib/bwrap/execution/exec.rb', line 20

def last_status
  @last_status
end

#log=(value) ⇒ Object (writeonly)

Sets the attribute log



16
17
18
# File 'lib/bwrap/execution/exec.rb', line 16

def log=(value)
  @log = value
end

#rootcmd=(value) ⇒ Object (writeonly)

Sets the attribute rootcmd



17
18
19
# File 'lib/bwrap/execution/exec.rb', line 17

def rootcmd=(value)
  @rootcmd = value
end

#wait=(value) ⇒ Object (writeonly)

Sets the attribute wait



18
19
20
# File 'lib/bwrap/execution/exec.rb', line 18

def wait=(value)
  @wait = value
end

Instance Method Details

#executeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bwrap/execution/exec.rb', line 42

def execute
  # Default to two steps back in history. Hopefully this is correct amount...
  log_callback = @log_callback || 1

  command = Bwrap::Execution::Execute.format_command @command, rootcmd: @rootcmd, config: @config
  Bwrap::Execution::Logging.handle_logging command, log_callback: log_callback, log: @log, dry_run: @dry_run
  return if @dry_run || Bwrap::Execution::Execute.dry_run

  Bwrap::Execution::Execute.open_pipes @direct_output

  # If command is an array, there can’t be arrays inside the array.
  # For convenience, the array is flattened here, so callers can construct commands more easily.
  if command.respond_to? :flatten!
    command.flatten!
  end

  env = @env || {}

  # If command is string, splat operator (the *) does not do anything. If array, it expand the arguments.
  # This causes spawning work correctly, as that’s how spawn() expects to have the arguments.
  pid = spawn(env, *command, err: [ :child, :out ], out: Bwrap::Execution::Execute.w, unsetenv_others: @clear_env)

  output = Bwrap::Execution::Execute.finish_execution(log: @log, wait: @wait, direct_output: @direct_output)
  return pid unless @wait

  # This is instant return, but allows us to have $?/$CHILD_STATUS set.
  Process.wait pid
  @last_status = $CHILD_STATUS

  output = Bwrap::Execution::Execute.process_output output: output
  Bwrap::Execution::Execute.handle_execution_fail fail: @fail, error: @error, output: output, command: command

  output
ensure
  Bwrap::Execution::Execute.clean_variables
end

#log_callback=(value) ⇒ Object



37
38
39
40
# File 'lib/bwrap/execution/exec.rb', line 37

def log_callback= value
  # Adds one since this is another method in the chain.
  @log_callback = value + 1
end