emqx.cmd 7.0 KB

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