Planetlab examples
[nepi.git] / examples / planetlab / ccn_simple_transfer.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 as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
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: Alina Quereilhac <alina.quereilhac@inria.fr>
19 #
20
21 # Example of how to run this experiment (replace with your information):
22 #
23 # $ cd <path-to-nepi>
24 # python examples/planetlab/ccn_simple_transfer.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
25
26 # CCN topology:
27 #
28 #                
29 #                 
30 #  content                  ccncat
31 #  Linux host               Linux host
32 #     0 ------- network -------- 1
33 #
34
35 from nepi.execution.ec import ExperimentController
36
37 from optparse import OptionParser, SUPPRESS_HELP
38 import os
39
40 usage = ("usage: %prog -s <pl-slice> -u <pl-user> -p <pl-password> "
41     "-k <pl-ssh-key> -a <hostanme1> -b <hostname2> ")
42
43 parser = OptionParser(usage = usage)
44 parser.add_option("-s", "--pl-slice", dest="pl_slice",
45         help="PlanetLab slicename", type="str")
46 parser.add_option("-u", "--pl-user", dest="pl_user",
47         help="PlanetLab web username", type="str")
48 parser.add_option("-p", "--pl-password", dest="pl_password",
49         help="PlanetLab web password", type="str")
50 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
51         help="Path to private SSH key associated with the PL account",
52         type="str")
53 parser.add_option("-a", "--hostname1", dest="hostname1", 
54         help="Remote host 1", type="str")
55 parser.add_option("-b", "--hostname2", dest="hostname2", 
56         help="Remote host 2", type="str")
57 (options, args) = parser.parse_args()
58
59 hostname1 = options.hostname1
60 hostname2 = options.hostname2
61 pl_slice = options.pl_slice
62 pl_ssh_key = options.pl_ssh_key
63 pl_user = options.pl_user
64 pl_password = options.pl_password
65
66 ## Create the experiment controller
67 ec = ExperimentController(exp_id = "pl_ccn_simple_transfer")
68
69 ##### CONFIGURING NODE 1
70
71 ## Register node 1
72 node1 = ec.register_resource("PlanetlabNode")
73 # Set the hostname of the first node to use for the experiment
74 ec.set(node1, "hostname", hostname1)
75 # username should be your SSH user 
76 ec.set(node1, "username", pl_slice)
77 # Path to the SSH private key
78 ec.set(node1, "identity", pl_ssh_key)
79 # Planetlab web site user and password
80 ec.set(node1, "pluser", pl_user)
81 ec.set(node1, "plpassword", pl_password)
82 # Clean all files, results, etc, from previous experiments wit the same exp_id
83 ec.set(node1, "cleanExperiment", True)
84 # Kill all running processes in the node before running the experiment
85 ec.set(node1, "cleanProcesses", True)
86
87 ## Register a CCN daemon in node 1
88 ccnd1 = ec.register_resource("LinuxCCND")
89 # Set ccnd log level to 7
90 ec.set(ccnd1, "debug", 7)
91 ec.register_connection(ccnd1, node1)
92
93 ## Register a repository in node 1
94 ccnr1 = ec.register_resource("LinuxCCNR")
95 ec.register_connection(ccnr1, ccnd1)
96
97 ## Push the file into the repository
98 local_path_to_content = os.path.join(
99         os.path.dirname(os.path.realpath(__file__)),
100             "..", "big_buck_bunny_240p_mpeg4_lq.ts")
101
102 content_name = "ccnx:/test/FILE"
103
104 # Add a content to the repository
105 co = ec.register_resource("LinuxCCNContent")
106 ec.set(co, "contentName", content_name)
107 # NEPI will upload the specified file to the remote node and write it
108 # into the CCN repository
109 ec.set(co, "content", local_path_to_content)
110 ec.register_connection(co, ccnr1)
111
112 ##### CONFIGURING NODE 2
113
114 ## Register node 2 
115 node2 = ec.register_resource("PlanetlabNode")
116 # Set the hostname of the first node to use for the experiment
117 ec.set(node2, "hostname", hostname2)
118 # username should be your SSH user 
119 ec.set(node2, "username", pl_slice)
120 # Path to the SSH private key
121 ec.set(node2, "identity", pl_ssh_key)
122 # Planetlab web site user and password
123 ec.set(node2, "pluser", pl_user)
124 ec.set(node2, "plpassword", pl_password)
125 # Clean all files, results, etc, from previous experiments wit the same exp_id
126 ec.set(node2, "cleanExperiment", True)
127 # Kill all running processes in the node before running the experiment
128 ec.set(node2, "cleanProcesses", True)
129
130 ## Register a CCN daemon in node 2
131 ccnd2 = ec.register_resource("LinuxCCND")
132 # Set ccnd log level to 7
133 ec.set(ccnd2, "debug", 7)
134 ec.register_connection(ccnd2, node2)
135
136 ## Retrieve the file stored in node 1 from node 2
137 ccncat = ec.register_resource("LinuxCCNCat")
138 ec.set(ccncat, "contentName", content_name)
139 ec.register_connection(ccncat, ccnd2)
140
141 ##### INTERCONNECTING CCN NODES ...
142
143 # Register a FIB entry from node 1 to node 2
144 entry1 = ec.register_resource("LinuxFIBEntry")
145 ec.set(entry1, "host", hostname2)
146 ec.register_connection(entry1, ccnd1)
147
148 # Register a FIB entry from node 2 to node 1
149 entry2 = ec.register_resource("LinuxFIBEntry")
150 ec.set(entry2, "host", hostname1)
151 ec.register_connection(entry2, ccnd2)
152
153 ##### STARTING THE EXPERIMENT
154
155 ## Deploy all resources
156 ec.deploy()
157
158 # Wait until the ccncat is finished
159 ec.wait_finished([ccncat])
160
161 stdout = ec.trace(ccncat, "stdout")
162 f = open("video.ts", "w")
163 f.write(stdout)
164 f.close()
165
166 ec.shutdown()
167
168 print "Transfered FILE stored localy at video.ts"
169