Пример #1
0
def get_applications():
    """get_applications()

    Return a list containing all applications.
    """
    applications = []
    for app in cache.get_applications():
        app = cache.get_application(app['id'])
        applications.append(application.appdef_from_app(app))
    return applications
Пример #2
0
def reuse_existing_application(appdef):
    """Try to re-use an existing application."""
    candidates = []
    pubkey = env.public_key
    if appdef.get('blueprint'):
        blueprint = cache.get_blueprint(name=appdef['blueprint'])
    else:
        blueprint = None
    project = env.manifest['project']
    for app in cache.get_applications():
        parts = app['name'].split(':')
        if len(parts) != 3:
            continue
        if parts[0] != project['name'] or parts[1] != appdef['name']:
            continue
        app = cache.get_application(app['id'])
        vms = app.get('vms', [])
        if not vms:
            continue
        state = get_application_state(app)
        if state not in vm_reuse_states:
            continue
        if blueprint and blueprint['name'] != app.get('blueprintName'):
            continue
        vmsfound = []
        for vmdef in appdef['vms']:
            for vm in vms:
                if vm['name'] == vmdef['name']:
                    break
            if not blueprint:
                image = cache.get_image(name=vmdef['image'])
                if not image:
                    continue
                if vm['shelfVmId'] != image['id']:
                    continue
            userdata = vm.get('customVmConfigurationData', {})
            keypair = userdata.get('keypair')
            if keypair.get('id') != pubkey['id']:
                continue
            vmsfound.append(vmdef['name'])
        if len(vmsfound) != len(appdef['vms']):
            continue
        candidates.append((state, app))
    if not candidates:
        return
    candidates.sort(key=lambda x: vm_reuse_states.index(x[0]))
    return candidates[0][1]
Пример #3
0
def create_new_application(appdef, name_is_template=True):
    """Create a new application based on ``appdef``."""
    if name_is_template:
        project = env.manifest['project']
        template = '{0}:{1}'.format(project['name'], appdef['name'])
        name = util.get_unused_name(template, cache.get_applications())
    else:
        name = appdef['name']
    app = { 'name': name }
    bpname = appdef.get('blueprint')
    if bpname:
        blueprint = cache.get_blueprint(name=bpname)
        app['blueprintId'] = blueprint['id']
    else:
        vms = []
        for vmdef in appdef.get('vms', []):
            vm = create_new_vm(vmdef)
            vms.append(vm)
        app['vms'] = vms
    app = env.api.create_application(app)
    app = cache.get_application(app['id'])  # update cache
    return app
Пример #4
0
def new_application_name(template):
    """Return a new application name based on *template*."""
    name = util.get_unused_name(template, cache.get_applications())
    return name