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