elabplab_dongleconfig.py 14.1 KB
Newer Older
1
#!/usr/bin/python
2 3
#
# Copyright (c) 2008-2010 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23
#
24 25 26 27 28 29 30 31 32 33 34 35 36 37

import sys
import traceback
import getopt
import os
import os.path
import pwd
import optparse
import xmlrpclib

#
# Depending on where you've installed m2crypto, edit/remove the following
# path addition...
#
38
#sys.path.append("/home/johnsond/r/usr/local/lib/python2.5/site-packages")
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
from M2Crypto.m2xmlrpclib import SSL_Transport
from M2Crypto import SSL

#
# Depending on where you've installed the emulab xmlrpc client
# support libs, tweak the following path append.
#
#sys.path.append("/usr/testbed/lib")
sys.path.append("/home/johnsond/elab")
import emulabclient

debug = 1

#
# The package version number
#
PACKAGE_VERSION = 0.1

BOSSNODE = "boss.emulab.net"

XMLRPC_SERVER = BOSSNODE
XMLRPC_PORT = 3069
SERVER_PATH = "/usr/testbed/devel/johnsond"
SERVER_DIR = "sbin"
ELAB_URI = "https://" + XMLRPC_SERVER + ":" + str(XMLRPC_PORT) + SERVER_PATH

PLAB_URI = "https://plc.protogeni.net/PLCAPI/"
PLAB_USER = None
PLAB_PASSWD = None

#
# check args
#
opp = optparse.OptionParser()
opp.add_option("-d", "--debug", dest="debug", default=False,
               action="store_true", help="Say A LOT about internal stuff")
opp.add_option("-e", "--emulab", dest="onlyelab", default=False,
               action="store_true", help="Only configure the Emulab slice")
opp.add_option("-p", "--planetlab", dest="onlyplab", default=False,
               action="store_true", help="Only configure the PlanetLab slice")
opp.add_option("-s", "--eslice", dest="eslice", default=None,
               action="store", help="Slice containing Emulab stuff")
opp.add_option("-S", "--pslice", dest="pslice", default=None,
               action="store", help="Slice containing PlanetLab stuff")
opp.add_option("-D", "--device", dest="device", default=None,
               action="store",
               help="Block device containing Emulab and PlanetLab slices.")
opp.add_option("-i", "--idempotent", dest="nowrite", default=False,
               action="store_true", help="Do not modify slices")
opp.add_option("-m", "--emnt", dest="emnt", default=None,
               action="store", help="Mount point of/for Emulab slice")
opp.add_option("-M", "--pmnt", dest="pmnt", default=None,
               action="store", help="Mount point of/for PlanetLab slice")
opp.add_option("-u", "--uid", dest="user", default=None,
               action="store", help="Use Emulab certs for this username")

opts, sys.argv = opp.parse_args(sys.argv)

if len(sys.argv) < 2:
    opp.error("must supply at least a node_id!")
else:
    node_id = sys.argv[1]
    pass

#
# Get currently mounted devices and their mount points.
#
def getMounts():
    retval = dict({})
    fd = os.popen('/sbin/mount')
    dmd = dict({})
    mdd = dict({})
    line = fd.readline()
    while line:
        ls = line.split(' on ')
        if len(ls) > 1:
            mp = ls[1].split(' ')[0]
            dmd[ls[0]] = mp
            mdd[mp] = ls[0]
            pass
        line = fd.readline()
        pass
    fd.close()
    return (dmd,mdd)

(onlyelab,onlyplab) = (opp.values.onlyelab,opp.values.onlyplab)
nowrite = opp.values.nowrite
emount = '/tmp/epwa.emnt'
pmount = '/tmp/epwa.pmnt'
username = opp.values.user
if opp.values.emnt:
    emount = opp.values.emnt
if opp.values.pmnt:
    pmount = opp.values.pmnt
eslice = None
pslice = None
if opp.values.eslice:
    eslice = opp.values.eslice
if opp.values.pslice:
    pslice = opp.values.pslice
device = None
if opp.values.device:
    device = opp.values.device

if onlyelab and onlyplab:
    opp.error("can only set one of '-e' and '-p'")

# Make sure we can grab a uid/cert to talk to Emulab
if username:
    try:
        pw = pwd.getpwnam(username)
    except KeyError:
        sys.stderr.write("error: unknown user id %d" % username)
        sys.exit(2)
        pass
    pass
else:
    try:
        pw = pwd.getpwuid(os.getuid())
    except KeyError:
        sys.stderr.write("error: unknown user id %d" % os.getuid())
        sys.exit(2)
        pass
    pass

ELAB_USER = pw.pw_name
ELAB_HOME = pw.pw_dir
ELAB_CERT = os.path.join(ELAB_HOME, ".ssl", "emulab.pem")

if not os.path.exists(ELAB_CERT) and not onlyplab:
    sys.stderr.write("error: missing emulab certificate: %s\n" %
                     ELAB_CERT)
    sys.exit(2)
    pass

(devmap,mntmap) = getMounts()

if debug:
    print "devmap: '%s'" % str(devmap)
    print "mntmap: '%s'" % str(mntmap)
    pass

def umount():
    (ndevmap,nmntmap) = getMounts()
    if nmntmap.has_key(emount) and not mntmap.has_key(emount):
        os.system('/sbin/umount %s' % emount)
    if nmntmap.has_key(pmount) and not mntmap.has_key(pmount):
        os.system('/sbin/umount %s' % pmount)
    pass

# safety hatch
mountopts = ''
if nowrite:
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    mountopts = '-r'
    pass

# figure out what our partition names should look like
(elab_part,plab_part) = (None,None)
(ufs_opts,vfat_opts) = ('','')
if sys.platform.startswith('linux'):
    (elab_part,plab_part) = ('1','2')
    (ufs_opts,vfat_opts) = ('-t ufs -o ufstype=ufs2','-t vfat')
elif sys.platform.startswith('freebsd'):
    (elab_part,plab_part) = ('s1a','s2')
    (ufs_opts,vfat_opts) = ('','-t msdosfs')
else:
    print "Unsupported platform %s, exiting!" % sys.platform
    sys.exit(99)
    pass
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

# Figure out what/where to mount the emulab device
if not onlyplab:
    if mntmap.has_key(emount):
        if eslice:
            raise RuntimeError("Device already mounted at %s; cannot mount" \
                               " %s there!" % (emount,eslice))
        if device:
            print "Warning: using mount %s instead of device %s" \
                  % (emount,device)
            pass
        pass
    elif os.path.isdir(emount) or not os.path.exists(emount):
        if not os.path.isdir(emount):
            os.makedirs(emount)
            pass
        # figure out if we need to mount using the slice or the device:
        if eslice:
            if os.path.exists(eslice):
227 228
                retval = os.system('/sbin/mount %s %s %s %s' \
                                   % (mountopts,ufs_opts,eslice,emount))
229 230 231 232 233 234 235 236 237
                if not retval == 0:
                    raise RuntimeError("mount(%s,%s) failed: %d" \
                                       % (eslice,emount,retval >> 8))
                else:
                    raise RuntimeError("Slice %s does not exist!" % eslice)
                pass
            pass
        elif device:
            if not device.find('/dev') == 0:
238
                rdev = "%s%s%s" % ('/dev/',device,elab_part)
239
            else:
240
                rdev = "%s%s" % (device,elab_part)
241 242 243 244
                pass
            if devmap.has_key(rdev):
                raise RuntimeError("Device %s already mounted!" % rdev)
            if os.path.exists(rdev):
245 246
                retval = os.system('/sbin/mount %s %s %s %s' \
                                   % (mountopts,ufs_opts,rdev,emount))
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
                if not retval == 0:
                    raise RuntimeError("mount(%s,%s) failed: %d" \
                                       % (rdev,emount,retval >> 8))
                pass
            else:
                raise RuntimeError("Device %s does not exist!" % rdev)
            pass
        pass
    else:
        raise RuntimeError("Invalid mount point %s!" % emount)
    pass

# Figure out what/where to mount the planetlab device
if not onlyelab:
    if mntmap.has_key(pmount):
        if pslice:
            umount()
            raise RuntimeError("Device already mounted at %s; cannot mount" \
                               " %s there!" % (pmount,pslice))
        if device:
            print "Warning: using mount %s instead of device %s" \
                  % (pmount,device)
            pass
        pass
    elif os.path.isdir(pmount) or not os.path.exists(pmount):
        if not os.path.isdir(pmount):
            os.makedirs(pmount)
            pass
        # figure out if we need to mount using the slice or the device:
        if pslice:
            if os.path.exists(pslice):
278 279
                retval = os.system('/sbin/mount %s %s %s %s' \
                                   % (mountopts,vfat_opts,pslice,pmount))
280 281 282 283 284 285 286 287 288 289 290
                if not retval == 0:
                    umount()
                    raise RuntimeError("mount(%s,%s) failed: %d" \
                                       % (pslice,pmount,retval >> 8))
                pass
            else:
                umount()
                raise RuntimeError("Slice %s does not exist!" % pslice)
            pass
        elif device:
            if not device.find('/dev') == 0:
291
                rdev = "%s%s%s" % ('/dev/',device,plab_part)
292
            else:
293
                rdev = "%s%s" % (device,plab_part)
294 295 296 297 298
                pass
            if devmap.has_key(rdev):
                umount()
                raise RuntimeError("Device %s already mounted!" % rdev)
            if os.path.exists(rdev):
299 300
                retval = os.system('/sbin/mount %s %s %s %s' \
                                   % (mountopts,vfat_opts,rdev,pmount))
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
                if not retval == 0:
                    umount()
                    raise RuntimeError("mount(%s,%s) failed: %d" \
                                       % (rdev,pmount,retval >> 8))
                pass
            else:
                umount()
                raise RuntimeError("Device %s does not exist!" % rdev)
            pass
        pass
    else:
        umount()
        raise RuntimeError("Invalid mount point %s!" % pmount)
    pass

def addMACDelim(mac,delim=':'):
    if mac.count(delim) == 0:
        return mac[0:2] + delim + mac[2:4] + delim + mac[4:6] + delim + \
               mac[6:8] + delim + mac[8:10] + delim + mac[10:12]
    else:
        return mac
    pass

#
# Grab the emulab widearea config for this node.
#
if not onlyplab:
    ctx = SSL.Context("sslv23")
    ctx.load_cert(ELAB_CERT, ELAB_CERT)
    ctx.set_verify(SSL.verify_none, 16)
    ctx.set_allow_unknown_ca(0)
    try:
        eserver = xmlrpclib.ServerProxy(ELAB_URI,SSL_Transport(ctx),
                                        verbose=debug,allow_none=1)
        waconfig = eserver.node.waconfig(PACKAGE_VERSION,{ 'node' : node_id })
        waconfig = waconfig['value']
        if debug:
            print "waconfig: '%s'" % str(waconfig)
            pass
        possible_keys = [ 'WA_BOOTMETHOD','WA_HOSTNAME','WA_DOMAIN','WA_MAC',
                          'WA_IP_ADDR','WA_IP_NETMASK','WA_IP_GATEWAY',
                          'WA_IP_DNS1','WA_IP_DNS2','WA_BWLIMIT' ]
        if not waconfig:
            print "error: received empty config from Emulab!"
            sys.exit(3)
            pass

        # whack the mac value into colon-delimited form
        if waconfig.has_key("WA_MAC"):
            waconfig["WA_MAC"] = addMACDelim(waconfig["WA_MAC"])
            pass

        elabstr = ''
        for k in possible_keys:
            if waconfig.has_key(k):
                elabstr = "%s%s=\"%s\"\n" % (elabstr,k,str(waconfig[k]))
                pass
            pass
        if debug:
            print "Emulab waconfig file contents:\n\n%s" % elabstr
            print "Emulab waconfig privkey: %s" % str(waconfig['PRIVKEY'])
            pass
        pass
    except:
        print "ERROR: contacting Emulab via XMLRPC..."
        traceback.print_exc()
        umount()
        sys.exit(2)
        pass
    pass

#
# Grab the plab config for this node.
#
if not onlyelab:
    if onlyplab:
        # user had better specify a real hostname, not an elab node_id
        hostname = node_id
    else:
        # grab the hostname from the elab db
        hostname = "%s.%s" % (waconfig['WA_HOSTNAME'],waconfig['WA_DOMAIN'])
        pass
    try:
        pserver = xmlrpclib.ServerProxy(PLAB_URI,allow_none=1)
        auth_info = { 'Username' : PLAB_USER, 'AuthMethod' : 'password',
                      'Role' : 'admin', 'AuthString' : PLAB_PASSWD }
        plabstr = pserver.AdmGenerateNodeConfFile(auth_info,hostname)
        if debug:
            print "Plab config file contents:\n\n%s" % plabstr
            pass
        pass
    except:
        print "ERROR: contacting Plab via XMLRPC..."
        traceback.print_exc()
        umount()
        sys.exit(2)
        pass
    pass

# Finally, actually write everything...
if not nowrite:
    if not onlyplab:
        try:
            fd = open('%s/etc/emulab/waconfig' % emount,'w')
            fd.write(elabstr)
            fd.close()
        except:
            print "Error writing Emulab waconfig file!"
            traceback.print_exc()
            umount()
            sys.exit(4)
        try:
            fd = open('%s/etc/emulab/emulab-privkey' % emount,'w')
            fd.write("%s\n" % waconfig['PRIVKEY'])
            fd.close()
        except:
            print "Error writing Emulab privkey file!"
            traceback.print_exc()
            umount()
            sys.exit(4)
        os.system('touch %s/etc/emulab/isrem' % emount)
        os.system('touch %s/etc/emulab/isflash' % emount)
        os.system('touch %s/etc/emulab/ismfs' % emount)
        try:
            fd = open('%s/etc/emulab/bossnode' % emount,'w')
            fd.write("%s\n" % BOSSNODE)
            fd.close()
        except:
            print "Error writing Emulab bossnode file!"
            traceback.print_exc()
            umount()
            sys.exit(4)
        pass
    if not onlyelab:
        try:
            fd = open('%s/plnode.txt' % pmount,'w')
            fd.write(plabstr)
            fd.close()
        except:
            print "Error writing PlanetLab plnode.txt file!"
            traceback.print_exc()
            umount()
            sys.exit(4)
        pass
    pass

umount()

print "Done."
sys.exit(0)