예제 #1
0
def create_new_vm(vmdef):
    image = cache.get_image(name=vmdef['image'])
    image = copy.deepcopy(image)
    vm = ravello.update_luids(image)
    vm['name'] = vmdef['name']
    vm['customVmConfigurationData'] = { 'keypair': env.public_key }
    vm['hostname'] = [ vmdef['name'] ]
    vm['numCpus'] = vmdef['smp']
    vm['memorySize'] = { 'unit': 'MB', 'value': vmdef['memory'] }
    vm.setdefault('suppliedServices', [])
    for svcdef in vmdef.get('services', []):
        if isinstance(svcdef, int):
            port = str(svcdef)
            svcdef = 'port-{0}'.format(svcdef)
        else:
            port = socket.getservbyname(svcdef)
        svc = { 'globalService': True, 'id': ravello.random_luid(),
                'ip': None, 'name': svcdef, 'portRange': port,
                'protocol': 'ANY_OVER_TCP' }
        vm['suppliedServices'].append({'baseService': svc})
    # Set a fixed Mac. This way applications created from blueprints
    # created from these VMs will have the same Mac.
    # See also https://github.com/ravello/testmill/issues/15
    conn = vm['networkConnections'][0]
    conn['device']['mac'] = get_new_mac()
    conn['device']['useAutomaticMac'] = False
    return vm
예제 #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 image_exists(name, nodepath):
     if not cache.get_image(name=name):
         msg = 'Image `{0}` does not exist.'.format(name)
         raise validate.ValidationError(msg, nodepath)
     return True