Patching identity for gateway in sshfuncs. Patch provided by Damien Saucez
[nepi.git] / examples / omf / nitos_testbed_bootstrap.py
1 #\r
2 #    NEPI, a framework to manage network experiments\r
3 #    Copyright (C) 2014 INRIA\r
4 #\r
5 #    This program is free software: you can redistribute it and/or modify\r
6 #    it under the terms of the GNU General Public License as published by\r
7 #    the Free Software Foundation, either version 3 of the License, or\r
8 #    (at your option) any later version.\r
9 #\r
10 #    This program is distributed in the hope that it will be useful,\r
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 #    GNU General Public License for more details.\r
14 #\r
15 #    You should have received a copy of the GNU General Public License\r
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
17 #\r
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>\r
19 #         Maksym Gabielkov <maksym.gabielkovc@inria.fr>\r
20 #\r
21 \r
22 ## This is a maintenance script used to bootstrap the nodes from\r
23 ## Nitos testbed (NITLab) before running a OMF experiment using\r
24 ## Nitos nodes. This fixes the problem of Resource Controller \r
25 ## misbehaving by restarting it and it also loads the ath5k driver.\r
26 \r
27 # Example of how to run this experiment (replace with your information):\r
28 #\r
29 # $ cd <path-to-nepi>\r
30 # python examples/linux/nitos_testbed_bootstrap.py -H <omf.nitos.node0XX,omf.nitos.node0ZZ,..> -u <nitos-username> -i <ssh-key> -g <nitos-gateway> -U <nitos-gateway-username>\r
31 #\r
32 \r
33 from nepi.execution.ec import ExperimentController\r
34 from nepi.execution.resource import ResourceAction, ResourceState\r
35 \r
36 from optparse import OptionParser\r
37 import os\r
38 \r
39 usage = ("usage: %prog -H <list-of-nitos-hosts> -u <nitos-username> -i <ssh-key> -g <nitos-gateway> -U <nitos-gateway-username>")\r
40 \r
41 parser = OptionParser(usage = usage)\r
42 parser.add_option("-H", "--hosts", dest="hosts", \r
43         help="Space separated list of hosts", type="str")\r
44 parser.add_option("-u", "--username", dest="username", \r
45         help="Username for the nitos hosts (usually root)", \r
46         type="str", default="root" )\r
47 parser.add_option("-g", "--gateway", dest="gateway", \r
48         help="Nitos gateway hostname", \r
49         type="str", default="nitlab.inf.uth.gr")\r
50 parser.add_option("-U", "--gateway-user", dest="gateway_username", \r
51         help="Nitos gateway username", \r
52         type="str", default="nitlab.inf.uth.gr")\r
53 parser.add_option("-i", "--ssh-key", dest="ssh_key", \r
54         help="Path to private SSH key to be used for connection", \r
55         type="str")\r
56 (options, args) = parser.parse_args()\r
57 \r
58 hosts = options.hosts\r
59 username = options.username\r
60 gateway = options.gateway\r
61 gateway_username = options.gateway_username\r
62 identity = options.ssh_key\r
63 \r
64 apps = []\r
65 \r
66 ec = ExperimentController(exp_id="nitos_bootstrap")\r
67 \r
68 gw_node = ec.register_resource("linux::Node")\r
69 ec.set(gw_node, "username", gateway_username)\r
70 ec.set(gw_node, "hostname", gateway)\r
71 ec.set(gw_node, "identity", identity)\r
72 ec.set(gw_node, "cleanExperiment", True)\r
73 \r
74 load_cmd = "omf load -i nepi_OMF6_VLC_baseline_grid.ndz -t %s" % hosts \r
75 load_app = ec.register_resource("linux::Application")\r
76 ec.set(load_app, "command", load_cmd)\r
77 ec.register_connection(load_app, gw_node)\r
78 \r
79 reboot_cmd = "omf tell -a on -t %s" % hosts \r
80 reboot_app = ec.register_resource("linux::Application")\r
81 ec.set(reboot_app, "command", reboot_cmd)\r
82 ec.register_connection(reboot_app, gw_node)\r
83 \r
84 ec.register_condition(reboot_app, ResourceAction.START, load_app, \r
85             ResourceState.STOPPED, time="20s") \r
86 \r
87 hosts = hosts.split(",")\r
88 \r
89 for hostname in hosts:\r
90     host = hostname.split(".")[-1]\r
91     node = ec.register_resource("linux::Node")\r
92     ec.set(node, "username", username)\r
93     ec.set(node, "hostname", host)\r
94     ec.set(node, "identity", identity)\r
95     ec.set(node, "gateway", gateway)\r
96     ec.set(node, "gatewayUser", gateway_username)\r
97     ec.set(node, "cleanExperiment", True)\r
98     ec.register_condition(node, ResourceAction.DEPLOY, reboot_app, \r
99             ResourceState.STOPPED, time="30s") \r
100  \r
101     app = ec.register_resource("linux::Application")\r
102     ec.set(app, "command", "modprobe ath5k && ip a | grep wlan0 && service omf_rc restart")\r
103     ec.register_connection(app, node)\r
104    \r
105     apps.append(app)\r
106 \r
107 ec.deploy(wait_all_ready=False)\r
108 \r
109 ec.wait_finished(apps)\r
110 \r
111 print ec.trace(load_app, "stdout")\r
112 print ec.trace(reboot_app, "stdout")\r
113 \r
114 for app in apps:\r
115     print ec.trace(app, "stdout")\r
116 \r
117 ec.shutdown()\r
118 \r