emqx_lua_hook_cli.erl 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_lua_hook_cli).
  17. -export([ load/0
  18. , cmd/1
  19. , unload/0
  20. ]).
  21. -include("emqx_lua_hook.hrl").
  22. -include_lib("luerl/src/luerl.hrl").
  23. -define(PRINT(Format, Args), io:format(Format, Args)).
  24. -define(PRINT_CMD(Cmd, Descr), io:format("~-48s# ~s~n", [Cmd, Descr])).
  25. -define(USAGE(CmdList), [?PRINT_CMD(Cmd, Descr) || {Cmd, Descr} <- CmdList]).
  26. load() ->
  27. emqx_ctl:register_command(luahook, {?MODULE, cmd}, []).
  28. unload() ->
  29. emqx_ctl:unregister_command(luahook).
  30. cmd(["load", Script]) ->
  31. case emqx_lua_hook:load_script(fullname(Script)) of
  32. ok -> emqx_ctl:print("Load ~p successfully~n", [Script]);
  33. error -> emqx_ctl:print("Load ~p error~n", [Script])
  34. end;
  35. cmd(["reload", Script]) ->
  36. FullName = fullname(Script),
  37. emqx_lua_hook:unload_script(FullName),
  38. case emqx_lua_hook:load_script(FullName) of
  39. ok -> emqx_ctl:print("Reload ~p successfully~n", [Script]);
  40. error -> emqx_ctl:print("Reload ~p error~n", [Script])
  41. end;
  42. cmd(["unload", Script]) ->
  43. emqx_lua_hook:unload_script(fullname(Script)),
  44. emqx_ctl:print("Unload ~p successfully~n", [Script]);
  45. cmd(["enable", Script]) ->
  46. FullName = fullname(Script),
  47. case file:rename(fullnamedisable(Script), FullName) of
  48. ok -> case emqx_lua_hook:load_script(FullName) of
  49. ok ->
  50. emqx_ctl:print("Enable ~p successfully~n", [Script]);
  51. error ->
  52. emqx_ctl:print("Fail to enable ~p~n", [Script])
  53. end;
  54. {error, Reason} ->
  55. emqx_ctl:print("Fail to enable ~p due to ~p~n", [Script, Reason])
  56. end;
  57. cmd(["disable", Script]) ->
  58. FullName = fullname(Script),
  59. emqx_lua_hook:unload_script(FullName),
  60. case file:rename(FullName, fullnamedisable(Script)) of
  61. ok ->
  62. emqx_ctl:print("Disable ~p successfully~n", [Script]);
  63. {error, Reason} ->
  64. emqx_ctl:print("Fail to disable ~p due to ~p~n", [Script, Reason])
  65. end;
  66. cmd(_) ->
  67. emqx_ctl:usage([{"luahook load <Script>", "load lua script into hook"},
  68. {"luahook unload <Script>", "unload lua script from hook"},
  69. {"luahook reload <Script>", "reload lua script into hook"},
  70. {"luahook enable <Script>", "enable lua script and load it into hook"},
  71. {"luahook disable <Script>", "unload lua script out of hook and disable it"}]).
  72. fullname(Script) ->
  73. filename:join([emqx_lua_hook:lua_dir(), Script]).
  74. fullnamedisable(Script) ->
  75. fullname(Script)++".x".