emqx.cmd 6.9 KB

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