From 9eb26b78a711cd2fa9c0c5e0211c7bda32b8fd40 2020-06-29 04:16:48 From: Branko Majic Date: 2020-06-29 04:16:48 Subject: [PATCH] Added the Emacs templates for init scripts for Debian/Gentoo init scripts and a template for bash scripts. --- diff --git a/templates/TEMPLATE.sh.tpl b/templates/TEMPLATE.sh.tpl new file mode 100644 index 0000000000000000000000000000000000000000..aee7889f35a54459c3147027b98ff99b0174d0bd --- /dev/null +++ b/templates/TEMPLATE.sh.tpl @@ -0,0 +1,85 @@ +#!/bin/bash +# +# (>>>FILE<<<) +# +# Copyright (C) (>>>YEAR<<<), Branko Majic +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +program="(>>>FILE<<<)" +version="(>>>VERSION<<<)" + +function usage() { + cat <>>SHORT_DESC<<<) + +Usage: $program [OPTIONS] (>>>ARGUMENTS<<<) + +$program (>>>LONG_DESC<<<) + +$program accepts the following options: + + -v show script version and licensing information + -h show usage help + + +Please report bugs and send feature requests to . +EOF +} + +function version() { + cat <>>YEAR<<<), Branko Majic | +| | +| This program is free software: you can redistribute it and/or modify | +| it under the terms of the GNU General Public License as published by | +| the Free Software Foundation, either version 3 of the License, or | +| (at your option) any later version. | +| | +| This program is distributed in the hope that it will be useful, | +| but WITHOUT ANY WARRANTY; without even the implied warranty of | +| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | +| GNU General Public License for more details. | +| | +| You should have received a copy of the GNU General Public License | +| along with this program. If not, see . | ++-----------------------------------------------------------------------+ + +EOF +} + +# If no arguments were given, just show usage help. +if [[ -z $1 ]]; then + usage + exit 0 +fi + +# Parse the arguments +while getopts "vh" opt; do + case "$opt" in + v) version + exit 0;; + h) usage + exit 0;; + *) usage + exit 1;; + esac +done +i=$OPTIND +shift $(($i-1)) + diff --git a/templates/init.d_debian.tpl b/templates/init.d_debian.tpl new file mode 100644 index 0000000000000000000000000000000000000000..6354cec8bdb0b61726d01ab89b6871e82197e0c1 --- /dev/null +++ b/templates/init.d_debian.tpl @@ -0,0 +1,231 @@ +#!/bin/bash + +### BEGIN INIT INFO +# Provides: (>>>PROVIDES<<<) +# Required-Start: (>>>REQ_START<<<) +# Required-Stop: (>>>REQ_STOP<<<) +# Default-Start: (>>>DEFAULT_START<<<) +# Default-Stop: (>>>DEFAULT_STOP<<<) +# Short-Description: (>>>SHORT_DESCRIPTION<<<) +# Description: (>>>DESCRIPTION<<<) +### END INIT INFO + + +# Include the LSB library functions +. /lib/lsb/init-functions + + +# Setup static variables +configFile='(>>>CONFIG_FILE<<<)' +daemonExec='(>>>DAEMON_EXEC<<<)' +daemonArgs='(>>>DAEMON_ARGS<<<)' +daemonName="$(basename "$daemonExec")" +pidFile='/var/run/(>>>FILE<<<).pid' + + +# +# Checks if the environment is capable of running the script (such as +# availability of programs etc). +# +# Return: 0 if the environmnt is properly setup for execution of init script, 1 +# if not all conditions have been met. +# +function checkEnvironment() { + # Verify that the necessary binaries are available for execution. + local binaries=((>>>REQ_BINARIES<<<)) + + for bin in "${binaries[@]}"; do + if ! which "$bin" > /dev/null; then + log_failure_msg "Binary '$bin' is not available. Please install \ +package containing it." + exit 5 + fi + done + + # Verify that the necessary environment variables have been set. + local envVars=((>>>REQ_ENV_VARIABLES<<<)) + + for var in "${envVars[@]}"; do + if printenv "$var" > /dev/null; then + log_failure_msg "Environment variable '$var' is not set. Please \ +check your global environment settings." + exit 6 + fi + done +} + + +# +# Checks if the configuration files are available and properly setup. +# +# Return: 0 if irssid if properly configured, 1 otherwise. +# +function checkConfig() { + # Make sure the configuration file has been created + if ! [[ -f $configFile ]]; then + log_failure_msg "Please populate the configuration file '$configFile' \ +before running." + exit 6 + fi + + # Make sure the required options have been set + local reqOptions=((>>>REQ_OPTIONS<<<)) + for option in "${reqOptions[@]}"; do + if ! grep -q -e "^[[:blank:]]*$option=" "$configFile"; then + log_failure_msg "Mandatory option '$option' was not specified in \ +'$configFile'" + exit 6 + fi + done +} + + +# +# Loads the configuration file and performs any additional configuration steps. +# +function configure() { + . "$configFile" + daemonCommand="$daemonExec $daemonArgs" +} + + +# +# Checks if the daemon is already running or not. If the daemon is running, its +# process id is stored within variable 'pid'. +# +# Return: 0 if the daemon is running, 1 otherwise. +# +function checkRunning() { + for pid in $(pidofproc "$daemonExec"); do + ps $pid | grep -qe "$daemonCommand" && return 0 + done + return 1 +} + + +# +# Starts the daemon. +# +# Return: LSB-compliant code. +# +function start() { + start-stop-daemon --start --quiet --oknodo --pidfile "$pidFile" \ + --make-pidfile --exec "$daemonExec" -- $daemonArgs +} + + +# +# Stops the daemon. +# +# Return: LSB-compliant code. +# +function stop() { + start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile "$pidFile" \ + --exec "$daemonExec" -- $daemonArgs +} + + +# +# Checks the status of the running daemon. +# +# Return: LSB-compliant status code. +# +function status() { + status_of_proc -p "$pidFile" "$daemonExec" "$(basename $daemonExec)" +} + + +# +# Starts the daemon using custom code. +# +# Return: LSB-compliant code. +# +function startCustom() { + if checkRunning; then + return 0 + elif start-stop-daemon --start --quiet --pidfile $pidFile --make-pidfile \ + --exec "$daemonExec" -- $daemonArgs; then + return 0 + else + return 1 + fi +} + + +# +# Stops the daemon using custom code. +# +# Return: LSB-compliant code. +# +function stopCustom() { + if checkRunning; then + # Try to stop the daemon using SIGTERM first + count=0 + while checkRunning && (( $count < 5 )); do + kill $pid + let count=count+1 + sleep 1 + done + + # If SIGTERM fails, use SIGKILL + if checkRunning; then + kill -9 $pid + rm '/var/run/(>>>FILE<<<).pid' + return 0 + else + rm '/var/run/(>>>FILE<<<).pid' + return 0 + fi + else + return 0 + fi +} + + +# +# Checks the status of the running daemon. +# +# Return: LSB-compliant status code. +# +function statusCustom() { + if checkRunning; then + log_success_msg "irssid ($pid) is running" + return 0 + else + log_success_msg "irssid is not running" + return 3 + fi +} + + +checkEnvironment +checkConfig +configure + +case "$1" in + start) + log_daemon_msg "Starting daemon" "(>>>FILE<<<)" + start && log_end_msg 0 || log_end_msg $? + ;; + stop) + log_daemon_msg "Stopping daemon" "(>>>FILE<<<)" + stop && log_end_msg 0 || log_end_msg $? + ;; + restart) + log_daemon_msg "Restarting daemon" "(>>>FILE<<<)" + stop + start && log_end_msg 0 || log_end_msg $? + ;; + force-reload) + log_daemon_msg "Restarting daemon" "(>>>FILE<<<)" + stop + start && log_end_msg 0 || log_end_msg $? + ;; + status) + status && exit 0 || exit $? + ;; + *) + echo "(>>>FILE<<<) (start|stop|restart|force-reload|status|help)" + ;; +esac + diff --git a/templates/init.d_gentoo.tpl b/templates/init.d_gentoo.tpl new file mode 100644 index 0000000000000000000000000000000000000000..1bb43d8068dd638d9ae566d6f66ec194f9992fcc --- /dev/null +++ b/templates/init.d_gentoo.tpl @@ -0,0 +1,102 @@ +#!/sbin/runscript + + +# Setup static variables +configFile='(>>>CONFIG_FILE<<<)' +daemonExec='(>>>DAEMON_EXEC<<<)' +daemonArgs='(>>>DAEMON_ARGS<<<)' +daemonName="$(basename "$daemonExec")" +pidFile='/var/run/(>>>FILE<<<).pid' + + +# +# Checks if the environment is capable of running the script (such as +# availability of programs etc). +# +# Return: 0 if the environmnt is properly setup for execution of init script, 1 +# if not all conditions have been met. +# +function checkEnvironment() { + # Verify that the necessary binaries are available for execution. + local binaries=((>>>REQ_BINARIES<<<)) + + for bin in "${binaries[@]}"; do + if ! which "$bin" > /dev/null; then + eerror "Binary '$bin' is not available. Please install \ +package containing it." + exit 5 + fi + done + + # Verify that the necessary environment variables have been set. + local envVars=((>>>REQ_ENV_VARIABLES<<<)) + + for var in "${envVars[@]}"; do + if printenv "$var" > /dev/null; then + eerror "Environment variable '$var' is not set. Please \ +check your global environment settings." + exit 6 + fi + done +} + + +# +# Checks if the configuration files are available and properly setup. +# +# Return: 0 if irssid if properly configured, 1 otherwise. +# +function checkConfig() { + # Make sure the configuration file has been created + if ! [[ -f $configFile ]]; then + eerror "Please populate the configuration file '$configFile' \ +before running." + exit 6 + fi + + # Make sure the required options have been set + local reqOptions=((>>>REQ_OPTIONS<<<)) + for option in "${reqOptions[@]}"; do + if ! grep -q -e "^[[:blank:]]*$option=" "$configFile"; then + eerror "Mandatory option '$option' was not specified in \ +'$configFile'" + exit 6 + fi + done +} + + +# +# Loads the configuration file and performs any additional configuration steps. +# +function configure() { + . "$configFile" + daemonCommand="$daemonExec $daemonArgs" +} + + +############################# +# Gentoo-specific functions # +############################# + +opts="${opts} (>>>GENTOO_OPTS<<<)" + +depend() { + need (>>>GENTOO_NEED<<<) + after (>>>GENTOO_AFTER<<<) +} + +start () { + ebegin "Starting irssid" + start-stop-daemon --start --quiet --oknodo --pidfile "$pidFile" \ + --make-pidfile --exec "$daemonExec" -- $daemonArgs + eend $? +} + +stop () { + ebegin "Stopping irssid" + start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile "$pidFile" \ + --exec "$daemonExec" -- $daemonArgs + eend $? +} +