Planetlab examples
[nepi.git] / examples / planetlab / ping_filters_experiment.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 as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
20
21 from nepi.execution.ec import ExperimentController
22 from nepi.execution.resource import ResourceAction, ResourceState
23
24 import os
25
26 def create_node(ec, username, pl_user, pl_password, hostname=None, country=None,
27                 operatingSystem=None, minBandwidth=None, minCpu=None):
28
29     node = ec.register_resource("PlanetlabNode")
30
31     if username:
32         ec.set(node, "username", username)
33     if pl_user:
34         ec.set(node, "pluser", pl_user)
35     if pl_password:
36         ec.set(node, "plpassword", pl_password)
37
38     if hostname:
39         ec.set(node, "hostname", hostname)
40     if country:
41         ec.set(node, "country", country)
42     if operatingSystem:
43         ec.set(node, "operatingSystem", operatingSystem)
44     if minBandwidth:
45         ec.set(node, "minBandwidth", minBandwidth)
46     if minCpu:
47         ec.set(node, "minCpu", minCpu)
48
49     ec.set(node, "cleanHome", True)
50     ec.set(node, "cleanProcesses", True)
51     
52     return node
53
54 def add_app(ec, command, node, sudo=None, video=None, depends=None, forward_x11=None, \
55         env=None):
56     app = ec.register_resource("LinuxApplication")
57     if sudo is not None:
58         ec.set(app, "sudo", sudo)
59     if video is not None:
60         ec.set(app, "sources", video)
61     if depends is not None:
62         ec.set(app, "depends", depends)
63     if forward_x11 is not None:
64         ec.set(app, "forwardX11", forward_x11)
65     if env is not None:
66         ec.set(app, "env", env)
67     ec.set(app, "command", command)
68
69     ec.register_connection(app, node)
70
71     return app
72
73 exp_id = "ping_filters_exp"
74
75 # Create the entity Experiment Controller:
76 ec = ExperimentController(exp_id)
77
78 # Register the nodes resources:
79
80 # The username in this case is the slice name, the one to use for login in 
81 # via ssh into PlanetLab nodes. Replace with your own slice name.
82 username = "inria_sfatest"
83
84 # The pluser and plpassword are the ones used to login in the PlanetLab web 
85 # site. Replace with your own user and password account information.
86 pl_user = "lucia.guevgeozian_odizzio@inria.fr"
87 pl_password =  os.environ.get("PL_PASS")
88
89 # Choose the PlanetLab nodes for the experiment, in this example 5 nodes are
90 # used, and they are picked according to different criterias.
91
92 # First node will be the one defined by its hostname.
93 hostname = "planetlab2.utt.fr"
94 node1 = create_node(ec, username, pl_user, pl_password, hostname=hostname)
95
96 # Second node will be any node in France.
97 country = "France"
98 node2 = create_node(ec, username, pl_user, pl_password, country=country)
99
100 # Third node will be a node in France that has Fedora 14 installed.
101 operatingSystem = "f14"
102 node3 = create_node(ec, username, pl_user, pl_password, country=country,
103                 operatingSystem=operatingSystem)
104
105 # Forth node will have at least 50% of CPU available
106 minCpu=50
107 node4 = create_node(ec, username, pl_user, pl_password, minCpu=minCpu)
108
109 # Fifth node can be any node, constrains are not important.
110 node5 = create_node(ec, username, pl_user, pl_password)
111
112 # Register the applications to run in the nodes, in this case just ping to the 
113 # first node:
114 apps_per_node = dict()
115 apps = []
116 for node in [node2, node3, node4, node5]:
117     command = "ping -c5 %s > ping%s.txt" % (hostname, node)
118     app = add_app(ec, command, node)
119     apps_per_node[node] = app
120     apps.append(app)
121
122 # Register conditions
123
124 # The nodes that are completely identified by their hostnames have to be provisioned 
125 # before the rest of the nodes. This assures that no other resource will use the
126 # identified node even if the constraints matchs. 
127 # In this example node2, node3, node4 and node5, are deployed after node1 is 
128 # provisioned. node1 must be the node planetlab2.utt.fr, meanwhile node2, node3,
129 # node4 and node5 just need to fulfill certain constraints.
130 # Applications are always deployed after nodes, so no need to register conditions
131 # for the apps in this example.
132
133 ec.register_condition(node2, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
134 ec.register_condition(node3, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
135 ec.register_condition(node4, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
136 ec.register_condition(node5, ResourceAction.DEPLOY, node1, ResourceState.PROVISIONED)
137     
138 # Deploy the experiment:
139 ec.deploy()
140
141 # Wait until the applications are finish to retrive the traces:
142 ec.wait_finished(apps)
143
144 traces = dict() 
145 for node, app in apps_per_node.iteritems():
146     ping_string = "ping%s.txt" % node
147     trace = ec.trace(app, ping_string)
148     traces[node]= trace
149
150 # Choose a directory to store the traces locally, change to a convenient path for you:
151 directory = "examples/planetlab/"
152 for node, trace in traces.iteritems():
153     trace_file = directory + "ping%s.txt" % node
154     f = open(trace_file, "w")
155     f.write(trace)
156     f.close()
157
158 # Do the experiment controller shutdown:
159 ec.shutdown()
160
161 # END