aboutsummaryrefslogtreecommitdiff
path: root/rumba/testbeds/jfed.py
blob: 8ad173bbb37ca4763d77ffa694792339fb5db99a (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
#
# jFed support for Rumba
#
#    Sander Vrijders  <sander.vrijders@intec.ugent.be>
#
# 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 subprocess
import getpass
import xml.dom.minidom as xml
import os.path
import wget
import tarfile

import rumba.model as mod

class Testbed(mod.Testbed):
    def __init__(self, exp_name, username, cert_file, exp_hours="2",
                 proj_name="ARCFIRE", authority="wall2.ilabt.iminds.be"):
        passwd = getpass.getpass(prompt="Password for certificate file: ")
        mod.Testbed.__init__(self, exp_name, username, passwd, proj_name)
        self.authority = "urn:publicid:IDN+" + authority + "+authority+cm"
        self.auth_name = authority
        self.cert_file = cert_file
        self.exp_hours = exp_hours
        self.if_id = dict()
        self.rspec = self.exp_name + ".rspec"
        self.manifest = self.exp_name + ".rrspec"
        self.jfed_jar = "jfed_cli/experimenter-cli.jar"

        if os.path.exists(self.jfed_jar) == False:
            print("Couldn't find jFed CLI. Downloading.")
            tarball = "jfed_cli.tar.gz"
            url = "http://jfed.iminds.be/downloads/stable/jar/" + tarball
            wget.download(url)
            tar = tarfile.open(tarball)
            tar.extractall()
            tar.close()
            print("Extracted in current directory")
            os.remove(tarball)


    def create_rspec(self, experiment):
        impl = xml.getDOMImplementation()
        doc = impl.createDocument(None, "rspec", None)

        top_el = doc.documentElement
        top_el.setAttribute("xmlns", "http://www.geni.net/resources/rspec/3")
        top_el.setAttribute("type", "request")
        top_el.setAttribute("xmlns:emulab", "http://www.protogeni.net/" +
                            "resources/rspec/ext/emulab/1")
        top_el.setAttribute("xmlns:jfedBonfire", "http://jfed.iminds.be/" +
                            "rspec/ext/jfed-bonfire/1")
        top_el.setAttribute("xmlns:delay", "http://www.protogeni.net/" +
                            "resources/rspec/ext/delay/1")
        top_el.setAttribute("xmlns:jfed-command", "http://jfed.iminds.be/" +
                            "rspec/ext/jfed-command/1")
        top_el.setAttribute("xmlns:client", "http://www.protogeni.net/" +
                            "resources/rspec/ext/client/1")
        top_el.setAttribute("xmlns:jfed-ssh-keys", "http://jfed.iminds.be/" +
                            "rspec/ext/jfed-ssh-keys/1")
        top_el.setAttribute("xmlns:jfed", "http://jfed.iminds.be/rspec/" +
                            "ext/jfed/1")
        top_el.setAttribute("xmlns:sharedvlan", "http://www.protogeni.net/" +
                            "resources/rspec/ext/shared-vlan/1")
        top_el.setAttribute("xmlns:xsi", "http://www.w3.org/2001/" +
                            "XMLSchema-instance")
        top_el.setAttribute("xsi:schemaLocation", "http://www.geni.net/" +
                            "resources/rspec/3 http://www.geni.net/" +
                            "resources/rspec/3/request.xsd")

        for node in experiment.nodes:
            el = doc.createElement("node")
            top_el.appendChild(el)
            el.setAttribute("client_id", node.name)
            el.setAttribute("exclusive", "true")
            el.setAttribute("component_manager_id", self.authority)

            el2 = doc.createElement("sliver_type")
            el.appendChild(el2)
            el2.setAttribute("name", "raw-pc")

            node.ifs = 0
            for ipcp in node.ipcps:
                if isinstance(ipcp, mod.ShimEthIPCP):
                    el3 = doc.createElement("interface")
                    self.if_id[ipcp] = node.name + ":if" + str(node.ifs)
                    el3.setAttribute("client_id", self.if_id[ipcp])
                    node.ifs += 1
                    el.appendChild(el3)

        for dif in experiment.dif_ordering:
            if isinstance(dif, mod.ShimEthDIF):
                el = doc.createElement("link")
                top_el.appendChild(el)
                el.setAttribute("client_id", dif.name)

                el2 = doc.createElement("component_manager_id")
                el2.setAttribute("name", self.authority)
                el.appendChild(el2)

                for ipcp in dif.ipcps:
                    el3 = doc.createElement("interface_ref")
                    el3.setAttribute("client_id", self.if_id[ipcp])
                    el.appendChild(el3)

        file = open(self.rspec, "w")
        file.write(doc.toprettyxml())
        file.close()

    def swap_in(self, experiment):
        self.create_rspec(experiment)

        for node in experiment.nodes:
            auth_name_r = self.auth_name.replace(".", "-")
            node.ssh_config.hostname = node.name + "." + self.exp_name + "." + \
                                       auth_name_r + "." + self.auth_name
            node.ssh_config.proxycommand = "ssh -i '" + self.cert_file + \
                                           "' -o StrictHostKeyChecking=no " + \
                                           self.username + \
                                           "@bastion.test.iminds.be nc " + \
                                           node.ssh_config.hostname + " 22"

        subprocess.call(["java", "-jar", self.jfed_jar, "create", "-S", \
                         self.proj_name, "--rspec", \
                         self.rspec, "-s", \
                         self.exp_name, "-p", self.cert_file, "-k", \
                         "usercert,userkeys,shareduserallkeys", \
                         "--create-slice",\
                         "--manifest", self.manifest,
                         "-P", self.password, \
                         "-e", self.exp_hours])

        rspec = xml.parse(self.manifest)
        xml_nodes = rspec.getElementsByTagName("node")

        # Complete details of the nodes after swapin
        for xml_node in xml_nodes:
            n_name = xml_node.getAttribute("client_id")
            intfs = xml_node.getElementsByTagName("interface")

            for node in experiment.nodes:
                if node.name == n_name:
                    node_n = node

            for intf in intfs:
                i_name = intf.getAttribute("client_id")
                for ipcp in node_n.ipcps:
                    if isinstance(ipcp, mod.ShimEthIPCP):
                        if self.if_id[ipcp] == i_name:
                            comp_id = intf.getAttribute("component_id")
                            comp_arr = comp_id.split(":")
                            ipcp.ifname = comp_arr[-1]
                            #xml_ip = intf.getElementsByTagName("ip")
                            #interface.ip = xml_ip[0].getAttribute("address")