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