emqx.cmd 7.0 KB

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