emqx.cmd 7.1 KB

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