| #!/bin/bash |
| # |
| # Licensed to the Apache Software Foundation (ASF) under one or more |
| # contributor license agreements. See the NOTICE file distributed with |
| # this work for additional information regarding copyright ownership. |
| # The ASF licenses this file to You under the Apache License, Version 2.0 |
| # (the "License"); you may not use this file except in compliance with |
| # the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| # This is a poor man's templating engine for generating init.d scripts to |
| # support all the Apache services that Bigtop distro has. An actual init.d |
| # script gets generate via running this script under bash and giving it |
| # a mandatory argument of a file containing the configuration for the service. |
| # The argument file should be a valid piece of bash code since it gets directly |
| # source into this template. E.g. |
| # $ bash ./init.d.tmpl hadoop-hdfs-namenode.svc rpm hadoop-hdfs-namenode |
| # |
| # You must declare the following in your .svc configuration file: |
| # DAEMON="name of the resulting init.d script" |
| # DESC="Free form human readable description of the service" |
| # EXEC_PATH="path to the upstream daemon management script" |
| # SVC_USER="user to run this service as" |
| # DAEMON_FLAGS="flags to be passed to the $EXEC_PATH" |
| # WORKING_DIR="working directory to cd into before starting the daemon" |
| # CONF_DIR="path to the configuration directory" |
| # PIDFILE="file holding a PID of the running daemon" |
| # LOCKFILE="file signifying the service lock" |
| # |
| # CHKCONFIG="chkconfig(8) registration signature" |
| # INIT_DEFAULT_START="run levels to use" |
| # INIT_DEFAULT_STOP="run levels not to use ;-)" |
| # |
| # You can, also, override parts of the generated init.d script by providing |
| # function definitions for: generate_start, generate_stop, generate_functions, and generate_extra_commands. |
| # See the default implemenations below and feel free to customize. Also look |
| # for exising .svc files in common to see how different services are tweaking |
| # the defaults. |
| # |
| # Of course, if this whole templating thing grows too big we might need to |
| # consider a real templating engine (I have played with m4, but it seems |
| # qutie brittle when used for manipulating pieces of the shell code -- think |
| # $0 the like). |
| |
| if [ $# -lt 3 ] ; then |
| echo "Usage: ${BASH_SOURCE-0} service.definition.svc rpm|deb init.d.script" |
| exit 1 |
| fi |
| |
| # Override anything necessary for supported systems |
| case "$2" in |
| "deb" ) |
| # The defaults will work well on Debian-based systems |
| ;; |
| "rpm" ) |
| # On RedHat, SuSE and Mageia run-level 2 is networkless, hence excluding it |
| CHKCONFIG="345 85 15" |
| INIT_DEFAULT_START="3 4 5" |
| INIT_DEFAULT_STOP="0 1 2 6" |
| ;; |
| "*" ) |
| ;; |
| esac |
| |
| # Create the init script, make it executable, and send stdout to it |
| mkdir -p `dirname $3` |
| touch $3 |
| chmod 0755 $3 |
| exec > $3 |
| |
| generate_start() { |
| |
| cat <<'__EOT__' |
| |
| start() { |
| [ -x $EXEC_PATH ] || exit $ERROR_PROGRAM_NOT_INSTALLED |
| [ -d $CONF_DIR ] || exit $ERROR_PROGRAM_NOT_CONFIGURED |
| |
| su -s /bin/bash $SVC_USER -c "cd $WORKING_DIR && $EXEC_PATH --config '$CONF_DIR' start $DAEMON_FLAGS" |
| |
| # Some processes are slow to start |
| sleep $SLEEP_TIME |
| checkstatusofproc |
| RETVAL=$? |
| |
| if [ $RETVAL -eq $STATUS_RUNNING ]; then |
| touch $LOCKFILE |
| log_success_msg "Started ${DESC}: " |
| else |
| log_failure_msg "Failed to start ${DESC}. Return value: $RETVAL" |
| fi |
| return $RETVAL |
| } |
| |
| __EOT__ |
| |
| } |
| |
| generate_stop() { |
| |
| cat <<'__EOT__' |
| |
| stop() { |
| start_daemon $EXEC_PATH --config "$CONF_DIR" stop $DAEMON_FLAGS |
| RETVAL=$? |
| |
| if [ $RETVAL -eq $RETVAL_SUCCESS ]; then |
| log_success_msg "Stopped ${DESC}: " |
| rm -f $LOCKFILE $PIDFILE |
| else |
| log_failure_msg "Failed to stop ${DESC}. Return value: $RETVAL." |
| fi |
| } |
| |
| __EOT__ |
| |
| } |
| |
| generate_functions() { |
| : |
| } |
| |
| generate_extra_commands() { |
| |
| cat <<'__EOT__' |
| *) |
| echo $"Usage: $0 {start|stop|status|restart|try-restart|condrestart}" |
| exit 1 |
| __EOT__ |
| |
| } |
| |
| ################################################################### |
| # Some reasonable defaults for the run-level settings, these are |
| # tweaked on a per-OS basis from the SPEC/rules files that call us |
| # and can be further tweaked inside of individual .svc templates |
| CHKCONFIG=${CHKCONFIG:-"2345 85 15"} |
| INIT_DEFAULT_START=${INIT_DEFAULT_START:-"2 3 4 5"} |
| INIT_DEFAULT_STOP=${INIT_DEFAULT_STOP:-"0 1 6"} |
| |
| ################################################################### |
| # NOTE how we are sourcing the argument here so that a user-defined |
| # settings have a chance to override the default values for |
| # generate_start, generate_stop, generate_functions and generate_extra_commands. If you |
| # ever want to make this template even more flexible -- define the |
| # default values above this line |
| . $1 |
| |
| cat <<__EOT__ |
| #!/bin/bash |
| # |
| # Licensed to the Apache Software Foundation (ASF) under one or more |
| # contributor license agreements. See the NOTICE file distributed with |
| # this work for additional information regarding copyright ownership. |
| # The ASF licenses this file to You under the Apache License, Version 2.0 |
| # (the "License"); you may not use this file except in compliance with |
| # the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| # |
| # Starts a $DESC |
| # |
| # chkconfig: $CHKCONFIG |
| # description: $DESC |
| # |
| ### BEGIN INIT INFO |
| # Provides: $DAEMON |
| # Short-Description: $DESC |
| # Default-Start: $INIT_DEFAULT_START |
| # Default-Stop: $INIT_DEFAULT_STOP |
| # Required-Start: \$syslog \$remote_fs |
| # Required-Stop: \$syslog \$remote_fs |
| # Should-Start: |
| # Should-Stop: |
| ### END INIT INFO |
| |
| . /lib/lsb/init-functions |
| BIGTOP_DEFAULTS_DIR=\${BIGTOP_DEFAULTS_DIR-/etc/default} |
| [ -n "\${BIGTOP_DEFAULTS_DIR}" -a -r \${BIGTOP_DEFAULTS_DIR}/hadoop ] && . \${BIGTOP_DEFAULTS_DIR}/hadoop |
| [ -n "\${BIGTOP_DEFAULTS_DIR}" -a -r \${BIGTOP_DEFAULTS_DIR}/$DAEMON ] && . \${BIGTOP_DEFAULTS_DIR}/$DAEMON |
| |
| # Autodetect JAVA_HOME if not defined |
| . /usr/lib/bigtop-utils/bigtop-detect-javahome |
| |
| RETVAL_SUCCESS=0 |
| |
| STATUS_RUNNING=0 |
| STATUS_DEAD=1 |
| STATUS_DEAD_AND_LOCK=2 |
| STATUS_NOT_RUNNING=3 |
| STATUS_OTHER_ERROR=102 |
| |
| |
| ERROR_PROGRAM_NOT_INSTALLED=5 |
| ERROR_PROGRAM_NOT_CONFIGURED=6 |
| |
| |
| RETVAL=0 |
| SLEEP_TIME=5 |
| PROC_NAME="java" |
| |
| DAEMON="$DAEMON" |
| DESC="$DESC" |
| EXEC_PATH="$EXEC_PATH" |
| SVC_USER="$SVC_USER" |
| DAEMON_FLAGS="$DAEMON_FLAGS" |
| CONF_DIR="$CONF_DIR" |
| PIDFILE="$PIDFILE" |
| LOCKDIR="/var/lock/subsys" |
| LOCKFILE="\$LOCKDIR/$DAEMON" |
| WORKING_DIR="${WORKING_DIR:-~/}" |
| |
| install -d -m 0755 -o $SVC_USER -g $SVC_USER $(dirname $PIDFILE) 1>/dev/null 2>&1 || : |
| [ -d "\$LOCKDIR" ] || install -d -m 0755 \$LOCKDIR 1>/dev/null 2>&1 || : |
| __EOT__ |
| |
| generate_functions |
| generate_start |
| generate_stop |
| |
| cat <<'__EOT__' |
| restart() { |
| stop |
| start |
| } |
| |
| checkstatusofproc(){ |
| pidofproc -p $PIDFILE $PROC_NAME > /dev/null |
| } |
| |
| checkstatus(){ |
| checkstatusofproc |
| status=$? |
| |
| case "$status" in |
| $STATUS_RUNNING) |
| log_success_msg "${DESC} is running" |
| ;; |
| $STATUS_DEAD) |
| log_failure_msg "${DESC} is dead and pid file exists" |
| ;; |
| $STATUS_DEAD_AND_LOCK) |
| log_failure_msg "${DESC} is dead and lock file exists" |
| ;; |
| $STATUS_NOT_RUNNING) |
| log_failure_msg "${DESC} is not running" |
| ;; |
| *) |
| log_failure_msg "${DESC} status is unknown" |
| ;; |
| esac |
| return $status |
| } |
| |
| condrestart(){ |
| [ -e $LOCKFILE ] && restart || : |
| } |
| |
| check_for_root() { |
| if [ $(id -ur) -ne 0 ]; then |
| echo 'Error: root user required' |
| echo |
| exit 1 |
| fi |
| } |
| |
| service() { |
| case "$1" in |
| start) |
| check_for_root |
| start |
| ;; |
| stop) |
| check_for_root |
| stop |
| ;; |
| status) |
| checkstatus |
| RETVAL=$? |
| ;; |
| restart) |
| check_for_root |
| restart |
| ;; |
| condrestart|try-restart) |
| check_for_root |
| condrestart |
| ;; |
| __EOT__ |
| |
| generate_extra_commands |
| |
| cat <<'__EOT__' |
| esac |
| } |
| |
| service "$1" |
| |
| exit $RETVAL |
| __EOT__ |