bf2543c62639b1c3783cbe67220f3629e9522063
[nepi.git] / examples / linux / ccn / vlc_2_hosts.py
1 #!/usr/bin/env python
2
3 #
4 #    NEPI, a framework to manage network experiments
5 #    Copyright (C) 2013 INRIA
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21
22 from nepi.execution.ec import ExperimentController, ECState 
23 from nepi.execution.resource import ResourceState, ResourceAction, \
24         populate_factory
25 from nepi.resources.linux.node import OSType
26
27 from optparse import OptionParser, SUPPRESS_HELP
28
29 import os
30 import time
31
32 def add_node(ec, host, user, ssh_key = None):
33     node = ec.register_resource("LinuxNode")
34     ec.set(node, "hostname", host)
35     ec.set(node, "username", user)
36     ec.set(node, "identity", ssh_key)
37     ec.set(node, "cleanHome", True)
38     ec.set(node, "cleanProcesses", True)
39     return node
40
41 def add_ccnd(ec, os_type, peers):
42     if os_type == OSType.FEDORA:
43         depends = ( " autoconf openssl-devel  expat-devel libpcap-devel "
44                 " ecryptfs-utils-devel libxml2-devel automake gawk " 
45                 " gcc gcc-c++ git pcre-devel make ")
46     else: # UBUNTU
47         depends = ( " autoconf libssl-dev libexpat-dev libpcap-dev "
48                 " libecryptfs0 libxml2-utils automake gawk gcc g++ "
49                 " git-core pkg-config libpcre3-dev make ")
50
51     sources = "http://www.ccnx.org/releases/ccnx-0.7.1.tar.gz"
52
53     build = (
54         # Evaluate if ccnx binaries are already installed
55         " ( "
56             "  test -f ${EXP_HOME}/ccnx/bin/ccnd"
57         " ) || ( "
58         # If not, untar and build
59             " ( "
60                 " mkdir -p ${SOURCES}/ccnx && "
61                 " tar xf ${SOURCES}/ccnx-0.7.1.tar.gz --strip-components=1 -C ${SOURCES}/ccnx "
62              " ) && "
63                 "cd ${SOURCES}/ccnx && "
64                 # Just execute and silence warnings...
65                 "( ./configure && make ) "
66          " )") 
67
68     install = (
69         # Evaluate if ccnx binaries are already installed
70         " ( "
71             "  test -f ${EXP_HOME}/ccnx/bin/ccnd"
72         " ) || ( "
73             "  mkdir -p ${EXP_HOME}/ccnx/bin && "
74             "  cp -r ${SOURCES}/ccnx ${EXP_HOME}"
75         " )"
76     )
77
78     env = "PATH=$PATH:${EXP_HOME}/ccnx/bin"
79
80     # BASH command -> ' ccndstart ; ccndc add ccnx:/ udp  host ;  ccnr '
81     command = "ccndstart && "
82     peers = map(lambda peer: "ccndc add ccnx:/ udp  %s" % peer, peers)
83     command += " ; ".join(peers) + " && "
84     command += " ccnr & "
85
86     app = ec.register_resource("LinuxApplication")
87     ec.set(app, "depends", depends)
88     ec.set(app, "sources", sources)
89     ec.set(app, "install", install)
90     ec.set(app, "build", build)
91     ec.set(app, "env", env)
92     ec.set(app, "command", command)
93
94     return app
95
96 def add_publish(ec, movie):
97     env = "PATH=$PATH:${EXP_HOME}/ccnx/bin"
98     command = "ccnseqwriter -r ccnx:/VIDEO"
99
100     app = ec.register_resource("LinuxApplication")
101     ec.set(app, "stdin", movie)
102     ec.set(app, "env", env)
103     ec.set(app, "command", command)
104
105     return app
106
107 def add_stream(ec):
108     env = "PATH=$PATH:${EXP_HOME}/ccnx/bin"
109     command = "sudo -S dbus-uuidgen --ensure ; ( ccncat ccnx:/VIDEO | vlc - ) "
110
111     app = ec.register_resource("LinuxApplication")
112     ec.set(app, "depends", "vlc")
113     ec.set(app, "forwardX11", True)
114     ec.set(app, "env", env)
115     ec.set(app, "command", command)
116
117     return app
118
119 def get_options():
120     slicename = os.environ.get("PL_SLICE")
121
122     # We use a specific SSH private key for PL if the PL_SSHKEY is specified or the
123     # id_rsa_planetlab exists 
124     default_key = "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'])
125     default_key = default_key if os.path.exists(default_key) else None
126     pl_ssh_key = os.environ.get("PL_SSHKEY", default_key)
127
128     usage = "usage: %prog -s <pl-slice> -u <username> -m <movie> -l <exp-id> -i <ssh_key>"
129
130     parser = OptionParser(usage=usage)
131     parser.add_option("-s", "--pl-slice", dest="pl_slice", 
132             help="PlanetLab slicename", default = slicename, type="str")
133     parser.add_option("-u", "--username", dest="username", 
134             help="User for extra host (non PlanetLab)", type="str")
135     parser.add_option("-m", "--movie", dest="movie", 
136             help="Stream movie", type="str")
137     parser.add_option("-l", "--exp-id", dest="exp_id", 
138             help="Label to identify experiment", type="str")
139     parser.add_option("-i", "--pl-ssh-key", dest="pl_ssh_key", 
140             help="Path to private SSH key to be used for connection", 
141             default = pl_ssh_key, type="str")
142
143     (options, args) = parser.parse_args()
144
145     if not options.movie:
146         parser.error("movie is a required argument")
147
148     return (options.pl_slice, options.username, options.movie, options.exp_id, 
149             options.pl_ssh_key)
150
151 if __name__ == '__main__':
152     ( pl_slice, username, movie, exp_id, pl_ssh_key ) = get_options()
153
154     # Search for available RMs
155     populate_factory()
156     
157     # PlanetLab node
158     host1 = 'planetlab2.u-strasbg.fr'
159     
160     # Another node 
161     # IMPORTANT NOTE: you must replace this host for another one
162     #       you have access to. You must set up your SSH keys so
163     #       the host can be accessed through SSH without prompting
164     #       for a password. The host must allow X forwarding using SSH.
165     host2 = 'roseval.pl.sophia.inria.fr'
166
167     # Create the ExperimentController instance
168     ec = ExperimentController(exp_id = exp_id)
169
170     # Register a ResourceManager (RM) for the PlanetLab node
171     node1 = add_node(ec, host1, pl_slice, pl_ssh_key)
172     
173     peers = [host2]
174     ccnd1 = add_ccnd(ec, OSType.FEDORA, peers)
175
176     ec.register_connection(ccnd1, node1)
177
178     pub = add_publish(ec, movie)
179     ec.register_connection(pub, node1)
180
181     # The movie can only be published after ccnd is running
182     ec.register_condition(pub, ResourceAction.START, 
183             ccnd1, ResourceState.STARTED)
184     
185     node2 = add_node(ec, host2, username)
186     peers = [host1]
187     ccnd2 = add_ccnd(ec, "ubuntu", peers)
188     ec.register_connection(ccnd2, node2)
189      
190     stream = add_stream(ec)
191     ec.register_connection(stream, node2)
192
193     # The stream can only be retrieved after ccnd is running
194     ec.register_condition(stream, ResourceAction.START, 
195             ccnd2, ResourceState.STARTED)
196
197     # And also, the stream can only be retrieved after it was published
198     ec.register_condition(stream, ResourceAction.START, 
199             pub, ResourceState.STARTED)
200  
201     # Deploy all ResourceManagers
202     ec.deploy()
203
204     # Wait until the applications are finished
205     apps = [ccnd1, pub, ccnd2, stream]
206     ec.wait_finished(apps)
207
208     # Shutdown the experiment controller
209     ec.shutdown()
210