emqx.cmd 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. :: * console - start the Erlang release in a `werl` Windows shell
  12. :: * attach - connect to a running node and open an interactive console
  13. :: * list - display a listing of installed Erlang services
  14. :: * usage - display available commands
  15. :: Set variables that describe the release
  16. @set rel_name=emqx
  17. @set rel_vsn={{ release_version }}
  18. @set REL_VSN=%rel_vsn%
  19. @set erts_vsn={{ erts_vsn }}
  20. @set erl_opts={{ erl_opts }}
  21. @set script=%~n0
  22. @set EPMD_ARG=-start_epmd false -epmd_module ekka_epmd -proto_dist ekka
  23. @set ERL_FLAGS=%EPMD_ARG%
  24. :: Discover the release root directory from the directory
  25. :: of this script
  26. @set script_dir=%~dp0
  27. @for %%A in ("%script_dir%\..") do @(
  28. set rel_root_dir=%%~fA
  29. )
  30. @set rel_dir=%rel_root_dir%\releases\%rel_vsn%
  31. @set RUNNER_ROOT_DIR=%rel_root_dir%
  32. @set RUNNER_ETC_DIR=%rel_root_dir%\etc
  33. @set etc_dir=%rel_root_dir%\etc
  34. @set lib_dir=%rel_root_dir%\lib
  35. @set data_dir=%rel_root_dir%\data
  36. @set emqx_conf=%etc_dir%\emqx.conf
  37. @call :find_erts_dir
  38. @call :find_vm_args
  39. @call :find_sys_config
  40. @call :set_boot_script_var
  41. @set service_name=%rel_name%_%rel_vsn%
  42. @set bindir=%erts_dir%\bin
  43. @set progname=erl.exe
  44. @set clean_boot_script=%rel_root_dir%\bin\start_clean
  45. @set erlsrv="%bindir%\erlsrv.exe"
  46. @set escript="%bindir%\escript.exe"
  47. @set werl="%bindir%\werl.exe"
  48. @set erl_exe="%bindir%\erl.exe"
  49. @set nodetool="%rel_root_dir%\bin\nodetool"
  50. @set cuttlefish="%rel_root_dir%\bin\cuttlefish"
  51. @set node_type="-name"
  52. @set schema_mod="emqx_conf_schema"
  53. @set conf_path="%etc_dir%\emqx.conf"
  54. :: Extract node name from emqx.conf
  55. @for /f "usebackq delims=" %%I in (`"%escript% %nodetool% hocon -s %schema_mod% -c %conf_path% get node.name"`) do @(
  56. @call :set_trim node_name %%I
  57. )
  58. :: Extract node cookie from emqx.conf
  59. @for /f "usebackq delims=" %%I in (`"%escript% %nodetool% hocon -s %schema_mod% -c %conf_path% get node.cookie"`) do @(
  60. @call :set_trim node_cookie %%I
  61. )
  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. @if "%1"=="install" @goto install
  69. @if "%1"=="uninstall" @goto uninstall
  70. @if "%1"=="start" @goto start
  71. @if "%1"=="stop" @goto stop
  72. @if "%1"=="restart" @call :stop && @goto start
  73. ::@if "%1"=="upgrade" @goto relup
  74. ::@if "%1"=="downgrade" @goto relup
  75. @if "%1"=="console" @goto console
  76. @if "%1"=="ping" @goto ping
  77. @if "%1"=="list" @goto list
  78. @if "%1"=="attach" @goto attach
  79. @if "%1"=="" @goto usage
  80. @echo Unknown command: "%1"
  81. @goto :eof
  82. :: Find the ERTS dir
  83. :find_erts_dir
  84. @set possible_erts_dir=%rel_root_dir%\erts-%erts_vsn%
  85. @if exist "%possible_erts_dir%" (
  86. call :set_erts_dir_from_default
  87. ) else (
  88. call :set_erts_dir_from_erl
  89. )
  90. @goto :eof
  91. :: Set the ERTS dir from the passed in erts_vsn
  92. :set_erts_dir_from_default
  93. @set erts_dir=%possible_erts_dir%
  94. @set rootdir=%rel_root_dir%
  95. @goto :eof
  96. :: Set the ERTS dir from erl
  97. :set_erts_dir_from_erl
  98. @for /f "delims=" %%i in ('where erl') do @(
  99. set erl=%%i
  100. )
  101. @set dir_cmd="%erl%" -noshell -eval "io:format(\"~s\", [filename:nativename(code:root_dir())])." -s init stop
  102. @for /f %%i in ('%%dir_cmd%%') do @(
  103. set erl_root=%%i
  104. )
  105. @set erts_dir=%erl_root%\erts-%erts_vsn%
  106. @set rootdir=%erl_root%
  107. @goto :eof
  108. :find_vm_args
  109. @set possible_vm=%etc_dir%\vm.args
  110. @if exist "%possible_vm%" (
  111. set args_file=-args_file "%possible_vm%"
  112. )
  113. @goto :eof
  114. :: Find the sys.config file
  115. :find_sys_config
  116. @set possible_sys=%etc_dir%\sys.config
  117. @if exist "%possible_sys%" (
  118. set sys_config=-config "%possible_sys%"
  119. )
  120. @goto :eof
  121. :create_mnesia_dir
  122. @set create_dir_cmd=%escript% %nodetool% mnesia_dir "%data_dir%\mnesia" %node_name%
  123. @for /f "delims=" %%Z in ('%%create_dir_cmd%%') do @(
  124. set mnesia_dir=%%Z
  125. )
  126. @set mnesia_dir="%mnesia_dir%"
  127. @goto :eof
  128. :: get the current time with hocon
  129. :get_cur_time
  130. @for /f "usebackq tokens=1-6 delims=." %%a in (`"%escript% %nodetool% hocon now_time"`) do @(
  131. set now_time=%%a.%%b.%%c.%%d.%%e.%%f
  132. )
  133. @goto :eof
  134. :generate_app_config
  135. @call :get_cur_time
  136. %escript% %nodetool% hocon -v -t %now_time% -s %schema_mod% -c "%etc_dir%\emqx.conf" -d "%data_dir%\configs" generate
  137. @set generated_config_args=-config "%data_dir%\configs\app.%now_time%.config" -args_file "%data_dir%\configs\vm.%now_time%.args"
  138. :: create one new line
  139. @echo.>>"%data_dir%\configs\vm.%now_time%.args"
  140. :: write the node type and node name in to vm args file
  141. @echo %node_type% %node_name%>>"%data_dir%\configs\vm.%now_time%.args"
  142. @goto :eof
  143. :: set boot_script variable
  144. :set_boot_script_var
  145. @if exist "%rel_dir%\%rel_name%.boot" (
  146. set boot_script=%rel_dir%\%rel_name%
  147. ) else (
  148. set boot_script=%rel_dir%\start
  149. )
  150. @goto :eof
  151. :: Write the erl.ini file
  152. :write_ini
  153. @set erl_ini=%erts_dir%\bin\erl.ini
  154. @set converted_bindir=%bindir:\=\\%
  155. @set converted_rootdir=%rootdir:\=\\%
  156. @echo [erlang] > "%erl_ini%"
  157. @echo Bindir=%converted_bindir% >> "%erl_ini%"
  158. @echo Progname=%progname% >> "%erl_ini%"
  159. @echo Rootdir=%converted_rootdir% >> "%erl_ini%"
  160. @goto :eof
  161. :: Display usage information
  162. :usage
  163. @echo usage: %~n0 ^(install^|uninstall^|start^|stop^|restart^|console^|ping^|list^|attach^)
  164. @goto :eof
  165. :: Install the release as a Windows service
  166. :: or install the specified version passed as argument
  167. :install
  168. @call :create_mnesia_dir
  169. @call :generate_app_config
  170. :: Install the service
  171. @set args="-boot %boot_script% %sys_config% %generated_config_args% -mnesia dir '%mnesia_dir%'"
  172. @set description=EMQX node %node_name% in %rootdir%
  173. @if "" == "%2" (
  174. %erlsrv% add %service_name% %node_type% "%node_name%" -on restart -c "%description%" ^
  175. -i "emqx" -w "%rootdir%" -m %erl_exe% -args %args% ^
  176. -st "init:stop()."
  177. sc config emqx start=delayed-auto
  178. ) else (
  179. :: relup and reldown
  180. goto relup
  181. )
  182. @goto :eof
  183. :: Uninstall the Windows service
  184. :uninstall
  185. @%erlsrv% remove %service_name%
  186. @goto :eof
  187. :: Start the Windows service
  188. :start
  189. :: window service?
  190. :: @%erlsrv% start %service_name%
  191. @call :create_mnesia_dir
  192. @call :generate_app_config
  193. @set args=-detached %sys_config% %generated_config_args% -mnesia dir '%mnesia_dir%'
  194. @echo off
  195. cd /d "%rel_root_dir%"
  196. @echo on
  197. @start "%rel_name%" %werl% -boot "%boot_script%" -mode embedded %args%
  198. @goto :eof
  199. :: Stop the Windows service
  200. :stop
  201. :: window service?
  202. :: @%erlsrv% stop %service_name%
  203. @%escript% %nodetool% %node_type% %node_name% -setcookie %node_cookie% stop
  204. @goto :eof
  205. :: Relup and reldown
  206. :relup
  207. @if "" == "%2" (
  208. echo Missing package argument
  209. echo Usage: %rel_name% %1 {package base name}
  210. echo NOTE {package base name} MUST NOT include the .tar.gz suffix
  211. set ERRORLEVEL=1
  212. exit /b %ERRORLEVEL%
  213. )
  214. @%escript% "%rootdir%/bin/install_upgrade.escript" "%rel_name%" "%node_name%" "%node_cookie%" "%2"
  215. @goto :eof
  216. :: Start a console
  217. :console
  218. @call :create_mnesia_dir
  219. @call :generate_app_config
  220. @set args=%sys_config% %generated_config_args% -mnesia dir '%mnesia_dir%'
  221. @echo off
  222. cd /d %rel_root_dir%
  223. @echo on
  224. @start "bin\%rel_name% console" %werl% -boot "%boot_script%" -mode embedded %args%
  225. @echo emqx is started!
  226. @goto :eof
  227. :: Ping the running node
  228. :ping
  229. @%escript% %nodetool% ping %node_type% "%node_name%" -setcookie "%node_cookie%"
  230. @goto :eof
  231. :: List installed Erlang services
  232. :list
  233. @%erlsrv% list %service_name%
  234. @goto :eof
  235. :: Attach to a running node
  236. :attach
  237. :: @start "%node_name% attach"
  238. @start "%node_name% attach" %werl% -boot "%clean_boot_script%" ^
  239. -remsh %node_name% %node_type% console_%node_name% -setcookie %node_cookie%
  240. @goto :eof
  241. :: Trim variable
  242. :set_trim
  243. @set %1=%2
  244. @goto :eof