use ArgumentParser instead of OptionParser in ping.py - make hostname required
[nepi.git] / examples / linux / ping.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 version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 # Example of how to run this experiment (replace with your information):
21 #
22 # $ cd <path-to-nepi>
23 # python examples/linux/ping.py -a <hostname> -u <username> -i <ssh-key>
24
25 from __future__ import print_function
26
27 from nepi.execution.ec import ExperimentController 
28
29 # SUPPRESS_HELP was not used here, but:
30 # to suppress an arg from the help, use
31 # add_argument( help=argparse.SUPPRESS)
32 from argparse import ArgumentParser
33 import os
34
35 parser = ArgumentParser()
36 parser.add_argument("-u", "--username", dest="username", 
37                     help="Username to SSH to remote host")
38 parser.add_argument("-i", "--ssh-key", dest="ssh_key", 
39                     help="Path to private SSH key to be used for connection")
40 # this is required
41 parser.add_argument("hostname", type=str)
42
43 args = parser.parse_args()
44
45 hostname = args.hostname
46 username = args.username
47 ssh_key = args.ssh_key
48
49 ec = ExperimentController(exp_id = "ping-exp")
50         
51 node = ec.register_resource("linux::Node")
52 ec.set(node, "hostname", hostname)
53 ec.set(node, "username", username)
54 ec.set(node, "identity", ssh_key)
55 ec.set(node, "cleanExperiment", True)
56 ec.set(node, "cleanProcesses", True)
57
58 app = ec.register_resource("linux::Application")
59 ec.set(app, "command", "ping -c3 nepi.inria.fr")
60 ec.register_connection(app, node)
61
62 ec.deploy()
63
64 ec.wait_finished(app)
65
66 print(ec.trace(app, "stdout"))
67
68 ec.shutdown()
69