예제 #1
0
파일: virt.py 프로젝트: mcarden/shakenfist
    def delete(self):
        with util.RecordedOperation('delete domain', self) as _:
            try:
                self.power_off()

                instance = self._get_domain()
                instance.undefine()
            except Exception:
                pass

        with util.RecordedOperation('delete disks', self) as _:
            try:
                shutil.rmtree(self.instance_path)
            except Exception:
                pass

        with util.RecordedOperation('release network addreses', self) as _:
            for ni in db.get_instance_interfaces(self.db_entry['uuid']):
                with db.get_lock('sf/ipmanager/%s' % ni['network_uuid'],
                                 ttl=120) as _:
                    ipm = db.get_ipmanager(ni['network_uuid'])
                    ipm.release(ni['ipv4'])
                    db.persist_ipmanager(ni['network_uuid'], ipm.save())

        db.update_instance_state(self.db_entry['uuid'], 'deleted')
        db.free_console_port(self.db_entry['console_port'])
        db.free_console_port(self.db_entry['vdi_port'])
예제 #2
0
파일: main.py 프로젝트: mcarden/shakenfist
def restore_instances():
    # Ensure all instances for this node are defined
    networks = []
    instances = []
    for inst in list(
            db.get_instances(only_node=config.parsed.get('NODE_NAME'))):
        for iface in db.get_instance_interfaces(inst['uuid']):
            if not iface['network_uuid'] in networks:
                networks.append(iface['network_uuid'])
        instances.append(inst['uuid'])

    with util.RecordedOperation('restore networks', None) as _:
        for network in networks:
            try:
                n = net.from_db(network)
                LOG.info('%s Restoring network' % n)
                n.create()
                n.ensure_mesh()
                n.update_dhcp()
            except Exception as e:
                LOG.error('%s Failed to restore network: %s' % (n, e))

    with util.RecordedOperation('restore instances', None) as _:
        for instance in instances:
            try:
                i = virt.from_db(instance)
                LOG.info('%s Restoring instance' % i)
                i.create()
            except Exception as e:
                LOG.error('%s Failed to restore instance: %s' % (i, e))
                db.update_instance_state(instance, 'error')
예제 #3
0
    def delete(self):
        with util.RecordedOperation('delete domain', self):
            try:
                self.power_off()

                instance = self._get_domain()
                if instance:
                    instance.undefine()
            except Exception as e:
                util.ignore_exception('instance delete', e)

        with util.RecordedOperation('delete disks', self):
            try:
                if os.path.exists(self.instance_path):
                    shutil.rmtree(self.instance_path)
            except Exception as e:
                util.ignore_exception('instance delete', e)

        with util.RecordedOperation('release network addresses', self):
            for ni in db.get_instance_interfaces(self.db_entry['uuid']):
                db.update_network_interface_state(ni['uuid'], 'deleted')
                with db.get_lock('ipmanager',
                                 None,
                                 ni['network_uuid'],
                                 ttl=120,
                                 op='Instance delete'):
                    ipm = db.get_ipmanager(ni['network_uuid'])
                    ipm.release(ni['ipv4'])
                    db.persist_ipmanager(ni['network_uuid'], ipm.save())

        db.free_console_port(self.db_entry['console_port'])
        db.free_console_port(self.db_entry['vdi_port'])
예제 #4
0
    def delete(self, instance_uuid=None, instance_from_db_virt=None):
        db.add_event('instance', instance_uuid, 'API DELETE', None, None, None)

        instance_networks = []
        for iface in list(db.get_instance_interfaces(instance_uuid)):
            if not iface['network_uuid'] in instance_networks:
                instance_networks.append(iface['network_uuid'])

        host_networks = []
        for inst in list(db.get_instances(local_only=True)):
            if not inst['uuid'] == instance_uuid:
                for iface in db.get_instance_interfaces(inst['uuid']):
                    if not iface['network_uuid'] in host_networks:
                        host_networks.append(iface['network_uuid'])

        instance_from_db_virt.delete()

        for network in instance_networks:
            n = net.from_db(network)
            if n:
                if network in host_networks:
                    with util.RecordedOperation('deallocate ip address',
                                                instance_from_db_virt) as _:
                        n.update_dhcp()
                else:
                    with util.RecordedOperation('remove network', n) as _:
                        n.delete()
예제 #5
0
def instance_start(instance_uuid, network):
    log = LOG.withField('instance', instance_uuid)

    with db.get_lock('instance',
                     None,
                     instance_uuid,
                     ttl=900,
                     timeout=120,
                     op='Instance start') as lock:
        instance = virt.from_db(instance_uuid)

        # Collect the networks
        nets = {}
        for netdesc in network:
            if netdesc['network_uuid'] not in nets:
                n = net.from_db(netdesc['network_uuid'])
                if not n:
                    db.enqueue_instance_error(instance_uuid, 'missing network')
                    return

                nets[netdesc['network_uuid']] = n

        # Create the networks
        with util.RecordedOperation('ensure networks exist', instance):
            for network_uuid in nets:
                n = nets[network_uuid]
                try:
                    n.create()
                    n.ensure_mesh()
                    n.update_dhcp()
                except exceptions.DeadNetwork as e:
                    log.withField(
                        'network',
                        n).warning('Instance tried to use dead network')
                    db.enqueue_instance_error(
                        instance_uuid, 'tried to use dead network: %s' % e)
                    return

        # Allocate console and VDI ports
        instance.allocate_instance_ports()

        # Now we can start the instance
        libvirt = util.get_libvirt()
        try:
            with util.RecordedOperation('instance creation', instance):
                instance.create(lock=lock)

        except libvirt.libvirtError as e:
            code = e.get_error_code()
            if code in (libvirt.VIR_ERR_CONFIG_UNSUPPORTED,
                        libvirt.VIR_ERR_XML_ERROR):
                db.enqueue_instance_error(instance_uuid,
                                          'instance failed to start: %s' % e)
                return

        for iface in db.get_instance_interfaces(instance_uuid):
            db.update_network_interface_state(iface['uuid'], 'created')
예제 #6
0
파일: net.py 프로젝트: bradh/shakenfist
    def deploy_nat(self):
        if not self.provide_nat:
            return

        subst = self.subst_dict()
        floatnet = from_db('floating')
        if not self.floating_gateway:
            self.floating_gateway = floatnet.ipmanager.get_random_free_address(
            )
            self.persist_floating_gateway()
            floatnet.persist_ipmanager()

        subst['floating_router'] = floatnet.ipmanager.get_address_at_index(1)
        subst['floating_gateway'] = self.floating_gateway
        subst['floating_netmask'] = floatnet.netmask

        with lockutils.lock('sf_net_%s' % self.uuid,
                            external=True,
                            lock_path='/tmp/'):
            if not subst['floating_gateway'] in list(
                    util.get_interface_addresses(
                        subst['namespace'], subst['physical_veth_inner'])):
                with util.RecordedOperation('enable virtual routing',
                                            self) as _:
                    processutils.execute(
                        '%(in_namespace)s ip addr add %(floating_gateway)s/%(floating_netmask)s '
                        'dev %(physical_veth_inner)s' % subst,
                        shell=True)
                    processutils.execute(
                        '%(in_namespace)s ip link set %(physical_veth_inner)s up'
                        % subst,
                        shell=True)
                    processutils.execute(
                        '%(in_namespace)s route add default gw %(floating_router)s'
                        % subst,
                        shell=True)

            if not util.nat_rules_for_ipblock(self.ipmanager.network_address):
                with util.RecordedOperation('enable nat', self) as _:
                    processutils.execute(
                        'echo 1 > /proc/sys/net/ipv4/ip_forward', shell=True)
                    processutils.execute(
                        '%(in_namespace)s iptables -A FORWARD -o %(physical_veth_inner)s '
                        '-i %(vx_veth_inner)s -j ACCEPT' % subst,
                        shell=True)
                    processutils.execute(
                        '%(in_namespace)s iptables -A FORWARD -i %(physical_veth_inner)s '
                        '-o %(vx_veth_inner)s -j ACCEPT' % subst,
                        shell=True)
                    processutils.execute(
                        '%(in_namespace)s iptables -t nat -A POSTROUTING -s %(ipblock)s/%(netmask)s '
                        '-o %(physical_veth_inner)s -j MASQUERADE' % subst,
                        shell=True)
예제 #7
0
    def deploy_nat(self):
        if not self.provide_nat:
            return

        subst = self.subst_dict()
        if not self.floating_gateway:
            with db.get_lock('ipmanager', None, 'floating', ttl=120):
                ipm = db.get_ipmanager('floating')
                self.floating_gateway = ipm.get_random_free_address()
                db.persist_ipmanager('floating', ipm.save())
                self.persist_floating_gateway()

        # No lock because no data changing
        ipm = db.get_ipmanager('floating')
        subst['floating_router'] = ipm.get_address_at_index(1)
        subst['floating_gateway'] = self.floating_gateway
        subst['floating_netmask'] = ipm.netmask

        with db.get_lock('network', None, self.uuid, ttl=120):
            if not subst['floating_gateway'] in list(
                    util.get_interface_addresses(
                        subst['netns'], subst['physical_veth_inner'])):
                with util.RecordedOperation('enable virtual routing', self):
                    util.execute(
                        None,
                        '%(in_netns)s ip addr add %(floating_gateway)s/%(floating_netmask)s '
                        'dev %(physical_veth_inner)s' % subst)
                    util.execute(
                        None,
                        '%(in_netns)s ip link set %(physical_veth_inner)s up' %
                        subst)
                    util.execute(
                        None,
                        '%(in_netns)s route add default gw %(floating_router)s'
                        % subst)

            if not util.nat_rules_for_ipblock(self.network_address):
                with util.RecordedOperation('enable nat', self):
                    util.execute(None,
                                 'echo 1 > /proc/sys/net/ipv4/ip_forward')
                    util.execute(
                        None,
                        '%(in_netns)s iptables -A FORWARD -o %(physical_veth_inner)s '
                        '-i %(vx_veth_inner)s -j ACCEPT' % subst)
                    util.execute(
                        None,
                        '%(in_netns)s iptables -A FORWARD -i %(physical_veth_inner)s '
                        '-o %(vx_veth_inner)s -j ACCEPT' % subst)
                    util.execute(
                        None,
                        '%(in_netns)s iptables -t nat -A POSTROUTING -s %(ipblock)s/%(netmask)s '
                        '-o %(physical_veth_inner)s -j MASQUERADE' % subst)
예제 #8
0
    def ensure_mesh(self):
        with util.RecordedOperation('ensure mesh', self) as _:
            instances = []
            for iface in db.get_network_interfaces(self.uuid):
                if not iface['instance_uuid'] in instances:
                    instances.append(iface['instance_uuid'])

            node_fqdns = []
            for inst in instances:
                i = db.get_instance(inst)
                if not i['node'] in node_fqdns:
                    node_fqdns.append(i['node'])

            # NOTE(mikal): why not use DNS here? Well, DNS might be outside
            # the control of the deployer if we're running in a public cloud
            # as an overlay cloud...
            node_ips = [config.parsed.get('NETWORK_NODE_IP')]
            for fqdn in node_fqdns:
                ip = db.get_node(fqdn)['ip']
                if ip not in node_ips:
                    node_ips.append(ip)

            discovered = list(self.discover_mesh())
            LOG.debug('%s: Discovered mesh elements %s' % (self, discovered))
            for node in discovered:
                if node in node_ips:
                    node_ips.remove(node)
                else:
                    self._remove_mesh_element(node)

            for node in node_ips:
                self._add_mesh_element(node)
예제 #9
0
    def _get(self, locks, related_object):
        """Fetch image if not downloaded and return image path."""
        actual_image = self.version_image_path()

        with util.RecordedOperation('fetch image', related_object):
            resp = self._open_connection()

            diff_field = self._new_image_available(resp)
            if diff_field:
                self.log.withField(
                    'diff_field',
                    diff_field).info('Fetch required due HTTP field change')
                if related_object:
                    t, u = related_object.unique_label()
                    msg = '%s: %s -> %s' % diff_field
                    db.add_event(t, u, 'image requires fetch', None, None, msg)

                actual_image = self._fetch(resp, locks)

                # Ensure checksum is correct
                if not self.correct_checksum(actual_image):
                    raise exceptions.BadCheckSum('url=%s' % self.url)

        _transcode(locks, actual_image, related_object)

        return actual_image
예제 #10
0
    def snapshot(self, all=False):
        disks = self.db_entry['block_devices']['devices']
        if not all:
            disks = [disks[0]]

        snapshot_uuid = str(uuid.uuid4())
        snappath = os.path.join(self.snapshot_path, snapshot_uuid)
        if not os.path.exists(snappath):
            LOG.withObj(self).debug('Creating snapshot storage at %s' %
                                    snappath)
            os.makedirs(snappath)
            with open(os.path.join(self.snapshot_path, 'index.html'),
                      'w') as f:
                f.write('<html></html>')

        for d in disks:
            if not os.path.exists(d['path']):
                continue

            if d['snapshot_ignores']:
                continue

            if d['type'] != 'qcow2':
                continue

            with util.RecordedOperation('snapshot %s' % d['device'], self):
                self._snapshot_device(d['path'],
                                      os.path.join(snappath, d['device']))
                db.create_snapshot(snapshot_uuid, d['device'],
                                   self.db_entry['uuid'], time.time())

        return snapshot_uuid
예제 #11
0
    def _get(self, locks, related_object):
        """Fetch image if not downloaded and return image path."""
        with db.get_lock('image', config.parsed.get('NODE_NAME'),
                         self.hashed_image_url) as image_lock:
            with util.RecordedOperation('fetch image', related_object):
                dirty_fields, resp = self._requires_fetch()

                if dirty_fields:
                    logutil.info([self],
                                 'Starting fetch due to dirty fields %s' %
                                 dirty_fields)
                    if related_object:
                        t, u = related_object.get_describing_tuple()
                        dirty_fields_pretty = []
                        for field in dirty_fields:
                            dirty_fields_pretty.append(
                                '%s: %s -> %s' %
                                (field, dirty_fields[field]['before'],
                                 dirty_fields[field]['after']))
                        db.add_event(t, u, 'image requires fetch', None, None,
                                     '\n'.join(dirty_fields_pretty))
                    actual_image = self._fetch(resp,
                                               locks=locks.append(image_lock))
                else:
                    actual_image = '%s.v%03d' % (self.hashed_image_path,
                                                 self.info['version'])

            _transcode(locks, actual_image, related_object)

        return actual_image
예제 #12
0
def restore_instances():
    # Ensure all instances for this node are defined
    networks = []
    instances = []
    for inst in list(
            db.get_instances(only_node=config.parsed.get('NODE_NAME'))):
        for iface in db.get_instance_interfaces(inst['uuid']):
            if not iface['network_uuid'] in networks:
                networks.append(iface['network_uuid'])
        instances.append(inst['uuid'])

    with util.RecordedOperation('restore networks', None):
        for network in networks:
            try:
                n = net.from_db(network)
                LOG.withObj(n).info('Restoring network')
                n.create()
                n.ensure_mesh()
                n.update_dhcp()
            except Exception as e:
                util.ignore_exception('restore network %s' % network, e)

    with util.RecordedOperation('restore instances', None):
        for instance in instances:
            try:
                with db.get_lock('instance',
                                 None,
                                 instance,
                                 ttl=120,
                                 timeout=120,
                                 op='Instance restore'):
                    i = virt.from_db(instance)
                    if not i:
                        continue
                    started = ['on', 'transition-to-on', 'initial', 'unknown']
                    if i.db_entry.get('power_state', 'unknown') not in started:
                        continue

                    LOG.withObj(i).info('Restoring instance')
                    i.create()
            except Exception as e:
                util.ignore_exception('restore instance %s' % instance, e)
                db.enqueue_instance_error(
                    instance,
                    'exception while restoring instance on daemon restart')
예제 #13
0
파일: net.py 프로젝트: bradh/shakenfist
    def delete(self):
        subst = self.subst_dict()

        # Cleanup local node
        with lockutils.lock('sf_net_%s' % self.uuid,
                            external=True,
                            lock_path='/tmp/'):
            if util.check_for_interface(subst['vx_bridge']):
                with util.RecordedOperation('delete vxlan bridge', self) as _:
                    processutils.execute('ip link delete %(vx_bridge)s' %
                                         subst,
                                         shell=True)

            if util.check_for_interface(subst['vx_interface']):
                with util.RecordedOperation('delete vxlan interface',
                                            self) as _:
                    processutils.execute('ip link delete %(vx_interface)s' %
                                         subst,
                                         shell=True)

        # If this is the network node do additional cleanup
        if config.parsed.get('NODE_IP') == config.parsed.get(
                'NETWORK_NODE_IP'):
            if util.check_for_interface(subst['vx_veth_outer']):
                with util.RecordedOperation('delete router veth', self) as _:
                    processutils.execute('ip link delete %(vx_veth_outer)s' %
                                         subst,
                                         shell=True)

            if util.check_for_interface(subst['physical_veth_outer']):
                with util.RecordedOperation('delete physical veth', self) as _:
                    processutils.execute(
                        'ip link delete %(physical_veth_outer)s' % subst,
                        shell=True)

            if os.path.exists('/var/run/netns/%(namespace)s' % subst):
                with util.RecordedOperation('delete netns', self) as _:
                    processutils.execute('ip netns del %(namespace)s' % subst,
                                         shell=True)

            if self.floating_gateway:
                floatnet = from_db('floating')
                floatnet.ipmanager.release(self.floating_gateway)
                floatnet.persist_ipmanager()
예제 #14
0
    def post(self, url=None):
        db.add_event('image', url, 'api', 'cache', None, None)

        with util.RecordedOperation('cache image', url) as _:
            image_url = images.resolve(url)
            hashed_image_path, info, image_dirty, resp = \
                images.requires_fetch(image_url)

            if image_dirty:
                images.fetch(hashed_image_path, info, resp)
예제 #15
0
def instance_start(instance_uuid, network):
    with db.get_lock('instance', None, instance_uuid, ttl=900) as lock:
        instance = virt.from_db(instance_uuid)

        # Collect the networks
        nets = {}
        for netdesc in network:
            if netdesc['network_uuid'] not in nets:
                n = net.from_db(netdesc['network_uuid'])
                if not n:
                    db.enqueue_instance_delete(config.parsed.get('NODE_NAME'),
                                               instance_uuid, 'error',
                                               'missing network')
                    return

                nets[netdesc['network_uuid']] = n

        # Create the networks
        with util.RecordedOperation('ensure networks exist', instance):
            for network_uuid in nets:
                n = nets[network_uuid]
                n.create()
                n.ensure_mesh()
                n.update_dhcp()

        # Now we can start the isntance
        libvirt = util.get_libvirt()
        try:
            with util.RecordedOperation('instance creation', instance):
                instance.create(lock=lock)

        except libvirt.libvirtError as e:
            code = e.get_error_code()
            if code in (libvirt.VIR_ERR_CONFIG_UNSUPPORTED,
                        libvirt.VIR_ERR_XML_ERROR):
                db.enqueue_instance_delete(config.parsed.get('NODE_NAME'),
                                           instance_uuid, 'error',
                                           'instance failed to start')
                return

        for iface in db.get_instance_interfaces(instance_uuid):
            db.update_network_interface_state(iface['uuid'], 'created')
예제 #16
0
    def delete(self):
        with util.RecordedOperation('delete domain', self) as _:
            try:
                self.power_off()
            except:
                pass

        with util.RecordedOperation('delete disks', self) as _:
            try:
                shutil.rmtree(self.instance_path)
            except:
                pass

        with util.RecordedOperation('release network addreses', self) as _:
            for ni in db.get_instance_interfaces(self.db_entry['uuid']):
                n = net.from_db(ni['network_uuid'])
                n.ipmanager.release(ni['ipv4'])
                n.persist_ipmanager()

        db.delete_instance(self.db_entry['uuid'])
예제 #17
0
    def delete(self):
        subst = self.subst_dict()
        LOG.withFields(subst).debug('net.delete()')

        # Cleanup local node
        with db.get_object_lock(self, ttl=120, op='Network delete'):
            if util.check_for_interface(subst['vx_bridge']):
                with util.RecordedOperation('delete vxlan bridge', self):
                    util.execute(None, 'ip link delete %(vx_bridge)s' % subst)

            if util.check_for_interface(subst['vx_interface']):
                with util.RecordedOperation('delete vxlan interface', self):
                    util.execute(None,
                                 'ip link delete %(vx_interface)s' % subst)

            # If this is the network node do additional cleanup
            if util.is_network_node():
                if util.check_for_interface(subst['vx_veth_outer']):
                    with util.RecordedOperation('delete router veth', self):
                        util.execute(
                            None, 'ip link delete %(vx_veth_outer)s' % subst)

                if util.check_for_interface(subst['physical_veth_outer']):
                    with util.RecordedOperation('delete physical veth', self):
                        util.execute(
                            None,
                            'ip link delete %(physical_veth_outer)s' % subst)

                if os.path.exists('/var/run/netns/%(netns)s' % subst):
                    with util.RecordedOperation('delete netns', self):
                        util.execute(None, 'ip netns del %(netns)s' % subst)

                if self.db_entry['floating_gateway']:
                    with db.get_lock('ipmanager',
                                     None,
                                     'floating',
                                     ttl=120,
                                     op='Network delete'):
                        ipm = db.get_ipmanager('floating')
                        ipm.release(self.db_entry['floating_gateway'])
                        db.persist_ipmanager('floating', ipm.save())
예제 #18
0
    def discover_mesh(self):
        mesh_re = re.compile(r'00:00:00:00:00:00 dst (.*) self permanent')

        with util.RecordedOperation('discover mesh', self) as _:
            stdout, _ = processutils.execute(
                'bridge fdb show brport %(vx_interface)s' % self.subst_dict(),
                shell=True)

            for line in stdout.split('\n'):
                m = mesh_re.match(line)
                if m:
                    yield m.group(1)
예제 #19
0
def instance_delete(instance_uuid):
    with db.get_lock('instance',
                     None,
                     instance_uuid,
                     timeout=120,
                     op='Instance delete'):
        db.add_event('instance', instance_uuid, 'queued', 'delete', None, None)

        # Create list of networks used by instance
        instance_networks = []
        for iface in list(db.get_instance_interfaces(instance_uuid)):
            if not iface['network_uuid'] in instance_networks:
                instance_networks.append(iface['network_uuid'])

        # Create list of networks used by all other instances
        host_networks = []
        for inst in list(
                db.get_instances(only_node=config.parsed.get('NODE_NAME'))):
            if not inst['uuid'] == instance_uuid:
                for iface in db.get_instance_interfaces(inst['uuid']):
                    if not iface['network_uuid'] in host_networks:
                        host_networks.append(iface['network_uuid'])

        instance_from_db_virt = virt.from_db(instance_uuid)
        if instance_from_db_virt:
            instance_from_db_virt.delete()

        # Check each network used by the deleted instance
        for network in instance_networks:
            n = net.from_db(network)
            if n:
                # If network used by another instance, only update
                if network in host_networks:
                    with util.RecordedOperation('deallocate ip address',
                                                instance_from_db_virt):
                        n.update_dhcp()
                else:
                    # Network not used by any other instance therefore delete
                    with util.RecordedOperation('remove network', n):
                        n.delete()
예제 #20
0
    def delete(self,
               instance_uuid=None,
               instance_from_db=None,
               instance_from_db_virt=None):
        # Check if instance has already been deleted
        if instance_from_db['state'] == 'deleted':
            return error(404, 'instance not found')

        with db.get_lock('/sf/instance/%s' % instance_uuid) as _:
            db.add_event('instance', instance_uuid, 'api', 'delete', None,
                         None)

            instance_networks = []
            for iface in list(db.get_instance_interfaces(instance_uuid)):
                if not iface['network_uuid'] in instance_networks:
                    instance_networks.append(iface['network_uuid'])
                    db.update_network_interface_state(iface['uuid'], 'deleted')

            host_networks = []
            for inst in list(
                    db.get_instances(
                        only_node=config.parsed.get('NODE_NAME'))):
                if not inst['uuid'] == instance_uuid:
                    for iface in db.get_instance_interfaces(inst['uuid']):
                        if not iface['network_uuid'] in host_networks:
                            host_networks.append(iface['network_uuid'])

            instance_from_db_virt.delete()

            for network in instance_networks:
                n = net.from_db(network)
                if n:
                    if network in host_networks:
                        with util.RecordedOperation(
                                'deallocate ip address',
                                instance_from_db_virt) as _:
                            n.update_dhcp()
                    else:
                        with util.RecordedOperation('remove network', n) as _:
                            n.delete()
예제 #21
0
파일: images.py 프로젝트: neillc/shakenfist
def _transcode(locks, actual_image, related_object):
    with util.RecordedOperation('transcode image', related_object):
        if os.path.exists(actual_image + '.qcow2'):
            return

        current_format = identify(actual_image).get('file format')
        if current_format == 'qcow2':
            os.link(actual_image, actual_image + '.qcow2')
            return

        util.execute(
            locks, 'qemu-img convert -t none -O qcow2 %s %s.qcow2' %
            (actual_image, actual_image))
예제 #22
0
 def remove_dhcp(self):
     if util.is_network_node():
         subst = self.subst_dict()
         with util.RecordedOperation('remove dhcp', self):
             with db.get_lock('network', None, self.uuid, ttl=120):
                 d = dhcp.DHCP(self.uuid, subst['vx_veth_inner'])
                 d.remove_dhcpd()
     else:
         db.enqueue('networknode', {
             'type': 'remove_dhcp',
             'network_uuid': self.uuid
         })
         db.add_event('network', self.uuid, 'remove dhcp', 'enqueued', None,
                      None)
예제 #23
0
 def remove_dhcp(self):
     if util.is_network_node():
         subst = self.subst_dict()
         with util.RecordedOperation('remove dhcp', self):
             with db.get_object_lock(self,
                                     ttl=120,
                                     op='Network remove DHCP'):
                 d = dhcp.DHCP(self, subst['vx_veth_inner'])
                 d.remove_dhcpd()
     else:
         db.enqueue('networknode',
                    RemoveDHCPNetworkTask(self.db_entry['uuid']))
         db.add_event('network', self.db_entry['uuid'], 'remove dhcp',
                      'enqueued', None, None)
예제 #24
0
def restore_instances():
    # Ensure all instances for this node are defined
    networks = []
    instances = []
    for inst in list(db.get_instances(local_only=True)):
        for iface in db.get_instance_interfaces(inst['uuid']):
            if not iface['network_uuid'] in networks:
                networks.append(iface['network_uuid'])
        instances.append(inst['uuid'])

    with util.RecordedOperation('restore networks', None) as _:
        for network in networks:
            LOG.info('Restoring network %s' % network)
            n = net.from_db(network)
            n.create()
            n.ensure_mesh()
            n.update_dhcp()

    with util.RecordedOperation('restore instances', None) as _:
        for instance in instances:
            LOG.info('Restoring instance %s' % instance)
            i = virt.from_db(instance)
            i.create()
예제 #25
0
    def delete(self):
        subst = self.subst_dict()

        # Cleanup local node
        with db.get_lock('sf/net/%s' % self.uuid, ttl=120) as _:
            if util.check_for_interface(subst['vx_bridge']):
                with util.RecordedOperation('delete vxlan bridge', self) as _:
                    processutils.execute('ip link delete %(vx_bridge)s' % subst,
                                         shell=True)

            if util.check_for_interface(subst['vx_interface']):
                with util.RecordedOperation('delete vxlan interface', self) as _:
                    processutils.execute('ip link delete %(vx_interface)s' % subst,
                                         shell=True)

            # If this is the network node do additional cleanup
            if config.parsed.get('NODE_IP') == config.parsed.get('NETWORK_NODE_IP'):
                if util.check_for_interface(subst['vx_veth_outer']):
                    with util.RecordedOperation('delete router veth', self) as _:
                        processutils.execute('ip link delete %(vx_veth_outer)s' % subst,
                                             shell=True)

                if util.check_for_interface(subst['physical_veth_outer']):
                    with util.RecordedOperation('delete physical veth', self) as _:
                        processutils.execute('ip link delete %(physical_veth_outer)s' % subst,
                                             shell=True)

                if os.path.exists('/var/run/netns/%(netns)s' % subst):
                    with util.RecordedOperation('delete netns', self) as _:
                        processutils.execute('ip netns del %(netns)s' % subst,
                                             shell=True)

                if self.floating_gateway:
                    with db.get_lock('sf/ipmanager/floating', ttl=120) as _:
                        ipm = db.get_ipmanager('floating')
                        ipm.release(self.floating_gateway)
                        db.persist_ipmanager('floating', ipm.save())
예제 #26
0
파일: net.py 프로젝트: bradh/shakenfist
 def remove_dhcp(self):
     if config.parsed.get('NODE_IP') == config.parsed.get(
             'NETWORK_NODE_IP'):
         subst = self.subst_dict()
         with util.RecordedOperation('remove dhcp', self) as _:
             with lockutils.lock('sf_net_%s' % self.uuid,
                                 external=True,
                                 lock_path='/tmp/'):
                 d = dhcp.DHCP(self.uuid, subst['vx_veth_inner'])
                 d.remove_dhcpd()
     else:
         requests.request('put', ('http://%s:%d/remove_dhcp' %
                                  (config.parsed.get('NETWORK_NODE_IP'),
                                   config.parsed.get('API_PORT'))),
                          data=json.dumps({'uuid': self.uuid}))
예제 #27
0
    def update_dhcp(self):
        if not self.db_entry['provide_dhcp']:
            return

        if util.is_network_node():
            subst = self.subst_dict()
            with util.RecordedOperation('update dhcp', self):
                with db.get_object_lock(self,
                                        ttl=120,
                                        op='Network update DHCP'):
                    d = dhcp.DHCP(self, subst['vx_veth_inner'])
                    d.restart_dhcpd()
        else:
            db.enqueue('networknode',
                       UpdateDHCPNetworkTask(self.db_entry['uuid']))
            db.add_event('network', self.db_entry['uuid'], 'update dhcp',
                         'enqueued', None, None)
예제 #28
0
    def update_dhcp(self):
        if not self.provide_dhcp:
            return

        if util.is_network_node():
            self.ensure_mesh()
            subst = self.subst_dict()
            with util.RecordedOperation('update dhcp', self):
                with db.get_lock('network', None, self.uuid, ttl=120):
                    d = dhcp.DHCP(self.uuid, subst['vx_veth_inner'])
                    d.restart_dhcpd()
        else:
            db.enqueue('networknode', {
                'type': 'update_dhcp',
                'network_uuid': self.uuid
            })
            db.add_event('network', self.uuid, 'update dhcp', 'enqueued', None,
                         None)
예제 #29
0
 def remove_dhcp(self):
     if config.parsed.get('NODE_IP') == config.parsed.get('NETWORK_NODE_IP'):
         subst = self.subst_dict()
         with util.RecordedOperation('remove dhcp', self) as _:
             with db.get_lock('sf/net/%s' % self.uuid, ttl=120) as _:
                 d = dhcp.DHCP(self.uuid, subst['vx_veth_inner'])
                 d.remove_dhcpd()
     else:
         admin_token = util.get_api_token(
             'http://%s:%d' % (config.parsed.get('NETWORK_NODE_IP'),
                               config.parsed.get('API_PORT')),
             namespace='system')
         requests.request(
             'put',
             ('http://%s:%d/remove_dhcp'
              % (config.parsed.get('NETWORK_NODE_IP'),
                 config.parsed.get('API_PORT'))),
             data=json.dumps({'uuid': self.uuid}),
             headers={'Authorization': admin_token,
                      'User-Agent': util.get_user_agent()})
예제 #30
0
파일: images.py 프로젝트: neillc/shakenfist
    def _get(self, locks, related_object):
        """Fetch image if not downloaded and return image path."""
        actual_image = self.version_image_path()

        with util.RecordedOperation('fetch image', related_object):
            resp = self._open_connection()

            diff_field = self._new_image_available(resp)
            if diff_field:
                self.log.withField(
                    'diff_field',
                    diff_field).info('Fetch required due HTTP field change')
                if related_object:
                    t, u = related_object.unique_label()
                    msg = '%s: %s -> %s' % diff_field
                    db.add_event(t, u, 'image requires fetch', None, None, msg)

                actual_image = self._fetch(resp, locks)

                # Ensure checksum is correct
                if not self.correct_checksum(actual_image):
                    if isinstance(related_object, virt.Instance):
                        related_object.add_event('fetch image', 'bad checksum')
                    raise exceptions.BadCheckSum('url=%s' % self.url)

                # Only persist values after the file has been verified.
                # Otherwise diff_field will not trigger a new download in the
                # case of a checksum verification failure.
                self.fetched = email.utils.formatdate()
                self.modified = resp.headers.get('Last-Modified')
                self.size = resp.headers.get('Content-Length')
                self.persist()

        _transcode(locks, actual_image, related_object)

        return actual_image