Class: Upstart::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/upstart-exporter.rb,
lib/upstart-exporter/version.rb

Constant Summary

DEFAULTS =
{
  'helper_dir' => '/var/local/upstart_helpers/',
  'upstart_dir' => '/etc/init/',
  'run_user' => 'service'
}
CONF =
'/etc/upstart-exporter.yaml'
HELPER_TPL =
"#!/bin/sh\n%s\n"
APP_TPL =
"pre-start script\n\nbash << \"EOF\"\nmkdir -p /var/log/%s\nchown -R %s /var/log/%s\nEOF\n\nend script\n"
COMMAND_TPL =
"start on starting %s\nstop on stopping %s\n"
COMMAND_REAL_TPL =
"start on starting %s\nstop on stopping %s\nrespawn\n\nexec sudo -u %s /bin/sh %s >> /var/log/%s/%s.log 2>&1\n"
VERSION =
"0.0.4"

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Exporter) initialize(options)

A new instance of Exporter



16
17
18
19
20
21
# File 'lib/upstart-exporter.rb', line 16

def initialize(options)
  process_opts(options)
  read_global_opts
  check_dir(@helper_dir)
  check_dir(@upstart_dir)
end

Instance Attribute Details

- (Object) helper_dir

Returns the value of attribute helper_dir



14
15
16
# File 'lib/upstart-exporter.rb', line 14

def helper_dir
  @helper_dir
end

- (Object) run_user

Returns the value of attribute run_user



14
15
16
# File 'lib/upstart-exporter.rb', line 14

def run_user
  @run_user
end

- (Object) upstart_dir

Returns the value of attribute upstart_dir



14
15
16
# File 'lib/upstart-exporter.rb', line 14

def upstart_dir
  @upstart_dir
end

Instance Method Details

- (Object) app_cmd(cmd_name)



126
127
128
# File 'lib/upstart-exporter.rb', line 126

def app_cmd(cmd_name)
  "#{@app_name}-#{cmd_name}"
end

- (Object) check_dir(dir)



75
76
77
78
# File 'lib/upstart-exporter.rb', line 75

def check_dir(dir)
  FileUtils.mkdir_p(dir) unless FileTest.directory?(dir)
  error "Path #{dir} does not exist" unless FileTest.directory?(dir)
end

- (Object) clear



138
139
140
141
142
143
144
145
146
# File 'lib/upstart-exporter.rb', line 138

def clear
  FileUtils.rm(upstart_conf) if FileTest.file?(upstart_conf)
  Dir[upstart_cmd_conf('*')].each do |f|
    FileUtils.rm(f)
  end
  Dir[helper_cmd_conf('*')].each do |f|
    FileUtils.rm(f)
  end
end

- (Object) error(msg)



179
180
181
# File 'lib/upstart-exporter.rb', line 179

def error(msg)
  raise Upstart::ExportError, msg 
end

- (Object) export



80
81
82
83
84
85
86
# File 'lib/upstart-exporter.rb', line 80

def export
  clear
  export_app
  @commands.each do |cmd_name, cmd|
    export_command(cmd_name, cmd)
  end
end

- (Object) export_app



148
149
150
151
152
153
# File 'lib/upstart-exporter.rb', line 148

def export_app
  app_conf = APP_TPL % [@app_name, @run_user, @app_name]
  File.open(upstart_conf, 'w') do |f|
    f.write(app_conf)
  end
end

- (Object) export_cmd_helper(cmd_name, cmd)



155
156
157
158
159
160
# File 'lib/upstart-exporter.rb', line 155

def export_cmd_helper(cmd_name, cmd)
  helper_script_cont = HELPER_TPL % [cmd]
  File.open(helper_cmd_conf(cmd_name), 'w') do |f|
    f.write(helper_script_cont)
  end
end

- (Object) export_cmd_upstart_confs(cmd_name)



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/upstart-exporter.rb', line 162

def export_cmd_upstart_confs(cmd_name)
  cmd_upstart_conf_content = COMMAND_TPL % [@app_name, @app_name]
  File.open(upstart_cmd_conf(cmd_name), 'w') do |f|
    f.write(cmd_upstart_conf_content)
  end
  
  cmd_upstart_conf_content_real = COMMAND_REAL_TPL % [app_cmd(cmd_name), app_cmd(cmd_name), @run_user, helper_cmd_conf(cmd_name), @app_name, cmd_name]
  File.open(upstart_cmd_conf(cmd_name + '-real'), 'w') do |f|
    f.write(cmd_upstart_conf_content_real)
  end
end

- (Object) export_command(cmd_name, cmd)



174
175
176
177
# File 'lib/upstart-exporter.rb', line 174

def export_command(cmd_name, cmd)
  export_cmd_helper(cmd_name, cmd)
  export_cmd_upstart_confs(cmd_name)
end

- (Object) helper_cmd_conf(cmd_name)



134
135
136
# File 'lib/upstart-exporter.rb', line 134

def helper_cmd_conf(cmd_name)
  File.join(@helper_dir, "#{app_cmd(cmd_name)}.sh")
end

- (Object) process_appname(app_name)



70
71
72
73
# File 'lib/upstart-exporter.rb', line 70

def process_appname(app_name)
  error "Application name should contain only letters (and underscore) and be nonempty, so #{app_name.inspect} is not suitable" unless app_name =~ /^\w+$/ 
  @app_name = "fb-#{app_name}"
end

- (Object) process_opts(options)



42
43
44
45
46
47
48
49
# File 'lib/upstart-exporter.rb', line 42

def process_opts(options)
  @commands = if options[:clear]
    {}
  else
    process_procfile(options[:procfile])
  end
  process_appname(options[:app_name])
end

- (Object) process_procfile(name)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/upstart-exporter.rb', line 51

def process_procfile(name)
  error "#{name} is not a readable file" unless FileTest.file?(name)
  commands = {}
  content = File.read(name)
  content.lines.each do |line|
    line.chomp!
    if line =~ /^(\w+?):(.*)$/
      label = $1
      command = $2
      commands[label] = command
    elsif line =~ /^\s*$/
      # do nothing
    else
      error "procfile lines should have the following format: 'some_label: command'"
    end
  end
  commands
end

- (Object) read_global_opts



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/upstart-exporter.rb', line 24

def read_global_opts
  config = if FileTest.file?(CONF)
    YAML::load(File.read(CONF))
  else
    STDERR.puts "#{CONF} not found"
    {}
  end
  %w{helper_dir upstart_dir run_user}.each do |param|
    value = if config[param]
      config[param]
    else
      STDERR.puts "Param #{param} is not set, taking default value #{DEFAULTS[param]}"
      DEFAULTS[param]
    end
    self.send "#{param}=", value
  end
end

- (Object) upstart_cmd_conf(cmd_name)



130
131
132
# File 'lib/upstart-exporter.rb', line 130

def upstart_cmd_conf(cmd_name)
  File.join(@upstart_dir, "#{app_cmd(cmd_name)}.conf")
end

- (Object) upstart_conf



122
123
124
# File 'lib/upstart-exporter.rb', line 122

def upstart_conf
  File.join(@upstart_dir, "#{@app_name}.conf")
end