plabnode.in 5.03 KB
Newer Older
1
#!/usr/local/bin/python
2
# -*- python -*-
Kirk Webb's avatar
Kirk Webb committed
3 4 5 6 7
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# All rights reserved.
#
8

Kirk Webb's avatar
 
Kirk Webb committed
9
# system imports
Kirk Webb's avatar
 
Kirk Webb committed
10
import sys, os
11
import getopt
Kirk Webb's avatar
 
Kirk Webb committed
12
import time
Kirk Webb's avatar
 
Kirk Webb committed
13
import traceback
Kirk Webb's avatar
Kirk Webb committed
14
import __builtin__
Kirk Webb's avatar
 
Kirk Webb committed
15

Kirk Webb's avatar
 
Kirk Webb committed
16 17 18 19
# Emulab imports
sys.path.append("@prefix@/lib")
from libtestbed import *
import libplab
Kirk Webb's avatar
Kirk Webb committed
20

21 22

def usage(me):
23 24 25 26 27 28
    print "Usage: %s \\\n" \
          "  [-vdfE] {alloc|renew|free|stop|start|restart|nmping} \\\n" \
          "          pid eid nodeid\n" \
          "  (or without Emulab DB):\n" \
          "          slicename nodeid" \
          % (me)
29 30
    sys.exit(1)

Kirk Webb's avatar
 
Kirk Webb committed
31 32 33 34 35 36 37
def processException(command, nodeid, e):
    print "plabnode: %s, %s, fail, %s" % (nodeid, command, str(e))
    if parser.values.debug:
        traceback.print_exc()
        pass
    pass

38
def main(args):
39
    me = args[0]
40
    Force = False
Kirk Webb's avatar
Kirk Webb committed
41 42 43 44 45 46 47 48 49 50 51 52 53

    #
    # Grab a parser, and add options:
    #
    __builtin__.parser = TBParser()
    parser.add_option("-f", "--force", dest="force", default=False,
                      action="store_true", help="Force vserver creation.")
    parser.add_option("--pollNode", action="store_true", dest="pollNode",
                      default=False,
                      help="Poll node before attempting to set it up.")
    parser.add_option("--noIS", dest="noIS", action="store_true",
                      default=False,
                      help="Don't run InstantiateSliver() in mod_PLC")
David Johnson's avatar
David Johnson committed
54 55 56
    parser.add_option("-E","--no-emulabify",dest="no_emulabify",default=False,
                      action="store_true",help="Do not install/config Emulab" \
                      " software on the node")
Kirk Webb's avatar
Kirk Webb committed
57 58 59 60

    # parse command line options
    opts, args = parser.parse_args(args[1:])

61 62 63 64
    if not libplab.debug:
        libplab.debug = opts.debug
    if not libplab.verbose:
        libplab.verbose = opts.verbose
David Johnson's avatar
David Johnson committed
65

David Johnson's avatar
David Johnson committed
66
    slicename = None
Kirk Webb's avatar
Kirk Webb committed
67
    # make sure we were told what to do on the command line.
David Johnson's avatar
David Johnson committed
68 69 70 71 72 73 74
    if len(args) == 3:
        command, slicename, nodeid = args
        pass
    elif len(args) == 4:
        command, pid, eid, nodeid = args
        pass
    else:
75 76
        usage(me)
        pass
David Johnson's avatar
David Johnson committed
77
    
78
    plab = libplab.Plab()
David Johnson's avatar
David Johnson committed
79
    if slicename != None:
80
        slice = plab.loadSliceDirect(slicename)
David Johnson's avatar
David Johnson committed
81 82
        pass
    else:
83
        slice = plab.loadSliceByNode(pid, eid, nodeid)
David Johnson's avatar
David Johnson committed
84
        pass
Kirk Webb's avatar
 
Kirk Webb committed
85

86
    if command == "alloc":
Kirk Webb's avatar
 
Kirk Webb committed
87 88
        try:
            node = slice.createNode(nodeid, force=parser.values.force)
89 90 91
            # With the v4 NM, we have to sleep a couple seconds to give
            # the slice the a chance to get keys/acct stuff straightened out
            # so we can actually slogin and copy fixsudo.sh to the node.
David Johnson's avatar
David Johnson committed
92 93 94 95
            if not opts.no_emulabify:
                time.sleep(2)
                node.emulabify()
                pass
Kirk Webb's avatar
 
Kirk Webb committed
96 97 98
            # Note that vnode_setup boots the node
            pass
        except Exception, e:
99
            traceback.print_exc()
Kirk Webb's avatar
 
Kirk Webb committed
100 101 102 103 104 105 106
            print "Node setup failed on %s" % nodeid
            processException(command, nodeid, e)
            try: node.free()
            except: pass
            sys.exit(1)
            pass
        pass
107

Kirk Webb's avatar
 
Kirk Webb committed
108 109 110
    elif command == "renew":
        try:
            node = slice.loadNode(nodeid)
Kirk Webb's avatar
 
Kirk Webb committed
111 112
            node.renew()
            pass
113
        except Exception, e:
Kirk Webb's avatar
 
Kirk Webb committed
114
            processException(command, nodeid, e)
Kirk Webb's avatar
 
Kirk Webb committed
115
            sys.exit(1)
Kirk Webb's avatar
 
Kirk Webb committed
116
            pass
Kirk Webb's avatar
 
Kirk Webb committed
117

Kirk Webb's avatar
 
Kirk Webb committed
118
    elif command == "free":
119
        try:
Kirk Webb's avatar
 
Kirk Webb committed
120
            node = slice.loadNode(nodeid)
Kirk Webb's avatar
 
Kirk Webb committed
121 122 123 124
            node.free()
            pass
        except Exception, e:
            processException(command, nodeid, e)
Kirk Webb's avatar
 
Kirk Webb committed
125
            sys.exit(1)
Kirk Webb's avatar
 
Kirk Webb committed
126 127
            pass
        pass
Kirk Webb's avatar
 
Kirk Webb committed
128

David Johnson's avatar
David Johnson committed
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
    elif command == "stop":
        try:
            node = slice.loadNode(nodeid)
            node.stop()
            pass
        except Exception, e:
            processException(command, nodeid, e)
            sys.exit(1)
            pass
        pass

    elif command == "start":
        try:
            node = slice.loadNode(nodeid)
            node.start()
            pass
        except Exception, e:
            processException(command, nodeid, e)
            sys.exit(1)
            pass
        pass

    elif command == "stop":
        try:
            node = slice.loadNode(nodeid)
            node.stop()
            pass
        except Exception, e:
            processException(command, nodeid, e)
            sys.exit(1)
            pass
        pass

    elif command == "restart":
        try:
            node = slice.loadNode(nodeid)
            node.restart()
            pass
        except Exception, e:
            processException(command, nodeid, e)
            sys.exit(1)
            pass
        pass

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    elif command == "nmping":
        exitval = 1
        try:
            node = slice.loadNode(nodeid)
            res = node.pingNM()
            if res:
                exitval = 0
                pass
            pass
        except:
            if parser.values.debug:
                traceback.print_exc()
                pass
            pass
        sys.exit(exitval)

189 190
    else:
        usage(me)
Kirk Webb's avatar
 
Kirk Webb committed
191 192 193
        pass

    pass
194 195 196

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