emqx.cmd 7.1 KB

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