コード例 #1
0
def init_server():
    logger = logging.getLogger('EucaAggregate')
    fileHandler = logging.FileHandler('/var/log/euca.log')
    fileHandler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
    logger.addHandler(fileHandler)
    fileHandler.setLevel(logging.DEBUG)
    logger.setLevel(logging.DEBUG)

    configParser = ConfigParser()
    configParser.read(['/etc/sfa/eucalyptus_aggregate.conf', 'eucalyptus_aggregate.conf'])
    if len(configParser.sections()) < 1:
        logger.error('No cloud defined in the config file')
        raise Exception('Cannot find cloud definition in configuration file.')

    # Only read the first section.
    cloudSec = configParser.sections()[0]
    cloud['name'] = cloudSec
    cloud['access_key'] = configParser.get(cloudSec, 'access_key')
    cloud['secret_key'] = configParser.get(cloudSec, 'secret_key')
    cloud['cloud_url']  = configParser.get(cloudSec, 'cloud_url')
    cloudURL = cloud['cloud_url']
    if cloudURL.find('https://') >= 0:
        cloudURL = cloudURL.replace('https://', '')
    elif cloudURL.find('http://') >= 0:
        cloudURL = cloudURL.replace('http://', '')
    (cloud['ip'], parts) = cloudURL.split(':')

    # Create image bundles
    images = getEucaConnection().get_all_images()
    cloud['images'] = images
    cloud['imageBundles'] = {}
    for i in images:
        if i.type != 'machine' or i.kernel_id is None: continue
        name = os.path.dirname(i.location)
        detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
        cloud['imageBundles'][name] = detail

    # Initialize sqlite3 database and tables.
    dbPath = '/etc/sfa/db'
    dbName = 'euca_aggregate.db'

    if not os.path.isdir(dbPath):
        logger.info('%s not found. Creating directory ...' % dbPath)
        os.mkdir(dbPath)

    conn = connectionForURI('sqlite://%s/%s' % (dbPath, dbName))
    sqlhub.processConnection = conn
    Slice.createTable(ifNotExists=True)
    EucaInstance.createTable(ifNotExists=True)
    Meta.createTable(ifNotExists=True)

    # Start the update process to keep track of the meta data
    # about Eucalyptus instance.
    Process(target=updateMeta).start()

    # Make sure the schema exists.
    if not os.path.exists(EUCALYPTUS_RSPEC_SCHEMA):
        err = 'Cannot location schema at %s' % EUCALYPTUS_RSPEC_SCHEMA
        logger.error(err)
        raise Exception(err)
コード例 #2
0
def updateMeta():
    logger = logging.getLogger('EucaMeta')
    fileHandler = logging.FileHandler('/var/log/euca_meta.log')
    fileHandler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
    logger.addHandler(fileHandler)
    fileHandler.setLevel(logging.DEBUG)
    logger.setLevel(logging.DEBUG)

    while True:
        sleep(30)

        # Get IDs of the instances that don't have IPs yet.
        dbResults = Meta.select(
                      AND(Meta.q.pri_addr == None,
                          Meta.q.state    != 'deleted')
                    )
        dbResults = list(dbResults)
        logger.debug('[update process] dbResults: %s' % dbResults)
        instids = []
        for r in dbResults:
            if not r.instance:
                continue
            instids.append(r.instance.instance_id)
        logger.debug('[update process] Instance Id: %s' % ', '.join(instids))

        # Get instance information from Eucalyptus
        conn = getEucaConnection()
        vmInstances = []
        reservations = conn.get_all_instances(instids)
        for reservation in reservations:
            vmInstances += reservation.instances

        # Check the IPs
        instIPs = [ {'id':i.id, 'pri_addr':i.private_dns_name, 'pub_addr':i.public_dns_name}
                    for i in vmInstances if i.private_dns_name != '0.0.0.0' ]
        logger.debug('[update process] IP dict: %s' % str(instIPs))

        # Update the local DB
        for ipData in instIPs:
            dbInst = EucaInstance.select(EucaInstance.q.instance_id == ipData['id']).getOne(None)
            if not dbInst:
                logger.info('[update process] Could not find %s in DB' % ipData['id'])
                continue
            dbInst.meta.pri_addr = ipData['pri_addr']
            dbInst.meta.pub_addr = ipData['pub_addr']
            dbInst.meta.state    = 'running'

        dumpInstanceInfo()