emqx.cmd 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. :: This batch file handles managing an Erlang node as a Windows service.
  2. ::
  3. :: Commands provided:
  4. ::
  5. :: * install - install the release as a Windows service
  6. :: * start - start the service and Erlang node
  7. :: * stop - stop the service and Erlang node
  8. :: * restart - run the stop command and start command
  9. :: * uninstall - uninstall the service and kill a running node
  10. :: * ping - check if the node is running
  11. :: * ctl - run management commands
  12. :: * console - start the Erlang release in a `werl` Windows shell
  13. :: * attach - connect to a running node and open an interactive console
  14. :: * remote_console - same as attach
  15. :: * list - display a listing of installed Erlang services
  16. :: * usage - display available commands
  17. :: Set variables that describe the release
  18. @set rel_name=emqx
  19. @set rel_vsn={{ release_version }}
  20. @set REL_VSN=%rel_vsn%
  21. @set erts_vsn={{ erts_vsn }}
  22. @set erl_opts={{ erl_opts }}
  23. @set script=%~n0
  24. :: for remote_console
  25. @set EPMD_ARG=-start_epmd false -epmd_module ekka_epmd -proto_dist ekka
  26. :: for erl command
  27. @set ERL_FLAGS=%EPMD_ARG%
  28. :: Discover the release root directory from the directory
  29. :: of this script
  30. @set script_dir=%~dp0
  31. @for %%A in ("%script_dir%\..") do @(
  32. set rel_root_dir=%%~fA
  33. )
  34. :: If release dir has space, change dir
  35. @if not "%rel_root_dir%"=="%rel_root_dir: =%" (
  36. @chdir /d "%rel_root_dir%"
  37. @set rel_root_dir=.
  38. )
  39. @set "erts_dir=%rel_root_dir%\erts-%erts_vsn%"
  40. @set "rootdir=%rel_root_dir%"
  41. @set "rel_dir=%rel_root_dir%\releases\%rel_vsn%"
  42. @set "RUNNER_ROOT_DIR=%rel_root_dir%"
  43. :: hard code etc dir
  44. @set "RUNNER_ETC_DIR=%rel_root_dir%\etc"
  45. @set "etc_dir=%rel_root_dir%\etc"
  46. @set "lib_dir=%rel_root_dir%\lib"
  47. @set "emqx_conf=%etc_dir%\emqx.conf"
  48. @set "boot_file_name=%rel_dir%\start"
  49. @set "service_name=%rel_name%_%rel_vsn%"
  50. @set "bindir=%erts_dir%\bin"
  51. @set progname=erl.exe
  52. @set "clean_boot_file_name=%rel_dir%\start_clean"
  53. @set "erlsrv=%bindir%\erlsrv.exe"
  54. @set "escript=%bindir%\escript.exe"
  55. @set "werl=%bindir%\werl.exe"
  56. @set "erl_exe=%bindir%\erl.exe"
  57. @set "nodetool=%rel_root_dir%\bin\nodetool"
  58. @set HOCON_ENV_OVERRIDE_PREFIX=EMQX_
  59. @set node_type=-name
  60. @set schema_mod=emqx_conf_schema
  61. @set conf_path="%etc_dir%\emqx.conf"
  62. :: Extract node name from emqx.conf
  63. @for /f "usebackq delims=" %%I in (`"%escript% %nodetool% hocon -s %schema_mod% -c %conf_path% get node.name"`) do @(
  64. @call :set_trim node_name %%I
  65. )
  66. @set node_name=%node_name:"=%
  67. :: Extract node cookie from emqx.conf
  68. @for /f "usebackq delims=" %%I in (`"%escript% %nodetool% hocon -s %schema_mod% -c %conf_path% get node.cookie"`) do @(
  69. @call :set_trim node_cookie %%I
  70. )
  71. @set node_cookie=%node_cookie:"=%
  72. :: Extract data_dir from emqx.conf
  73. @for /f "usebackq delims=" %%I in (`"%escript% %nodetool% hocon -s %schema_mod% -c %conf_path% get node.data_dir"`) do @(
  74. @call :set_trim data_dir %%I
  75. )
  76. @set data_dir=%data_dir:"=%
  77. :: remove trailing /
  78. @if %data_dir:~-1%==/ SET data_dir=%data_dir:~0,-1%
  79. :: remove trailing \
  80. @if %data_dir:~-1%==\ SET data_dir=%data_dir:~0,-1%
  81. @set abs_data_dir=%rel_root_dir%\%data_dir%
  82. @if exist %abs_data_dir% (
  83. @set data_dir=%abs_data_dir%
  84. )
  85. @if not exist %data_dir%\ (
  86. @echo ERROR: data_dir %data_dir% does not exist
  87. @goto :eof
  88. )
  89. :: Write the erl.ini file to set up paths relative to this script
  90. @call :write_ini
  91. :: If a start.boot file is not present, copy one from the named .boot file
  92. @if not exist "%rel_dir%\start.boot" (
  93. copy "%rel_dir%\%rel_name%.boot" "%rel_dir%\start.boot" >nul
  94. )
  95. @if "%1"=="install" @goto install
  96. @if "%1"=="uninstall" @goto uninstall
  97. @if "%1"=="start" @goto start
  98. @if "%1"=="stop" @goto stop
  99. @if "%1"=="restart" @call :stop && @goto start
  100. @if "%1"=="console" @goto console
  101. @if "%1"=="ping" @goto ping
  102. @if "%1"=="ctl" @goto ctl
  103. @if "%1"=="list" @goto list
  104. @if "%1"=="attach" @goto attach
  105. @if "%1"=="remote_console" @goto attach
  106. @if "%1"=="" @goto usage
  107. @echo Unknown command: "%1"
  108. @goto :eof
  109. :create_mnesia_dir
  110. @set create_dir_cmd=%escript% %nodetool% mnesia_dir "%data_dir%\mnesia" %node_name%
  111. @for /f "delims=" %%Z in ('%%create_dir_cmd%%') do @(
  112. set mnesia_dir=%%Z
  113. )
  114. @goto :eof
  115. :: get the current time with hocon
  116. :get_cur_time
  117. @for /f "usebackq tokens=1-6 delims=." %%a in (`"%escript% %nodetool% hocon now_time"`) do @(
  118. set now_time=%%a.%%b.%%c.%%d.%%e.%%f
  119. )
  120. @goto :eof
  121. :generate_app_config
  122. @call :get_cur_time
  123. @%escript% %nodetool% hocon -v -t %now_time% -s %schema_mod% -c "%etc_dir%\emqx.conf" -d "%data_dir%\configs" generate
  124. @set generated_config_args=-config "%data_dir%\configs\app.%now_time%.config" -args_file "%data_dir%\configs\vm.%now_time%.args"
  125. :: create one new line
  126. @echo.>>"%data_dir%\configs\vm.%now_time%.args"
  127. :: write the node type and node name in to vm args file
  128. @echo %node_type% %node_name%>>"%data_dir%\configs\vm.%now_time%.args"
  129. @goto :eof
  130. :: Write the erl.ini file
  131. :write_ini
  132. @set "erl_ini=%erts_dir%\bin\erl.ini"
  133. @set converted_bindir=%bindir:\=\\%
  134. @set converted_rootdir=%rootdir:\=\\%
  135. @echo [erlang] > "%erl_ini%"
  136. @echo Bindir=%converted_bindir% >> "%erl_ini%"
  137. @echo Progname=%progname% >> "%erl_ini%"
  138. @echo Rootdir=%converted_rootdir% >> "%erl_ini%"
  139. @goto :eof
  140. :: Display usage information
  141. :usage
  142. @echo usage: %~n0 ^(install^|uninstall^|start^|stop^|restart^|console^|ping^|ctl^|list^|remote_console^|attach^)
  143. @goto :eof
  144. :: Install the release as a Windows service
  145. :: or install the specified version passed as argument
  146. :install
  147. @call :create_mnesia_dir
  148. @call :generate_app_config
  149. :: Install the service
  150. @set args="-boot %boot_file_name% %generated_config_args% -mnesia dir '%mnesia_dir%'"
  151. @set description=EMQX node %node_name% in %rootdir%
  152. @if "" == "%2" (
  153. %erlsrv% add %service_name% %node_type% "%node_name%" -on restart -c "%description%" ^
  154. -i "emqx" -w "%rootdir%" -m %erl_exe% -args %args% ^
  155. -st "init:stop()."
  156. sc config emqx start=delayed-auto
  157. )
  158. @goto :eof
  159. :: Uninstall the Windows service
  160. :uninstall
  161. @%erlsrv% remove %service_name%
  162. @goto :eof
  163. :: Start the Windows service
  164. :start
  165. :: window service?
  166. :: @%erlsrv% start %service_name%
  167. @call :create_mnesia_dir
  168. @call :generate_app_config
  169. @set args=-detached %generated_config_args% -mnesia dir '%mnesia_dir%'
  170. @echo off
  171. cd /d "%rel_root_dir%"
  172. @echo on
  173. @start "%rel_name%" %werl% -mode embedded -boot "%boot_file_name%" %args%
  174. @goto :eof
  175. :: Stop the Windows service
  176. :stop
  177. :: window service?
  178. :: @%erlsrv% stop %service_name%
  179. @%escript% %nodetool% %node_type% %node_name% -setcookie %node_cookie% stop
  180. @goto :eof
  181. :: Start a console
  182. :console
  183. @call :create_mnesia_dir
  184. @call :generate_app_config
  185. @set args=%generated_config_args% -mnesia dir '%mnesia_dir%'
  186. @echo off
  187. cd /d %rel_root_dir%
  188. @echo on
  189. @start "%rel_name% console" %werl% -mode embedded -boot "%boot_file_name%" %args%
  190. @echo emqx is started!
  191. @goto :eof
  192. :: Ping the running node
  193. :ping
  194. @%escript% %nodetool% ping %node_type% "%node_name%" -setcookie "%node_cookie%"
  195. @goto :eof
  196. :: ctl to execute management commands
  197. :ctl
  198. @for /f "usebackq tokens=1*" %%i in (`echo %*`) DO @ set params=%%j
  199. @%escript% %nodetool% %node_type% "%node_name%" -setcookie "%node_cookie%" rpc_infinity emqx_ctl run_command %params%
  200. @goto :eof
  201. :: List installed Erlang services
  202. :list
  203. @%erlsrv% list %service_name%
  204. @goto :eof
  205. :: Attach to a running node
  206. :attach
  207. @start "remsh_%nodename%" %werl% -hidden -remsh "%node_name%" -boot "%clean_boot_file_name%" "%node_type%" "remsh_%node_name%" -setcookie "%node_cookie%"
  208. @goto :eof
  209. :: Trim variable
  210. :set_trim
  211. @set %1=%2
  212. @goto :eof