vanilla_elastic.py 4.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#!/usr/bin/env python

##
## This file defines a helper class for vanilla (i.e. bare-bones) elastic slices.
## No custom functionality is implemented in this helper; all necessary functions 
## come from the superclass SimpleElasticSliceManagerHelper.
##
## This file is meant to be imported by a manager.
## The manager looks for two key variables: ELASTICSLICE_CLASS and ELASTICSLICE_HELPER.
## The former is a class it will instantiate; the latter is a an object it will just invoke operations on.
##
## Example ivocation of a manager:
## python -i elasticslice.py -d -D johnsond -U <username> -P <password> \
## -S 'urn:publicid:IDN+emulab.net:<projectname>+slice+<slicename>' --default-cm apt -C ~/.ssl/apt-cacert.pem \
## -H ~/elasticslice/clients/vanilla/vanilla_elastic.py
##

import protogeniclientlib
import geni.portal as portal
import geni.rspec.pg as RSpec
import geni.rspec.igext as IG
from lxml import etree as ET
import crypt
import random
import os
import argparse
from argparse import Namespace
28 29
import logging
from xml.sax.saxutils import escape
30 31 32 33

helper = None

# ToDo: UPDATE
34
COMPUTEIMAGE = "urn:publicid:IDN+emulab.net+image+emulab-ops:UBUNTU14-64-STD"
35 36 37 38 39 40 41
TBURL = ""
TBCMD = ""
TBCMD_ADDNODE = ""
TBCMD_DELNODE = ""
TBPATH = ""

class VanillaElasticSliceManagerHelper(protogeniclientlib.SimpleElasticSliceManagerHelper):
42
    def __init__(self,manager=None,server=None,debug=True):
43
        super(VanillaElasticSliceManagerHelper,self).__init__()
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
	# Override superclass variables if necessary:
        # self.debug = debug
        # self.rspec = RSpec.Request()
        # self.numPCs = numPCs
        self.imageURN = COMPUTEIMAGE
        # self.numLans = numLans
        # self.multiplexLans = multiplexLans
        # self.tarballs = tarballs
        # self.startupCommand = startupCommand
        self.nodetype = 'r320'
        # self.node_prefix = node_prefix
        # self.lan_prefix = lan_prefix
        # self.generated = False
        # self.idx = 0

        self._configure_logging(debug=debug)
        self.log.debug("Constructor finished, logger is set")
        
    def _configure_logging(self,debug=False):
        formatter = logging.Formatter("%(asctime)s - %(name)-16s - %(levelname)-8s - %(message)s")
        console = logging.StreamHandler()
        console.setFormatter(formatter)
        logging.getLogger('').addHandler(console)
        self.log = logging.getLogger(__name__)  
        if debug:
            self.log.setLevel(logging.DEBUG)
        else:
            self.log.setLevel(logging.INFO)   
 
    def _setState(self):
        if not self.generated:
            wrappedPGManifest = self.server.get_wrapped_manifest()
            self.idx = self._getMaxNodeID(wrappedPGManifest)
            self.lans = wrappedPGManifest.getLinkClientIds()

    def _getMaxNodeID(self,wrappedPGManifest):
        retval = 0
        for node in wrappedPGManifest.nodes:
            cid = node.name
            if cid.startswith(self.node_prefix):
                # "+1" supports nodenames formatted as follows: <node_prefix>-<ID>
                cid_num = int(cid[(len(self.node_prefix)+1):])
                if cid_num > retval:
                    retval = cid_num
        return retval

    def generateAddNodesArgs(self,count=1):
	self.log.debug("Running generateAddNodesArgs")

        self._setState() 
        
        retval = {}
        for i in range(self.idx + 1,self.idx + count + 1):
            nn = "%s-%d" % (self.node_prefix,i)

            # lans = []
            # #map(lambda(x): "%s-%d" % (self.node_prefix,x,),range(0,self.numLans))
            # for j in range(0,self.numLans):
            #     lans.append({ 'name' : "%s-%s" % (self.lan_prefix,j) })
            #     pass
            # lans lists is now set in _setState()

            nd = {}
            if self.imageURN:
                nd["diskimage"] = self.imageURN

            #if self.startupCommand:
            #    nd["startup"] = escape(self.startupCommand)
            #if self.tarballs:
            #    nd["tarballs"] = tarballs

            if self.nodetype:
                nd['hardware_type'] = self.nodetype
            if len(self.lans) > 0:
                nd["lans"] = self.lans
                pass
            retval[nn] = nd
            pass
        self.idx += count

        self.log.debug("Created NodeArgs: %s" % retval)

        return retval
127 128 129

ELASTICSLICE_CLASS = VanillaElasticSliceManagerHelper
ELASTICSLICE_HELPER = None