Adding Hello World example for Linux
[nepi.git] / examples / linux / hello_world.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21 # Example of how to run this experiment (replace with your information):
22 #
23 # $ cd <path-to-nepi>
24 # python examples/linux/hello_world.py -a <hostname> -u <username> -i <ssh-key>
25
26 from nepi.execution.ec import ExperimentController 
27
28 from optparse import OptionParser, SUPPRESS_HELP
29 import os
30
31 usage = ("usage: %prog -a <hostanme> -u <username> -i <ssh-key>")
32
33 parser = OptionParser(usage = usage)
34 parser.add_option("-a", "--hostname", dest="hostname", 
35         help="Remote host", type="str")
36 parser.add_option("-u", "--username", dest="username", 
37         help="Username to SSH to remote host", type="str")
38 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
39         help="Path to private SSH key to be used for connection", 
40         type="str")
41 (options, args) = parser.parse_args()
42
43 hostname = options.hostname
44 username = options.username
45 ssh_key = options.ssh_key
46
47 ec = ExperimentController(exp_id = "src-up-exp")
48         
49 node = ec.register_resource("LinuxNode")
50 ec.set(node, "hostname", hostname)
51 ec.set(node, "username", username)
52 ec.set(node, "identity", ssh_key)
53 ec.set(node, "cleanHome", True)
54 ec.set(node, "cleanProcesses", True)
55
56 path_to_sources = os.path.join(
57         os.path.dirname(os.path.realpath(__file__)),
58         "hello.c")
59
60 app = ec.register_resource("LinuxApplication")
61 ec.set(app, "sources", path_to_sources)
62 ec.set(app, "build", "gcc ${SRC}/hello.c -o ${BIN}/hello")
63 ec.set(app, "command", "${BIN}/hello")
64 ec.register_connection(node, app)
65
66 ec.deploy()
67
68 ec.wait_finished(app)
69
70 print ec.trace(app, "stdout")
71
72 ec.shutdown()
73