foreman 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,6 +47,10 @@ The following options control how the application is run:
47
47
  Specify the number of each process type to run. The value passed in
48
48
  should be in the format `process=num,process=num`
49
49
 
50
+ * `-u`, `--user`:
51
+ Specify the user the application should be run as. Defaults to the
52
+ app name
53
+
50
54
  ## OPTIONS
51
55
 
52
56
  These options control all modes of foreman's operation.
@@ -2,12 +2,7 @@ pre-start script
2
2
 
3
3
  bash << "EOF"
4
4
  mkdir -p /var/log/<%= app %>
5
-
6
- <% engine.processes.keys.sort.each do |process| %>
7
- <% 1.upto(concurrency[process]).each do |num| %>
8
- start <%=app%>-<%=process%>-<%=num%>
9
- <% end %>
10
- <% end %>
5
+ chown -R <%= user %> /var/log/<%= app %>
11
6
  EOF
12
7
 
13
8
  end script
@@ -1,5 +1,6 @@
1
- stop on stopping <%= app %>
1
+ start on starting <%= app %>-<%= process.name %>
2
+ stop on stopping <%= app %>-<%= process.name %>
2
3
  respawn
3
4
 
4
5
  chdir <%= engine.directory %>
5
- exec <%= process.command %> >> /var/log/<%=app%>/<%=process.name%>-<%=num%>.log 2>&1
6
+ exec su <%= user %> -c "<%= process.command %> >> /var/log/<%=app%>/<%=process.name%>-<%=num%>.log 2>&1"
@@ -0,0 +1,2 @@
1
+ start on starting <%= app %>
2
+ stop on stopping <%= app %>
@@ -1,6 +1,6 @@
1
1
  module Foreman
2
2
 
3
- VERSION = "0.4.4"
3
+ VERSION = "0.4.5"
4
4
 
5
5
  class AppDoesNotExist < Exception; end
6
6
 
@@ -1,5 +1,4 @@
1
1
  require "foreman"
2
- require "foreman/configuration"
3
2
  require "foreman/engine"
4
3
  require "foreman/export"
5
4
  require "thor"
@@ -27,19 +26,21 @@ class Foreman::CLI < Thor
27
26
  desc "export FORMAT LOCATION", "Export the application to another process management format"
28
27
 
29
28
  method_option :app, :type => :string, :aliases => "-a"
29
+ method_option :user, :type => :string, :aliases => "-u"
30
30
  method_option :concurrency, :type => :string, :aliases => "-c",
31
31
  :banner => '"alpha=5,bar=3"'
32
-
33
32
  def export(format, location=nil)
34
33
  check_procfile!
35
34
 
36
35
  formatter = case format
37
36
  when "upstart" then Foreman::Export::Upstart
37
+ when "inittab" then Foreman::Export::Inittab
38
38
  else error "Unknown export format: #{format}."
39
39
  end
40
40
 
41
41
  formatter.new(engine).export(location,
42
42
  :name => options[:app],
43
+ :user => options[:user],
43
44
  :concurrency => options[:concurrency]
44
45
  )
45
46
  rescue Foreman::Export::Exception => ex
@@ -5,3 +5,4 @@ module Foreman::Export
5
5
  end
6
6
 
7
7
  require "foreman/export/upstart"
8
+ require "foreman/export/inittab"
@@ -1,4 +1,3 @@
1
- require "foreman/configuration"
2
1
  require "foreman/export"
3
2
 
4
3
  class Foreman::Export::Base
@@ -0,0 +1,31 @@
1
+ require "foreman/export/base"
2
+
3
+ class Foreman::Export::Inittab < Foreman::Export::Base
4
+
5
+ def export(fname=nil, options={})
6
+ app = options[:app] || File.basename(engine.directory)
7
+ user = options[:user] || app
8
+ log_dir = "/var/log/#{app}"
9
+
10
+ concurrency = parse_concurrency(options[:concurrency])
11
+
12
+ inittab = []
13
+ inittab << "# ----- foreman #{app} processes -----"
14
+ engine.processes.values.each_with_index do |process, num|
15
+ id = app.slice(0, 2).upcase + sprintf("%02d", num+1)
16
+ inittab << "#{id}:4:respawn:/bin/su - #{user} -c '#{process.command} >> #{log_dir}/#{process.name}-#{num+1}.log 2>&1'"
17
+ end
18
+ inittab << "# ----- end foreman #{app} processes -----"
19
+
20
+ inittab = inittab.join("\n") + "\n"
21
+
22
+ if fname
23
+ FileUtils.mkdir_p(log_dir)
24
+ FileUtils.chown(user, nil, log_dir)
25
+ write_file(fname, inittab)
26
+ else
27
+ puts inittab
28
+ end
29
+ end
30
+
31
+ end
@@ -1,5 +1,4 @@
1
1
  require "erb"
2
- require "foreman/configuration"
3
2
  require "foreman/export/base"
4
3
 
5
4
  class Foreman::Export::Upstart < Foreman::Export::Base
@@ -10,6 +9,7 @@ class Foreman::Export::Upstart < Foreman::Export::Base
10
9
  FileUtils.mkdir_p location
11
10
 
12
11
  app = options[:app] || File.basename(engine.directory)
12
+ user = options[:user] || app
13
13
 
14
14
  Dir["#{location}/#{app}*.conf"].each do |file|
15
15
  say "cleaning up: #{file}"
@@ -23,27 +23,17 @@ class Foreman::Export::Upstart < Foreman::Export::Base
23
23
  write_file "#{location}/#{app}.conf", master_config
24
24
 
25
25
  process_template = export_template("upstart/process.conf.erb")
26
-
26
+
27
27
  engine.processes.values.each do |process|
28
+ process_master_template = export_template("upstart/process_master.conf.erb")
29
+ process_master_config = ERB.new(process_master_template).result(binding)
30
+ write_file "#{location}/#{app}-#{process.name}.conf", process_master_config
31
+
28
32
  1.upto(concurrency[process.name]) do |num|
29
33
  process_config = ERB.new(process_template).result(binding)
30
34
  write_file "#{location}/#{app}-#{process.name}-#{num}.conf", process_config
31
35
  end
32
36
  end
33
-
34
- return
35
- write_file "#{location}/#{app}.conf", <<-UPSTART_MASTER
36
- UPSTART_MASTER
37
-
38
- engine.processes.each do |process|
39
- write_file process_conf, <<-UPSTART_CHILD
40
- UPSTART_CHILD
41
- end
42
-
43
- engine.processes.each do |name, process|
44
- config.processes[name] ||= 1
45
- end
46
- config.write
47
37
  end
48
38
 
49
39
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 4
10
- version: 0.4.4
9
+ - 5
10
+ version: 0.4.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Dollar
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-23 00:00:00 -04:00
18
+ date: 2010-06-29 00:00:00 -04:00
19
19
  default_executable: foreman
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -143,16 +143,16 @@ files:
143
143
  - bin/foreman
144
144
  - export/upstart/master.conf.erb
145
145
  - export/upstart/process.conf.erb
146
+ - export/upstart/process_master.conf.erb
146
147
  - lib/foreman.rb
147
148
  - lib/foreman/cli.rb
148
- - lib/foreman/configuration.rb
149
149
  - lib/foreman/engine.rb
150
150
  - lib/foreman/export.rb
151
151
  - lib/foreman/export/base.rb
152
+ - lib/foreman/export/inittab.rb
152
153
  - lib/foreman/export/upstart.rb
153
154
  - lib/foreman/process.rb
154
155
  - spec/foreman/cli_spec.rb
155
- - spec/foreman/configuration_spec.rb
156
156
  - spec/foreman/engine_spec.rb
157
157
  - spec/foreman/export/upstart_spec.rb
158
158
  - spec/foreman/export_spec.rb
@@ -196,7 +196,6 @@ specification_version: 3
196
196
  summary: Process manager for applications with multiple components
197
197
  test_files:
198
198
  - spec/foreman/cli_spec.rb
199
- - spec/foreman/configuration_spec.rb
200
199
  - spec/foreman/engine_spec.rb
201
200
  - spec/foreman/export/upstart_spec.rb
202
201
  - spec/foreman/export_spec.rb
@@ -1,55 +0,0 @@
1
- require "foreman"
2
-
3
- class Foreman::Configuration
4
-
5
- attr_reader :app
6
- attr_reader :processes
7
-
8
- def initialize(app)
9
- @app = app
10
- @processes = {}
11
- read_initial_config
12
- end
13
-
14
- def scale(process, amount)
15
- old_amount = processes[process].to_i
16
- processes[process] = amount.to_i
17
- amount = amount.to_i
18
-
19
- if (old_amount < amount)
20
- ((old_amount + 1) .. amount).each { |num| system "start #{app}-#{process} NUM=#{num}" }
21
- elsif (amount < old_amount)
22
- ((amount + 1) .. old_amount).each { |num| system "stop #{app}-#{process} NUM=#{num}" }
23
- end
24
-
25
- write
26
- end
27
-
28
- def write
29
- write_file "/etc/foreman/#{app}.conf", <<-UPSTART_CONFIG
30
- #{app}_processes="#{processes.keys.sort.join(' ')}"
31
- #{processes.keys.sort.map { |k| "#{app}_#{k}=\"#{processes[k]}\"" }.join("\n")}
32
- UPSTART_CONFIG
33
- end
34
-
35
- private ######################################################################
36
-
37
- def read_initial_config
38
- config = File.read("/etc/foreman/#{app}.conf").split("\n").inject({}) do |accum, line|
39
- key, value = line.match(/^(.+?)\s*=\s*"(.+?)"\s*$/).captures
40
- #accum.update(parts(1) => parts(2))
41
- accum.update(key => value)
42
- end
43
- config["#{app}_processes"].split(" ").each do |process|
44
- processes[process] = config["#{app}_#{process}"].to_i
45
- end
46
- rescue Errno::ENOENT
47
- end
48
-
49
- def write_file(filename, contents)
50
- File.open(filename, "w") do |file|
51
- file.puts contents
52
- end
53
- end
54
-
55
- end
@@ -1,49 +0,0 @@
1
- require "spec_helper"
2
- require "foreman/configuration"
3
-
4
- describe "Foreman::Configuration" do
5
- subject { Foreman::Configuration.new("testapp") }
6
-
7
- describe "initialize" do
8
- describe "without an existing config" do
9
- it "has no processes" do
10
- subject.processes.length.should == 0
11
- end
12
- end
13
-
14
- describe "with an existing config" do
15
- it "has processes" do
16
- write_foreman_config("testapp")
17
- subject.processes["alpha"].should == 1
18
- subject.processes["bravo"].should == 2
19
- end
20
- end
21
- end
22
-
23
- describe "scale" do
24
- before(:each) { write_foreman_config("testapp") }
25
-
26
- it "can scale up" do
27
- mock(subject).system("start testapp-alpha NUM=2")
28
- mock(subject).system("start testapp-alpha NUM=3")
29
- subject.scale("alpha", 3)
30
- end
31
-
32
- it "can scale down" do
33
- mock(subject).system("stop testapp-bravo NUM=2")
34
- subject.scale("bravo", 1)
35
- end
36
- end
37
-
38
- describe "wite" do
39
- it "can write a configuration file" do
40
- subject.scale("charlie", 3)
41
- subject.scale("delta", 4)
42
- File.read("/etc/foreman/testapp.conf").should == <<-FOREMAN_CONFIG
43
- testapp_processes="charlie delta"
44
- testapp_charlie="3"
45
- testapp_delta="4"
46
- FOREMAN_CONFIG
47
- end
48
- end
49
- end