plabnode.in 2.2 KB
Newer Older
1
#!/usr/local/bin/python
2 3 4 5
# -*- python -*-

import sys
sys.path.append("@prefix@/lib")
6
import getopt
7
import libplab
Kirk Webb's avatar
 
Kirk Webb committed
8 9 10 11 12
import xmlrpclib
import time

TRIES = 3
SLEEPINT = 5
13 14

def usage(me):
15
    print "Usage: %s [ -vd ] { alloc | free } pid eid nodeid" % me
16 17 18
    sys.exit(1)

def main(args):
19
    me = args[0]
Kirk Webb's avatar
 
Kirk Webb committed
20 21 22
    alloctries = TRIES
    setuptries = TRIES
    
23
    try:
24 25 26
        command, pid, eid, nodeid = libplab.handleArgs(args[1:])
    except getopt.GetoptError:
        usage(me)
27
    except ValueError:
28
        usage(me)
29 30 31

    plab = libplab.Plab()
    slice = plab.loadSlice(pid, eid)
Kirk Webb's avatar
 
Kirk Webb committed
32

33
    if command == "alloc":
Kirk Webb's avatar
 
Kirk Webb committed
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
        while 1:
            try:
                node = slice.createNode(nodeid)
            except xmlrpclib.Fault, e:
                print "XML-RPC Fault happened while attempting node alloc:"
                print "Code: %s, Error: %s" % (e.faultCode, e.faultString)
                alloctries = alloctries - 1
                if alloctries > 0:
                    print "Sleeping %s and trying again" % SLEEPINT
                    time.sleep(SLEEPINT)
                else:
                    print "Giving up after %s tries" % TRIES
                    sys.exit(1)
            else:
                break

        while 1:
            try:
                node.addKey("/root/.ssh/identity.pub")
                node.emulabify()
                # XXX This file is redundant
                # node.putConfig("/etc/vnodeid", nodeid)
                # Note that vnode_setup boots the node
            except:
                print "Node setup failed."
                setuptries = setuptries - 1
                if setuptries > 0:
                    print "Sleeping for %s seconds and retrying" % \
                          SLEEPINT
                    time.sleep(SLEEPINT)
                else:
                    print "Giving up after %s tries" % TRIES
                    node.free()
                    raise
            else:
                break

    elif command == "free":
72
        try:
Kirk Webb's avatar
 
Kirk Webb committed
73
            node = slice.loadNode(nodeid)
74
        except:
Kirk Webb's avatar
 
Kirk Webb committed
75 76 77
            print "Node %s wasn't really allocated" % nodeid
            sys.exit(0)

78
        node.free()
Kirk Webb's avatar
 
Kirk Webb committed
79

80 81 82 83 84
    else:
        usage(me)

if __name__ == "__main__":
    main(sys.argv)