Module: Bixby::Agent::ShellExec

Included in:
Bixby::Agent
Defined in:
lib/bixby-agent/agent/shell_exec.rb

Instance Method Summary collapse

Instance Method Details

#shell_exec(params) ⇒ CommandResponse

Shell exec a local command with the given params

Parameters:

  • params (Hash)

    CommandSpec hash

Options Hash (params):

  • :repo (String)
  • :bundle (String)
  • :command (String)
  • :args (String)
  • :stdin (String)
  • :digest (String)

    Expected bundle digest

  • :env (Hash)

    Hash of extra ENV key/values to pass to sub-shell

  • :user (String)

    User to run as

  • :group (String)

    Group to run as

Returns:

  • (CommandResponse)

Raises:

  • (BundleNotFound)

    If bundle doesn’t exist or digest does not match

  • (CommandNotFound)

    If command doesn’t exist



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
55
# File 'lib/bixby-agent/agent/shell_exec.rb', line 26

def shell_exec(params)
  digest = params.delete("digest") || params.delete(:digest)

  spec = CommandSpec.new(params)
  log.debug { "shell_exec:\n" + spec.to_s + "\n" }
  spec.validate(digest)

  cmd = "#{spec.command_file} #{spec.args}"

  # Cleanup the ENV and execute
  old_env = {}
  %W{BUNDLE_BIN_PATH BUNDLE_GEMFILE}.each{ |r|
    old_env[r] = ENV.delete(r) if ENV.include?(r) }

  logger.debug("exec: #{cmd}")
  uid = lookup_id(:uid, spec.user)
  gid = lookup_id(:gid, spec.group)
  shell = Mixlib::ShellOut.new(cmd, :input => spec.stdin,
                                    :env   => reset_env(spec.env, uid),
                                    :user  => uid,
                                    :group => gid)

  shell.run_command

  old_env.each{ |k,v| ENV[k] = v } # reset the ENV

  return CommandResponse.new({ :status => shell.exitstatus,
                               :stdout => shell.stdout,
                               :stderr => shell.stderr })
end