emqx.cmd 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. :: Extract node name from emqx.conf
  54. @for /f "usebackq delims=" %%I in (`"%escript% %nodetool% hocon -s %schema_mod% -c %etc_dir%\emqx.conf get node.name"`) do @(
  55. @call :set_trim node_name %%I
  56. )
  57. :: Extract node cookie from emqx.conf
  58. @for /f "usebackq delims=" %%I in (`"%escript% %nodetool% hocon -s %schema_mod% -c %etc_dir%\emqx.conf get node.cookie"`) do @(
  59. @call :set_trim node_cookie %%I
  60. )
  61. :: Write the erl.ini file to set up paths relative to this script
  62. @call :write_ini
  63. :: If a start.boot file is not present, copy one from the named .boot file
  64. @if not exist "%rel_dir%\start.boot" (
  65. copy "%rel_dir%\%rel_name%.boot" "%rel_dir%\start.boot" >nul
  66. )
  67. @if "%1"=="install" @goto install
  68. @if "%1"=="uninstall" @goto uninstall
  69. @if "%1"=="start" @goto start
  70. @if "%1"=="stop" @goto stop
  71. @if "%1"=="restart" @call :stop && @goto start
  72. ::@if "%1"=="upgrade" @goto relup
  73. ::@if "%1"=="downgrade" @goto relup
  74. @if "%1"=="console" @goto console
  75. @if "%1"=="ping" @goto ping
  76. @if "%1"=="list" @goto list
  77. @if "%1"=="attach" @goto attach
  78. @if "%1"=="" @goto usage
  79. @echo Unknown command: "%1"
  80. @goto :eof
  81. :: Find the ERTS dir
  82. :find_erts_dir
  83. @set possible_erts_dir=%rel_root_dir%\erts-%erts_vsn%
  84. @if exist "%possible_erts_dir%" (
  85. call :set_erts_dir_from_default
  86. ) else (
  87. call :set_erts_dir_from_erl
  88. )
  89. @goto :eof
  90. :: Set the ERTS dir from the passed in erts_vsn
  91. :set_erts_dir_from_default
  92. @set erts_dir=%possible_erts_dir%
  93. @set rootdir=%rel_root_dir%
  94. @goto :eof
  95. :: Set the ERTS dir from erl
  96. :set_erts_dir_from_erl
  97. @for /f "delims=" %%i in ('where erl') do @(
  98. set erl=%%i
  99. )
  100. @set dir_cmd="%erl%" -noshell -eval "io:format(\"~s\", [filename:nativename(code:root_dir())])." -s init stop
  101. @for /f %%i in ('%%dir_cmd%%') do @(
  102. set erl_root=%%i
  103. )
  104. @set erts_dir=%erl_root%\erts-%erts_vsn%
  105. @set rootdir=%erl_root%
  106. @goto :eof
  107. :find_vm_args
  108. @set possible_vm=%etc_dir%\vm.args
  109. @if exist %possible_vm% (
  110. set args_file=-args_file "%possible_vm%"
  111. )
  112. @goto :eof
  113. :: Find the sys.config file
  114. :find_sys_config
  115. @set possible_sys=%etc_dir%\sys.config
  116. @if exist %possible_sys% (
  117. set sys_config=-config "%possible_sys%"
  118. )
  119. @goto :eof
  120. :create_mnesia_dir
  121. @set create_dir_cmd=%escript% %nodetool% mnesia_dir %data_dir%\mnesia %node_name%
  122. @for /f "delims=" %%Z in ('%%create_dir_cmd%%') do @(
  123. set mnesia_dir=%%Z
  124. )
  125. @goto :eof
  126. :: get the current time with hocon
  127. :get_cur_time
  128. @for /f "usebackq tokens=1-6 delims=." %%a in (`"%escript% %nodetool% hocon now_time"`) do @(
  129. set now_time=%%a.%%b.%%c.%%d.%%e.%%f
  130. )
  131. @goto :eof
  132. :generate_app_config
  133. @call :get_cur_time
  134. %escript% %nodetool% hocon -v -t %now_time% -s %schema_mod% -c "%etc_dir%\emqx.conf" -d "%data_dir%\configs" generate
  135. @set generated_config_args=-config %data_dir%\configs\app.%now_time%.config -args_file %data_dir%\configs\vm.%now_time%.args
  136. :: create one new line
  137. @echo.>>%data_dir%\configs\vm.%now_time%.args
  138. :: write the node type and node name in to vm args file
  139. @echo %node_type% %node_name%>>%data_dir%\configs\vm.%now_time%.args
  140. @goto :eof
  141. :: set boot_script variable
  142. :set_boot_script_var
  143. @if exist "%rel_dir%\%rel_name%.boot" (
  144. set boot_script=%rel_dir%\%rel_name%
  145. ) else (
  146. set boot_script=%rel_dir%\start
  147. )
  148. @goto :eof
  149. :: Write the erl.ini file
  150. :write_ini
  151. @set erl_ini=%erts_dir%\bin\erl.ini
  152. @set converted_bindir=%bindir:\=\\%
  153. @set converted_rootdir=%rootdir:\=\\%
  154. @echo [erlang] > "%erl_ini%"
  155. @echo Bindir=%converted_bindir% >> "%erl_ini%"
  156. @echo Progname=%progname% >> "%erl_ini%"
  157. @echo Rootdir=%converted_rootdir% >> "%erl_ini%"
  158. @goto :eof
  159. :: Display usage information
  160. :usage
  161. @echo usage: %~n0 ^(install^|uninstall^|start^|stop^|restart^|console^|ping^|list^|attach^)
  162. @goto :eof
  163. :: Install the release as a Windows service
  164. :: or install the specified version passed as argument
  165. :install
  166. @call :create_mnesia_dir
  167. @call :generate_app_config
  168. :: Install the service
  169. @set args="-boot %boot_script% %sys_config% %generated_config_args% -mnesia dir '%mnesia_dir%'"
  170. @set description=EMQX node %node_name% in %rootdir%
  171. @if "" == "%2" (
  172. %erlsrv% add %service_name% %node_type% "%node_name%" -on restart -c "%description%" ^
  173. -i "emqx" -w "%rootdir%" -m %erl_exe% -args %args% ^
  174. -st "init:stop()."
  175. sc config emqx start=delayed-auto
  176. ) else (
  177. :: relup and reldown
  178. goto relup
  179. )
  180. @goto :eof
  181. :: Uninstall the Windows service
  182. :uninstall
  183. @%erlsrv% remove %service_name%
  184. @goto :eof
  185. :: Start the Windows service
  186. :start
  187. :: window service?
  188. :: @%erlsrv% start %service_name%
  189. @call :create_mnesia_dir
  190. @call :generate_app_config
  191. @set args=-detached %sys_config% %generated_config_args% -mnesia dir '%mnesia_dir%'
  192. @echo off
  193. cd /d %rel_root_dir%
  194. @echo on
  195. @start "%rel_name%" %werl% -boot "%boot_script%" -mode embedded %args%
  196. @goto :eof
  197. :: Stop the Windows service
  198. :stop
  199. :: window service?
  200. :: @%erlsrv% stop %service_name%
  201. @%escript% %nodetool% %node_type% %node_name% -setcookie %node_cookie% stop
  202. @goto :eof
  203. :: Relup and reldown
  204. :relup
  205. @if "" == "%2" (
  206. echo Missing package argument
  207. echo Usage: %rel_name% %1 {package base name}
  208. echo NOTE {package base name} MUST NOT include the .tar.gz suffix
  209. set ERRORLEVEL=1
  210. exit /b %ERRORLEVEL%
  211. )
  212. @%escript% "%rootdir%/bin/install_upgrade.escript" "%rel_name%" "%node_name%" "%node_cookie%" "%2"
  213. @goto :eof
  214. :: Start a console
  215. :console
  216. @call :create_mnesia_dir
  217. @call :generate_app_config
  218. @set args=%sys_config% %generated_config_args% -mnesia dir '%mnesia_dir%'
  219. @echo off
  220. cd /d %rel_root_dir%
  221. @echo on
  222. @start "bin\%rel_name% console" %werl% -boot "%boot_script%" -mode embedded %args%
  223. @echo emqx is started!
  224. @goto :eof
  225. :: Ping the running node
  226. :ping
  227. @%escript% %nodetool% ping %node_type% "%node_name%" -setcookie "%node_cookie%"
  228. @goto :eof
  229. :: List installed Erlang services
  230. :list
  231. @%erlsrv% list %service_name%
  232. @goto :eof
  233. :: Attach to a running node
  234. :attach
  235. :: @start "%node_name% attach"
  236. @start "%node_name% attach" %werl% -boot "%clean_boot_script%" ^
  237. -remsh %node_name% %node_type% console_%node_name% -setcookie %node_cookie%
  238. @goto :eof
  239. :: Trim variable
  240. :set_trim
  241. @set %1=%2
  242. @goto :eof