82ad58b8a5d31aa44e08d623d1c7231eaef6a6ac
[nepi.git] / examples / planetlab / select_nodes.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 #
19
20 # Example of how to run this experiment (replace with your information):
21 #
22 # $ cd <path-to-nepi>
23 # python examples/planetlab/select_nodes.py -s <pl-slice> -u <pl-user> -p <pl-password> -k <pl-ssh-key> -c <country> -o <operating-system> -n <node-count> 
24
25
26 from nepi.execution.ec import ExperimentController
27
28 from optparse import OptionParser
29 import os
30
31 usage = ("usage: %prog -s <pl-slice> -u <pl-user> -p <pl-password> "
32     "-k <pl-ssh-key> -c <country> -o <operating-system> -n <node-count> ")
33
34 parser = OptionParser(usage = usage)
35 parser.add_option("-s", "--pl-slice", dest="pl_slice",
36         help="PlanetLab slicename", type="str")
37 parser.add_option("-u", "--pl-user", dest="pl_user",
38         help="PlanetLab web username", type="str")
39 parser.add_option("-p", "--pl-password", dest="pl_password",
40         help="PlanetLab web password", type="str")
41 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
42         help="Path to private SSH key associated with the PL account",
43         type="str")
44 parser.add_option("-c", "--country", dest="country",
45         help="Country for the PL hosts",
46         type="str")
47 parser.add_option("-o", "--os", dest="os",
48         help="Operating system for the PL hosts",
49         type="str")
50 parser.add_option("-n", "--node-count", dest="node_count",
51         help="Number of PL hosts to provision",
52         default = 2,
53         type="int")
54
55 (options, args) = parser.parse_args()
56
57 pl_slice = options.pl_slice
58 pl_ssh_key = options.pl_ssh_key
59 pl_user = options.pl_user
60 pl_password = options.pl_password
61 country = options.country
62 os = options.os
63 node_count = options.node_count
64
65 def add_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, country, os):
66     node = ec.register_resource("planetlab::Node")
67     ec.set(node, "username", pl_slice)
68     ec.set(node, "identity", pl_ssh_key)
69     ec.set(node, "pluser", pl_user)
70     ec.set(node, "plpassword", pl_password)
71
72     if country:
73         ec.set(node, "country", country)
74     if os:
75         ec.set(node, "operatingSystem", os)
76
77     ec.set(node, "cleanExperiment", True)
78     ec.set(node, "cleanProcesses", True)
79
80     return node
81
82 ## Create the experiment controller
83 ec = ExperimentController(exp_id="host_select")
84
85 nodes = []
86
87 for i in xrange(node_count):
88     node = add_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, country, os)
89     nodes.append(node)
90
91 ec.deploy()
92
93 ec.wait_deployed(nodes)
94
95 print "SELECTED HOSTS"
96
97 for node in nodes:
98     print ec.get(node, "hostname")
99
100 ec.shutdown()
101
102