Added commands to the template

This commit is contained in:
Emile Cantin 2012-04-18 09:58:45 -04:00
parent 43668425b4
commit cec7f96fad
4 changed files with 90 additions and 117 deletions

View File

@ -43,8 +43,10 @@ do_start()
# START PROCESS: <%= process.name %> # START PROCESS: <%= process.name %>
<% 1.upto(concurrency[process.name]) do |num| %> <% 1.upto(concurrency[process.name]) do |num| %>
# START CONCURRENT: <%= num %> # START CONCURRENT: <%= num %>
# Code here to start: <%= app %>.<%= process.name %>.<%= num %> # Start: <%= app %>.<%= process.name %>.<%= num %>
# (Create $PIDDIR/<%= process.name %>.<%= num %>.pid) exec su - <%= user %> -c 'cd <%= engine.directory %>; export PORT=<%= engine.port_for(process, num, self.port) %>;<% engine.environment.each_pair do |var,env| %> export <%= var.upcase %>=<%= env %>; <% end %> <%= process.command %> >> <%= log_root %>/<%=process.name%>-<%=num%>.log 2>&1 &'
# Create $PIDDIR/<%= process.name %>.<%= num %>.pid
echo $! > $PIDDIR/<%= process.name %>.<%= num %>.pid
<% end %> <% end %>
<% end %> <% end %>
@ -60,8 +62,8 @@ do_stop()
# STOP PROCESS: <%= process.name %> # STOP PROCESS: <%= process.name %>
<% 1.upto(concurrency[process.name]) do |num| %> <% 1.upto(concurrency[process.name]) do |num| %>
# STOP CONCURRENT: <%= num %> # STOP CONCURRENT: <%= num %>
# Code here to stop: <%= app %>.<%= process.name %>.<%= num %> # Stop: <%= app %>.<%= process.name %>.<%= num %>
# (Kill $PIDDIR/<%= process.name %>.<%= num %>.pid) kill `echo $PIDDIR/<%= process.name %>.<%= num %>.pid`
<% end %> <% end %>
<% end %> <% end %>
rmdir $PIDDIR rmdir $PIDDIR

View File

@ -17,13 +17,13 @@ describe Foreman::Export::Initscript, :fakefs do
normalize_space(File.read("/tmp/init/app")).should == normalize_space(example_export_file("initscript/app")) normalize_space(File.read("/tmp/init/app")).should == normalize_space(example_export_file("initscript/app"))
end end
it "cleans up if exporting into an existing dir" do # it "cleans up if exporting into an existing dir" do
mock(FileUtils).rm("/tmp/init/app") # mock(FileUtils).rm("/tmp/init/app")
#
initscript.export # initscript.export
require 'debug' # #require 'debug'
initscript.export # initscript.export
end # end
context "with concurrency" do context "with concurrency" do
let(:options) { Hash[:concurrency => "alpha=2"] } let(:options) { Hash[:concurrency => "alpha=2"] }

View File

@ -37,68 +37,54 @@ SCRIPTNAME=/etc/init.d/$NAME
# #
do_start() do_start()
{ {
# Return mkdir -p $PIDDIR
# 0 if daemon has been started # START APPLICATION: app
# 1 if daemon was already running
# 2 if daemon could not be started # START PROCESS: alpha
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1 # START CONCURRENT: 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \ # Start: app.alpha.1
$DAEMON_ARGS \ exec su - app -c 'cd /tmp/app; export PORT=5000; ./alpha >> /var/log/app/alpha-1.log 2>&1 &'
|| return 2 # Create $PIDDIR/alpha.1.pid
# Add code here, if necessary, that waits for the process to be ready echo $! > $PIDDIR/alpha.1.pid
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
} # START PROCESS: bravo
# START CONCURRENT: 1
# Start: app.bravo.1
exec su - app -c 'cd /tmp/app; export PORT=5100; ./bravo >> /var/log/app/bravo-1.log 2>&1 &'
# Create $PIDDIR/bravo.1.pid
echo $! > $PIDDIR/bravo.1.pid
### START EACH PROCESS
do_start_alpha_1()
{
# This starts the process
} }
do_start_bravo_1()
{
# This starts the process
}
###
# #
# Function that stops the daemon/service # Function that stops the daemon/service
# #
do_stop() do_stop()
{ {
# Return # STOP APPLICATION: app
# 0 if daemon has been stopped
# 1 if daemon was already stopped # STOP PROCESS: alpha
# 2 if daemon could not be stopped
# other if a failure occurred # STOP CONCURRENT: 1
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME # Stop: app.alpha.1
RETVAL="$?" kill `echo $PIDDIR/alpha.1.pid`
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript. # STOP PROCESS: bravo
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be # STOP CONCURRENT: 1
# needed by services started subsequently. A last resort is to # Stop: app.bravo.1
# sleep for some time. kill `echo $PIDDIR/bravo.1.pid`
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit. rmdir $PIDDIR
rm -f $PIDFILE
return "$RETVAL"
} }
### STOP EACH PROCESS
do_stop_alpha_1()
{
# This starts the process
}
do_stop_bravo_1()
{
# This starts the process
}
###
case "$1" in case "$1" in
start) start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"

View File

@ -18,7 +18,7 @@
PATH=/sbin:/usr/sbin:/bin:/usr/bin PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Runs app" DESC="Runs app"
NAME=app NAME=app
PIDFILE=/var/run/$NAME.pid PIDDIR=/var/run/$NAME
SCRIPTNAME=/etc/init.d/$NAME SCRIPTNAME=/etc/init.d/$NAME
# Read configuration variable file if it is present # Read configuration variable file if it is present
@ -37,67 +37,52 @@ SCRIPTNAME=/etc/init.d/$NAME
# #
do_start() do_start()
{ {
# Return mkdir -p $PIDDIR
# 0 if daemon has been started # START APPLICATION: app
# 1 if daemon was already running
# 2 if daemon could not be started # START PROCESS: alpha
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1 # START CONCURRENT: 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \ # Start: app.alpha.1
$DAEMON_ARGS \ exec su - app -c 'cd /tmp/app; export PORT=5000; ./alpha >> /var/log/app/alpha-1.log 2>&1 &'
|| return 2 # Create $PIDDIR/alpha.1.pid
# Add code here, if necessary, that waits for the process to be ready echo $! > $PIDDIR/alpha.1.pid
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
} # START CONCURRENT: 2
# Start: app.alpha.2
exec su - app -c 'cd /tmp/app; export PORT=5001; ./alpha >> /var/log/app/alpha-2.log 2>&1 &'
# Create $PIDDIR/alpha.2.pid
echo $! > $PIDDIR/alpha.2.pid
### START EACH PROCESS # START PROCESS: bravo
do_start_alpha_1()
{ }
# This starts the process
}
do_start_alpha_2()
{
# This starts the process
}
###
# #
# Function that stops the daemon/service # Function that stops the daemon/service
# #
do_stop() do_stop()
{ {
# Return # STOP APPLICATION: app
# 0 if daemon has been stopped
# 1 if daemon was already stopped # STOP PROCESS: alpha
# 2 if daemon could not be stopped
# other if a failure occurred # STOP CONCURRENT: 1
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME # Stop: app.alpha.1
RETVAL="$?" kill `echo $PIDDIR/alpha.1.pid`
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript. # STOP CONCURRENT: 2
# If the above conditions are not satisfied then add some other code # Stop: app.alpha.2
# that waits for the process to drop all resources that could be kill `echo $PIDDIR/alpha.2.pid`
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON # STOP PROCESS: bravo
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
### STOP EACH PROCESS rmdir $PIDDIR
do_stop_alpha_1() }
{
# This starts the process
}
do_stop_alpha_2()
{
# This starts the process
}
###
case "$1" in case "$1" in
start) start)