014d3eb6db281ab4249770a6a3ce225e49f91d71
[nepi.git] / examples / omf / nitos_testbed_bootstrap.py
1 #!/usr/bin/env python\r
2 #\r
3 #    NEPI, a framework to manage network experiments\r
4 #    Copyright (C) 2013 INRIA\r
5 #\r
6 #    This program is free software: you can redistribute it and/or modify\r
7 #    it under the terms of the GNU General Public License version 2 as\r
8 #    published by the Free Software Foundation;\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 <node-username> -i <ssh-key> -g <nitos-gateway> -u <nitos-slice>\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 <node-username> -i <ssh-key> -g <nitos-gateway> -u <slicename>")\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 (slicename)", \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="60s") \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="300s") \r
100  \r
101     modprobe_app = ec.register_resource("linux::Application")\r
102     ec.set(modprobe_app, "command", "modprobe ath5k && ip a | grep wlan0")\r
103     ec.register_connection(modprobe_app, node)\r
104     apps.append(modprobe_app)\r
105 \r
106     rc_app = ec.register_resource("linux::Application")\r
107     ec.set(rc_app, "command", "service omf_rc stop; service omf_rc start")\r
108     ec.register_connection(rc_app, node)\r
109     apps.append(rc_app)\r
110 \r
111 print "This might take time..."\r
112 \r
113 ec.deploy(wait_all_ready=False)\r
114 \r
115 ec.wait_finished(apps)\r
116 \r
117 print ec.trace(load_app, "stdout")\r
118 print ec.trace(reboot_app, "stdout")\r
119 \r
120 for app in apps:\r
121     print ec.trace(app, "stdout")\r
122 \r
123 ec.shutdown()\r
124 \r