foreman-systemd 0.78.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +64 -0
  3. data/bin/foreman +7 -0
  4. data/bin/foreman-runner +41 -0
  5. data/data/example/Procfile +4 -0
  6. data/data/example/Procfile.without_colon +2 -0
  7. data/data/example/error +7 -0
  8. data/data/example/log/neverdie.log +4 -0
  9. data/data/example/spawnee +14 -0
  10. data/data/example/spawner +7 -0
  11. data/data/example/ticker +14 -0
  12. data/data/example/utf8 +11 -0
  13. data/data/export/bluepill/master.pill.erb +28 -0
  14. data/data/export/daemon/master.conf.erb +14 -0
  15. data/data/export/daemon/process.conf.erb +8 -0
  16. data/data/export/daemon/process_master.conf.erb +2 -0
  17. data/data/export/launchd/launchd.plist.erb +33 -0
  18. data/data/export/runit/log/run.erb +7 -0
  19. data/data/export/runit/run.erb +4 -0
  20. data/data/export/supervisord/app.conf.erb +28 -0
  21. data/data/export/systemd/master.target.erb +6 -0
  22. data/data/export/systemd/process.service.erb +16 -0
  23. data/data/export/systemd/process_master.target.erb +4 -0
  24. data/data/export/upstart/master.conf.erb +2 -0
  25. data/data/export/upstart/process.conf.erb +14 -0
  26. data/data/export/upstart/process_master.conf.erb +2 -0
  27. data/lib/foreman.rb +17 -0
  28. data/lib/foreman/cli.rb +161 -0
  29. data/lib/foreman/distribution.rb +9 -0
  30. data/lib/foreman/engine.rb +441 -0
  31. data/lib/foreman/engine/cli.rb +104 -0
  32. data/lib/foreman/env.rb +29 -0
  33. data/lib/foreman/export.rb +36 -0
  34. data/lib/foreman/export/base.rb +156 -0
  35. data/lib/foreman/export/bluepill.rb +12 -0
  36. data/lib/foreman/export/daemon.rb +28 -0
  37. data/lib/foreman/export/inittab.rb +42 -0
  38. data/lib/foreman/export/launchd.rb +22 -0
  39. data/lib/foreman/export/runit.rb +34 -0
  40. data/lib/foreman/export/supervisord.rb +16 -0
  41. data/lib/foreman/export/systemd.rb +32 -0
  42. data/lib/foreman/export/upstart.rb +43 -0
  43. data/lib/foreman/helpers.rb +45 -0
  44. data/lib/foreman/process.rb +80 -0
  45. data/lib/foreman/procfile.rb +92 -0
  46. data/lib/foreman/version.rb +5 -0
  47. data/man/foreman.1 +278 -0
  48. data/spec/foreman/cli_spec.rb +107 -0
  49. data/spec/foreman/engine_spec.rb +112 -0
  50. data/spec/foreman/export/base_spec.rb +19 -0
  51. data/spec/foreman/export/bluepill_spec.rb +37 -0
  52. data/spec/foreman/export/daemon_spec.rb +97 -0
  53. data/spec/foreman/export/inittab_spec.rb +40 -0
  54. data/spec/foreman/export/launchd_spec.rb +31 -0
  55. data/spec/foreman/export/runit_spec.rb +36 -0
  56. data/spec/foreman/export/supervisord_spec.rb +36 -0
  57. data/spec/foreman/export/systemd_spec.rb +91 -0
  58. data/spec/foreman/export/upstart_spec.rb +118 -0
  59. data/spec/foreman/export_spec.rb +24 -0
  60. data/spec/foreman/helpers_spec.rb +26 -0
  61. data/spec/foreman/process_spec.rb +71 -0
  62. data/spec/foreman/procfile_spec.rb +43 -0
  63. data/spec/foreman_spec.rb +16 -0
  64. data/spec/helper_spec.rb +19 -0
  65. data/spec/resources/Procfile +5 -0
  66. data/spec/resources/bin/echo +2 -0
  67. data/spec/resources/bin/env +2 -0
  68. data/spec/resources/bin/test +2 -0
  69. data/spec/resources/bin/utf8 +2 -0
  70. data/spec/resources/export/bluepill/app-concurrency.pill +49 -0
  71. data/spec/resources/export/bluepill/app.pill +81 -0
  72. data/spec/resources/export/daemon/app-alpha-1.conf +7 -0
  73. data/spec/resources/export/daemon/app-alpha-2.conf +7 -0
  74. data/spec/resources/export/daemon/app-alpha.conf +2 -0
  75. data/spec/resources/export/daemon/app-bravo-1.conf +7 -0
  76. data/spec/resources/export/daemon/app-bravo.conf +2 -0
  77. data/spec/resources/export/daemon/app.conf +14 -0
  78. data/spec/resources/export/inittab/inittab.concurrency +4 -0
  79. data/spec/resources/export/inittab/inittab.default +6 -0
  80. data/spec/resources/export/launchd/launchd-a.default +29 -0
  81. data/spec/resources/export/launchd/launchd-b.default +29 -0
  82. data/spec/resources/export/launchd/launchd-c.default +30 -0
  83. data/spec/resources/export/runit/app-alpha-1/log/run +7 -0
  84. data/spec/resources/export/runit/app-alpha-1/run +4 -0
  85. data/spec/resources/export/runit/app-alpha-2/log/run +7 -0
  86. data/spec/resources/export/runit/app-alpha-2/run +4 -0
  87. data/spec/resources/export/runit/app-bravo-1/log/run +7 -0
  88. data/spec/resources/export/runit/app-bravo-1/run +4 -0
  89. data/spec/resources/export/supervisord/app-alpha-1.conf +46 -0
  90. data/spec/resources/export/supervisord/app-alpha-2.conf +24 -0
  91. data/spec/resources/export/systemd/concurrency/app-alpha-1.service +14 -0
  92. data/spec/resources/export/systemd/concurrency/app-alpha-2.service +14 -0
  93. data/spec/resources/export/systemd/concurrency/app-alpha.target +3 -0
  94. data/spec/resources/export/systemd/concurrency/app.target +6 -0
  95. data/spec/resources/export/systemd/standard/app-alpha-1.service +14 -0
  96. data/spec/resources/export/systemd/standard/app-alpha.target +3 -0
  97. data/spec/resources/export/systemd/standard/app-bravo-1.service +14 -0
  98. data/spec/resources/export/systemd/standard/app-bravo.target +3 -0
  99. data/spec/resources/export/systemd/standard/app.target +6 -0
  100. data/spec/resources/export/upstart/app-alpha-1.conf +11 -0
  101. data/spec/resources/export/upstart/app-alpha-2.conf +11 -0
  102. data/spec/resources/export/upstart/app-alpha.conf +2 -0
  103. data/spec/resources/export/upstart/app-bravo-1.conf +11 -0
  104. data/spec/resources/export/upstart/app-bravo.conf +2 -0
  105. data/spec/resources/export/upstart/app.conf +2 -0
  106. data/spec/spec_helper.rb +166 -0
  107. metadata +164 -0
@@ -0,0 +1,45 @@
1
+ module Foreman::Helpers
2
+ # Copied whole sale from, https://github.com/defunkt/resque/
3
+
4
+ # Given a word with dashes, returns a camel cased version of it.
5
+ #
6
+ # classify('job-name') # => 'JobName'
7
+ def classify(dashed_word)
8
+ dashed_word.split('-').each { |part| part[0] = part[0].chr.upcase }.join
9
+ end # Tries to find a constant with the name specified in the argument string:
10
+
11
+ #
12
+ # constantize("Module") # => Module
13
+ # constantize("Test::Unit") # => Test::Unit
14
+ #
15
+ # The name is assumed to be the one of a top-level constant, no matter
16
+ # whether it starts with "::" or not. No lexical context is taken into
17
+ # account:
18
+ #
19
+ # C = 'outside'
20
+ # module M
21
+ # C = 'inside'
22
+ # C # => 'inside'
23
+ # constantize("C") # => 'outside', same as ::C
24
+ # end
25
+ #
26
+ # NameError is raised when the constant is unknown.
27
+ def constantize(camel_cased_word)
28
+ camel_cased_word = camel_cased_word.to_s
29
+
30
+ names = camel_cased_word.split('::')
31
+ names.shift if names.empty? || names.first.empty?
32
+
33
+ constant = Object
34
+ names.each do |name|
35
+ args = Module.method(:const_get).arity != 1 ? [false] : []
36
+
37
+ if constant.const_defined?(name, *args)
38
+ constant = constant.const_get(name)
39
+ else
40
+ constant = constant.const_missing(name)
41
+ end
42
+ end
43
+ constant
44
+ end
45
+ end
@@ -0,0 +1,80 @@
1
+ require "foreman"
2
+ require "shellwords"
3
+
4
+ class Foreman::Process
5
+
6
+ attr_reader :command
7
+ attr_reader :env
8
+
9
+ # Create a Process
10
+ #
11
+ # @param [String] command The command to run
12
+ # @param [Hash] options
13
+ #
14
+ # @option options [String] :cwd (./) Change to this working directory before executing the process
15
+ # @option options [Hash] :env ({}) Environment variables to set for this process
16
+ #
17
+ def initialize(command, options={})
18
+ @command = command
19
+ @options = options.dup
20
+
21
+ @options[:env] ||= {}
22
+ end
23
+
24
+ # Get environment-expanded command for a +Process+
25
+ #
26
+ # @param [Hash] custom_env ({}) Environment variables to merge with defaults
27
+ #
28
+ # @return [String] The expanded command
29
+ #
30
+ def expanded_command(custom_env={})
31
+ env = @options[:env].merge(custom_env)
32
+ expanded_command = command.dup
33
+ env.each do |key, val|
34
+ expanded_command.gsub!("$#{key}", val)
35
+ end
36
+ expanded_command
37
+ end
38
+
39
+ # Run a +Process+
40
+ #
41
+ # @param [Hash] options
42
+ #
43
+ # @option options :env ({}) Environment variables to set for this execution
44
+ # @option options :output ($stdout) The output stream
45
+ #
46
+ # @returns [Fixnum] pid The +pid+ of the process
47
+ #
48
+ def run(options={})
49
+ env = @options[:env].merge(options[:env] || {})
50
+ output = options[:output] || $stdout
51
+ runner = "#{Foreman.runner}".shellescape
52
+
53
+ Dir.chdir(cwd) do
54
+ Process.spawn env, expanded_command(env), :out => output, :err => output
55
+ end
56
+ end
57
+
58
+ # Exec a +Process+
59
+ #
60
+ # @param [Hash] options
61
+ #
62
+ # @option options :env ({}) Environment variables to set for this execution
63
+ #
64
+ # @return Does not return
65
+ def exec(options={})
66
+ env = @options[:env].merge(options[:env] || {})
67
+ env.each { |k, v| ENV[k] = v }
68
+ Dir.chdir(cwd)
69
+ Kernel.exec expanded_command(env)
70
+ end
71
+
72
+ # Returns the working directory for this +Process+
73
+ #
74
+ # @returns [String]
75
+ #
76
+ def cwd
77
+ File.expand_path(@options[:cwd] || ".")
78
+ end
79
+
80
+ end
@@ -0,0 +1,92 @@
1
+ require "foreman"
2
+
3
+ # Reads and writes Procfiles
4
+ #
5
+ # A valid Procfile entry is captured by this regex:
6
+ #
7
+ # /^([A-Za-z0-9_]+):\s*(.+)$/
8
+ #
9
+ # All other lines are ignored.
10
+ #
11
+ class Foreman::Procfile
12
+
13
+ # Initialize a Procfile
14
+ #
15
+ # @param [String] filename (nil) An optional filename to read from
16
+ #
17
+ def initialize(filename=nil)
18
+ @entries = []
19
+ load(filename) if filename
20
+ end
21
+
22
+ # Yield each +Procfile+ entry in order
23
+ #
24
+ def entries(&blk)
25
+ @entries.each do |(name, command)|
26
+ yield name, command
27
+ end
28
+ end
29
+
30
+ # Retrieve a +Procfile+ command by name
31
+ #
32
+ # @param [String] name The name of the Procfile entry to retrieve
33
+ #
34
+ def [](name)
35
+ @entries.detect { |n,c| name == n }.last
36
+ end
37
+
38
+ # Create a +Procfile+ entry
39
+ #
40
+ # @param [String] name The name of the +Procfile+ entry to create
41
+ # @param [String] command The command of the +Procfile+ entry to create
42
+ #
43
+ def []=(name, command)
44
+ delete name
45
+ @entries << [name, command]
46
+ end
47
+
48
+ # Remove a +Procfile+ entry
49
+ #
50
+ # @param [String] name The name of the +Procfile+ entry to remove
51
+ #
52
+ def delete(name)
53
+ @entries.reject! { |n,c| name == n }
54
+ end
55
+
56
+ # Load a Procfile from a file
57
+ #
58
+ # @param [String] filename The filename of the +Procfile+ to load
59
+ #
60
+ def load(filename)
61
+ @entries.replace parse(filename)
62
+ end
63
+
64
+ # Save a Procfile to a file
65
+ #
66
+ # @param [String] filename Save the +Procfile+ to this file
67
+ #
68
+ def save(filename)
69
+ File.open(filename, 'w') do |file|
70
+ file.puts self.to_s
71
+ end
72
+ end
73
+
74
+ # Get the +Procfile+ as a +String+
75
+ #
76
+ def to_s
77
+ @entries.map do |name, command|
78
+ [ name, command ].join(": ")
79
+ end.join("\n")
80
+ end
81
+
82
+ private
83
+
84
+ def parse(filename)
85
+ File.read(filename).gsub("\r\n","\n").split("\n").map do |line|
86
+ if line =~ /^([A-Za-z0-9_-]+):\s*(.+)$/
87
+ [$1, $2]
88
+ end
89
+ end.compact
90
+ end
91
+
92
+ end
@@ -0,0 +1,5 @@
1
+ module Foreman
2
+
3
+ VERSION = "0.78.0"
4
+
5
+ end
@@ -0,0 +1,278 @@
1
+ .\" generated with Ronn/v0.7.3
2
+ .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
+ .
4
+ .TH "FOREMAN" "1" "January 2015" "Foreman 0.77.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 run <command>\fR
14
+ .
15
+ .br
16
+ \fBforeman export <format> [location]\fR
17
+ .
18
+ .SH "DESCRIPTION"
19
+ Foreman 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\.
20
+ .
21
+ .SH "RUNNING"
22
+ \fBforeman start\fR is used to run your application directly from the command line\.
23
+ .
24
+ .P
25
+ If no additional parameters are passed, foreman will run one instance of each type of process defined in your Procfile\.
26
+ .
27
+ .P
28
+ If a parameter is passed, foreman will run one instance of the specified application type\.
29
+ .
30
+ .P
31
+ The following options control how the application is run:
32
+ .
33
+ .TP
34
+ \fB\-c\fR, \fB\-\-concurrency\fR
35
+ Specify the number of each process type to run\. The value passed in should be in the format \fBprocess=num,process=num\fR
36
+ .
37
+ .TP
38
+ \fB\-e\fR, \fB\-\-env\fR
39
+ Specify one or more \.env files to load
40
+ .
41
+ .TP
42
+ \fB\-f\fR, \fB\-\-procfile\fR
43
+ Specify an alternate Procfile to load, implies \fB\-d\fR at the Procfile root\.
44
+ .
45
+ .TP
46
+ \fB\-p\fR, \fB\-\-port\fR
47
+ Specify which port to use as the base for this application\. Should be a multiple of 1000\.
48
+ .
49
+ .TP
50
+ \fB\-t\fR, \fB\-\-timeout\fR
51
+ Specify the amount of time (in seconds) processes have to shutdown gracefully before receiving a SIGKILL, defaults to 5\.
52
+ .
53
+ .P
54
+ \fBforeman run\fR is used to run one\-off commands using the same environment as your defined processes\.
55
+ .
56
+ .SH "EXPORTING"
57
+ \fBforeman export\fR is used to export your application to another process management format\.
58
+ .
59
+ .P
60
+ An location to export can be passed as an argument\. This argument may be either required or optional depending on the export format\.
61
+ .
62
+ .P
63
+ The following options control how the application is run:
64
+ .
65
+ .TP
66
+ \fB\-a\fR, \fB\-\-app\fR
67
+ Use this name rather than the application\'s root directory name as the name of the application when exporting\.
68
+ .
69
+ .TP
70
+ \fB\-c\fR, \fB\-\-concurrency\fR
71
+ Specify the number of each process type to run\. The value passed in should be in the format \fBprocess=num,process=num\fR
72
+ .
73
+ .TP
74
+ \fB\-l\fR, \fB\-\-log\fR
75
+ Specify the directory to place process logs in\.
76
+ .
77
+ .TP
78
+ \fB\-p\fR, \fB\-\-port\fR
79
+ Specify which port to use as the base for this application\. Should be a multiple of 1000\.
80
+ .
81
+ .TP
82
+ \fB\-t\fR, \fB\-\-template\fR
83
+ Specify an alternate template to use for creating export files\. See \fIhttps://github\.com/ddollar/foreman/tree/master/data/export\fR for examples\.
84
+ .
85
+ .TP
86
+ \fB\-u\fR, \fB\-\-user\fR
87
+ Specify the user the application should be run as\. Defaults to the app name
88
+ .
89
+ .SH "GLOBAL OPTIONS"
90
+ These options control all modes of foreman\'s operation\.
91
+ .
92
+ .TP
93
+ \fB\-d\fR, \fB\-\-root\fR
94
+ Specify an alternate application root\. This defaults to the directory containing the Procfile\.
95
+ .
96
+ .TP
97
+ \fB\-e\fR, \fB\-\-env\fR
98
+ Specify an alternate environment file\. You can specify more than one file by using: \fB\-\-env file1,file2\fR\.
99
+ .
100
+ .TP
101
+ \fB\-f\fR, \fB\-\-procfile\fR
102
+ 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\.
103
+ .
104
+ .SH "EXPORT FORMATS"
105
+ foreman currently supports the following output formats:
106
+ .
107
+ .IP "\(bu" 4
108
+ bluepill
109
+ .
110
+ .IP "\(bu" 4
111
+ inittab
112
+ .
113
+ .IP "\(bu" 4
114
+ launchd
115
+ .
116
+ .IP "\(bu" 4
117
+ runit
118
+ .
119
+ .IP "\(bu" 4
120
+ supervisord
121
+ .
122
+ .IP "\(bu" 4
123
+ systemd
124
+ .
125
+ .IP "\(bu" 4
126
+ upstart
127
+ .
128
+ .IP "" 0
129
+ .
130
+ .SH "INITTAB EXPORT"
131
+ Will export a chunk of inittab\-compatible configuration:
132
+ .
133
+ .IP "" 4
134
+ .
135
+ .nf
136
+
137
+ # \-\-\-\-\- foreman example processes \-\-\-\-\-
138
+ EX01:4:respawn:/bin/su \- example \-c \'PORT=5000 bundle exec thin start >> /var/log/web\-1\.log 2>&1\'
139
+ EX02:4:respawn:/bin/su \- example \-c \'PORT=5100 bundle exec rake jobs:work >> /var/log/job\-1\.log 2>&1\'
140
+ # \-\-\-\-\- end foreman example processes \-\-\-\-\-
141
+ .
142
+ .fi
143
+ .
144
+ .IP "" 0
145
+ .
146
+ .SH "SYSTEMD EXPORT"
147
+ Will create a series of systemd scripts in the location you specify\. Scripts will be structured to make the following commands valid:
148
+ .
149
+ .P
150
+ \fBsystemctl start appname\.target\fR
151
+ .
152
+ .P
153
+ \fBsystemctl stop appname\-processname\.target\fR
154
+ .
155
+ .P
156
+ \fBsystemctl restart appname\-processname\-3\.service\fR
157
+ .
158
+ .SH "UPSTART EXPORT"
159
+ Will create a series of upstart scripts in the location you specify\. Scripts will be structured to make the following commands valid:
160
+ .
161
+ .P
162
+ \fBstart appname\fR
163
+ .
164
+ .P
165
+ \fBstop appname\-processname\fR
166
+ .
167
+ .P
168
+ \fBrestart appname\-processname\-3\fR
169
+ .
170
+ .SH "PROCFILE"
171
+ A Procfile should contain both a name for the process and the command used to run it\.
172
+ .
173
+ .IP "" 4
174
+ .
175
+ .nf
176
+
177
+ web: bundle exec thin start
178
+ job: bundle exec rake jobs:work
179
+ .
180
+ .fi
181
+ .
182
+ .IP "" 0
183
+ .
184
+ .P
185
+ A process name may contain letters, numbers and the underscore character\. You can validate your Procfile format using the \fBcheck\fR command:
186
+ .
187
+ .IP "" 4
188
+ .
189
+ .nf
190
+
191
+ $ foreman check
192
+ .
193
+ .fi
194
+ .
195
+ .IP "" 0
196
+ .
197
+ .SH "ENVIRONMENT"
198
+ 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\.
199
+ .
200
+ .IP "" 4
201
+ .
202
+ .nf
203
+
204
+ FOO=bar
205
+ BAZ=qux
206
+ .
207
+ .fi
208
+ .
209
+ .IP "" 0
210
+ .
211
+ .SH "DEFAULT OPTIONS"
212
+ 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:
213
+ .
214
+ .IP "" 4
215
+ .
216
+ .nf
217
+
218
+ concurrency: alpha=0,bravo=1
219
+ port: 15000
220
+ .
221
+ .fi
222
+ .
223
+ .IP "" 0
224
+ .
225
+ .SH "EXAMPLES"
226
+ Start one instance of each process type, interleave the output on stdout:
227
+ .
228
+ .IP "" 4
229
+ .
230
+ .nf
231
+
232
+ $ foreman start
233
+ .
234
+ .fi
235
+ .
236
+ .IP "" 0
237
+ .
238
+ .P
239
+ Export the application in upstart format:
240
+ .
241
+ .IP "" 4
242
+ .
243
+ .nf
244
+
245
+ $ foreman export upstart /etc/init
246
+ .
247
+ .fi
248
+ .
249
+ .IP "" 0
250
+ .
251
+ .P
252
+ Run one process type from the application defined in a specific Procfile:
253
+ .
254
+ .IP "" 4
255
+ .
256
+ .nf
257
+
258
+ $ foreman start alpha \-f ~/myapp/Procfile
259
+ .
260
+ .fi
261
+ .
262
+ .IP "" 0
263
+ .
264
+ .P
265
+ Start all processes except the one named worker:
266
+ .
267
+ .IP "" 4
268
+ .
269
+ .nf
270
+
271
+ $ foreman start \-c all=1,worker=0
272
+ .
273
+ .fi
274
+ .
275
+ .IP "" 0
276
+ .
277
+ .SH "COPYRIGHT"
278
+ Foreman is Copyright (C) 2010 David Dollar \fIhttp://daviddollar\.org\fR