Beispiel #1
0
def delete_virtual_compute(model, event):
    if not ICompute.providedBy(model.__parent__.__parent__):
        return

    if IDeployed.providedBy(model):
        log.msg(
            'Deleting compute %s which is in IDeployed state, shutting down and '
            'undeploying first' % model.hostname,
            system='compute-backend')
        yield DestroyComputeAction(model).execute(DetachedProtocol(), object())
        yield UndeployAction(model).execute(DetachedProtocol(), object())
    else:
        log.msg('Deleting compute %s which is already in IUndeployed state' %
                model.hostname,
                system='compute-backend')

    owner = (yield db.get(model, '__owner__'))
    ulog = UserLogger(subject=model, owner=owner)
    ulog.log('Deleted %s' % model)

    @db.transact
    def deallocate_ip():
        ippools = db.get_root()['oms_root']['ippools']
        ip = netaddr.IPAddress(model.ipv4_address.split('/')[0])
        if ippools.free(ip):
            ulog.log('Deallocated IP: %s', ip)

    yield deallocate_ip()
Beispiel #2
0
def delete_virtual_compute(model, event):
    if not ICompute.providedBy(model.__parent__.__parent__):
        return

    if IDeployed.providedBy(model):
        log.msg('Deleting compute %s which is in IDeployed state, shutting down and '
                'undeploying first' % model.hostname, system='compute-backend')
        yield DestroyComputeAction(model).execute(DetachedProtocol(), object())
        yield UndeployAction(model).execute(DetachedProtocol(), object())
    else:
        log.msg('Deleting compute %s which is already in IUndeployed state' %
                model.hostname, system='compute-backend')

    owner = (yield db.get(model, '__owner__'))
    ulog = UserLogger(subject=model, owner=owner)
    ulog.log('Deleted %s' % model)

    @db.transact
    def deallocate_ip():
        ippools = db.get_root()['oms_root']['ippools']
        ip = netaddr.IPAddress(model.ipv4_address.split('/')[0])
        if ippools.free(ip):
            ulog.log('Deallocated IP: %s', ip)

    yield deallocate_ip()
Beispiel #3
0
def allocate_virtual_compute_from_hangar(model, event):
    if not IVirtualizationContainer.providedBy(model.__parent__):
        return

    if IDeployed.providedBy(model):
        return

    auto_allocate = get_config().getboolean('vms', 'auto_allocate', True)

    if not auto_allocate:
        return

    if IHangar.providedBy(model.__parent__.__parent__):
        action = AllocateAction
        msg = 'Allocated compute %s'
    elif ICompute.providedBy(model.__parent__.__parent__):
        action = DeployAction
        msg = 'Deployed compute %s'
    else:
        return

    try:
        path = canonical_path(model)
        owner = model.__owner__
        ul = UserLogger(subject=model, owner=owner)
        log.msg('Attempting %s for %s (%s, %s)' % (action.__name__, model, path, owner),
                system='create-event')
        d = task.deferLater(reactor, 2.0, virtual_compute_action, action, path, event)
        d.addCallback(lambda r: ul.log(msg % path))
        if not get_config().getboolean('stats', 'only_report_on_sync', True):
            d.addCallback(lambda r: defer.maybeDeferred(getUtility(IUserStatisticsProvider).update, owner))
        d.addErrback(log.err)
    except Exception:
        log.err(system='create-event')
Beispiel #4
0
 def get_computes(self, username):
     computes = db.get_root()['oms_root']['computes']
     user_computes = []
     for compute in map(follow_symlinks, computes.listcontent()):
         if not IVirtualCompute.providedBy(compute):
             continue
         if compute.__owner__ == username and IDeployed.providedBy(compute):
             user_computes.append(compute)
     return user_computes
Beispiel #5
0
    def sync_vm(self, vm):
        compute = TmpObj(self.context)
        compute.state = unicode(vm['state'])

        # Ensure IDeployed marker is set, unless not in another state
        if not IDeployed.providedBy(compute):
            noLongerProvides(self.context, IUndeployed)
            noLongerProvides(self.context, IDeploying)
            alsoProvides(self.context, IDeployed)

        if 'ctid' in vm:
            compute.ctid = vm['ctid'] if vm['ctid'] != '-' else -1

        for idx, console in enumerate(vm['consoles']):
            if console['type'] == 'pty' and not self.context.consoles['tty%s' % idx]:
                self.context.consoles.add(TtyConsole('tty%s' % idx, console['pty']))
            if console['type'] == 'openvz' and not self.context.consoles['tty%s' % idx]:
                self.context.consoles.add(OpenVzConsole('tty%s' % idx, console['cid']))
            if console['type'] == 'vnc' and not self.context.consoles['vnc']:
                self.context.consoles.add(VncConsole(
                    self.context.__parent__.__parent__.hostname, int(console['port'])))

        # XXX TODO: handle removal of consoles when they are no longer reported from upstream
        # networks
        for interface in vm['interfaces']:
            if not self.context.interfaces[interface['name']]:
                iface = NetworkInterface(interface['name'], None, interface['mac'], 'active')
                if 'ipv4_address' in interface:
                    iface.ipv4_address = interface['ipv4_address']
                self.context.interfaces.add(iface)

        # XXX TODO: handle removal of interfaces when they are no longer reported from upstream
        # XXX hack, openvz specific
        compute.cpu_info = self.context.__parent__.__parent__.cpu_info
        compute.memory = vm['memory']

        diskspace = dict((unicode(k), v) for k, v in vm['diskspace'].items())
        diskspace[u'total'] = sum([0.0] + vm['diskspace'].values())

        # round diskspace values
        for i in diskspace:
            diskspace[i] = round(diskspace[i], 2)

        compute.diskspace = diskspace

        if compute.state == 'active':
            compute.uptime = get_f(vm, 'uptime')
        else:
            compute.uptime = None

        compute.num_cores = vm['vcpu']
        compute.swap_size = vm.get('swap') or compute.swap_size
        compute.kernel = vm.get('kernel') or compute.kernel

        with SuppressEvents(self.context):
            compute.apply()
Beispiel #6
0
    def get_computes(self, username):
        computes = db.get_root()['oms_root']['computes']
        user_computes = []
        for compute in map(follow_symlinks, computes.listcontent()):
            if not IVirtualCompute.providedBy(compute):
                continue

            if not compute.license_activated:
                continue

            if compute.__owner__ == username and IDeployed.providedBy(compute):
                user_computes.append(compute)

        return user_computes
Beispiel #7
0
def allocate_virtual_compute_from_hangar(model, event):
    if not IVirtualizationContainer.providedBy(model.__parent__):
        return

    if IDeployed.providedBy(model):
        return

    auto_allocate = get_config().getboolean('vms', 'auto_allocate', True)

    if not auto_allocate:
        return

    if IHangar.providedBy(model.__parent__.__parent__):
        action = AllocateAction
        msg = 'Allocated compute %s'
    elif ICompute.providedBy(model.__parent__.__parent__):
        action = DeployAction
        msg = 'Deployed compute %s'
    else:
        return

    try:
        path = canonical_path(model)
        owner = model.__owner__
        ul = UserLogger(subject=model, owner=owner)
        log.msg('Attempting %s for %s (%s, %s)' %
                (action.__name__, model, path, owner),
                system='create-event')
        d = task.deferLater(reactor, 2.0, virtual_compute_action, action, path,
                            event)
        d.addCallback(lambda r: ul.log(msg % path))
        if not get_config().getboolean('stats', 'only_report_on_sync', True):
            d.addCallback(lambda r: defer.maybeDeferred(
                getUtility(IUserStatisticsProvider).update, owner))
        d.addErrback(log.err)
    except Exception:
        log.err(system='create-event')
Beispiel #8
0
    def sync_vm(self, vm):
        compute = TmpObj(self.context)
        compute.state = unicode(vm['state'])

        # Ensure IDeployed marker is set, unless not in another state
        if not IDeployed.providedBy(compute):
            noLongerProvides(self.context, IUndeployed)
            noLongerProvides(self.context, IDeploying)
            alsoProvides(self.context, IDeployed)

        if 'ctid' in vm:
            compute.ctid = vm['ctid'] if vm['ctid'] != '-' else -1

        for idx, console in enumerate(vm['consoles']):
            if console['type'] == 'pty' and not self.context.consoles['tty%s' %
                                                                      idx]:
                self.context.consoles.add(
                    TtyConsole('tty%s' % idx, console['pty']))
            if console['type'] == 'openvz' and not self.context.consoles[
                    'tty%s' % idx]:
                self.context.consoles.add(
                    OpenVzConsole('tty%s' % idx, console['cid']))
            if console['type'] == 'vnc' and not self.context.consoles['vnc']:
                self.context.consoles.add(
                    VncConsole(self.context.__parent__.__parent__.hostname,
                               int(console['port'])))

        # XXX TODO: handle removal of consoles when they are no longer reported from upstream
        # networks
        for interface in vm['interfaces']:
            if not self.context.interfaces[interface['name']]:
                iface = NetworkInterface(interface['name'], None,
                                         interface['mac'], 'active')
                if 'ipv4_address' in interface:
                    iface.ipv4_address = interface['ipv4_address']
                self.context.interfaces.add(iface)

        # XXX TODO: handle removal of interfaces when they are no longer reported from upstream
        # XXX hack, openvz specific
        compute.cpu_info = self.context.__parent__.__parent__.cpu_info
        compute.memory = vm['memory']

        diskspace = dict((unicode(k), v) for k, v in vm['diskspace'].items())
        diskspace[u'total'] = sum([0.0] + vm['diskspace'].values())

        # round diskspace values
        for i in diskspace:
            diskspace[i] = round(diskspace[i], 2)

        compute.diskspace = diskspace

        if compute.state == 'active':
            compute.uptime = get_f(vm, 'uptime')
        else:
            compute.uptime = None

        compute.num_cores = vm['vcpu']
        compute.swap_size = vm.get('swap') or compute.swap_size
        compute.kernel = vm.get('kernel') or compute.kernel

        with SuppressEvents(self.context):
            compute.apply()