Example #1
0
    def setUp(self):
        # clean out the test db
        dbcon = AutoOamMongo()
        dbcon.db().clusters.remove()
        dbcon.db().allocs.remove()

        self._basedir = os.path.dirname(os.path.abspath(__file__))
        self._propcmp = '%s/hadoop.properties' % self._basedir
        self._nodecmp = '%s/nodes-byon.yaml' % self._basedir
Example #2
0
class VagrantSubnetAlloc(object):
    '''
    The creation of Vagrant clusters requires the allocation of an IP subnet
    for VirtualBox host-only networking.  There are any number of ways this 
    could be accomplished, but for now VagrantSubnetAlloc scans the cluster
    database and looks for the first unused subnet.  It starts scanning at
    192.168.1.x and will look until it finds one.
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self._dbcon = AutoOamMongo()
        self._db = self._dbcon.db().clusters

    def alloc(self, cluster):
        # when allocating new we need to first find an unused subnet
        start = 0xc0a801 # 192.168.1
        subnet = '%d.%d.%d' % ((start >> 16) & 0xff,(start >> 8) & 0xff,start & 0xff)
        while self._db.find( { 'vmi.subnet' : subnet }).count():
            start += 1
            subnet = '%d.%d.%d' % ((start >> 16) & 0xff,(start >> 8) & 0xff,start & 0xff)

        return subnet
Example #3
0
    def setUp(self):
        # clean out the test db
        dbcon = AutoOamMongo()
        dbcon.db().clusters.remove()
        dbcon.db().allocs.remove()

        s = """{
            "name" : "a-cluster",
            "idbversion" : "3.5.1-5", 
            "boxtype" : "cal-precise64",
            "rolespec" : {
                "pm" : {
                    "count" : 2,
                    "memory" : 1024,
                    "dbroots_per" : 1
                }
              }
            }
            """
        self._defcfg = ConfigSpec(s)
Example #4
0
    def setUp(self):
        # turn on unit-test mode so that we don't actually run vagrant commands
        common.props['vmi.vagrantvmi.unit-test'] = True

        # clean out the test db
        dbcon = AutoOamMongo()
        dbcon.db().clusters.remove()
        dbcon.db().allocs.remove()

        s = """{
            "name" : "a-cluster",
            "idbversion" : "3.5.1-5", 
            "boxtype" : "cal-precise64",
            "rolespec" : {
                "pm" : {
                    "count" : 2,
                    "memory" : 1024,
                    "dbroots_per" : 1
                }
              }
            }
            """
        self._defcfg = ConfigSpec(s)
Example #5
0
    def forceclean(self, args):
        if not len(args) == 1:
            self.usage()

        dbcon = AutoOamMongo()
        cdefn = dbcon.db().clusters.find_one({"name": args[0]})
        if not cdefn:
            Log.error("Cluster %s not found!" % args[0])
            return -1

        # clean the two databases
        dbcon.db().allocs.remove({"cid": cdefn["_id"]})
        dbcon.db().clusters.remove({"_id": cdefn["_id"]})

        # now wipe any remnants in the file system
        cmd = "rm -rf %s" % cdefn["vmi"]["rundir"]
        os.system(cmd)
Example #6
0
    def setUp(self):
        # clean out the test db
        dbcon = AutoOamMongo()
        dbcon.db().clusters.remove()
        dbcon.db().allocs.remove()

        s = """{
            "name" : "a-cluster",
            "idbversion" : "3.5.1-5", 
            "boxtype" : "cal-precise64",
            "rolespec" : {
                "pm" : {
                    "count" : 4,
                    "memory" : 1024,
                    "dbroots_per" : 1
                },
                "um" : {
                    "count" : 1,
                    "memory" : 2048
                }
              }
            }
            """
        self._defcfg = ConfigSpec(s)

        s = """{
            "name" : "a-cluster",
            "idbversion" : "3.5.1-5", 
            "boxtype" : "cal-precise64",
            "idbuser" : "calpont",
            "rolespec" : {
                "pm" : {
                    "count" : 4,
                    "memory" : 1024,
                    "dbroots_per" : 1
                },
                "um" : {
                    "count" : 1,
                    "memory" : 2048
                }
              }
            }
            """
        self._nonrootcfg = ConfigSpec(s)

        # cluster used to test external dbroots
        s = """{
            "name"       : "e-cluster",
            "idbversion" : "3.5.1-5", 
            "boxtype"    : "cal-centos6",
            "storage"    : "external",
            "rolespec" : {
                "pm" : {
                    "count" : 2,
                    "dbroots_per" : 2
                },
                "um" : {
                    "count" : 1
                }
              }
            }
            """
        self._extcfg = ConfigSpec(s)

        # cluster used to test pm_query
        s = """{
            "name"       : "e-cluster",
            "idbversion" : "4.5.0-1", 
            "boxtype"    : "cal-precise64",
            "binary"     : true,
            "pm_query"   : true,
            "rolespec" : {
                "pm" : {
                    "count" : 2,
                    "dbroots_per" : 2
                },
                "um" : {
                    "count" : 1
                }
              }
            }
            """
        self._pmquerycfg = ConfigSpec(s)
Example #7
0
 def __init__(self):
     '''
     Constructor
     '''
     self._dbcon = AutoOamMongo()
     self._db = self._dbcon.db().clusters
Example #8
0
class ClusterMgr(object):
    '''
    The ClusterMgr provides an interface to create and remove clusters
    as well as list active clusters.  Internally it uses a MongoDB
    instance for persistence
        
    __init__      - constructor that connects to the database
    list_clusters - list all clusters
    alloc_new     - allocate a new cluster instance using a ConfigSpec
    remove        - removes a cluster
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self._dbcon = AutoOamMongo()
        self._db = self._dbcon.db().clusters
                 
    def list_clusters(self):
        '''Returns the list of clusters as (name, id) tuples.'''
        ret = []
        for c in self._db.find(): 
	    name='ERROR -no name'
	    try:
        	name=c['name'] 
	    except:
 		pass
            ret.append( (str(name), str(c['_id'])) )
        return ret
    
    def alloc_new(self,name, configspec, vmitype, chefmode=True):
        '''Allocates a new cluster.
        
        @param name - name for the cluster (ideally unique)
        @param configspec - a ConfigSpec instance
        @param vmitype - string specifying vmi type (supported: 'vagrant')
        '''
        cid = self._db.insert({})        
        try:
            configspec.validate()
            cluster = Cluster( name, configspec, cid, chefmode=chefmode )
            VMI.Create(vmitype, cluster)
            self._db.update( 
                            { '_id' : cluster.id() }, 
                            { u'name' : name,
                              u'config' : configspec.jsonmap, 
                              u'vmi' : cluster.get_vmi().jsonmap(), 
                              u'machines' : cluster.machines()} )
        except Exception as exc:
            import traceback
            Log.error('Cluster creation failed: %s' % exc)
            Log.error( traceback.format_exc() )
            self._db.remove( { '_id' : cid } )
            raise Exception('Failed to create cluster: %s' % name)
        
        return cluster
    
    def attach(self,name):
        '''Attempts to attach to an existing cluster.
        
        @param name - cluster name to attach to
                          
        raises eusterception of number found is not found
        '''
        try:
            cursor = self._db.find({'name' : name})
            defn = cursor[0]
            cluster = Cluster(defn['name'], ConfigSpec(json.dumps(defn['config'])), defn['_id'],machines=defn['machines'], attach=True)
            VMI.Attach(cluster, defn['vmi'])
            return cluster
        except Exception as err:
            raise Exception("Error attaching to cluster %s: %s!" % (name,err))
    
    def destroy(self,cluster):
        '''Destroys the cluster and removes from the database.  DESTRUCTIVE operation.'''
        crit = { '_id' : cluster.id() }
        clusterdefn = self._db.find_one( crit )
        if not clusterdefn:
            Log.error("Cluster id %s not in database!" % cluster.id())
        else:
            try:
                cluster.destroy()
                self._db.remove( crit )
                return True
            except Exception as err:
                Log.error("Unable to destroy cluster: %s" % err)

        return False

    def destroy_by_id(self,idnum):
        '''Destroys the cluster and removes from the database.  DESTRUCTIVE operation.'''
	crit = { '_id' : self._dbcon.getobjectid( idnum ) }
        clusterdefn = self._db.find_one( crit )
        if not clusterdefn:
            Log.error("id %s not in database!" % idnum)
        else:
            try:
                self._db.remove( crit )
                return True
            except Exceptionoammongo.py  as err:
                Log.error("Unable to destroy cluster by id: %s" % err)

        return False
Example #9
0
 def setUp(self):
     # use an alternate database for unit-testing
     dbcon = AutoOamMongo()
     dbcon.db().clusters.remove()
     dbcon.db().allocs.remove()