Class: Yast::Execute

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
I18n
Defined in:
library/system/src/lib/yast2/execute.rb

Overview

A module for executing scripts/programs in a safe way (not prone to shell quoting bugs). It uses Cheetah as the backend, but adds support for chrooting during the installation. It also globally switches the default Cheetah logger to Y2Logger.

To limit logging sensitive input/output/arguments, you can pass a ReducedRecorder as the recorder option.

Examples:

Methods of this class can be chained.


Yast::Execute.locally!.stdout("ls", "-l")
Yast::Execute.stdout.on_target!("ls", "-l")

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Execute

Constructor

Parameters:

  • options (Hash<Symbol, Object>) (defaults to: {})

    options to add for the execution. Some of these options are directly passed to Cheetah#run, and others are used to control the behavior when running commands (e.g., to indicate if a popup should be shown when the command fails). See #options.



60
61
62
63
64
# File 'library/system/src/lib/yast2/execute.rb', line 60

def initialize(options = {})
  textdomain "base"

  @options = options
end

Instance Method Details

#locally(*args) ⇒ Object

Runs without chroot; a failure becomes a popup. Runs a command described by args, disregarding a chroot(2) specified by the installation (WFM.scr_root). Shows a popup if the command fails and returns nil in such case. It also globally switches the default Cheetah logger to Y2Logger.



99
100
101
# File 'library/system/src/lib/yast2/execute.rb', line 99

def locally(*args)
  chaining_object(yast_popup: true).locally!(*args)
end

#locally!(*args) ⇒ Object

Runs without chroot; a failure becomes an exception. Runs a command described by args, disregarding a chroot(2) specified by the installation (WFM.scr_root). It also globally switches the default Cheetah logger to Y2Logger.

Parameters:

Raises:

  • Cheetah::ExecutionFailed if the command fails



110
111
112
# File 'library/system/src/lib/yast2/execute.rb', line 110

def locally!(*args)
  run_or_chain(args)
end

#on_target(*args) ⇒ Object

Runs with chroot; a failure becomes a popup. Runs a command described by args, in a chroot(2) specified by the installation (WFM.scr_root). Shows a popup if the command fails and returns nil in such case. It also globally switches the default Cheetah logger to Y2Logger.



74
75
76
# File 'library/system/src/lib/yast2/execute.rb', line 74

def on_target(*args)
  chaining_object(yast_popup: true).on_target!(*args)
end

#on_target!(*args) ⇒ Object

Runs with chroot; a failure becomes an exception. Runs a command described by args, in a chroot(2) specified by the installation (WFM.scr_root). It also globally switches the default Cheetah logger to Y2Logger.

Parameters:

Raises:

  • Cheetah::ExecutionFailed if the command fails



85
86
87
88
89
# File 'library/system/src/lib/yast2/execute.rb', line 85

def on_target!(*args)
  root = Yast::WFM.scr_root

  chaining_object(chroot: root).run_or_chain(args)
end

#run_or_chain(args) ⇒ Object, ExecuteClass (protected)

Decides either to run the command or to chain the call in case that no argmuments are given.

Parameters:

Returns:

  • (Object, ExecuteClass)

    result of running the command or a chaining object.



132
133
134
# File 'library/system/src/lib/yast2/execute.rb', line 132

def run_or_chain(args)
  args.none? ? self : run(*args)
end

#stdout(*args) ⇒ String

Runs a command described by args and returns its output

It also globally switches the default Cheetah logger to Y2Logger.

Parameters:

Returns:

  • (String)

    command output or an empty string if the command fails.



121
122
123
# File 'library/system/src/lib/yast2/execute.rb', line 121

def stdout(*args)
  chaining_object(yast_stdout: true, stdout: :capture).run_or_chain(args)
end