Class: Foreman::Export::Runit

Inherits:
Base
  • Object
show all
Defined in:
lib/foreman/export/runit.rb

Constant Summary collapse

ENV_VARIABLE_REGEX =
/([a-zA-Z_]+[a-zA-Z0-9_]*)=(\S+)/

Instance Attribute Summary

Attributes inherited from Base

#app, #concurrency, #engine, #location, #log, #port, #template, #user

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Foreman::Export::Base

Instance Method Details

#exportObject



7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/foreman/export/runit.rb', line 7

def export
  error("Must specify a location") unless location

  app = self.app || File.basename(engine.directory)
  user = self.user || app
  log_root = self.log || "/var/log/#{app}"
  template_root = self.template

  run_template = export_template('runit', 'run.erb', template_root)
  log_run_template = export_template('runit', 'log_run.erb', template_root)

  engine.procfile.entries.each do |process|
    1.upto(self.concurrency[process.name]) do |num|
      process_directory     = "#{location}/#{app}-#{process.name}-#{num}"
      process_env_directory = "#{process_directory}/env"
      process_log_directory = "#{process_directory}/log"

      create_directory process_directory
      create_directory process_env_directory
      create_directory process_log_directory

      run = ERB.new(run_template).result(binding)
      write_file "#{process_directory}/run", run
      FileUtils.chmod 0755, "#{process_directory}/run"

      port = engine.port_for(process, num, self.port)
      environment_variables = {'PORT' => port}.
          merge(engine.environment).
          merge(inline_variables(process.command))

      environment_variables.each_pair do |var, env|
        write_file "#{process_env_directory}/#{var.upcase}", env
      end

      log_run = ERB.new(log_run_template).result(binding)
      write_file "#{process_log_directory}/run", log_run
      FileUtils.chmod 0755, "#{process_log_directory}/run"
    end
  end

end