Module: Asgard::Shell

Included in:
Base
Defined in:
lib/asgard/shell.rb

Instance Method Summary collapse

Instance Method Details

#sh(script, silent: false, exec: false) ⇒ Object

Run a shell script. Multiline strings are passed to bash -c; single-line strings are passed to system directly. Exits with the command's status code on failure.

Pass exec: true to replace the current process instead of forking — useful for a task's final, long-running command (e.g. a dev server) so the asgard/ruby process doesn't sit resident in memory alongside it.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/asgard/shell.rb', line 15

def sh(script, silent: false, exec: false)
  script = script.strip
  $stdout.puts script unless silent
  argv = shell_argv(script)

  if exec
    $stdout.flush
    Kernel.exec(*argv)
  else
    exit($CHILD_STATUS.exitstatus) unless system(*argv)
  end
end

#shebang(interpreter, script, silent: false) ⇒ Object

Write script to a tempfile and execute it with interpreter. Useful for embedding Python, Node, Ruby, or any shebang-style body.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asgard/shell.rb', line 30

def shebang(interpreter, script, silent: false)
  extensions = {
    python3: ".py", python: ".py",
    node:    ".js",
    ruby:    ".rb",
    perl:    ".pl",
    bash:    ".sh", sh: ".sh"
  }
  ext = extensions.fetch(interpreter.to_sym, ".tmp")

  $stdout.puts script unless silent

  Tempfile.create(["asgard_", ext]) do |f|
    f.write(script)
    f.flush
    system(interpreter.to_s, f.path)
    exit($CHILD_STATUS.exitstatus) unless $CHILD_STATUS.success?
  end
end