use print() - import print_function - should be fine for both py2 and py3
[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 from __future__ import print_function
26
27 from nepi.execution.ec import ExperimentController
28
29 from optparse import OptionParser
30 import os
31
32 usage = ("usage: %prog -s <pl-slice> -u <pl-user> -p <pl-password> "
33     "-k <pl-ssh-key> -c <country> -o <operating-system> -n <node-count> ")
34
35 parser = OptionParser(usage = usage)
36 parser.add_option("-s", "--pl-slice", dest="pl_slice",
37         help="PlanetLab slicename", type="str")
38 parser.add_option("-u", "--pl-user", dest="pl_user",
39         help="PlanetLab web username", type="str")
40 parser.add_option("-p", "--pl-password", dest="pl_password",
41         help="PlanetLab web password", type="str")
42 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
43         help="Path to private SSH key associated with the PL account",
44         type="str")
45 parser.add_option("-c", "--country", dest="country",
46         help="Country for the PL hosts",
47         type="str")
48 parser.add_option("-o", "--os", dest="os",
49         help="Operating system for the PL hosts",
50         type="str")
51 parser.add_option("-n", "--node-count", dest="node_count",
52         help="Number of PL hosts to provision",
53         default = 2,
54         type="int")
55
56 (options, args) = parser.parse_args()
57
58 pl_slice = options.pl_slice
59 pl_ssh_key = options.pl_ssh_key
60 pl_user = options.pl_user
61 pl_password = options.pl_password
62 country = options.country
63 os = options.os
64 node_count = options.node_count
65
66 def add_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, country, os):
67     node = ec.register_resource("planetlab::Node")
68     ec.set(node, "username", pl_slice)
69     ec.set(node, "identity", pl_ssh_key)
70     ec.set(node, "pluser", pl_user)
71     ec.set(node, "plpassword", pl_password)
72
73     if country:
74         ec.set(node, "country", country)
75     if os:
76         ec.set(node, "operatingSystem", os)
77
78     ec.set(node, "cleanExperiment", True)
79     ec.set(node, "cleanProcesses", True)
80
81     return node
82
83 ## Create the experiment controller
84 ec = ExperimentController(exp_id="host_select")
85
86 nodes = []
87
88 for i in xrange(node_count):
89     node = add_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, country, os)
90     nodes.append(node)
91
92 ec.deploy()
93
94 ec.wait_deployed(nodes)
95
96 print("SELECTED HOSTS")
97
98 for node in nodes:
99     print(ec.get(node, "hostname"))
100
101 ec.shutdown()
102
103