8f9c3cf4b9bb652e80bf2a3a8dda4b1dd641f482
[nepi.git] / examples / planetlab / ping_with_filters.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
19
20 # Example of how to run this experiment (replace with your information):
21 #
22 # $ cd <path-to-nepi>
23 # python examples/planetlab/ping_with_filters.py -s <pl-slice> -u <pl-user> -p <pl-password> -k <pl-ssh-key>  
24
25 from nepi.execution.ec import ExperimentController
26 from nepi.execution.resource import ResourceAction, ResourceState
27
28 from optparse import OptionParser
29 import os
30
31
32 def create_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, 
33        hostname = None, country = None, operatingSystem = None, 
34        minBandwidth = None, minCpu = None):
35
36     node = ec.register_resource("planetlab::Node")
37
38     ec.set(node, "username", pl_slice)
39     ec.set(node, "identity", pl_ssh_key)
40     ec.set(node, "pluser", pl_user)
41     ec.set(node, "plpassword", pl_password)
42
43     if hostname:
44         ec.set(node, "hostname", hostname)
45     if country:
46         ec.set(node, "country", country)
47     if operatingSystem:
48         ec.set(node, "operatingSystem", operatingSystem)
49     if minBandwidth:
50         ec.set(node, "minBandwidth", minBandwidth)
51     if minCpu:
52         ec.set(node, "minCpu", minCpu)
53
54     ec.set(node, "cleanExperiment", True)
55     ec.set(node, "cleanProcesses", True)
56     
57     return node
58
59 def add_app(ec, command, node, newname = None, sudo = None, 
60         video = None, depends = None, forward_x11 = None, env = None):
61     app = ec.register_resource("linux::Application")
62
63     if sudo is not None:
64         ec.set(app, "sudo", sudo)
65     if video is not None:
66         ec.set(app, "sources", video)
67     if depends is not None:
68         ec.set(app, "depends", depends)
69     if forward_x11 is not None:
70         ec.set(app, "forwardX11", forward_x11)
71     if env is not None:
72         ec.set(app, "env", env)
73
74     ec.set(app, "command", command)
75
76     ec.register_connection(app, node)
77
78     # add collector to download application standar output
79     collector = ec.register_resource("Collector")
80     ec.set(collector, "traceName", "stdout")
81     if newname:
82         ec.set(collector, "rename", newname)
83     ec.register_connection(app, collector)
84
85     return app
86
87 usage = ("usage: %prog -s <pl-slice> -u <pl-user> -p <pl-password> "
88     "-k <pl-ssh-key> -c <country> -o <operating-system> -H <hostname> ")
89
90 parser = OptionParser(usage = usage)
91 parser.add_option("-s", "--pl-slice", dest="pl_slice",
92         help="PlanetLab slicename", type="str")
93 parser.add_option("-u", "--pl-user", dest="pl_user",
94         help="PlanetLab web username", type="str")
95 parser.add_option("-p", "--pl-password", dest="pl_password",
96         help="PlanetLab web password", type="str")
97 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
98         help="Path to private SSH key associated with the PL account",
99         type="str")
100 parser.add_option("-c", "--country", dest="country",
101         help="Country for the PL hosts",
102         type="str")
103 parser.add_option("-o", "--os", dest="os",
104         help="Operating system for the PL hosts", default="f14",
105         type="str")
106 parser.add_option("-H", "--hostname", dest="hostname",
107         help="PlanetLab hostname",
108         type="str")
109
110 (options, args) = parser.parse_args()
111
112 pl_slice = options.pl_slice
113 pl_ssh_key = options.pl_ssh_key
114 pl_user = options.pl_user
115 pl_password = options.pl_password
116 hostname = options.hostname
117 country = options.country
118 os = options.os
119
120 # Create the entity Experiment Controller:
121 ec = ExperimentController("pl_ping_filters")
122
123 # Register the nodes resources:
124
125 # Choose the PlanetLab nodes for the experiment, in this example 5 nodes are
126 # used, and they are picked according to different criterias.
127
128 # First node will be the one defined by its hostname.
129 node1 = create_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, 
130         hostname = hostname)
131
132 # Second node will be any node in the selected country.
133 node2 = create_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, 
134         country=country)
135
136 # Third node will be a node in the selected country and with the selected
137 # fedora OS
138 node3 = create_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, 
139         country = country,
140         operatingSystem = os)
141
142 # Forth node will have at least 50% of CPU available
143 minCpu=50
144 node4 = create_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password, 
145         minCpu = minCpu)
146
147 # Fifth node can be any node, constrains are not important.
148 node5 = create_node(ec, pl_slice, pl_ssh_key, pl_user, pl_password)
149
150 # Register the applications to run in the nodes, in this case just ping to the 
151 # first node:
152 apps = []
153 for node in [node2, node3, node4, node5]:
154     command = "ping -c5 %s" % hostname
155     trace_name = "%s.ping" % hostname
156     app = add_app(ec, command, node, newname = trace_name)
157     apps.append(app)
158
159 # Register conditions
160
161 # The nodes that are completely identified by their hostnames have to be provisioned 
162 # before the rest of the nodes. This assures that no other resource will use the
163 # identified node even if the constraints matchs. 
164 # In this example node2, node3, node4 and node5, are deployed after node1 is 
165 # provisioned. node1 must be the node hostname, meanwhile node2, node3,
166 # node4 and node5 just need to fulfill certain constraints.
167 # Applications are always deployed after nodes, so no need to register conditions
168 # for the apps in this example.
169
170 ec.register_condition(node2, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
171 ec.register_condition(node3, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
172 ec.register_condition(node4, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
173 ec.register_condition(node5, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
174     
175 # Deploy the experiment:
176 ec.deploy()
177
178 # Wait until the applications are finish to retrive the traces:
179 ec.wait_finished(apps)
180
181 print "Results stored at", ec.exp_dir
182
183 # Do the experiment controller shutdown:
184 ec.shutdown()
185
186 # END