Class: Exec
- Inherits:
-
Oxidized::Hook
- Object
- Oxidized::Hook
- Exec
- Includes:
- Process
- Defined in:
- lib/oxidized/hook/exec.rb
Instance Attribute Summary
Attributes inherited from Oxidized::Hook
Instance Method Summary collapse
-
#initialize ⇒ Exec
constructor
A new instance of Exec.
- #make_env(ctx) ⇒ Object
- #run_cmd!(env) ⇒ Object
- #run_hook(ctx) ⇒ Object
- #validate_cfg! ⇒ Object
Methods inherited from Oxidized::Hook
Constructor Details
#initialize ⇒ Exec
Returns a new instance of Exec.
4 5 6 7 8 |
# File 'lib/oxidized/hook/exec.rb', line 4 def initialize super @timeout = 60 @async = false end |
Instance Method Details
#make_env(ctx) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/oxidized/hook/exec.rb', line 58 def make_env(ctx) env = { "OX_EVENT" => ctx.event.to_s } if ctx.node env.merge!( "OX_NODE_NAME" => ctx.node.name.to_s, "OX_NODE_IP" => ctx.node.ip.to_s, "OX_NODE_FROM" => ctx.node.from.to_s, "OX_NODE_MSG" => ctx.node.msg.to_s, "OX_NODE_GROUP" => ctx.node.group.to_s, "OX_NODE_MODEL" => ctx.node.model.class.name, "OX_REPO_COMMITREF" => ctx.commitref.to_s, "OX_REPO_NAME" => ctx.node.repo.to_s, "OX_ERR_TYPE" => ctx.node.err_type.to_s, "OX_ERR_REASON" => ctx.node.err_reason.to_s ) end if ctx.job env["OX_JOB_STATUS"] = ctx.job.status.to_s env["OX_JOB_TIME"] = ctx.job.time.to_s end env end |
#run_cmd!(env) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/oxidized/hook/exec.rb', line 40 def run_cmd!(env) pid, status = nil, nil Timeout.timeout(@timeout) do pid = spawn env, @cmd, unsetenv_others: true pid, status = wait2 pid unless status.exitstatus.zero? msg = "#{@cmd.inspect} failed with exit value #{status.exitstatus}" log msg, :error raise msg end end rescue Timeout::Error kill "TERM", pid msg = "#{@cmd} timed out" log msg, :error raise Timeout::Error, msg end |
#run_hook(ctx) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/oxidized/hook/exec.rb', line 29 def run_hook(ctx) env = make_env ctx log "Execute: #{@cmd.inspect}", :debug th = Thread.new do run_cmd! env rescue StandardError => e raise e unless @async end th.join unless @async end |
#validate_cfg! ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/oxidized/hook/exec.rb', line 10 def validate_cfg! # Syntax check if cfg.has_key? "timeout" @timeout = cfg.timeout raise "invalid timeout value" unless @timeout.is_a?(Integer) && @timeout.positive? end @async = !!cfg.async if cfg.has_key? "async" if cfg.has_key? "cmd" @cmd = cfg.cmd raise "invalid cmd value" unless @cmd.is_a?(String) || @cmd.is_a?(Array) end rescue RuntimeError => e raise ArgumentError, "#{self.class.name}: configuration invalid: #{e.}" end |