| 1 | #!/bin/sh |
| 2 | # -*- tab-width:4;indent-tabs-mode:nil -*- |
| 3 | # ex: ts=4 sw=4 et |
| 4 | |
| 5 | # /bin/sh on Solaris is not a POSIX compatible shell, but /usr/bin/ksh is. |
| 6 | if [ `uname -s` = 'SunOS' -a "${POSIX_SHELL}" != "true" ]; then |
| 7 | POSIX_SHELL="true" |
| 8 | export POSIX_SHELL |
| 9 | exec /usr/bin/ksh $0 "$@" |
| 10 | fi |
| 11 | |
| 12 | # clear it so if we invoke other scripts, they run as ksh |
| 13 | unset POSIX_SHELL |
| 14 | |
| 15 | RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd -P) |
| 16 | |
| 17 | CALLER_DIR=$PWD |
| 18 | |
| 19 | RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*} |
| 20 | RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc |
| 21 | # Note the trailing slash on $PIPE_DIR/ |
| 22 | PIPE_DIR=/tmp/$RUNNER_BASE_DIR/ |
| 23 | RUNNER_USER= |
| 24 | WHOAMI=$(id -un) |
| 25 | |
| 26 | # Make sure this script is running as the appropriate user |
| 27 | if ([ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]); then |
| 28 | type sudo > /dev/null 2>&1 |
| 29 | if [ $? -ne 0 ]; then |
| 30 | echo "sudo doesn't appear to be installed and your EUID isn't $RUNNER_USER" 1>&2 |
| 31 | exit 1 |
| 32 | fi |
| 33 | echo "Attempting to restart script through sudo -H -u $RUNNER_USER" >&2 |
| 34 | exec sudo -H -u $RUNNER_USER -i $RUNNER_SCRIPT_DIR/$RUNNER_SCRIPT $@ |
| 35 | fi |
| 36 | |
| 37 | # Identify the script name |
| 38 | SCRIPT=`basename $0` |
| 39 | |
| 40 | # Parse out release and erts info |
| 41 | START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data` |
| 42 | ERTS_VSN=${START_ERL% *} |
| 43 | APP_VSN=${START_ERL#* } |
| 44 | |
| 45 | # Use $CWD/vm.args if exists, otherwise releases/APP_VSN/vm.args, or |
| 46 | # else etc/vm.args |
| 47 | if [ -e "$CALLER_DIR/vm.args" ]; then |
| 48 | VMARGS_PATH=$CALLER_DIR/vm.args |
| 49 | USE_DIR=$CALLER_DIR |
| 50 | else |
| 51 | USE_DIR=$RUNNER_BASE_DIR |
| 52 | if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args" ]; then |
| 53 | VMARGS_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args" |
| 54 | else |
| 55 | VMARGS_PATH="$RUNNER_ETC_DIR/vm.args" |
| 56 | fi |
| 57 | fi |
| 58 | |
| 59 | RUNNER_LOG_DIR=$USE_DIR/log |
| 60 | # Make sure log directory exists |
| 61 | mkdir -p $RUNNER_LOG_DIR |
| 62 | |
| 63 | # Use releases/VSN/sys.config if it exists otherwise use etc/app.config |
| 64 | if [ -e "$USE_DIR/sys.config" ]; then |
| 65 | CONFIG_PATH="$USE_DIR/sys.config" |
| 66 | else |
| 67 | if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config" ]; then |
| 68 | CONFIG_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config" |
| 69 | else |
| 70 | CONFIG_PATH="$RUNNER_ETC_DIR/app.config" |
| 71 | fi |
| 72 | fi |
| 73 | |
| 74 | # Extract the target node name from node.args |
| 75 | NAME_ARG=`egrep '^\-s?name' $VMARGS_PATH` |
| 76 | if [ -z "$NAME_ARG" ]; then |
| 77 | echo "vm.args needs to have either -name or -sname parameter." |
| 78 | exit 1 |
| 79 | fi |
| 80 | |
| 81 | # Extract the name type and name from the NAME_ARG for REMSH |
| 82 | REMSH_TYPE=`echo $NAME_ARG | awk '{print $1}'` |
| 83 | REMSH_NAME=`echo $NAME_ARG | awk '{print $2}'` |
| 84 | |
| 85 | # Note the `date +%s`, used to allow multiple remsh to the same node |
| 86 | # transparently |
| 87 | REMSH_NAME_ARG="$REMSH_TYPE remsh`date +%s`@`echo $REMSH_NAME | awk -F@ '{print $2}'`" |
| 88 | REMSH_REMSH_ARG="-remsh $REMSH_NAME" |
| 89 | |
| 90 | # Extract the target cookie |
| 91 | COOKIE_ARG=`grep '^\-setcookie' $VMARGS_PATH` |
| 92 | if [ -z "$COOKIE_ARG" ]; then |
| 93 | echo "vm.args needs to have a -setcookie parameter." |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | # Make sure CWD is set to the right dir |
| 98 | cd $USE_DIR |
| 99 | |
| 100 | # Make sure log directory exists |
| 101 | mkdir -p $USE_DIR/log |
| 102 | |
| 103 | # Add ERTS bin dir to our path |
| 104 | ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin |
| 105 | |
| 106 | # Setup command to control the node |
| 107 | NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG" |
| 108 | |
| 109 | # Setup remote shell command to control node |
| 110 | REMSH="$ERTS_PATH/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG" |
| 111 | |
| 112 | # Common functions |
| 113 | |
| 114 | # Ping node without allowing nodetool to take stdin |
| 115 | ping_node() { |
| 116 | $NODETOOL ping < /dev/null |
| 117 | } |
| 118 | |
| 119 | # Set the PID global variable, return 1 on error |
| 120 | get_pid() { |
| 121 | PID=`$NODETOOL getpid < /dev/null` |
| 122 | ES=$? |
| 123 | if [ "$ES" -ne 0 ]; then |
| 124 | echo "Node is not running!" |
| 125 | return 1 |
| 126 | fi |
| 127 | |
| 128 | # don't allow empty or init pid's |
| 129 | if [ -z $PID ] || [ "$PID" -le 1 ]; then |
| 130 | return 1 |
| 131 | fi |
| 132 | |
| 133 | return 0 |
| 134 | } |
| 135 | |
| 136 | # Check the first argument for instructions |
| 137 | case "$1" in |
| 138 | start|start_boot) |
| 139 | # Make sure there is not already a node running |
| 140 | RES=`ping_node` |
| 141 | if [ "$RES" = "pong" ]; then |
| 142 | echo "Node is already running!" |
| 143 | exit 1 |
| 144 | fi |
| 145 | case "$1" in |
| 146 | start) |
| 147 | shift |
| 148 | START_OPTION="console" |
| 149 | HEART_OPTION="start" |
| 150 | ;; |
| 151 | start_boot) |
| 152 | shift |
| 153 | START_OPTION="console_boot" |
| 154 | HEART_OPTION="start_boot" |
| 155 | ;; |
| 156 | esac |
| 157 | RUN_PARAM=$(printf "\'%s\' " "$@") |
| 158 | HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT $HEART_OPTION $RUN_PARAM" |
| 159 | export HEART_COMMAND |
| 160 | mkdir -p $PIPE_DIR |
| 161 | $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT $START_OPTION $RUN_PARAM" 2>&1 |
| 162 | ;; |
| 163 | |
| 164 | stop) |
| 165 | # Wait for the node to completely stop... |
| 166 | case `uname -s` in |
| 167 | Darwin) |
| 168 | # Make sure we explicitly set this because iTerm.app doesn't for |
| 169 | # some reason. |
| 170 | COMMAND_MODE=unix2003 |
| 171 | esac |
| 172 | |
| 173 | # Get the PID from nodetool |
| 174 | get_pid |
| 175 | GPR=$? |
| 176 | if [ "$GPR" -ne 0 ] || [ -z $PID ]; then |
| 177 | exit $GPR |
| 178 | fi |
| 179 | |
| 180 | # Tell nodetool to initiate a stop |
| 181 | $NODETOOL stop |
| 182 | ES=$? |
| 183 | if [ "$ES" -ne 0 ]; then |
| 184 | exit $ES |
| 185 | fi |
| 186 | |
| 187 | # Wait for the node to completely stop... |
| 188 | while `kill -s 0 $PID 2>/dev/null` |
| 189 | do |
| 190 | sleep 1 |
| 191 | done |
| 192 | ;; |
| 193 | |
| 194 | restart) |
| 195 | ## Restart the VM without exiting the process |
| 196 | $NODETOOL restart |
| 197 | ES=$? |
| 198 | if [ "$ES" -ne 0 ]; then |
| 199 | exit $ES |
| 200 | fi |
| 201 | ;; |
| 202 | |
| 203 | reboot) |
| 204 | ## Restart the VM completely (uses heart to restart it) |
| 205 | $NODETOOL reboot |
| 206 | ES=$? |
| 207 | if [ "$ES" -ne 0 ]; then |
| 208 | exit $ES |
| 209 | fi |
| 210 | ;; |
| 211 | |
| 212 | ping) |
| 213 | ## See if the VM is alive |
| 214 | ping_node |
| 215 | ES=$? |
| 216 | if [ "$ES" -ne 0 ]; then |
| 217 | exit $ES |
| 218 | fi |
| 219 | ;; |
| 220 | |
| 221 | attach) |
| 222 | # Make sure a node is running |
| 223 | ping_node |
| 224 | ES=$? |
| 225 | if [ "$ES" -ne 0 ]; then |
| 226 | echo "Node is not running!" |
| 227 | exit $ES |
| 228 | fi |
| 229 | |
| 230 | shift |
| 231 | exec $ERTS_PATH/to_erl $PIPE_DIR |
| 232 | ;; |
| 233 | |
| 234 | eval) |
| 235 | # Make sure a node IS running |
| 236 | ping_node > /dev/null 2>&1 |
| 237 | ES=$? |
| 238 | if [ "$ES" -ne 0 ]; then |
| 239 | echo "Node is not running!" |
| 240 | exit $ES |
| 241 | fi |
| 242 | |
| 243 | shift |
| 244 | $NODETOOL eval "$1" |
| 245 | ES_EVAL=$? |
| 246 | if [ "$ES_EVAL" -ne 0 ]; then |
| 247 | exit $ES_EVAL |
| 248 | fi |
| 249 | ;; |
| 250 | |
| 251 | remote_console) |
| 252 | # Make sure a node is running |
| 253 | ping_node |
| 254 | ES=$? |
| 255 | if [ "$ES" -ne 0 ]; then |
| 256 | echo "Node is not running!" |
| 257 | exit $ES |
| 258 | fi |
| 259 | |
| 260 | shift |
| 261 | exec $REMSH |
| 262 | ;; |
| 263 | |
| 264 | upgrade) |
| 265 | if [ -z "$2" ]; then |
| 266 | echo "Missing upgrade package argument" |
| 267 | echo "Usage: $SCRIPT upgrade {package base name}" |
| 268 | echo "NOTE {package base name} MUST NOT include the .tar.gz suffix" |
| 269 | exit 1 |
| 270 | fi |
| 271 | |
| 272 | # Make sure a node IS running |
| 273 | ping_node |
| 274 | ES=$? |
| 275 | if [ "$ES" -ne 0 ]; then |
| 276 | echo "Node is not running!" |
| 277 | exit $ES |
| 278 | fi |
| 279 | |
| 280 | $NODETOOL upgrade $2 |
| 281 | ;; |
| 282 | |
| 283 | console|console_clean|console_boot) |
| 284 | # .boot file typically just $SCRIPT (ie, the app name) |
| 285 | # however, for debugging, sometimes start_clean.boot is useful. |
| 286 | # For e.g. 'setup', one may even want to name another boot script. |
| 287 | case "$1" in |
| 288 | console) BOOTFILE=$SCRIPT ;; |
| 289 | console_clean) BOOTFILE=start_clean ;; |
| 290 | console_boot) |
| 291 | shift |
| 292 | BOOTFILE="$1" |
| 293 | shift |
| 294 | ;; |
| 295 | esac |
| 296 | # Setup beam-required vars |
| 297 | ROOTDIR=$RUNNER_BASE_DIR |
| 298 | BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin |
| 299 | EMU=beam |
| 300 | PROGNAME=`echo $0 | sed 's/.*\\///'` |
| 301 | CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH" |
| 302 | export EMU |
| 303 | export ROOTDIR |
| 304 | export BINDIR |
| 305 | export PROGNAME |
| 306 | |
| 307 | # Dump environment info for logging purposes |
| 308 | echo "Exec: $CMD" -- ${1+"$@"} |
| 309 | echo "Root: $ROOTDIR" |
| 310 | |
| 311 | # Log the startup |
| 312 | logger -t "$SCRIPT[$$]" "Starting up" |
| 313 | |
| 314 | # Start the VM |
| 315 | exec $CMD -- ${1+"$@"} |
| 316 | ;; |
| 317 | |
| 318 | foreground) |
| 319 | # start up the release in the foreground for use by runit |
| 320 | # or other supervision services |
| 321 | |
| 322 | BOOTFILE=$SCRIPT |
| 323 | FOREGROUNDOPTIONS="-noinput +Bd" |
| 324 | |
| 325 | # Setup beam-required vars |
| 326 | ROOTDIR=$RUNNER_BASE_DIR |
| 327 | BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin |
| 328 | EMU=beam |
| 329 | PROGNAME=`echo $0 | sed 's/.*\///'` |
| 330 | CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH" |
| 331 | export EMU |
| 332 | export ROOTDIR |
| 333 | export BINDIR |
| 334 | export PROGNAME |
| 335 | |
| 336 | # Dump environment info for logging purposes |
| 337 | echo "Exec: $CMD" -- ${1+"$@"} |
| 338 | echo "Root: $ROOTDIR" |
| 339 | |
| 340 | # Start the VM |
| 341 | exec $CMD -- ${1+"$@"} |
| 342 | ;; |
| 343 | getpid) |
| 344 | # Get the PID from nodetool |
| 345 | get_pid |
| 346 | ES=$? |
| 347 | if [ "$ES" -ne 0 ] || [ -z $PID ]; then |
| 348 | exit $ES |
| 349 | fi |
| 350 | echo $PID |
| 351 | ;; |
| 352 | *) |
| 353 | echo "Usage: $SCRIPT {start|start_boot <file>|foreground|stop|restart|reboot|ping|console|getpid|console_clean|console_boot <file>|attach|eval|remote_console|upgrade}" |
| 354 | exit 1 |
| 355 | ;; |
| 356 | esac |
| 357 | |
| 358 | exit 0 |