emqttd 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/bin/sh
  2. # -*- tab-width:4;indent-tabs-mode:nil -*-
  3. # ex: ts=4 sw=4 et
  4. # /bin/sh on Solaris is not a POSIX compatible shell, but /usr/bin/ksh is.
  5. if [ `uname -s` = 'SunOS' -a "${POSIX_SHELL}" != "true" ]; then
  6. POSIX_SHELL="true"
  7. export POSIX_SHELL
  8. # To support 'whoami' add /usr/ucb to path
  9. PATH=/usr/ucb:$PATH
  10. export PATH
  11. exec /usr/bin/ksh $0 "$@"
  12. fi
  13. unset POSIX_SHELL # clear it so if we invoke other scripts, they run as ksh as well
  14. RUNNER_SCRIPT_DIR={{runner_script_dir}}
  15. RUNNER_SCRIPT=${0##*/}
  16. RUNNER_BASE_DIR={{runner_base_dir}}
  17. RUNNER_ETC_DIR={{runner_etc_dir}}
  18. RUNNER_LIB_DIR={{platform_lib_dir}}
  19. RUNNER_LOG_DIR={{runner_log_dir}}
  20. RUNNER_DATA_DIR=$RUNNER_BASE_DIR/data
  21. RUNNER_PLUGINS_DIR=$RUNNER_BASE_DIR/plugins
  22. # Note the trailing slash on $PIPE_DIR/
  23. PIPE_DIR={{pipe_dir}}
  24. RUNNER_USER={{runner_user}}
  25. PLATFORM_DATA_DIR={{platform_data_dir}}
  26. SSL_DIST_CONFIG=$PLATFORM_DATA_DIR/ssl_distribution.args_file
  27. RIAK_VERSION="git"
  28. WHOAMI=$(whoami)
  29. # Make sure this script is running as the appropriate user
  30. if ([ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]); then
  31. type sudo > /dev/null 2>&1
  32. if [ $? -ne 0 ]; then
  33. echo "sudo doesn't appear to be installed and your EUID isn't $RUNNER_USER" 1>&2
  34. exit 1
  35. fi
  36. echo "Attempting to restart script through sudo -H -u $RUNNER_USER" >&2
  37. exec sudo -H -u $RUNNER_USER -i $RUNNER_SCRIPT_DIR/$RUNNER_SCRIPT $@
  38. fi
  39. # Warn the user if ulimit -n is less than 1024
  40. ULIMIT_F=`ulimit -n`
  41. if [ "$ULIMIT_F" -lt 1024 ]; then
  42. echo "!!!!"
  43. echo "!!!! WARNING: ulimit -n is ${ULIMIT_F}; 1024 is the recommended minimum."
  44. echo "!!!!"
  45. fi
  46. # Make sure CWD is set to runner base dir
  47. cd $RUNNER_BASE_DIR
  48. # Make sure log directory exists
  49. mkdir -p $RUNNER_LOG_DIR
  50. # Make sure the data directory exists
  51. mkdir -p $PLATFORM_DATA_DIR
  52. # Warn the user if they don't have write permissions on the log dir
  53. if [ ! -w $RUNNER_LOG_DIR ]; then
  54. echo "!!!!"
  55. echo "!!!! WARNING: $RUNNER_LOG_DIR not writable; logs and crash dumps unavailable."
  56. echo "!!!!"
  57. fi
  58. # Extract the target node name from node.args
  59. NAME_ARG=`egrep '^\-s?name' $RUNNER_ETC_DIR/vm.args`
  60. if [ -z "$NAME_ARG" ]; then
  61. echo "vm.args needs to have either -name or -sname parameter."
  62. exit 1
  63. fi
  64. NODE_NAME=${NAME_ARG##* }
  65. # Extract the target cookie
  66. COOKIE_ARG=`grep '^\-setcookie' $RUNNER_ETC_DIR/vm.args`
  67. if [ -z "$COOKIE_ARG" ]; then
  68. echo "vm.args needs to have a -setcookie parameter."
  69. exit 1
  70. fi
  71. # Identify the script name
  72. SCRIPT=`basename $0`
  73. # Parse out release and erts info
  74. START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data`
  75. ERTS_VSN=${START_ERL% *}
  76. APP_VSN=${START_ERL#* }
  77. # Add ERTS bin dir to our path
  78. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  79. # Setup command to control the node
  80. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  81. NODETOOL_LITE="$ERTS_PATH/escript $ERTS_PATH/nodetool"
  82. # Common functions
  83. # Ping node without allowing nodetool to take stdin
  84. ping_node() {
  85. $NODETOOL ping < /dev/null
  86. }
  87. # Set the PID global variable, return 1 on error
  88. get_pid() {
  89. PID=`$NODETOOL getpid < /dev/null`
  90. ES=$?
  91. if [ "$ES" -ne 0 ]; then
  92. echo "Node is not running!"
  93. return 1
  94. fi
  95. # don't allow empty or init pid's
  96. if [ -z $PID ] || [ "$PID" -le 1 ]; then
  97. return 1
  98. fi
  99. return 0
  100. }
  101. # Scrape out SSL distribution config info from vm.args into $SSL_DIST_CONFIG
  102. rm -f $SSL_DIST_CONFIG
  103. sed -n '/Begin SSL distribution items/,/End SSL distribution items/p' \
  104. $RUNNER_ETC_DIR/vm.args > $SSL_DIST_CONFIG
  105. # Check the first argument for instructions
  106. case "$1" in
  107. start)
  108. # Make sure there is not already a node running
  109. RES=`ping_node`
  110. if [ "$RES" = "pong" ]; then
  111. echo "Node is already running!"
  112. exit 1
  113. fi
  114. # Sanity check the emqttd.config file
  115. RES=`$NODETOOL_LITE chkconfig $RUNNER_ETC_DIR/emqttd.config`
  116. if [ $? != 0 ]; then
  117. echo "Error reading $RUNNER_ETC_DIR/emqttd.config"
  118. echo $RES
  119. exit 1
  120. fi
  121. HEART_COMMAND="$RUNNER_SCRIPT_DIR/$SCRIPT start"
  122. export HEART_COMMAND
  123. mkdir -p $PIPE_DIR
  124. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR \
  125. "exec $RUNNER_SCRIPT_DIR/$SCRIPT console" 2>&1
  126. # Wait for the node to come up. We can't just ping it because
  127. # distributed erlang comes up for a second before emqttd crashes
  128. # (eg. in the case of an unwriteable disk). Once the node comes
  129. # up we check for the node watcher process. If that's running
  130. # then we assume things are good enough. This will at least let
  131. # the user know when emqttd is crashing right after startup.
  132. WAIT=${WAIT_FOR_ERLANG:-15}
  133. while [ $WAIT -gt 0 ]; do
  134. WAIT=`expr $WAIT - 1`
  135. sleep 1
  136. RES=`ping_node`
  137. if [ "$?" -ne 0 ]; then
  138. continue
  139. fi
  140. echo "emqttd is started successfully!"
  141. exit 0
  142. done
  143. echo "emqttd failed to start within ${WAIT_FOR_ERLANG:-15} seconds,"
  144. echo "see the output of 'emqttd console' for more information."
  145. echo "If you want to wait longer, set the environment variable"
  146. echo "WAIT_FOR_ERLANG to the number of seconds to wait."
  147. exit 1
  148. ;;
  149. stop)
  150. UNAME_S=`uname -s`
  151. case $UNAME_S in
  152. Darwin)
  153. # Make sure we explicitly set this because iTerm.app doesn't for
  154. # some reason.
  155. COMMAND_MODE=unix2003
  156. esac
  157. # Get the PID from nodetool
  158. get_pid
  159. GPR=$?
  160. if [ "$GPR" -ne 0 ] || [ -z $PID ]; then
  161. exit $GPR
  162. fi
  163. # Tell nodetool to initiate a stop
  164. $NODETOOL stop
  165. ES=$?
  166. if [ "$ES" -ne 0 ]; then
  167. exit $ES
  168. fi
  169. # Wait for the node to completely stop...
  170. while `kill -s 0 $PID 2>/dev/null`;
  171. do
  172. sleep 1
  173. done
  174. ;;
  175. restart)
  176. ## Restart the VM without exiting the process
  177. $NODETOOL restart
  178. ES=$?
  179. if [ "$ES" -ne 0 ]; then
  180. exit $ES
  181. fi
  182. ;;
  183. reboot)
  184. ## Restart the VM completely (uses heart to restart it)
  185. $NODETOOL reboot
  186. ES=$?
  187. if [ "$ES" -ne 0 ]; then
  188. exit $ES
  189. fi
  190. ;;
  191. ping)
  192. ## See if the VM is alive
  193. ping_node
  194. ES=$?
  195. if [ "$ES" -ne 0 ]; then
  196. exit $ES
  197. fi
  198. ;;
  199. attach)
  200. if [ "$2" = "-f" ]; then
  201. echo "Forcing connection..."
  202. else
  203. # Make sure a node is running
  204. RES=`ping_node`
  205. ES=$?
  206. if [ "$ES" -ne 0 ]; then
  207. echo "Node is not running!"
  208. exit $ES
  209. fi
  210. fi
  211. shift
  212. exec $ERTS_PATH/to_erl $PIPE_DIR
  213. ;;
  214. console)
  215. RES=`ping_node`
  216. if [ "$RES" = "pong" ]; then
  217. echo "Node is already running - use '$SCRIPT attach' instead"
  218. exit 1
  219. fi
  220. # Sanity check the emqttd.config file
  221. RES=`$NODETOOL_LITE chkconfig $RUNNER_ETC_DIR/emqttd.config`
  222. if [ $? != 0 ]; then
  223. echo "Error reading $RUNNER_ETC_DIR/emqttd.config"
  224. echo $RES
  225. exit 1
  226. fi
  227. # Setup beam-required vars
  228. ROOTDIR=$RUNNER_BASE_DIR
  229. ERL_LIBS=$ROOTDIR/plugins
  230. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  231. EMU=beam
  232. PROGNAME=`echo $0 | sed 's/.*\///'`
  233. # Setup Mnesia Dir
  234. MNESIA_DIR="$RUNNER_DATA_DIR/mnesia/$NODE_NAME"
  235. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$SCRIPT \
  236. -embedded -config $RUNNER_ETC_DIR/emqttd.config \
  237. -pa $RUNNER_LIB_DIR/basho-patches \
  238. -mnesia dir "\"${MNESIA_DIR}\"" \
  239. -args_file $RUNNER_ETC_DIR/vm.args -- ${1+"$@"}"
  240. export EMU
  241. export ROOTDIR
  242. export ERL_LIBS
  243. export BINDIR
  244. export PROGNAME
  245. # Dump environment info for logging purposes
  246. echo "Exec: $CMD"
  247. echo "Root: $ROOTDIR"
  248. # Log the startup
  249. logger -t "$SCRIPT[$$]" "Starting up"
  250. # Start the VM
  251. exec $CMD
  252. ;;
  253. chkconfig)
  254. RES=`$NODETOOL_LITE chkconfig $RUNNER_ETC_DIR/emqttd.config`
  255. if [ $? != 0 ]; then
  256. echo "Error reading $RUNNER_ETC_DIR/emqttd.config"
  257. echo $RES
  258. exit 1
  259. fi
  260. echo "config is OK"
  261. ;;
  262. escript)
  263. shift
  264. $ERTS_PATH/escript "$@"
  265. ;;
  266. version)
  267. echo $RIAK_VERSION
  268. ;;
  269. getpid)
  270. # Get the PID from nodetool
  271. get_pid
  272. ES=$?
  273. if [ "$ES" -ne 0 ] || [ -z $PID ]; then
  274. exit $ES
  275. fi
  276. echo $PID
  277. ;;
  278. *)
  279. echo "Usage: $SCRIPT {start|stop|restart|reboot|ping|console|attach|chkconfig|escript|version|getpid}"
  280. exit 1
  281. ;;
  282. esac
  283. exit 0