aboutsummaryrefslogtreecommitdiff
path: root/rumba/testbeds/emulab.py
blob: f0c0350b48a5ec9adc5f83a859db2274511bb5e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#
# A library to manage ARCFIRE experiments
#
#    Copyright (C) 2017-2018 Nextworks S.r.l.
#    Copyright (C) 2017-2018 imec
#
#    Sander Vrijders   <sander.vrijders@ugent.be>
#    Dimitri Staessens <dimitri.staessens@ugent.be>
#    Vincenzo Maffione <v.maffione@nextworks.it>
#    Marco Capitani    <m.capitani@nextworks.it>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., http://www.fsf.org/about/contact/.
#

import os
import time
import re
from ast import literal_eval
import warnings

import rumba.ssh_support as ssh
import rumba.model as mod
import rumba.log as log

from rumba.executors.ssh import SSHExecutor


logger = log.get_logger(__name__)


warnings.filterwarnings("ignore")

class Testbed(mod.Testbed):
    """
    Represents an emulab testbed.
    """
    def __init__(self, exp_name, username, password="",
                 proj_name="ARCFIRE", url="wall2.ilabt.iminds.be",
                 image="UBUNTU14-64-STD"):
        """
        Initializes the testbed class.

        :param exp_name: The experiment name.
        :param username: User of the experiment.
        :param password: Password of the user.
        :param proj_name: Project name of the experiment.
        :param url: URL of the testbed.
        :param image: Specific image to use.
        """
        mod.Testbed.__init__(self, exp_name, username, password, proj_name)
        self.url = url
        self.image = image
        self.ip = dict()
        self.ops_ssh_config = mod.SSHConfig(self._ops_server())

        self.executor = SSHExecutor

        if "wall" in url:
            self.http_proxy="https://proxy.atlantis.ugent.be:8080"

    def _ops_server(self):
        """
        Return server name of the ops-server (is testbed specific).

        :return: Server name of the ops-server.
        """
        return 'ops.' + self.url

    def _full_name(self, node_name):
        """
        Return server name of a node.

        :param node_name: Name of the node.
        :return: Server name of the node.
        """
        return node_name + '.' + self.exp_name + '.' + \
            self.proj_name + '.' + self.url

    def _get_experiment_list(self, project_name=None):
        """
        Get list of made emulab experiments accessible with your credentials.

        :param project_name: Optional filter on project.
        :return: List of created experiments.
        """
        cmd = '/usr/testbed/bin/sslxmlrpc_client.py -m experiment getlist'
        out = ssh.execute_command(self, self.ops_ssh_config, cmd)

        try:
            if project_name is not None:
                return literal_eval(out)[project_name][project_name]
            else:
                return literal_eval(out)
        except:
            return {project_name: {project_name: []}}

    def _swap_exp_in(self):
        """
        Swaps experiment in.

        :return: Is the experiment newly swapped in?
        """
        cmd = '/usr/testbed/bin/sslxmlrpc_client.py swapexp proj=' + \
              self.proj_name + \
              ' exp=' + \
              self.exp_name + \
              ' direction=in'

        try:
            ssh.execute_command(self, self.ops_ssh_config, cmd)
        except ssh.SSHException as e:
            line = re.findall(r'not swapped out', str(e))
            if line:
                logger.info("Experiment is already swapped in.")
                return False
            else:
                raise e

        return True

    def _create_experiment(self, experiment):
        """
        Creates an emulab experiment

        :param experiment: The experiment.
        """
        proj_name = self.proj_name
        exp_name = self.exp_name

        exp_list = self._get_experiment_list()

        try:
            if exp_name in exp_list[proj_name][proj_name]:
                logger.info("Experiment already exists.")
                return
        except:
            logger.info("First experiment to be created for that project.")

        ns = self._generate_ns_script(experiment)
        dest_file_name = '/users/' + self.username + \
                         '/temp_ns_file.%s.ns' % os.getpid()
        ssh.write_text_to_file(self, self.ops_ssh_config, ns, dest_file_name)

        cmd = '/usr/testbed/bin/sslxmlrpc_client.py startexp ' + \
              'batch=false wait=true proj="' + proj_name + \
              '" exp="' + exp_name + '" noswapin=true ' + \
              'nsfilepath="' + dest_file_name + '"'

        try:
            ssh.execute_command(self, self.ops_ssh_config, cmd, time_out=None)
            logger.info("New experiment succesfully created.")
        except:
            logger.info("Experiment already exists.")
        finally:
            ssh.execute_command(self, self.ops_ssh_config,
                                'rm ' + dest_file_name)

    def _generate_ns_script(self, experiment):
        """
        Generate ns script based on network graph.
        Enables to customize default node image.

        :param experiment: The experiment.
        :return: ns2 script for Emulab experiment.
        """

        ns2_script = "# ns script generated by Rumba\n"
        ns2_script += "set ns [new Simulator]\n"
        ns2_script += "source tb_compat.tcl\n"

        for node in experiment.nodes:
            ns2_script += "set " + node.name + " [$ns node]\n"
            ns2_script += "tb-set-node-os $" + node.name + " " + \
                          self.image + "\n"

        for dif in experiment.dif_ordering:
            if isinstance(dif, mod.ShimEthDIF):
                if len(dif.ipcps) != 2:
                    continue
                ns2_script += "set " + dif.name + \
                              " [$ns duplex-link $" + \
                              dif.members[0].name + " $" + \
                              dif.members[1].name + " 1000Mb 0ms DropTail]\n"

        ns2_script += "$ns run\n"

        return ns2_script

    def _wait_until_nodes_up(self):
        """
        Checks if nodes are up.
        """
        logger.info("Waiting until all nodes are up")

        cmd = '/usr/testbed/bin/script_wrapper.py expinfo -e' + \
              self.proj_name + \
              ',' + \
              self.exp_name + \
              ' -a | grep State | cut -f2,2 -d " "'

        res = ssh.execute_command(self, self.ops_ssh_config, cmd)
        active = False
        if res == "active":
            active = True
        while not active:
            res = ssh.execute_command(self, self.ops_ssh_config, cmd)
            if res == "active":
                active = True
            logger.info("Still waiting")
            time.sleep(5)

    def _complete_experiment_graph(self, experiment):
        """
        Gets the interface (ethx) to link mapping.

        :param experiment: The experiment.
        """

        for node in experiment.nodes:
            node.ssh_config.hostname = self._full_name(node.name)
            node.ssh_config.set_username(self.username)
            node.ssh_config.set_password(self.password)

        cmd = 'cat /var/emulab/boot/topomap'
        topomap = node.execute_command(cmd)
        # Almost as ugly as yo momma
        index = topomap.rfind("# lans")
        topo_array = topomap[:index].split('\n')[1:-1]
        # Array contains things like 'r2b1,link7:10.1.6.3 link6:10.1.5.3'
        for item in topo_array:
            item_array = re.split(',? ?', item)
            node_name = item_array[0]
            for item2 in item_array[1:]:
                item2 = item2.split(':')
                link_name = item2[0]
                link_ip = item2[1]
                for node in experiment.nodes:
                    if node.name != node_name:
                        continue
                    for ipcp in node.ipcps:
                        if ipcp.dif.name == link_name:
                            self.ip[ipcp] = link_ip

        for node in experiment.nodes:
            cmd = 'cat /var/emulab/boot/ifmap'
            output = node.execute_command(cmd)
            output = re.split('\n', output)
            for item in output:
                item = item.split()
                for ipcp in node.ipcps:
                    if isinstance(ipcp, mod.ShimEthIPCP):
                        if self.ip[ipcp] == item[1]:
                            ipcp.ifname = item[0]

    def _swap_in(self, experiment):
        """
        Swaps experiment in

        :param experiment: The experiment.
        """
        self._create_experiment(experiment)
        wait = self._swap_exp_in()
        if wait:
            self._wait_until_nodes_up()
        self._complete_experiment_graph(experiment)

    def _swap_out(self, experiment):
        """
        Swaps experiment out

        :param experiment: The experiment.
        """
        cmd = '/usr/testbed/bin/sslxmlrpc_client.py swapexp proj=' + \
              self.proj_name + \
              ' exp=' + \
              self.exp_name + \
              ' direction=out'

        ssh.execute_command(self, self.ops_ssh_config, cmd)