Skip to content

Commit

Permalink
update init templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierig Le Saux committed Mar 6, 2016
1 parent fb621f7 commit 014c177
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 47 deletions.
13 changes: 7 additions & 6 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

}

file { '/var/log/kibana':
ensure => 'directory',
owner => $kibana4::kibana4_user,
group => $kibana4::kibana4_group,
mode => '0755',
}
# Log dir is now created and chowned by init script
# file { '/var/log/kibana':
# ensure => 'directory',
# owner => $kibana4::kibana4_user,
# group => $kibana4::kibana4_group,
# mode => '0755',
# }

}
24 changes: 12 additions & 12 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
it { should_not contain_file('/opt/kibana').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('false').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana\-4.0.0\-linux\-x64\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
end

context 'installs via archive and no symlink and service ensure and no user' do
Expand All @@ -36,7 +36,7 @@
it { should_not contain_file('/opt/kibana').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana\-4.0.0\-linux\-x64\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
end

context 'installs via archive and no init file' do
Expand Down Expand Up @@ -69,7 +69,7 @@
it { should_not contain_file('/opt/kibana').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('true') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana\-4.0.0\-linux\-x64\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
end

context 'installs via archive and symlink and no user' do
Expand All @@ -86,7 +86,7 @@
it { should contain_file('/opt/kibana4').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('false').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana4\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
end

context 'installs via archive and symlink and service ensure and no user' do
Expand All @@ -103,7 +103,7 @@
it { should contain_file('/opt/kibana4').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana4\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
end

context 'installs via archive and symlink and service ensure/enable and no user' do
Expand All @@ -120,7 +120,7 @@
it { should contain_file('/opt/kibana4').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('true') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana4\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kibana4:kibana4 \/ sh -c "/) }
end

context 'installs via archive and no symlink and user' do
Expand All @@ -142,7 +142,7 @@
it { should_not contain_file('/opt/kibana').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('false').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana\-4.0.0\-linux\-x64\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
end

context 'installs via archive and no symlink and service ensure and user' do
Expand All @@ -164,7 +164,7 @@
it { should_not contain_file('/opt/kibana').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana\-4.0.0\-linux\-x64\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
end

context 'installs via archive and no symlink and service ensure/enable and no user' do
Expand All @@ -186,7 +186,7 @@
it { should_not contain_file('/opt/kibana').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('true') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana\-4.0.0\-linux\-x64\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
end

context 'installs via archive and symlink and user' do
Expand All @@ -208,7 +208,7 @@
it { should contain_file('/opt/kibana4').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('false').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana4\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
end

context 'installs via archive and symlink and service ensure and user' do
Expand All @@ -230,7 +230,7 @@
it { should contain_file('/opt/kibana4').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('false') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana4\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
end

context 'installs via archive and symlink and service ensure/enable and user' do
Expand All @@ -252,7 +252,7 @@
it { should contain_file('/opt/kibana4').with_ensure('link').with_target('/opt/kibana-4.0.0-linux-x64') }
it { should contain_service('kibana4').with_ensure('true').with_enable('true') }
it { should contain_file('/etc/init.d/kibana4').with_content(/^program=\/opt\/kibana4\/bin\/kibana/) }
it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
#it { should contain_file('/etc/init.d/kibana4').with_content(/^ chroot --userspec kib4:kib4 \/ sh -c "/) }
end

context 'installs via package and no symlink and service ensure/enable and user' do
Expand Down
8 changes: 7 additions & 1 deletion templates/default.erb
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export BABEL_CACHE_PATH=<%= scope.lookupvar('kibana4::babel_cache_path') %>
export BABEL_CACHE_PATH=<%= scope.lookupvar('kibana4::babel_cache_path') %>

user="<% scope['kibana4::kibana4_user'] -%>"
group="<% scope['kibana4::kibana4_group'] -%>"
chroot="/"
chdir="/"
nice=""
87 changes: 59 additions & 28 deletions templates/kibana.init.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/sh
# Init script for kibana
# Maintained by Pierig Le Saux
# Maintained by
# Generated by pleaserun.
# Implemented based on LSB Core 3.1:
# * Sections: 20.2, 20.3
#
### BEGIN INIT INFO
# Provides: kibana4
# Provides: kibana
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
Expand All @@ -18,14 +18,19 @@
PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

#name=kibana
name=kibana4

#program=/opt/kibana/bin/kibana
<% if scope['kibana4::symlink'] -%>
program=<%= scope['kibana4::symlink_name'] %>/bin/kibana
<% else -%>
program=<%= scope['kibana4::install_dir'] %>/kibana-<%= scope['kibana4::package_ensure'] %>/bin/kibana
<% end -%>

args=''

#pidfile="/var/run/$name.pid"
<% if scope['kibana4::config'].is_a?(Hash) and scope['kibana4::config'].has_key?('pid_file') -%>
pidfile=<%= scope['kibana4::config']['pid_file'] -%>
<% elsif scope['kibana4::config'].is_a?(Hash) and scope['kibana4::config'].has_key?('pid.file') -%>
Expand All @@ -34,55 +39,71 @@ pidfile=<%= scope['kibana4::config']['pid.file'] -%>
pidfile=/var/run/kibana.pid
<% end -%>

rundir=`dirname $pidfile`
if [ ! -d $rundir ]; then
mkdir -p $rundir
<% if scope['kibana4::kibana4_user'] and scope['kibana4::kibana4_group'] -%>
chown <%= scope['kibana4::kibana4_user'] %>:<%= scope['kibana4::kibana4_group'] %> $rundir
<% end -%>
fi

[ -r /etc/default/$name ] && . /etc/default/$name
[ -r /etc/sysconfig/$name ] && . /etc/sysconfig/$name

trace() {
logger -t "/etc/init.d/kibana" "$@"
}

emit() {
trace "$@"
echo "$@"
}

start() {

# Ensure the log directory is setup correctly.
[ ! -d "/var/log/kibana/" ] && mkdir "/var/log/kibana/"
chown "$user":"$group" "/var/log/kibana/"
chmod 755 "/var/log/kibana/"


# Setup any environmental stuff beforehand


# Run the program!
chroot --userspec <%= scope['kibana4::kibana4_user'] %>:<%= scope['kibana4::kibana4_group'] %> / sh -c "
cd /

chroot --userspec "$user":"$group" "$chroot" sh -c "
cd \"$chdir\"
exec \"$program\" $args
" > /var/log/kibana/$name.log 2> /var/log/kibana/$name.err &
" >> /var/log/kibana/kibana.stdout 2>> /var/log/kibana/kibana.stderr &

echo "$name started."
# Generate the pidfile from here. If we instead made the forked process
# generate it there will be a race condition between the pidfile writing
# and a process possibly asking for status.
echo $! > $pidfile

emit "$name started"
return 0
}

stop() {
# Try a few times to kill TERM the program
if status ; then
pid=`cat "$pidfile"`
echo "Killing $name (pid $pid) with SIGTERM"
pid=$(cat "$pidfile")
trace "Killing $name (pid $pid) with SIGTERM"
kill -TERM $pid
# Wait for it to exit.
for i in 1 2 3 4 5 ; do
echo "Waiting $name (pid $pid) to die..."
trace "Waiting $name (pid $pid) to die..."
status || break
sleep 1
done
if status ; then
echo "$name stop failed; still running."
emit "$name stop failed; still running."
else
echo "$name stopped."
rm -f $pidfile
emit "$name stopped."
fi
fi
}

status() {
if [ -f "$pidfile" ] ; then
pid=`cat "$pidfile"`
if kill -0 $pid > /dev/null 2> /dev/null ; then
pid=$(cat "$pidfile")
if ps -p $pid > /dev/null 2> /dev/null ; then
# process by this pid is running.
# It may not be our pid, but that's what you get with just pidfiles.
# TODO(sissel): Check if this process seems to be the same as the one we
Expand All @@ -100,32 +121,42 @@ status() {
force_stop() {
if status ; then
stop
status && kill -KILL `cat "$pidfile"`
rm -f $pidfile
status && kill -KILL $(cat "$pidfile")
fi
}


case "$1" in
force-start|start|stop|force-stop|restart)
trace "Attempting '$1' on kibana"
;;
esac

case "$1" in
force-start)
PRESTART=no
exec "$0" start
;;
start)
status
code=$?
if [ $code -eq 0 ]; then
echo "$name is already running"
emit "$name is already running"
exit $code
else
start
exit $?
fi
#exit $code
;;
stop) stop ;;
force-stop) force_stop ;;
status)
status
code=$?
if [ $code -eq 0 ] ; then
echo "$name is running"
emit "$name is running"
else
echo "$name is not running"
emit "$name is not running"
fi
exit $code
;;
Expand All @@ -134,7 +165,7 @@ case "$1" in
stop && start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|force-stop|status|restart}" >&2
echo "Usage: $SCRIPTNAME {start|force-start|stop|force-start|force-stop|status|restart}" >&2
exit 3
;;
esac
Expand Down

0 comments on commit 014c177

Please sign in to comment.