Permalink
Newer
Older
100644 131 lines (109 sloc) 4.09 KB
1
%%%-------------------------------------------------------------------
Feb 11, 2016
2
%%% Copyright (C) 2013-2016 by Joseph Wayne Norton <norton@alum.mit.edu>
3
%%% Copyright (c) 2012 Gemini Mobile Technologies, Inc. All rights reserved.
4
%%%
5
%%% Licensed under the Apache License, Version 2.0 (the "License");
6
%%% you may not use this file except in compliance with the License.
7
%%% You may obtain a copy of the License at
8
%%%
9
%%% http://www.apache.org/licenses/LICENSE-2.0
10
%%%
11
%%% Unless required by applicable law or agreed to in writing, software
12
%%% distributed under the License is distributed on an "AS IS" BASIS,
13
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
%%% See the License for the specific language governing permissions and
15
%%% limitations under the License.
16
%%%
17
%%% File : qc_slave.erl
18
%%% Purpose : Wrapper for slave nodes adapted from gproc_dist_tests.erl
19
%%%-------------------------------------------------------------------
20
21
%% ``The contents of this file are subject to the Erlang Public License,
22
%% Version 1.1, (the "License"); you may not use this file except in
23
%% compliance with the License. You should have received a copy of the
24
%% Erlang Public License along with this software. If not, it can be
25
%% retrieved via the world wide web at http://www.erlang.org/.
26
%%
27
%% Software distributed under the License is distributed on an "AS IS"
28
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29
%% the License for the specific language governing rights and limitations
30
%% under the License.
31
%%
32
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
33
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
34
%% AB. All Rights Reserved.''
35
%%
36
%% @author Ulf Wiger <ulf.wiger@erlang-solutions.com>
37
%%
38
39
-module(qc_slave).
40
41
%% API
Oct 1, 2011
42
-export([start_slave/1, restart_slave/1, stop_slave/1]).
43
-export([start_slaves/1, restart_slaves/1, stop_slaves/1]).
44
45
46
%%%----------------------------------------------------------------------
47
%%% API
48
%%%----------------------------------------------------------------------
49
50
start_slave(Name) ->
51
start(Name).
52
53
restart_slave(Name) ->
54
restart(Name).
55
Oct 1, 2011
56
stop_slave(Name) ->
57
stop(Name).
58
59
start_slaves(Names) when is_list(Names) ->
60
[H|T] = Nodes = [start(N) || N <- Names],
61
_ = [rpc:call(H, net_adm, ping, [N]) || N <- T],
62
Nodes.
63
Oct 1, 2011
64
restart_slaves(Names) when is_list(Names) ->
65
[H|T] = Nodes = [restart(N) || N <- Names],
66
_ = [rpc:call(H, net_adm, ping, [N]) || N <- T],
67
Nodes.
68
Oct 1, 2011
69
stop_slaves(Names) when is_list(Names) ->
70
[stop(N) || N <- Names].
71
72
73
%%%----------------------------------------------------------------------
74
%%% Internal
75
%%%----------------------------------------------------------------------
76
77
start(Name) ->
78
ok = init(),
79
Args = args(),
80
{ok, Node} = slave:start(host(), Name, Args),
81
Node.
82
83
restart(Name) ->
84
ok = init(),
85
Args = args(),
86
case slave:start(host(), Name, Args) of
87
{ok, Node} ->
88
Node;
89
{error, {already_running, Node}} ->
90
Node
91
end.
92
Oct 1, 2011
93
stop(Name) ->
94
slave:stop(slave(Name)).
95
96
init() ->
97
case node() of
98
nonode@nohost ->
99
_ = os:cmd("epmd -daemon"),
100
{ok, _} = net_kernel:start([?MODULE, shortnames]),
101
ok;
102
_ ->
103
ok
104
end.
105
106
args() ->
107
{Pa, Pz} = paths(),
108
"-pa ./ -pz ../ebin" ++
109
lists:flatten([[" -pa " ++ Path || Path <- Pa],
110
[" -pz " ++ Path || Path <- Pz]]).
111
112
paths() ->
113
Path = code:get_path(),
114
{ok, [[Root]]} = init:get_argument(root),
115
{Pas, Rest} = lists:splitwith(fun(P) ->
116
not lists:prefix(Root, P)
117
end, Path),
118
{_, Pzs} = lists:splitwith(fun(P) ->
119
lists:prefix(Root, P)
120
end, Rest),
121
{Pas, Pzs}.
122
123
host() ->
124
[_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
125
list_to_atom(Host).
Oct 1, 2011
126
127
slave(Name) when is_atom(Name) ->
128
slave(atom_to_list(Name));
129
slave(Name) when is_list(Name) ->
130
list_to_atom(Name ++ "@" ++ atom_to_list(host())).