Minor typos
[nepi.git] / examples / linux / ccnx / simple_topo.py
1 #!/usr/bin/env python
2 from neco.execution.ec import ExperimentController, ECState 
3 from neco.execution.resource import ResourceState, ResourceAction, \
4         populate_factory
5
6 from optparse import OptionParser, SUPPRESS_HELP
7
8 import os
9 import time
10
11 def add_node(ec, host, user):
12     node = ec.register_resource("LinuxNode")
13     ec.set(node, "hostname", host)
14     ec.set(node, "username", user)
15     #ec.set(node, "cleanHome", True)
16     ec.set(node, "cleanProcesses", True)
17     return node
18
19 def add_ccnd(ec, os_type, peers):
20     if os_type == "f12":
21         depends = ( " autoconf openssl-devel  expat-devel libpcap-devel "
22                 " ecryptfs-utils-devel libxml2-devel automake gawk " 
23                 " gcc gcc-c++ git pcre-devel ")
24     elif os_type == "ubuntu":
25         depends = ( " autoconf libssl-dev libexpat-dev libpcap-dev "
26                 " libecryptfs0 libxml2-utils automake gawk gcc g++ "
27                 " git-core pkg-config libpcre3-dev ")
28
29     sources = "http://www.ccnx.org/releases/ccnx-0.7.1.tar.gz"
30
31     build = (
32         # Evaluate if ccnx binaries are already installed
33         " ( "
34             "  test -d ${EXP_HOME}/ccnx/bin"
35         " ) || ( "
36         # If not, untar and build
37             " ( "
38                 " mkdir -p ${SOURCES}/ccnx && "
39                 " tar xf ${SOURCES}/ccnx-0.7.1.tar.gz --strip-components=1 -C ${SOURCES}/ccnx "
40              " ) && "
41                 "cd ${SOURCES}/ccnx && "
42                 # Just execute and silence warnings...
43                 "(  ( ./configure && make )  2>&1 )"
44          " )") 
45
46     install = (
47         # Evaluate if ccnx binaries are already installed
48         " ( "
49             "  test -d ${EXP_HOME}/ccnx/bin "
50         " ) || ( "
51             "  mkdir -p ${EXP_HOME}/ccnx/bin && "
52             "  cp -r ${SOURCES}/ccnx ${EXP_HOME}"
53         " )"
54     )
55
56     env = "PATH=$PATH:${EXP_HOME}/ccnx/bin"
57
58     # BASH command -> ' ccndstart 2>&1 ; ccndc add ccnx:/ udp  host ;  ccnr 2>&1 '
59     command = "ccndstart 2>&1 ; "
60     peers = map(lambda peer: "ccndc add ccnx:/ udp  %s" % peer, peers)
61     command += " ; ".join(peers) + " ; "
62     command += " ccnr 2>&1 "
63
64     app = ec.register_resource("LinuxApplication")
65     ec.set(app, "depends", depends)
66     ec.set(app, "sources", sources)
67     ec.set(app, "install", install)
68     ec.set(app, "build", build)
69     ec.set(app, "env", env)
70     ec.set(app, "command", command)
71
72     return app
73
74 def add_publish(ec, movie):
75     env = "PATH=$PATH:${EXP_HOME}/ccnx/bin"
76     command = "ccnseqwriter -r ccnx:/VIDEO"
77
78     app = ec.register_resource("LinuxApplication")
79     ec.set(app, "stdin", movie)
80     ec.set(app, "env", env)
81     ec.set(app, "command", command)
82
83     return app
84
85 def add_stream(ec):
86     env = "PATH=$PATH:${EXP_HOME}/ccnx/bin"
87     command = "sudo -S dbus-uuidgen --ensure ; ( ccncat ccnx:/VIDEO | vlc - ) 2>&1"
88
89     app = ec.register_resource("LinuxApplication")
90     ec.set(app, "depends", "vlc")
91     ec.set(app, "forwardX11", True)
92     ec.set(app, "env", env)
93     ec.set(app, "command", command)
94
95     return app
96
97 def get_options():
98     slicename = os.environ.get("PL_SLICE")
99
100     usage = "usage: %prog -s <pl-slice> -u <user-2> -m <movie> -l <exp-id>"
101
102     parser = OptionParser(usage=usage)
103     parser.add_option("-s", "--pl-slice", dest="pl_slice", 
104             help="PlanetLab slicename", default=slicename, type="str")
105     parser.add_option("-u", "--user-2", dest="user2", 
106             help="User for non PlanetLab machine", type="str")
107     parser.add_option("-m", "--movie", dest="movie", 
108             help="Stream movie", type="str")
109     parser.add_option("-l", "--exp-id", dest="exp_id", 
110             help="Label to identify experiment", type="str")
111
112     (options, args) = parser.parse_args()
113
114     if not options.movie:
115         parser.error("movie is a required argument")
116
117     return (options.pl_slice, options.user2, options.movie, options.exp_id)
118
119 if __name__ == '__main__':
120     ( pl_slice, user2, movie, exp_id ) = get_options()
121
122     # Search for available RMs
123     populate_factory()
124     
125     #host1 = 'nepi2.pl.sophia.inria.fr'
126     host1 = 'planetlab2.u-strasbg.fr'
127     host2 = 'roseval.pl.sophia.inria.fr'
128
129     ec = ExperimentController(exp_id = exp_id)
130
131     node1 = add_node(ec, host1, pl_slice)
132     
133     peers = [host2]
134     ccnd1 = add_ccnd(ec, "f12", peers)
135
136     ec.register_connection(ccnd1, node1)
137
138     pub = add_publish(ec, movie)
139     ec.register_connection(pub, node1)
140     # The movie can only be published after ccnd is running
141     ec.register_condition(pub, ResourceAction.START, 
142             ccnd1, ResourceState.STARTED)
143     
144     node2 = add_node(ec, host2, user2)
145     peers = [host1]
146     ccnd2 = add_ccnd(ec, "ubuntu", peers)
147     ec.register_connection(ccnd2, node2)
148      
149     stream = add_stream(ec)
150     ec.register_connection(stream, node2)
151     # The stream can only be retrieved after ccnd is running
152     ec.register_condition(stream, ResourceAction.START, 
153             ccnd2, ResourceState.STARTED)
154     # And also, the stream can only be retrieved after it was published
155     ec.register_condition(stream, ResourceAction.START, 
156             pub, ResourceState.STARTED)
157  
158     ec.deploy()
159
160     apps = [ccnd1, pub, ccnd2, stream]
161     ec.wait_finished(apps)
162
163     ec.shutdown()
164