use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / omf / nitos_testbed_bootstrap.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 #         Maksym Gabielkov <maksym.gabielkovc@inria.fr>
20 #
21
22 ## This is a maintenance script used to bootstrap the nodes from
23 ## Nitos testbed (NITLab) before running a OMF experiment using
24 ## Nitos nodes. This fixes the problem of Resource Controller 
25 ## misbehaving by restarting it and it also loads the ath5k driver.
26
27 # Example of how to run this experiment (replace with your information):
28 #
29 # $ cd <path-to-nepi>
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>
31 #
32
33 from __future__ import print_function
34
35 from nepi.execution.ec import ExperimentController
36 from nepi.execution.resource import ResourceAction, ResourceState
37
38 from optparse import OptionParser
39 import os
40
41 usage = ("usage: %prog -H <list-of-nitos-hosts> -U <node-username> -i <ssh-key> -g <nitos-gateway> -u <slicename>")
42
43 parser = OptionParser(usage = usage)
44 parser.add_option("-H", "--hosts", dest="hosts", 
45         help="Space separated list of hosts", type="str")
46 parser.add_option("-U", "--username", dest="username", 
47         help="Username for the nitos hosts (usually root)", 
48         type="str", default="root" )
49 parser.add_option("-g", "--gateway", dest="gateway", 
50         help="Nitos gateway hostname", 
51         type="str", default="nitlab.inf.uth.gr")
52 parser.add_option("-u", "--gateway-user", dest="gateway_username", 
53         help="Nitos gateway username (slicename)", 
54         type="str", default="nitlab.inf.uth.gr")
55 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
56         help="Path to private SSH key to be used for connection", 
57         type="str")
58 (options, args) = parser.parse_args()
59
60 hosts = options.hosts
61 username = options.username
62 gateway = options.gateway
63 gateway_username = options.gateway_username
64 identity = options.ssh_key
65
66 apps = []
67
68 ec = ExperimentController(exp_id="nitos_bootstrap")
69
70 gw_node = ec.register_resource("linux::Node")
71 ec.set(gw_node, "username", gateway_username)
72 ec.set(gw_node, "hostname", gateway)
73 ec.set(gw_node, "identity", identity)
74 ec.set(gw_node, "cleanExperiment", True)
75
76 load_cmd = "omf load -i nepi_OMF6_VLC_baseline_grid.ndz -t %s" % hosts 
77 load_app = ec.register_resource("linux::Application")
78 ec.set(load_app, "command", load_cmd)
79 ec.register_connection(load_app, gw_node)
80
81 reboot_cmd = "omf tell -a on -t %s" % hosts 
82 reboot_app = ec.register_resource("linux::Application")
83 ec.set(reboot_app, "command", reboot_cmd)
84 ec.register_connection(reboot_app, gw_node)
85
86 ec.register_condition(reboot_app, ResourceAction.START, load_app, 
87             ResourceState.STOPPED, time="60s") 
88
89 hosts = hosts.split(",")
90
91 for hostname in hosts:
92     host = hostname.split(".")[-1]
93     node = ec.register_resource("linux::Node")
94     ec.set(node, "username", username)
95     ec.set(node, "hostname", host)
96     ec.set(node, "identity", identity)
97     ec.set(node, "gateway", gateway)
98     ec.set(node, "gatewayUser", gateway_username)
99     ec.set(node, "cleanExperiment", True)
100     ec.register_condition(node, ResourceAction.DEPLOY, reboot_app, 
101             ResourceState.STOPPED, time="300s") 
102  
103     modprobe_app = ec.register_resource("linux::Application")
104     ec.set(modprobe_app, "command", "modprobe ath5k && ip a | grep wlan0")
105     ec.register_connection(modprobe_app, node)
106     apps.append(modprobe_app)
107
108     rc_app = ec.register_resource("linux::Application")
109     ec.set(rc_app, "command", "service omf_rc stop; service omf_rc start")
110     ec.register_connection(rc_app, node)
111     apps.append(rc_app)
112
113 print("This might take time...")
114
115 ec.deploy(wait_all_ready=False)
116
117 ec.wait_finished(apps)
118
119 print(ec.trace(load_app, "stdout"))
120 print(ec.trace(reboot_app, "stdout"))
121
122 for app in apps:
123     print(ec.trace(app, "stdout"))
124
125 ec.shutdown()
126