plabnode.in 2.54 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
import xmlrpclib
import time
10
import socket
Kirk Webb's avatar
 
Kirk Webb committed
11 12 13

TRIES = 3
SLEEPINT = 5
14 15

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

def main(args):
20
    me = args[0]
Kirk Webb's avatar
 
Kirk Webb committed
21 22
    alloctries = TRIES
    setuptries = TRIES
23

24
    try:
25 26 27
        command, pid, eid, nodeid = libplab.handleArgs(args[1:])
    except getopt.GetoptError:
        usage(me)
28
    except ValueError:
29
        usage(me)
30 31 32

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

34
    if command == "alloc":
Kirk Webb's avatar
 
Kirk Webb committed
35 36 37 38
        while 1:
            try:
                node = slice.createNode(nodeid)
            except xmlrpclib.Fault, e:
39 40 41 42 43 44
                print "XML-RPC Fault happened while attempting " \
                      "node alloc for %s" % nodeid
                print "\tCode: %s, Error: %s" % (e.faultCode, e.faultString)
            except (socket.error, xmlrpclib.ProtocolError):
                print "Encountered problem communicating with an agent " \
                      "while setting up plab vnode %s" % nodeid
Kirk Webb's avatar
 
Kirk Webb committed
45 46
            else:
                break
47 48 49 50 51 52 53 54 55 56
            
            alloctries = alloctries - 1
            if alloctries > 0:
                print "Sleeping for %s seconds, then retrying alloc on %s" % \
                      (SLEEPINT, nodeid)
                time.sleep(SLEEPINT)
            else:
                print "Giving up after %s tries" % TRIES
                raise

Kirk Webb's avatar
 
Kirk Webb committed
57 58 59 60 61 62 63 64 65

        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:
66
                print "Node setup failed on %s" % nodeid
Kirk Webb's avatar
 
Kirk Webb committed
67 68 69
            else:
                break

70 71 72 73 74 75 76 77 78 79 80
            setuptries = setuptries - 1
            if setuptries > 0:
                print "Sleeping for %s seconds and then retrying " \
                      "setup on %s" % (SLEEPINT, nodeid)
                time.sleep(SLEEPINT)
            else:
                print "Giving up after %s setup attempts on %s" % \
                      (TRIES, nodeid)
                node.free()
                raise

Kirk Webb's avatar
 
Kirk Webb committed
81
    elif command == "free":
82
        try:
Kirk Webb's avatar
 
Kirk Webb committed
83
            node = slice.loadNode(nodeid)
84
        except:
Kirk Webb's avatar
 
Kirk Webb committed
85 86 87
            print "Node %s wasn't really allocated" % nodeid
            sys.exit(0)

88
        node.free()
Kirk Webb's avatar
 
Kirk Webb committed
89

90 91 92 93 94
    else:
        usage(me)

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