aboutsummaryrefslogtreecommitdiff
path: root/rumba/prototypes/irati.py
blob: 0d4058007e0509e987281807a27eea692b639632 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#
# Commands to setup and instruct IRATI
#
#    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., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301  USA
import copy
import json

import subprocess

import os

import rumba.ssh_support as ssh
import rumba.model as mod
import rumba.prototypes.irati_templates as irati_templates


# An experiment over the IRATI implementation
from rumba import ssh_support


class Experiment(mod.Experiment):

    @staticmethod
    def real_sudo(s):
        return 'sudo ' + s

    @staticmethod
    def fake_sudo(s):
        return s

    def __init__(self, testbed, nodes=None):
        mod.Experiment.__init__(self, testbed, nodes)
        self.manager = False
        self.conf_files = None
        self.shim2vlan = {}

        if self.testbed.username == 'root':
            self.sudo = self.fake_sudo
        else:
            self.sudo = self.real_sudo

        self._conf_dir = os.path.join(os.getcwd(), 'IRATI_conf')
        try:
            os.mkdir(self._conf_dir)
        except OSError:
            # Already there, nothing to do
            pass

    def conf_dir(self, path):
        return os.path.join(self._conf_dir, path)

    def setup(self):
        """Installs IRATI on the vms."""
        setup_irati = False
        if setup_irati:
            cmds = list()

            cmds.append("sudo apt-get update")
            cmds.append("sudo apt-get install g++ gcc "
                        "protobuf-compiler libprotobuf-dev git --yes")
            cmds.append("sudo rm -rf ~/irati")
            cmds.append("cd && git clone https://github.com/IRATI/stack irati")
            cmds.append("cd ~/irati && sudo ./install-from-scratch")
            cmds.append("sudo nohup ipcm &> ipcm.log &")

            for node in self.nodes:
                ssh.execute_commands(self.testbed, node.ssh_config,
                                     cmds, time_out=None)

    def bootstrap_network(self):
        """Creates the network by enrolling and configuring the nodes"""
        for node in self.nodes:
            self.process_node(node)
        self.enroll_nodes()

    def run_prototype(self):
        print("irati: setting up")
        self.setup()
        print("irati: software initialized on all nodes")
        self.conf_files = self.write_conf()
        print("irati: configuration files generated for all nodes")
        self.bootstrap_network()
        print("irati: IPCPs created and enrolled on all nodes")

    def process_node(self, node):
        """
        Installs the configuration and boots up rina on a node
        :type node: mod.Node
        :param node: 
        :return: 
        """
        name = node.name

        for ipcp in node.ipcps:
            if isinstance(ipcp, mod.ShimEthIPCP):
                if_name = ipcp.ifname
                vlan = self.shim2vlan[ipcp.dif.name]
                ssh_support.setup_vlan(self.testbed, node,
                                       vlan, if_name)

        gen_files_conf = self.conf_files[node] + ['da.map']
        dir_path = os.path.dirname(os.path.abspath(__file__))
        gen_files_bin = 'enroll.py'
        gen_files_conf_full = [self.conf_dir(x) for x in gen_files_conf]
        gen_files_bin_full = [os.path.join(dir_path, 'enroll.py')]

        ipcm_components = ['scripting', 'console']
        if self.manager:
            ipcm_components.append('mad')
        ipcm_components = ', '.join(ipcm_components)

        gen_files = gen_files_conf_full + gen_files_bin_full

        format_args = {'name': name,
                       'ssh': node.ssh_config.port,
                       'username': self.testbed.username,
                       'genfiles': gen_files,
                       'genfilesconf': ' '.join(gen_files_conf),
                       'genfilesbin': gen_files_bin,
                       'installpath': '/usr',
                       'verb': 'DBG',
                       'ipcmcomps': ipcm_components}

        # TODO: watch out for empty path...
        ssh.copy_paths_to_testbed(self.testbed,
                                  node.ssh_config,
                                  gen_files,
                                  '')

        cmds = [self.sudo('hostname %(name)s' % format_args),
                self.sudo('chmod a+rw /dev/irati'),
                self.sudo('mv %(genfilesconf)s /etc' % format_args),
                self.sudo('mv %(genfilesbin)s /usr/bin') % format_args,
                self.sudo('chmod a+x /usr/bin/enroll.py') % format_args]

        cmds += [self.sudo('modprobe rina-default-plugin'),
                 self.sudo('%(installpath)s/bin/ipcm -a \"%(ipcmcomps)s\" '
                           '-c /etc/%(name)s.ipcm.conf -l %(verb)s &> log &'
                           % format_args)]

        print('DEBUG: sending node setup via ssh.')
        # print('Credentials:')
        # print(node.ssh_config.hostname, node.ssh_config.port,
        #       self.testbed.username, self.testbed.password)
        ssh.execute_commands(self.testbed, node.ssh_config, cmds)

    def enroll_nodes(self):
        """Runs the enrollments one by one, respecting dependencies"""
        for enrollment_list in self.enrollments:
            for e in enrollment_list:
                print(
                    'I am going to enroll %s to DIF %s against neighbor %s,'
                    ' through lower DIF %s'
                    % (e['enrollee'].name,
                       e['dif'].name,
                       e['enroller'].name,
                       e['lower_dif'].name))

                subprocess.check_call('sleep 2'. split())  # Important!

                e_args = {'ldif': self.dif_name(e['lower_dif']),
                          'dif': e['dif'].name,
                          'name': e['enrollee'].name,
                          'o_name': e['enroller'].name}

                cmd = self.sudo('enroll.py --lower-dif %(ldif)s --dif %(dif)s '
                                '--ipcm-conf /etc/%(name)s.ipcm.conf '
                                '--enrollee-name %(dif)s.%(name)s.IPCP '
                                '--enroller-name %(dif)s.%(o_name)s.IPCP'
                                % e_args)
                print('DEBUG: sending enrollment operation via ssh.')
                # print('Credentials:')
                # print(e['enrollee'].ssh_config.hostname,
                #       e['enrollee'].ssh_config.port,
                #       self.testbed.username, self.testbed.password)
                ssh.execute_command(self.testbed,
                                    e['enrollee'].ssh_config,
                                    cmd)

    def dif_name(self, dif):
        try:
            return str(self.shim2vlan[dif.name])
        except KeyError:
            return dif.name

    def write_conf(self):
        """Write the configuration files"""
        # Constants and initializations
        ipcmconfs = dict()
        difconfs = dict()
        ipcp2shim_map = {}
        node2id_map = {}
        mgmt_dif_name = 'NMS'
        conf_files = {}  # dict of per-nod conf files

        # Translating Shim Eth difs to vlan tags.
        next_vlan = 10
        for dif in self.dif_ordering:
            if isinstance(dif, mod.ShimEthDIF):
                try:
                    vlan = int(dif.name)
                    self.shim2vlan[dif.name] = vlan
                except ValueError:
                    vlan = next_vlan
                    next_vlan += 10
                    self.shim2vlan[dif.name] = vlan

        # TODO: what format are the mappings registered in? Is this ok?
        app_mappings = []
        for node in self.nodes:
            app_mappings += [{'name': app, 'dif': self.dif_name(dif)}
                             for app in node.registrations
                             for dif in node.registrations[app]]

        # If some app directives were specified, use those to build da.map.
        # Otherwise, assume the standard applications are to be mapped in
        # the DIF with the highest rank.
        if len(app_mappings) == 0:
            if len(self.dif_ordering) > 0:
                for adm in \
                        irati_templates.da_map_base["applicationToDIFMappings"]:
                    adm["difName"] = "%s" % (self.dif_ordering[-1],)
        else:
            irati_templates.da_map_base["applicationToDIFMappings"] = []
            for apm in app_mappings:
                irati_templates.da_map_base["applicationToDIFMappings"]\
                    .append({"encodedAppName": apm['name'],
                             "difName": "%s" % (apm['dif'])
                             })

        # TODO ask: I guess this will need to be added,
        # and in that case we should add it to the qemu plugin too...
        # Where should we take it in input?

        if self.manager:
            # Add MAD/Manager configuration
            irati_templates.ipcmconf_base["addons"] = {
                "mad": {
                    "managerAppName": "",
                    "NMSDIFs": [{"DIF": "%s" % mgmt_dif_name}],
                    "managerConnections": [{
                        "managerAppName": "manager-1--",
                        "DIF": "%s" % mgmt_dif_name
                    }]
                }
            }

        node_number = 1
        for node in self.nodes:  # type: mod.Node
            node2id_map[node.name] = node_number
            node_number += 1
            ipcmconfs[node.name] = copy.deepcopy(irati_templates.ipcmconf_base)
            if self.manager:
                ipcmconfs[node.name]["addons"]["mad"]["managerAppName"] \
                    = "%s.mad-1--" % (node.name,)

        for dif in self.dif_ordering:  # type: mod.DIF
            if isinstance(dif, mod.ShimEthDIF):
                ipcp2shim_map.update({ipcp.name: dif for ipcp in dif.ipcps})
            elif isinstance(dif, mod.NormalDIF):
                difconfs[dif.name] = dict()
                for node in dif.members:
                    difconfs[dif.name][node.name] = copy.deepcopy(
                        irati_templates.normal_dif_base
                    )

        for node in self.nodes:  # type: mod.Node
            ipcmconf = ipcmconfs[node.name]

            for ipcp in node.ipcps:  # type: mod.ShimEthIPCP
                if isinstance(ipcp, mod.ShimEthIPCP):
                    shim = ipcp2shim_map[ipcp.name]  # type: mod.ShimEthDIF
                    shim_name = self.dif_name(shim)
                    ipcmconf["ipcProcessesToCreate"].append({
                        "apName": "eth.%s.IPCP" % ipcp.name,
                        "apInstance": "1",
                        "difName": shim_name
                    })


                    template_file_name = self.conf_dir(
                        'shimeth.%s.%s.dif'
                        % (node.name, shim_name))
                    ipcmconf["difConfigurations"].append({
                        "name": shim_name,
                        "template": os.path.basename(template_file_name)
                    })

                    fout = open(template_file_name, 'w')
                    fout.write(json.dumps(
                        {"difType": "shim-eth-vlan",
                         "configParameters": {
                             "interface-name": ipcp.ifname
                         }
                         },
                        indent=4, sort_keys=True))
                    fout.close()
                    conf_files.setdefault(node, []).append(
                        'shimeth.%s.%s.dif'
                        % (node.name, shim_name))

        # Run over dif_ordering array, to make sure each IPCM config has
        # the correct ordering for the ipcProcessesToCreate list of operations.
        # If we iterated over the difs map, the order would be randomic, and so
        # some IPCP registrations in lower DIFs may fail.
        #  This would happen because at the moment of registration,
        #  it may be that the IPCP of the lower DIF has not been created yet.
        shims = ipcp2shim_map.values()
        for dif in self.dif_ordering:  # type: mod.NormalDIF

            if dif in shims:
                # Shims are managed separately, in the previous loop
                continue

            for node in dif.members:  # type: mod.Node
                node_name = node.name
                ipcmconf = ipcmconfs[node_name]

                normal_ipcp = {"apName": "%s.%s.IPCP" % (dif.name, node_name),
                               "apInstance": "1",
                               "difName": "%s" % (dif.name,),
                               "difsToRegisterAt": []}

                for lower_dif in node.dif_registrations[dif]:  # type: mod.DIF
                    normal_ipcp["difsToRegisterAt"].append(
                        self.dif_name(lower_dif))

                ipcmconf["ipcProcessesToCreate"].append(normal_ipcp)

                ipcmconf["difConfigurations"].append({
                    "name": "%s" % (dif.name,),
                    "template": "normal.%s.%s.dif" % (node_name, dif.name,)
                })

                # Fill in the map of IPCP addresses.
                # This could be moved at difconfs
                for other_node in dif.members:  # type: mod.Node
                    difconfs[dif.name][other_node.name] \
                        ["knownIPCProcessAddresses"].append({
                         "apName": "%s.%s.IPCP" % (dif.name, node_name),
                         "apInstance": "1",
                         "address": 16 + node2id_map[node_name]})
                for path, ps in dif.policies.items():
                    # if policy['nodes'] == [] or vmname in policy['nodes']:
                    # TODO: manage per-node-policies
                    irati_templates.translate_policy(
                        difconfs[dif.name][node_name], path, ps, parms=[])

        # Dump the DIF Allocator map
        with open(self.conf_dir('da.map'), 'w') as da_map_file:
            json.dump(irati_templates.da_map_base,
                      da_map_file,
                      indent=4,
                      sort_keys=True)

        for node in self.nodes:
            # Dump the IPCM configuration files
            with open(self.conf_dir('%s.ipcm.conf'
                                    % (node.name,)), 'w') as node_file:
                json.dump(ipcmconfs[node.name],
                          node_file,
                          indent=4,
                          sort_keys=True)
            conf_files.setdefault(node, []).append(
                '%s.ipcm.conf' % (node.name,))

        for dif in self.dif_ordering:  # type: mod.DIF
            dif_conf = difconfs.get(dif.name, None)
            if dif_conf:
                # Dump the normal DIF configuration files
                for node in dif.members:
                    with open(self.conf_dir('normal.%s.%s.dif'
                                            % (node.name, dif.name)), 'w') \
                            as dif_conf_file:
                        json.dump(dif_conf[node.name],
                                  dif_conf_file,
                                  indent=4,
                                  sort_keys=True)
                    conf_files.setdefault(node, []).append(
                        'normal.%s.%s.dif' % (node.name, dif.name))
        return conf_files