emqx.cmd 7.0 KB

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