Class: Bosh::Agent::Message::DrainScript

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = Bosh::Agent::Config.base_dir, job_template = nil) ⇒ DrainScript

Returns a new instance of DrainScript.



153
154
155
156
# File 'lib/bosh_agent/message/drain.rb', line 153

def initialize(base_dir=Bosh::Agent::Config.base_dir, job_template=nil)
  @base_dir = base_dir
  @job_template = job_template || Bosh::Agent::Config.state.to_hash.fetch('job', {}).fetch('template', 'job')
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



151
152
153
# File 'lib/bosh_agent/message/drain.rb', line 151

def base_dir
  @base_dir
end

#job_templateObject (readonly)

Returns the value of attribute job_template.



151
152
153
# File 'lib/bosh_agent/message/drain.rb', line 151

def job_template
  @job_template
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/bosh_agent/message/drain.rb', line 158

def exists?
  File.exists?(script_file)
end

#run(job_updated, hash_updated, updated_packages) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/bosh_agent/message/drain.rb', line 162

def run(job_updated, hash_updated, updated_packages)
  env = {
      'PATH' => '/usr/sbin:/usr/bin:/sbin:/bin',
      'BOSH_CURRENT_STATE' => Yajl::Encoder.encode(@old_spec),
      'BOSH_APPLY_SPEC' => Yajl::Encoder.encode(@spec)
  }

  # Drain contract: on success the drain script should return a number exit(0)
  stdout_rd, stdout_wr = IO.pipe

  args = [  env, script_file, job_updated, hash_updated, *updated_packages ]
  args += [ :out => stdout_wr, :unsetenv_others => true ]
  Process.spawn(*args)
  Process.wait
  stdout_wr.close

  exit_status = $?.exitstatus
  output = stdout_rd.read

  unless output.match(/\A-{0,1}\d+\Z/) && exit_status == 0
    raise Bosh::Agent::MessageHandlerError,
          "Drain script exit #{exit_status}: #{output}"
  end

  output.to_i
end