HeSYINUvSBZfxqA-foreman 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +30 -0
- data/bin/foreman +7 -0
- data/bin/foreman-runner +2 -0
- data/data/example/Procfile +2 -0
- data/data/example/Procfile.without_colon +2 -0
- data/data/example/error +5 -0
- data/data/example/log/neverdie.log +4 -0
- data/data/example/ticker +12 -0
- data/data/export/upstart/master.conf.erb +8 -0
- data/data/export/upstart/process.conf.erb +5 -0
- data/data/export/upstart/process_master.conf.erb +2 -0
- data/lib/foreman.rb +8 -0
- data/lib/foreman/cli.rb +94 -0
- data/lib/foreman/engine.rb +233 -0
- data/lib/foreman/export.rb +9 -0
- data/lib/foreman/export/base.rb +44 -0
- data/lib/foreman/export/inittab.rb +38 -0
- data/lib/foreman/export/upstart.rb +42 -0
- data/lib/foreman/process.rb +14 -0
- data/lib/foreman/utils.rb +15 -0
- data/lib/foreman/version.rb +5 -0
- data/man/foreman.1 +212 -0
- data/spec/foreman/cli_spec.rb +84 -0
- data/spec/foreman/engine_spec.rb +89 -0
- data/spec/foreman/export/upstart_spec.rb +55 -0
- data/spec/foreman/export_spec.rb +2 -0
- data/spec/foreman/process_spec.rb +2 -0
- data/spec/foreman_spec.rb +11 -0
- data/spec/resources/export/upstart/app-alpha-1.conf +5 -0
- data/spec/resources/export/upstart/app-alpha-2.conf +5 -0
- data/spec/resources/export/upstart/app-alpha.conf +2 -0
- data/spec/resources/export/upstart/app-bravo-1.conf +5 -0
- data/spec/resources/export/upstart/app-bravo.conf +2 -0
- data/spec/resources/export/upstart/app.conf +8 -0
- data/spec/spec_helper.rb +60 -0
- metadata +238 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
require "foreman/export"
|
2
|
+
require "foreman/utils"
|
3
|
+
|
4
|
+
class Foreman::Export::Base
|
5
|
+
|
6
|
+
attr_reader :engine
|
7
|
+
|
8
|
+
def initialize(engine)
|
9
|
+
@engine = engine
|
10
|
+
end
|
11
|
+
|
12
|
+
def export
|
13
|
+
raise "export method must be overridden"
|
14
|
+
end
|
15
|
+
|
16
|
+
private ######################################################################
|
17
|
+
|
18
|
+
def error(message)
|
19
|
+
raise Foreman::Export::Exception.new(message)
|
20
|
+
end
|
21
|
+
|
22
|
+
def say(message)
|
23
|
+
puts "[foreman export] %s" % message
|
24
|
+
end
|
25
|
+
|
26
|
+
def export_template(exporter, file, template_root)
|
27
|
+
if template_root && File.exist?(file_path = File.join(template_root, file))
|
28
|
+
File.read(file_path)
|
29
|
+
elsif File.exist?(file_path = File.join("~/.foreman/templates", file))
|
30
|
+
File.read(file_path)
|
31
|
+
else
|
32
|
+
File.read(File.expand_path("../../../../data/export/#{exporter}/#{file}", __FILE__))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def write_file(filename, contents)
|
37
|
+
say "writing: #{filename}"
|
38
|
+
|
39
|
+
File.open(filename, "w") do |file|
|
40
|
+
file.puts contents
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "foreman/export"
|
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_root = options[:log] || "/var/log/#{app}"
|
9
|
+
|
10
|
+
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
|
11
|
+
|
12
|
+
inittab = []
|
13
|
+
inittab << "# ----- foreman #{app} processes -----"
|
14
|
+
|
15
|
+
engine.processes.values.inject(1) do |index, process|
|
16
|
+
1.upto(concurrency[process.name]) do |num|
|
17
|
+
id = app.slice(0, 2).upcase + sprintf("%02d", index)
|
18
|
+
port = engine.port_for(process, num, options[:port])
|
19
|
+
inittab << "#{id}:4:respawn:/bin/su - #{user} -c 'PORT=#{port} #{process.command} >> #{log_root}/#{process.name}-#{num}.log 2>&1'"
|
20
|
+
index += 1
|
21
|
+
end
|
22
|
+
index
|
23
|
+
end
|
24
|
+
|
25
|
+
inittab << "# ----- end foreman #{app} processes -----"
|
26
|
+
|
27
|
+
inittab = inittab.join("\n") + "\n"
|
28
|
+
|
29
|
+
if fname
|
30
|
+
FileUtils.mkdir_p(log_root) rescue error "could not create #{log_root}"
|
31
|
+
FileUtils.chown(user, nil, log_root) rescue error "could not chown #{log_root} to #{user}"
|
32
|
+
write_file(fname, inittab)
|
33
|
+
else
|
34
|
+
puts inittab
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "erb"
|
2
|
+
require "foreman/export"
|
3
|
+
|
4
|
+
class Foreman::Export::Upstart < Foreman::Export::Base
|
5
|
+
|
6
|
+
def export(location, options={})
|
7
|
+
error("Must specify a location") unless location
|
8
|
+
|
9
|
+
FileUtils.mkdir_p location
|
10
|
+
|
11
|
+
app = options[:app] || File.basename(engine.directory)
|
12
|
+
user = options[:user] || app
|
13
|
+
log_root = options[:log] || "/var/log/#{app}"
|
14
|
+
template_root = options[:template]
|
15
|
+
|
16
|
+
Dir["#{location}/#{app}*.conf"].each do |file|
|
17
|
+
say "cleaning up: #{file}"
|
18
|
+
FileUtils.rm(file)
|
19
|
+
end
|
20
|
+
|
21
|
+
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
|
22
|
+
|
23
|
+
master_template = export_template("upstart", "master.conf.erb", template_root)
|
24
|
+
master_config = ERB.new(master_template).result(binding)
|
25
|
+
write_file "#{location}/#{app}.conf", master_config
|
26
|
+
|
27
|
+
process_template = export_template("upstart", "process.conf.erb", template_root)
|
28
|
+
|
29
|
+
engine.processes.values.each do |process|
|
30
|
+
process_master_template = export_template("upstart", "process_master.conf.erb", template_root)
|
31
|
+
process_master_config = ERB.new(process_master_template).result(binding)
|
32
|
+
write_file "#{location}/#{app}-#{process.name}.conf", process_master_config
|
33
|
+
|
34
|
+
1.upto(concurrency[process.name]) do |num|
|
35
|
+
port = engine.port_for(process, num, options[:port])
|
36
|
+
process_config = ERB.new(process_template).result(binding)
|
37
|
+
write_file "#{location}/#{app}-#{process.name}-#{num}.conf", process_config
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "foreman"
|
2
|
+
|
3
|
+
class Foreman::Utils
|
4
|
+
|
5
|
+
def self.parse_concurrency(concurrency)
|
6
|
+
@concurrency ||= begin
|
7
|
+
pairs = concurrency.to_s.gsub(/\s/, "").split(",")
|
8
|
+
pairs.inject(Hash.new(1)) do |hash, pair|
|
9
|
+
process, amount = pair.split("=")
|
10
|
+
hash.update(process => amount.to_i)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/man/foreman.1
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
.\" generated with Ronn/v0.7.3
|
2
|
+
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
|
+
.
|
4
|
+
.TH "FOREMAN" "1" "August 2011" "Foreman 0.19.0" "Foreman Manual"
|
5
|
+
.
|
6
|
+
.SH "NAME"
|
7
|
+
\fBforeman\fR \- manage Procfile\-based applications
|
8
|
+
.
|
9
|
+
.SH "SYNOPSIS"
|
10
|
+
\fBforeman start [process]\fR
|
11
|
+
.
|
12
|
+
.br
|
13
|
+
\fBforeman export <format> [location]\fR
|
14
|
+
.
|
15
|
+
.SH "DESCRIPTION"
|
16
|
+
\fBForeman\fR is a manager for Procfile\-based applications\. Its aim is to abstract away the details of the Procfile format, and allow you to either run your application directly or export it to some other process management format\.
|
17
|
+
.
|
18
|
+
.SH "RUNNING"
|
19
|
+
\fBforeman start\fR is used to run your application directly from the command line\.
|
20
|
+
.
|
21
|
+
.P
|
22
|
+
If no additional parameters are passed, foreman will run one instance of each type of process defined in your Procfile\.
|
23
|
+
.
|
24
|
+
.P
|
25
|
+
If a parameter is passed, foreman will run one instance of the specified application type\.
|
26
|
+
.
|
27
|
+
.P
|
28
|
+
The following options control how the application is run:
|
29
|
+
.
|
30
|
+
.TP
|
31
|
+
\fB\-c\fR, \fB\-\-concurrency\fR
|
32
|
+
Specify the number of each process type to run\. The value passed in should be in the format \fBprocess=num,process=num\fR
|
33
|
+
.
|
34
|
+
.TP
|
35
|
+
\fB\-p\fR, \fB\-\-port\fR
|
36
|
+
Specify which port to use as the base for this application\. Should be a multiple of 1000\.
|
37
|
+
.
|
38
|
+
.SH "EXPORTING"
|
39
|
+
\fBforeman export\fR is used to export your application to another process management format\.
|
40
|
+
.
|
41
|
+
.P
|
42
|
+
An location to export can be passed as an argument\. This argument may be either required or optional depending on the export format\.
|
43
|
+
.
|
44
|
+
.P
|
45
|
+
The following options control how the application is run:
|
46
|
+
.
|
47
|
+
.TP
|
48
|
+
\fB\-a\fR, \fB\-\-app\fR
|
49
|
+
Use this name rather than the application\'s root directory name as the name of the application when exporting\.
|
50
|
+
.
|
51
|
+
.TP
|
52
|
+
\fB\-c\fR, \fB\-\-concurrency\fR
|
53
|
+
Specify the number of each process type to run\. The value passed in should be in the format \fBprocess=num,process=num\fR
|
54
|
+
.
|
55
|
+
.TP
|
56
|
+
\fB\-l\fR, \fB\-\-log\fR
|
57
|
+
Specify the directory to place process logs in\.
|
58
|
+
.
|
59
|
+
.TP
|
60
|
+
\fB\-p\fR, \fB\-\-port\fR
|
61
|
+
Specify which port to use as the base for this application\. Should be a multiple of 1000\.
|
62
|
+
.
|
63
|
+
.TP
|
64
|
+
\fB\-u\fR, \fB\-\-user\fR
|
65
|
+
Specify the user the application should be run as\. Defaults to the app name
|
66
|
+
.
|
67
|
+
.SH "OPTIONS"
|
68
|
+
These options control all modes of foreman\'s operation\.
|
69
|
+
.
|
70
|
+
.TP
|
71
|
+
\fB\-e\fR, \fB\-\-env\fR
|
72
|
+
Specify an alternate environment file\.
|
73
|
+
.
|
74
|
+
.TP
|
75
|
+
\fB\-f\fR, \fB\-\-procfile\fR
|
76
|
+
Specify an alternate location for the application\'s Procfile\. This file\'s containing directory will be assumed to be the root directory of the application\.
|
77
|
+
.
|
78
|
+
.SH "EXPORT FORMATS"
|
79
|
+
foreman currently supports the following output formats:
|
80
|
+
.
|
81
|
+
.IP "\(bu" 4
|
82
|
+
inittab
|
83
|
+
.
|
84
|
+
.IP "\(bu" 4
|
85
|
+
upstart
|
86
|
+
.
|
87
|
+
.IP "" 0
|
88
|
+
.
|
89
|
+
.SH "INITTAB EXPORT"
|
90
|
+
Will export a chunk of inittab\-compatible configuration:
|
91
|
+
.
|
92
|
+
.IP "" 4
|
93
|
+
.
|
94
|
+
.nf
|
95
|
+
|
96
|
+
# \-\-\-\-\- foreman example processes \-\-\-\-\-
|
97
|
+
EX01:4:respawn:/bin/su \- example \-c \'PORT=5000 bundle exec thin start >> /var/log/web\-1\.log 2>&1\'
|
98
|
+
EX02:4:respawn:/bin/su \- example \-c \'PORT=5100 bundle exec rake jobs:work >> /var/log/job\-1\.log 2>&1\'
|
99
|
+
# \-\-\-\-\- end foreman example processes \-\-\-\-\-
|
100
|
+
.
|
101
|
+
.fi
|
102
|
+
.
|
103
|
+
.IP "" 0
|
104
|
+
.
|
105
|
+
.SH "UPSTART EXPORT"
|
106
|
+
Will create a series of upstart scripts in the location you specify\. Scripts will be structured to make the following commands valid:
|
107
|
+
.
|
108
|
+
.P
|
109
|
+
\fBstart appname\fR
|
110
|
+
.
|
111
|
+
.P
|
112
|
+
\fBstop appname\-processname\fR
|
113
|
+
.
|
114
|
+
.P
|
115
|
+
\fBrestart appname\-processname\-3\fR
|
116
|
+
.
|
117
|
+
.SH "PROCFILE"
|
118
|
+
A Procfile should contain both a name for the process and the command used to run it\.
|
119
|
+
.
|
120
|
+
.IP "" 4
|
121
|
+
.
|
122
|
+
.nf
|
123
|
+
|
124
|
+
web: bundle exec thin start
|
125
|
+
job: bundle exec rake jobs:work
|
126
|
+
.
|
127
|
+
.fi
|
128
|
+
.
|
129
|
+
.IP "" 0
|
130
|
+
.
|
131
|
+
.P
|
132
|
+
You can validate your Procfile format using the \fBcheck\fR command
|
133
|
+
.
|
134
|
+
.IP "" 4
|
135
|
+
.
|
136
|
+
.nf
|
137
|
+
|
138
|
+
$ foreman check
|
139
|
+
.
|
140
|
+
.fi
|
141
|
+
.
|
142
|
+
.IP "" 0
|
143
|
+
.
|
144
|
+
.SH "ENVIRONMENT"
|
145
|
+
If a \fB\.env\fR file exists in the current directory, the default environment will be read from it\. This file should contain key/value pairs, separated by \fB=\fR, with one key/value pair per line\.
|
146
|
+
.
|
147
|
+
.IP "" 4
|
148
|
+
.
|
149
|
+
.nf
|
150
|
+
|
151
|
+
FOO=bar
|
152
|
+
BAZ=qux
|
153
|
+
.
|
154
|
+
.fi
|
155
|
+
.
|
156
|
+
.IP "" 0
|
157
|
+
.
|
158
|
+
.SH "DEFAULT OPTIONS"
|
159
|
+
If a \fB\.foreman\fR file exists in the current directory, default options will be read from it\. This file should be in YAML format with the long option name as keys\. Example:
|
160
|
+
.
|
161
|
+
.IP "" 4
|
162
|
+
.
|
163
|
+
.nf
|
164
|
+
|
165
|
+
concurrency: alpha=0
|
166
|
+
port: 15000
|
167
|
+
.
|
168
|
+
.fi
|
169
|
+
.
|
170
|
+
.IP "" 0
|
171
|
+
.
|
172
|
+
.SH "EXAMPLES"
|
173
|
+
Start one instance of each process type, interleave the output on stdout:
|
174
|
+
.
|
175
|
+
.IP "" 4
|
176
|
+
.
|
177
|
+
.nf
|
178
|
+
|
179
|
+
$ foreman start
|
180
|
+
.
|
181
|
+
.fi
|
182
|
+
.
|
183
|
+
.IP "" 0
|
184
|
+
.
|
185
|
+
.P
|
186
|
+
Export the application in upstart format:
|
187
|
+
.
|
188
|
+
.IP "" 4
|
189
|
+
.
|
190
|
+
.nf
|
191
|
+
|
192
|
+
$ foreman export upstart /etc/init
|
193
|
+
.
|
194
|
+
.fi
|
195
|
+
.
|
196
|
+
.IP "" 0
|
197
|
+
.
|
198
|
+
.P
|
199
|
+
Run one process type from the application defined in a specific Procfile:
|
200
|
+
.
|
201
|
+
.IP "" 4
|
202
|
+
.
|
203
|
+
.nf
|
204
|
+
|
205
|
+
$ foreman start alpha \-p ~/myapp/Procfile
|
206
|
+
.
|
207
|
+
.fi
|
208
|
+
.
|
209
|
+
.IP "" 0
|
210
|
+
.
|
211
|
+
.SH "COPYRIGHT"
|
212
|
+
Foreman is Copyright (C) 2010 David Dollar \fIhttp://daviddollar\.org\fR
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "foreman/cli"
|
3
|
+
|
4
|
+
describe "Foreman::CLI" do
|
5
|
+
subject { Foreman::CLI.new }
|
6
|
+
|
7
|
+
describe "start" do
|
8
|
+
describe "with a non-existent Procfile" do
|
9
|
+
it "prints an error" do
|
10
|
+
mock_error(subject, "Procfile does not exist.") do
|
11
|
+
dont_allow.instance_of(Foreman::Engine).start
|
12
|
+
subject.start
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "with a Procfile" do
|
18
|
+
before(:each) { write_procfile }
|
19
|
+
|
20
|
+
it "runs successfully" do
|
21
|
+
dont_allow(subject).error
|
22
|
+
mock.instance_of(Foreman::Engine).start({})
|
23
|
+
subject.start
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "export" do
|
29
|
+
describe "with a non-existent Procfile" do
|
30
|
+
it "prints an error" do
|
31
|
+
mock_error(subject, "Procfile does not exist.") do
|
32
|
+
dont_allow.instance_of(Foreman::Engine).export
|
33
|
+
subject.export("testapp")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "with a Procfile" do
|
39
|
+
before(:each) { write_procfile }
|
40
|
+
|
41
|
+
describe "with an invalid formatter" do
|
42
|
+
it "prints an error" do
|
43
|
+
mock_error(subject, "Unknown export format: invalidformatter.") do
|
44
|
+
subject.export("invalidformatter")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "with a valid config" do
|
50
|
+
before(:each) { write_foreman_config("testapp") }
|
51
|
+
|
52
|
+
it "runs successfully" do
|
53
|
+
dont_allow(subject).error
|
54
|
+
mock.instance_of(Foreman::Export::Upstart).export("/tmp/foo", {})
|
55
|
+
subject.export("upstart", "/tmp/foo")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "check" do
|
62
|
+
describe "with a valid Procfile" do
|
63
|
+
before { write_procfile }
|
64
|
+
|
65
|
+
it "displays the jobs" do
|
66
|
+
mock(subject).display("valid procfile detected (alpha, bravo)")
|
67
|
+
subject.check
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "with a blank Procfile" do
|
72
|
+
before do
|
73
|
+
FileUtils.touch("Procfile")
|
74
|
+
end
|
75
|
+
|
76
|
+
it "displays an error" do
|
77
|
+
mock_error(subject, "no processes defined") do
|
78
|
+
subject.check
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|