upstart-exporter 2.0.1 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -4,3 +4,4 @@ rvm:
4
4
  branches:
5
5
  only:
6
6
  - master
7
+ script: bundle exec rspec spec
@@ -89,6 +89,7 @@ module Upstart
89
89
  :stop_on => "stopping #{app_name}",
90
90
  :respawn => 'respawn',
91
91
  :respawn_limit => '',
92
+ :kill_timeout => options[:kill_timeout],
92
93
  :run_user => options[:run_user],
93
94
  :run_group => options[:run_group],
94
95
  :cmd_name => cmd_name,
@@ -12,29 +12,36 @@ class Upstart::Exporter
12
12
  @options = options
13
13
  @commands = @config['commands']
14
14
  @env = @config['env'] || {}
15
- @dir = @config['working_directory'] || ''
16
15
  end
17
16
 
18
17
  def export
19
- @commands.each do |command, value|
20
- if count = value['count']
18
+ @commands.each do |command, cmd_options|
19
+ if count = cmd_options['count']
21
20
  count.times do |counter|
22
- export_cmd("#{command}_#{counter}", value)
21
+ export_cmd("#{command}_#{counter}", cmd_options)
23
22
  end
24
23
  else
25
- export_cmd(command, value)
24
+ export_cmd(command, cmd_options)
26
25
  end
27
26
  end
28
27
  end
29
28
 
30
29
  private
31
30
 
32
- def export_cmd(command, value)
33
- script = value['command']
34
- script = add_env_command(script, value)
35
- script = add_dir_command(script, value)
36
- export_cmd_helper(command, script)
37
- export_cmd_upstart_conf(command, value)
31
+ def export_cmd(command, cmd_options)
32
+ cmd_options = {
33
+ 'working_directory' => working_directory(cmd_options),
34
+ 'log' => log(cmd_options),
35
+ 'kill_timeout' => kill_timeout(cmd_options)
36
+ }.merge(cmd_options)
37
+
38
+ script = cmd_options['command']
39
+ script = add_env_command(script, cmd_options)
40
+ script = add_dir_command(script, cmd_options)
41
+ script = add_log_command(script, cmd_options)
42
+
43
+ export_cmd_helper(command, script, cmd_options)
44
+ export_cmd_upstart_conf(command, cmd_options)
38
45
  end
39
46
 
40
47
  def add_env_command(script, command)
@@ -51,7 +58,7 @@ class Upstart::Exporter
51
58
  end
52
59
 
53
60
  def add_dir_command(script, command)
54
- dir = command['working_directory'] || @dir
61
+ dir = command['working_directory']
55
62
  if dir.empty?
56
63
  script
57
64
  else
@@ -59,6 +66,15 @@ class Upstart::Exporter
59
66
  end
60
67
  end
61
68
 
69
+ def add_log_command(script, command)
70
+ log = command['log']
71
+ if log.empty?
72
+ script
73
+ else
74
+ "#{script} >> #{log} 2>&1"
75
+ end
76
+ end
77
+
62
78
  def respawn_options(cmd_options)
63
79
  if cmd_options.has_key?('respawn')
64
80
  cmd_options['respawn']
@@ -87,6 +103,18 @@ class Upstart::Exporter
87
103
  "stopping #{app_name}"
88
104
  end
89
105
 
106
+ def working_directory(cmd_options)
107
+ command_option(cmd_options, 'working_directory')
108
+ end
109
+
110
+ def log(cmd_options)
111
+ command_option(cmd_options, 'log')
112
+ end
113
+
114
+ def kill_timeout(cmd_options)
115
+ command_option(cmd_options, 'kill_timeout')
116
+ end
117
+
90
118
  def export_cmd_upstart_conf(cmd_name, cmd_options)
91
119
  cmd_upstart_conf_content = Templates.command(
92
120
  :app_name => app_name,
@@ -97,11 +125,18 @@ class Upstart::Exporter
97
125
  :cmd_name => cmd_name,
98
126
  :helper_cmd_conf => helper_cmd_conf(cmd_name),
99
127
  :respawn => respawn(cmd_options),
100
- :respawn_limit => respawn_limit(cmd_options)
128
+ :respawn_limit => respawn_limit(cmd_options),
129
+ :kill_timeout => kill_timeout(cmd_options)
101
130
  )
102
131
  File.open(upstart_cmd_conf(cmd_name), 'w') do |f|
103
132
  f.write(cmd_upstart_conf_content)
104
133
  end
105
134
  end
135
+
136
+ private
137
+
138
+ def command_option(cmd_options, key)
139
+ cmd_options[key.to_s] || @config[key.to_s] || @options[key.to_sym] || ''
140
+ end
106
141
  end
107
142
  end
@@ -1,7 +1,7 @@
1
1
  class Upstart::Exporter
2
2
  module ExporterHelpers
3
- def export_cmd_helper(cmd_name, cmd)
4
- helper_script_cont = Templates.helper :cmd => cmd
3
+ def export_cmd_helper(cmd_name, cmd, binds={})
4
+ helper_script_cont = Templates.helper append_cmd(cmd, binds)
5
5
  File.open(helper_cmd_conf(cmd_name), 'w') do |f|
6
6
  f.write(helper_script_cont)
7
7
  end
@@ -22,5 +22,17 @@ class Upstart::Exporter
22
22
  def helper_cmd_conf(cmd_name)
23
23
  File.join(@options[:helper_dir], "#{app_cmd(cmd_name)}.sh")
24
24
  end
25
+
26
+ private
27
+
28
+ def append_cmd(cmd, binds)
29
+ return binds unless cmd
30
+
31
+ parts = cmd.split /\s*(&&|\|\|)\s*/
32
+ parts.last.gsub!(/\A(exec\s*|\s*)/, "exec ")
33
+ cmd = parts.join(" ")
34
+
35
+ binds.merge(:cmd => cmd)
36
+ end
25
37
  end
26
38
  end
@@ -9,7 +9,8 @@ module Upstart::Exporter::Options
9
9
  'run_group' => 'service',
10
10
  'prefix' => 'fb-',
11
11
  'start_on_runlevel' => '[3]',
12
- 'stop_on_runlevel' => '[3]'
12
+ 'stop_on_runlevel' => '[3]',
13
+ 'kill_timeout' => 30
13
14
  }
14
15
 
15
16
  CONF = '/etc/upstart-exporter.yaml'
@@ -25,9 +25,9 @@ module Upstart
25
25
 
26
26
  HELPER_TPL = <<-HEREDOC
27
27
  #!/bin/bash
28
- if [ -f /etc/profile.d/rbenv.sh ]; then
29
- source /etc/profile.d/rbenv.sh
30
- fi
28
+
29
+ [[ -r /etc/profile.d/rbenv.sh ]] && source /etc/profile.d/rbenv.sh
30
+
31
31
  {{cmd}}
32
32
  HEREDOC
33
33
 
@@ -52,6 +52,7 @@ start on {{start_on}}
52
52
  stop on {{stop_on}}
53
53
  {{respawn}}
54
54
  {{respawn_limit}}
55
+ kill timeout {{kill_timeout}}
55
56
 
56
57
  script
57
58
  touch /var/log/{{app_name}}/{{cmd_name}}.log
@@ -65,7 +66,7 @@ HEREDOC
65
66
  def self.interpolate(str, substitutes)
66
67
  str_copy = str.dup
67
68
  substitutes.each do |k, v|
68
- str_copy.gsub!("{{#{k}}}", v)
69
+ str_copy.gsub!("{{#{k}}}", v.to_s)
69
70
  end
70
71
  str_copy
71
72
  end
@@ -1,5 +1,5 @@
1
1
  module Upstart
2
2
  class Exporter
3
- VERSION = "2.0.1"
3
+ VERSION = "2.1.1"
4
4
  end
5
5
  end
@@ -56,4 +56,45 @@ describe Upstart::Exporter::ExpandedExporter do
56
56
 
57
57
  described_class.export(options)
58
58
  end
59
+
60
+ it 'passes all calculated binds to the helper exporter' do
61
+ options = {
62
+ :version => 2,
63
+ :app_name => 'appname',
64
+ :working_directory => "/",
65
+ :log => "public.log",
66
+ :commands => {
67
+ 'working_directory' => '/var/log',
68
+ 'commands' => {
69
+ 'rm1' => {
70
+ 'command' => 'rm *',
71
+ 'log' => 'private.log'
72
+ },
73
+ 'rm2' => {
74
+ 'command' => 'rm -rf *',
75
+ 'working_directory' => '/home'
76
+ },
77
+ 'rm3' => {
78
+ 'command' => 'rm -f vmlinuz',
79
+ }
80
+ }
81
+ }
82
+ }.merge(@defaults)
83
+ Upstart::Exporter::Templates.should_receive(:helper) do |options|
84
+ options.should include('working_directory' => '/var/log') # propagated from 'commands'
85
+ options.should include('log' => 'private.log') # redefined by command
86
+ options.should include(:cmd => "cd '/var/log' && exec rm * >> private.log 2>&1")
87
+ end
88
+ Upstart::Exporter::Templates.should_receive(:helper) do |options|
89
+ options.should include('working_directory' => '/home') # redefined by command
90
+ options.should include('log' => 'public.log') # propagated from the very top level
91
+ options.should include(:cmd => "cd '/home' && exec rm -rf * >> public.log 2>&1")
92
+ end
93
+ Upstart::Exporter::Templates.should_receive(:helper) do |options|
94
+ options.should include('working_directory' => '/var/log') # propagated from 'commands'
95
+ options.should include('log' => 'public.log') # propagated from the very top level
96
+ options.should include(:cmd => "cd '/var/log' && exec rm -f vmlinuz >> public.log 2>&1")
97
+ end
98
+ described_class.export(options)
99
+ end
59
100
  end
@@ -21,8 +21,8 @@ end script
21
21
  HEREDOC
22
22
 
23
23
  described_class.app(
24
- :run_user => 'SOMEUSER',
25
- :run_group => 'SOMEGROUP',
24
+ :run_user => 'SOMEUSER',
25
+ :run_group => 'SOMEGROUP',
26
26
  :app_name => 'SOMEAPP',
27
27
  :start_on => '12',
28
28
  :stop_on => '13'
@@ -35,13 +35,13 @@ HEREDOC
35
35
 
36
36
  conf = <<-HEREDOC
37
37
  #!/bin/bash
38
- if [ -f /etc/profile.d/rbenv.sh ]; then
39
- source /etc/profile.d/rbenv.sh
40
- fi
38
+
39
+ [[ -r /etc/profile.d/rbenv.sh ]] && source /etc/profile.d/rbenv.sh
40
+
41
41
  SOME COMMAND
42
42
  HEREDOC
43
43
 
44
- described_class.helper(:cmd => 'SOME COMMAND').should == conf
44
+ described_class.helper('cmd' => 'SOME COMMAND').should == conf
45
45
  end
46
46
  end
47
47
 
@@ -54,6 +54,7 @@ start on starting SOMEAPP
54
54
  stop on stopping SOMEAPP
55
55
  respawn
56
56
  respawn limit 5 10
57
+ kill timeout 24
57
58
 
58
59
  script
59
60
  touch /var/log/SOMEAPP/SOMECMD.log
@@ -72,6 +73,7 @@ HEREDOC
72
73
  :respawn_limit => 'respawn limit 5 10',
73
74
  :start_on => 'starting SOMEAPP',
74
75
  :stop_on => 'stopping SOMEAPP',
76
+ :kill_timeout => 24,
75
77
  :helper_cmd_conf => 'HELPERPATH').should == conf
76
78
  end
77
79
  end
@@ -32,7 +32,7 @@ describe Upstart::Exporter do
32
32
  it 'created scripts, folders and sh helpers should have valid content' do
33
33
  exporter.export
34
34
 
35
- File.read('/h/p-app-ls_cmd.sh').should == tpl.helper(:cmd => ' ls')
35
+ File.read('/h/p-app-ls_cmd.sh').should == tpl.helper(:cmd => 'exec ls')
36
36
  File.read('/u/p-app.conf').should == tpl.app(:run_user => 'u',
37
37
  :run_group => 'g',
38
38
  :app_name => 'p-app',
@@ -46,6 +46,7 @@ describe Upstart::Exporter do
46
46
  :stop_on => 'stopping p-app',
47
47
  :respawn => 'respawn',
48
48
  :respawn_limit => '',
49
+ :kill_timeout => 30,
49
50
  :helper_cmd_conf => '/h/p-app-ls_cmd.sh')
50
51
  end
51
52
  end
@@ -1,4 +1,4 @@
1
- def make_procfile(fname, content)
1
+ def make_procfile(fname, content)
2
2
  File.open(fname, 'w') do |f|
3
3
  f.write content
4
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upstart-exporter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 0
9
8
  - 1
10
- version: 2.0.1
9
+ - 1
10
+ version: 2.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ilya Averyanov
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2014-01-21 00:00:00 +04:00
19
+ date: 2014-02-14 00:00:00 +04:00
20
20
  default_executable:
21
21
  dependencies: []
22
22