Guest User

Untitled

a guest
Feb 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #!/usr/bin/env escript
  2.  
  3. config() ->
  4. [{app_node, app@localhost},
  5. {appctl_node, appctl@localhost},
  6. {cookie, app},
  7. {timeout, 100}].
  8.  
  9. main(Args) ->
  10. ScriptDir = filename:dirname(filename:absname(escript:script_name())),
  11. code:add_path(filename:join(ScriptDir, "ebin")),
  12.  
  13. {AppCtlNode, Cookie} = get_values([appctl_node, cookie]),
  14.  
  15. os:cmd("epmd -daemon"),
  16. net_kernel:start([AppCtlNode, shortnames]),
  17. erlang:set_cookie(node(), Cookie),
  18.  
  19. try process(Args, ScriptDir)
  20. catch
  21. error:{cant_start, already_running} ->
  22. error("Error: can't start because App is already running");
  23. error:{cant_stop, isnt_running} ->
  24. error("Error: can't stop because App is not running");
  25. _:Reason ->
  26. Message = io_lib:format("Unknown error occured.~n"
  27. "Reason: ~p.~n"
  28. "Stack trace: ~p",
  29. [Reason, erlang:get_stacktrace()]),
  30. error(Message)
  31. end.
  32.  
  33. process([Arg], ScriptDir) ->
  34. {AppNode, Cookie} = get_values([app_node, cookie]),
  35. if
  36. Arg =:= "--start"; Arg =:= "-s" ->
  37. start(AppNode, Cookie, ScriptDir);
  38. Arg =:= "--stop"; Arg =:= "-q" ->
  39. stop(AppNode);
  40. Arg =:= "--restart"; Arg =:= "-r" ->
  41. stop(AppNode),
  42. start(AppNode, Cookie, ScriptDir);
  43. true ->
  44. usage()
  45. end;
  46. process(_, _) ->
  47. usage().
  48.  
  49. start(AppNode, Cookie, ScriptDir) ->
  50. case net_adm:ping(AppNode) of
  51. pong -> erlang:error({cant_start, already_running});
  52. pang -> ok
  53. end,
  54.  
  55. Deps = string:join(lists:foldl(fun(Dep, Acc) ->
  56. ["-pa", filename:join(ScriptDir, Dep) | Acc]
  57. end, [], ["ebin", "deps/*/ebin"]), " "),
  58.  
  59. Cmd = io_lib:format("erl -detached -sname ~p -setcookie ~p "
  60. "+K true "
  61. "-boot start_sasl -config log "
  62. "~s "
  63. "-s app",
  64. [AppNode, Cookie, Deps]),
  65. os:cmd(Cmd).
  66.  
  67. stop(AppNode) ->
  68. case net_adm:ping(AppNode) of
  69. pong -> ok;
  70. pang -> erlang:error({cant_stop, isnt_running})
  71. end,
  72.  
  73. rpc:call(AppNode, init, stop, []),
  74. wait_for_stop(AppNode, get_value(timeout)).
  75.  
  76. wait_for_stop(AppNode, Timeout) ->
  77. case net_adm:ping(AppNode) of
  78. pong ->
  79. timer:sleep(Timeout),
  80. wait_for_stop(AppNode, Timeout);
  81. pang ->
  82. ok
  83. end.
  84.  
  85. usage() ->
  86. io:format("Usage: ~s [--start | -s] [--stop | -q] [--restart | -r]~n",
  87. [escript:script_name()]).
  88.  
  89. get_value(Key) ->
  90. proplists:get_value(Key, config()).
  91.  
  92. get_values(Keys) ->
  93. get_values(Keys, config()).
  94. get_values(Keys, Config) when is_tuple(Keys) ->
  95. get_values(tuple_to_list(Keys), Config);
  96. get_values(Keys, Config) when is_list (Keys) ->
  97. Values = lists:foldl(fun(Key, Values) ->
  98. [proplists:get_value(Key, Config) | Values]
  99. end, [], Keys),
  100. list_to_tuple(lists:reverse(Values)).
  101.  
  102. error(Message) ->
  103. io:format(Message ++ ".~n"),
  104. halt(1).
Add Comment
Please, Sign In to add comment