Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip/bashscript refactoring #348

Merged
merged 3 commits into from
Oct 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
${{header}}
${{loader-functions}}
${{control-functions}}

addGroup ${{daemon_group}}
addUser ${{daemon_user}} ${{daemon_group}} "${{app_name}} user-daemon" "${{daemon_shell}}"

${{chown-paths}}

startService ${{app_name}} || echo "${{app_name}} could not be registered or started"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
${{header}}
${{control-functions}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely like this idea a lot, splitting the template into re-usable components....

If only we could auto-document this stuff...


# Deleting user: ${{daemon_user}} and group: ${{daemon_group}}
case "$1" in
remove|failed-upgrade|abort-upgrade|abort-install|disappear)
;;
purge)
deleteUser ${{daemon_user}}
deleteGroup ${{daemon_group}}
;;
upgrade)
;;
*)
echo "postinst called with unknown argument \`\$1'" >&2
;;
esac
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
${{header}}
${{control-functions}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
${{header}}
${{loader-functions}}

stopService ${{app_name}} || echo "${{app_name}} wasn't even running!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
${{loader-functions}}
${{control-functions}}

addGroup ${{daemon_group}}
addUser ${{daemon_user}} ${{daemon_group}} "${{app_name}} user-daemon" "${{daemon_shell}}"

startService ${{app_name}} || echo "${{app_name}} could not be registered or started"
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ then
echo "Try deleting system user and group [${{daemon_user}}:${{daemon_group}}]"
if getent passwd | grep -q "^${{daemon_user}}:";
then
echo "Deleting system user: ${{daemon_user}}"
userdel ${{daemon_user}}
echo "Deleting system user: ${{daemon_user}}"
deleteUser ${{daemon_user}}
fi
if getent group | grep -q "^${{daemon_group}}:" ;
then
echo "Deleting system group: ${{daemon_group}}"
groupdel ${{daemon_group}}
echo "Deleting system group: ${{daemon_group}}"
deleteGroup ${{daemon_group}}
fi
fi
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ then
echo "Creating system group: ${{daemon_group}}"
groupadd --system ${{daemon_group}}
fi
if ! getent passwd | grep -q "^${{daemon_user}}:";
if ! getent passwd | grep -q "^${{daemon_user}}:";
then
echo "Creating system user: ${{daemon_user}}"
useradd --gid ${{daemon_group}} --no-create-home --system -c '${{descr}}' ${{daemon_user}}
useradd --gid ${{daemon_group}} --no-create-home --system -c '${{app_name}} daemon-user' ${{daemon_user}}
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Halting ${{app_name}}
echo "Shutdown ${{app_name}}"
service ${{app_name}} stop || echo "Could not stop ${{app_name}}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Adding service to autostart
# $1 = service name
#
startService() {
app_name=$1
systemctl enable "$app_name.service"
systemctl start "$app_name.service"
}

#
# Removing service from autostart
# $1 = service name
#
stopService() {
app_name=$1
systemctl disable "$app_name.service"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Restart=always
RestartSec=${{retryTimeout}}

[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Adding service to autostart
# $1 = service name
#
startService() {
app_name=$1
if hash update-rc.d 2>/dev/null; then
echo "Adding $app_name to autostart using update-rc.d"
update-rc.d $app_name defaults
service $app_name start
elif hash chkconfig 2>/dev/null; then
echo "Adding $app_name to autostart using chkconfig"
chkconfig --add ${{app_name}}
chkconfig $app_name on
service $app_name start
else
echo "WARNING: Could not put $app_name in autostart: update-rc and chkconfig or found!"
fi
}

#
# Removing service from autostart
# $1 = service name
#
stopService() {
app_name=$1
service $app_name stop
if hash update-rc.d 2>/dev/null; then
echo "Removing $app_name from autostart using update-rc.d"
update-rc.d -f $app_name remove
service $app_name stop
elif chkconfig 2>/dev/null; then
echo "Removing $app_name from autostart using chkconfig"
chkconfig $app_name off
chkconfig --del $app_name
service $app_name stop
else
echo "WARNING: Could not put $app_name in autostart: update-rc or chkconfig not found!"
fi

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

### -----------------
# This script was created using following sources
#
#
# http://stackoverflow.com/questions/8124345/call-to-daemon-in-a-etc-init-d-script-is-blocking-not-running-in-background
# https://fedoraproject.org/wiki/Packaging:SysVInitScript#Initscript_template
### -----------------
Expand Down Expand Up @@ -50,19 +50,19 @@ start() {
[ -x $RUN_CMD ] || exit 5
echo -n $"Starting $prog: "
cd ${{chdir}}

# FIXME figure out how to use daemon correctly
nohup runuser $DAEMON_USER ${RUN_CMD} >> /var/log/${{app_name}}/daemon.log 2>&1 &

# The way to go, but doesn't work properly
# If the app creates the pid file this gets messy
# daemon --user $DAEMON_USER --pidfile $PIDFILE $RUN_CMD &


retval=$? # last error code
PID=$! # pid of last backgrounded process
[ $retval -eq 0 ] && touch ${lockfile} && success || failure

# Insert pid into pid file for CentOS killproc function
[ -d "/var/run/${{app_name}}" ] || install -d -o "$DAEMON_USER" -m750 "/var/run/${{app_name}}"
echo
Expand Down Expand Up @@ -103,32 +103,32 @@ rh_status_q() {

case "$1" in
start)
rh_status_q && exit 0
$1
;;
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
rh_status_q || exit 0
$1
;;
restart)
$1
;;
$1
;;
reload)
rh_status || exit 7
$1
;;
rh_status || exit 7
$1
;;
force-reload)
force_reload
;;
force_reload
;;
status)
rh_status
;;
rh_status
;;
condrestart|try-restart)
rh_status || exit 0
restart
;;
rh_status || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Adding service to autostart
# $1 = service name
#
startService() {
app_name=$1
initctl reload-configuration
service $app_name start
}

#
# Removing service from autostart
# $1 = service name
#
stopService() {
app_name=$1
service $app_name stop
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ author "${{author}}"

# When to start the service
start on runlevel ${{start_runlevels}}
${{start_facilities}}

# When to stop the service
stop on runlevel ${{stop_runlevels}}
${{stop_facilities}}

# Automatically restart process if crashed. Tries ${{retries}} times every ${{retryTimeout}} seconds
respawn
respawn limit ${{retries}} ${{retryTimeout}}

pre-start script
[ -d /var/run/${{app_name}} ] || install -m 755 -o ${{daemon_user}} -g ${{daemon_user}} -d /var/run/${{app_name}}
[ -d /var/run/${{app_name}} ] || install -m 755 -o ${{daemon_user}} -g ${{daemon_user}} -d /var/run/${{app_name}}
end script

# set the working directory of the job processes
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading