57c58089bff00374b85a863616e2365c3abd80dd
[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 # Instructions to run this example:
21 #
22 # 1. First edit the script file where required (See ASSING messages)
23 #
24 # 2. Then, run the script:
25 #
26 # $ cd <path-to-nepi>
27 # $ PYTHONPATH=$PYTHONPATHS:src python examples/linux/ccn/two_nodes_file_retrieval.py
28 #
29
30 from nepi.execution.ec import ExperimentController
31
32 import os
33
34 pl_user = ######### <<< ASSIGN the username used to login to the PlanetLab website >>>
35 pl_pass = ######## <<< ASSIGN the password used to login to the PlanetLab website >>>
36 pl_ssh_key = ####### <<< ASSING the absolute path to the private SSH key used for Planetlab >>>
37 slicename = ####### <<< ASSING the PlanetLab slicename >>>
38
39 results_dir = "/tmp/demo_CCN_results"
40
41 ## Create the experiment controller
42 ec = ExperimentController(exp_id = "demo_CCN", 
43         local_dir = results_dir)
44
45 ## Register node 1
46 node1 = ec.register_resource("PlanetlabNode")
47 # Configure NEPI to automatically find and allocate a node in France
48 # ec.set(node1, "country", "France")
49 # Else, if you want a node in particular set the hostname
50 ec.set(node1, "hostname", "peeramidion.irisa.fr")
51 # PlanetLab (website) account username
52 ec.set(node1, "pluser", pl_user)
53 # PlanetLab (website) account password
54 ec.set(node1, "plpassword", pl_pass)
55 # username should be your PlanetLab slice name 
56 ec.set(node1, "username", slicename)
57 # Absolute path to the SSH private key for PlanetLab
58 ec.set(node1, "identity", pl_ssh_key)
59 # Clean all files, results, etc, from previous experiments wit the same exp_id
60 ec.set(node1, "cleanExperiment", True)
61 # Kill all running processes in the PlanetLab node before running the experiment
62 ec.set(node1, "cleanProcesses", True)
63
64 ## Register node 2 
65 node2 = ec.register_resource("PlanetlabNode")
66 # Configure NEPI to automatically find and allocate a node in Spain
67 #ec.set(node2, "country", "Spain")
68 # Else, if you want a node in particular set the hostname
69 ec.set(node2, "hostname", "planetlab2.upc.es")
70 # PlanetLab (website) account username
71 ec.set(node2, "pluser", pl_user)
72 # PlanetLab (website) account password
73 ec.set(node2, "plpassword", pl_pass)
74 # username should be your PlanetLab slice name 
75 ec.set(node2, "username", slicename)
76 # Absolute path to the SSH private key for PlanetLab
77 ec.set(node2, "identity", pl_ssh_key)
78 # Clean all files, results, etc, from previous experiments wit the same exp_id
79 ec.set(node2, "cleanExperiment", True)
80 # Kill all running processes in the PlanetLab node before running the experiment
81 ec.set(node2, "cleanProcesses", True)
82
83 ## Register a CCN daemon in node 1
84 ccnd1 = ec.register_resource("LinuxCCND")
85 # Set ccnd log level to 7
86 ec.set(ccnd1, "debug", 7)
87 ec.register_connection(ccnd1, node1)
88
89 ## Register a CCN daemon in node 2
90 ccnd2 = ec.register_resource("LinuxCCND")
91 # Set ccnd log level to 7
92 ec.set(ccnd2, "debug", 7)
93 ec.register_connection(ccnd2, node2)
94
95 ## Register a repository in node 1
96 ccnr1 = ec.register_resource("LinuxCCNR")
97 ec.register_connection(ccnr1, ccnd1)
98
99 ## Push the file into the repository
100 local_path_to_content = os.path.join(
101         os.path.dirname(os.path.realpath(__file__)),
102             "..", "..",
103             "big_buck_bunny_240p_mpeg4_lq.ts")
104
105 co = ec.register_resource("LinuxCCNContent")
106 ec.set(co, "contentName", "ccnx:/test/FILE1")
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 ## Deploy all resources
113 ec.deploy()
114
115 ## Wait until node 1 and 2 are deployed, so we can retrieve the hostnames
116 ## of the nodes automatically allocated in planetlab
117 ec.wait_deployed([node1, node2])
118
119 ## Get the hostnames of the two PlanetLab nodes
120 hostname1 = ec.get(node1, "hostname")
121 print "hostname 1: ", hostname1
122 hostname2 = ec.get(node2, "hostname")
123 print "hostname 2: ", hostname2
124
125 # Register a FIB entry from node 1 to node 2
126 entry1 = ec.register_resource("LinuxFIBEntry")
127 ec.set(entry1, "host", hostname2)
128 ec.register_connection(entry1, ccnd1)
129
130 # Register a FIB entry from node 1 to node 2
131 entry2 = ec.register_resource("LinuxFIBEntry")
132 ec.set(entry2, "host", hostname1)
133 ec.register_connection(entry2, ccnd2)
134
135 ## Retrieve the file stored in node 1 from node 2
136 command = "ccncat ccnx:/test/FILE1"
137 app = ec.register_resource("LinuxCCNApplication")
138 ec.set(app, "command", command)
139 ec.register_connection(app, ccnd2)
140
141 # Register a collector to automatically collect the ccnd logs
142 # to a local directory
143 col1 = ec.register_resource("Collector")
144 ec.set(col1, "traceName", "stderr")
145 ec.set(col1, "subDir", hostname1)
146 ec.register_connection(col1, ccnd1)
147
148 col2 = ec.register_resource("Collector")
149 ec.set(col2, "traceName", "stderr")
150 ec.set(col2, "subDir", hostname2)
151 ec.register_connection(col2, ccnd2)
152
153 ## Deploy the rest of the resources
154 ec.deploy(guids=[entry1, entry2, app, col1, col2])
155
156 # Wait until the ccncat is finished
157 ec.wait_finished([app])
158
159 ## CCND logs will be collected to the results_dir upon shutdown.
160 ## We can aldo get the content of the logs now:
161 #print "LOG2", ec.trace(ccnd1, "stderr")
162 #print "LOG 1", ec.trace(ccnd2, "stderr")
163
164 ec.shutdown()
165