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