plabslice.in 6.52 KB
Newer Older
1
#!/usr/local/bin/python
2
# -*- python -*-
Kirk Webb's avatar
Kirk Webb committed
3
#
4
# Copyright (c) 2000-2003, 2008 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
Kirk Webb's avatar
Kirk Webb committed
24
#
25 26 27

import sys
sys.path.append("@prefix@/lib")
28
import getopt
29
from libtestbed import *
30
import libplab
31
import traceback
32

33 34 35
usage = "\t%prog [-vd] {create|destroy|metaup} pid eid [-p plc slicename]\n" \
        "\t%prog [-vd] [-u <user1,...>] [-n <node1,...>] [-m <instmethod>] \n"\
        "\t      -c <slicedesc> [-w <url>] {create|destroy} slicename"
36 37

def main(args):
38
    me = args[0]
39
    parser = TBParser(usage)
40 41 42 43 44 45 46 47
    parser.add_option("-u","--users",dest="users",action="store",
                      help="Comma-separated list of users to add to the slice",
                      default=None)
    parser.add_option("-n","--nodes",dest="nodes",action="store",
                      help="Comma-separated list of hostnames to add to " \
                      "the slice",default=None)
    parser.add_option("-c","--slicedesc",dest="slicedesc",action="store",
                      help="Slice description",default=None)
48 49
    parser.add_option("-m","--instmethod",dest="instmethod",action="store",
                      help="Slice instantiation method",default=None)
50 51 52 53 54 55
    parser.add_option("-w","--sliceurl",dest="sliceurl",action="store",
                      help="Slice URL",default=None)
    parser.add_option("-p","--plc",dest="plc",action="store",
                      help="PLC Name",default=None)

    command,slicename = None,None
56
    userlist,nodelist = None,None
57
    pid,eid = None,None
58
    
59
    (opts, args) = parser.parse_args()
David Johnson's avatar
David Johnson committed
60

61 62 63 64
    if not libplab.debug:
        libplab.debug = opts.debug
    if not libplab.verbose:
        libplab.verbose = opts.verbose
65 66 67 68 69
    
    if len(args) == 2:
        command,slicename = args
        if args[0] == 'create' and (opts.slicedesc == None \
                                    or opts.slicedesc == ''):
70 71 72 73 74
            parser.error("Must supply a slice description if creating" \
                         " by slicename!")
            pass
        if opts.plc == None:
            parser.error("Must supply a plc if creating by slicename!")
75 76 77 78
            pass
        if opts.nodes != None:
            nodelist = opts.nodes.split(',')
            pass
79 80 81
        else:
            nodelist = []
            pass
82
        if opts.users != None:
83
            userlist = opts.users.split(',')
84
            pass
85 86 87
        else:
            userlist = []
            pass
88 89 90 91
        pass
    elif len(args) == 3:
        command,pid,eid = args
        pass
92 93 94 95 96 97 98
    elif len(args) == 4:
        command,pid,eid,slicename = args
        if not opts.plc or opts.plc == '':
            parser.error("When specifying a slicename, you must also specify" \
                         " a PLC!")
            pass
        pass
99
    else:
100
        parser.error("Incorrect number of arguments")
101
        pass
102 103

    plab = libplab.Plab()
104
    slicelist = []
105
    if command == "create":
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
        if pid and eid and slicename:
            slice = plab.createSlice(pid,eid,opts.plc,slicename)
            slicelist.append(slice)
            pass
        elif pid and eid:
            slicelist = plab.createSlices(pid,eid)
        else:
            slice = plab.createSliceDirect(opts.plc,slicename,
                                           opts.slicedesc,opts.sliceurl,
                                           userlist=userlist,nodelist=nodelist,
                                           instmethod=opts.instmethod)
            slicelist.append(slice)
            pass
        pass
    elif command == "destroy":
        if pid and eid and slicename:
            slice = plab.loadSlice(pid,eid,opts.plc,slicename)
            slice.destroy()
            pass
        elif pid and eid:
            failedslices = []
            slicelist = plab.loadSlices(pid,eid)
            for slice in slicelist:
                try:
                    slice.destroy()
                except:
                    print "Failed to destroy %s:" % str(slice.slicename)
                    failedslices.append(slice.slicename)
                    traceback.print_exc()
                    pass
                pass

            if not failedslices == []:
                raise RuntimeError("Could not destroy some slices: %s" % \
                                   ','.join(failedslices))
141 142
            pass
        else:
143 144
            slice = plab.loadSliceDirect(opts.plc,slicename,
                                         slicedescr=opts.slicedesc,
145 146
                                         userlist=userlist,nodelist=nodelist,
                                         instmethod=opts.instmethod)
147
            slice.destroy()
148 149
            pass
        pass
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    elif command == "metaup":
        if pid and eid and slicename:
            slice = plab.loadSlice(pid,eid,opts.plc,slicename)
            slice.updateSliceMeta()
            pass
        elif pid and eid:
            failedslices = []
            slicelist = plab.loadSlices(pid, eid)
            for slice in slicelist:
                try:
                    slice.updateSliceMeta()
                except:
                    print "Failed to update ticket for %s:" % \
                          str(slice.slicename)
                    failedslices.append(slice.slicename)
                    traceback.print_exc()
                    pass
                pass

            if not failedslices == []:
                raise RuntimeError("Could not update tickets for some slices:"
                                   " %s" % '.'.join(failedslices))
172 173
            pass
        else:
174 175 176 177
            slice = plab.loadSliceDirect(slicename,slicedescr=opts.slicedesc,
                                         userlist=userlist,nodelist=nodelist,
                                         instmethod=opts.instmethod)
            slice.updateSliceMeta()
178 179
            pass
        pass
180 181
    else:
        usage(me)
182
        pass
183 184 185

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