Class: Bosh::Agent::Message::RunErrand

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_agent/message/run_errand.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ RunErrand

Returns a new instance of RunErrand.



14
15
16
17
# File 'lib/bosh_agent/message/run_errand.rb', line 14

def initialize(args)
  @base_dir = Bosh::Agent::Config.base_dir
  @logger = Bosh::Agent::Config.logger
end

Class Method Details

.long_running?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/bosh_agent/message/run_errand.rb', line 10

def self.long_running?
  true
end

.process(args) ⇒ Object



6
7
8
# File 'lib/bosh_agent/message/run_errand.rb', line 6

def self.process(args)
  self.new(args).start
end

Instance Method Details

#startObject



19
20
21
22
23
24
25
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
# File 'lib/bosh_agent/message/run_errand.rb', line 19

def start
  state = Bosh::Agent::Config.state.to_hash

  job_templates = state.fetch('job', {}).fetch('templates', [])
  if job_templates.empty?
    raise Bosh::Agent::MessageHandlerError,
          "At least one job template is required to run an errand"
  end

  job_template_name = job_templates.first.fetch('name')

  env  = { 'PATH' => '/usr/sbin:/usr/bin:/sbin:/bin' }
  cmd  = "#{@base_dir}/jobs/#{job_template_name}/bin/run"
  opts = { :unsetenv_others => true }

  unless File.executable?(cmd)
    raise Bosh::Agent::MessageHandlerError,
          "Job template #{job_template_name} does not have executable bin/run"
  end

  begin
    stdout, stderr, status = Open3.capture3(env, cmd, opts)
    {
      'exit_code' => status.exitstatus,
      'stdout' => stdout,
      'stderr' => stderr,
    }
  rescue Exception => e
    @logger.warn("%s\n%s" % [e.inspect, e.backtrace.join("\n")])
    raise Bosh::Agent::MessageHandlerError, e.inspect
  end
end