Esempio n. 1
0
 def _get_current_resource(self, uuid, values):
     try:
         r_type = json.loads(values['type']).replace('_', '-')
         return Resource(r_type, uuid=uuid)
     except KeyError:
         printo("[%s] incomplete, no type" % uuid)
         return False
Esempio n. 2
0
 def wrapper(*args):
     if JSON_OUTPUT:
         old_stdout = sys.stdout
         sys.stdout = my_stdout = StringIO()
     start = time.time()
     try:
         r = fun(*args)
     except (Exception, NotFound) as e:
         if JSON_OUTPUT:
             r = -1
             printo(text_type(e))
         else:
             raise
     end = time.time()
     if JSON_OUTPUT:
         sys.stdout = old_stdout
         if r == -1:
             total = -1
         elif isinstance(r, list):
             total = len(r)
         else:
             total = 1
         printo(
             json_log(fun, total, my_stdout.getvalue(),
                      (end - start) * 1000.0))
         my_stdout.close()
     return r
Esempio n. 3
0
    def __call__(self,
                 uuids=None,
                 cassandra_servers=None,
                 force=False,
                 **kwargs):
        super(CheckBadRefs, self).__call__(**kwargs)
        self.force = force
        pool = ConnectionPool('config_db_uuid', server_list=cassandra_servers)
        uuid_cf = ColumnFamily(pool, 'obj_uuid_table')
        if uuids:

            def uuids_g():
                for uuid in uuids:
                    yield uuid
        else:

            def uuids_g():
                for k, v in uuid_cf.get_range(column_count=1,
                                              filter_empty=True):
                    yield k

        for uuid in uuids_g():
            values = dict(uuid_cf.xget(uuid))
            res = self._get_current_resource(uuid, values)
            bad_refs = self._check_resource_refs(uuid, values)
            if not res or bad_refs:
                printo(self._props_to_json(values))
            if not res and not self.check:
                if self.force or continue_prompt(message="Delete ?"):
                    self._delete(uuid_cf, uuid)
Esempio n. 4
0
    def wrapper(*args):
        old_stdout = sys.stdout
        sys.stdout = my_stdout = StringIO()
        root = logging.getLogger()
        ch = logging.StreamHandler(my_stdout)
        ch.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        root.addHandler(ch)

        def cleanup():
            sys.stdout = old_stdout
            root.removeHandler(ch)
            output = my_stdout.getvalue()
            my_stdout.close()
            return output

        try:
            fun(*args)
            output = cleanup()
            printo(output)
            # return a list for log_json count
            return range(1, output.count('\n'))
        except CommandError as e:
            raise CommandError("%s:\n%s" % (text_type(e), cleanup()))
Esempio n. 5
0
 def wrapper(*args):
     r = fun(*args)
     if len(r) > 1:
         printo('Found %d %s:' % (len(r), fun.__doc__.strip()))
         for r_ in r:
             printo('  - %s/%s - %s' % (r_.type, r_.uuid, r_.fq_name))
     return r
Esempio n. 6
0
 def __call__(self, **kwargs):
     super(MigrateHostRoutes, self).__call__(**kwargs)
     for irt in self.resources:
         if irt.fq_name[-1].startswith(NEUTRON_IRT_PREFIX):
             printo("Found IRT to migrate %s" % irt.fq_name)
             if not self.check:
                 self._migrate(irt)
Esempio n. 7
0
def clean_acl_without_sg(acls):
    for acl in acls:
        try:
            acl.delete()
            printo('Deleted %s' % acl)
        except ResourceNotFound:
            continue
Esempio n. 8
0
def clean_vn_with_iip_without_vmi(iips):
    for iip in iips:
        try:
            iip.delete()
            printo('Deleted %s' % iip)
        except ResourceNotFound:
            continue
Esempio n. 9
0
    def _is_stale_snat(self, si):
        """Return True if the snat SI is stale.
        """
        if 'logical_router_back_refs' not in si:
            printo('[%s] No logical router attached to SI' % si.uuid)
            return True

        return False
Esempio n. 10
0
 def _check_ref(self, ref, uuid):
     _, ref_type, ref_uuid = ref.split(':')
     try:
         Resource(ref_type.replace('_', '-'), uuid=ref_uuid, check=True)
         return False
     except ResourceNotFound:
         printo("[%s] broken ref to missing %s" % (uuid, ref))
         return True
Esempio n. 11
0
 def _clean_kv(self, session, stale_subnets, dry_run=False):
     if continue_prompt("Do you really want to delete these entries ?"):
         for key in stale_subnets:
             if not dry_run:
                 session.remove_kv_store(key)
             printo(
                 "Entry with key \"%s\" has been removed from the KV store"
                 % key)
Esempio n. 12
0
 def test_printo(self):
     origout = sys.stdout
     tmpout = io.BytesIO()
     sys.stdout = tmpout
     utils.printo('é', encoding='ascii')
     utils.printo('é', encoding='utf-8')
     sys.stdout = origout
     self.assertEqual(tmpout.getvalue(), b'?\n\xc3\xa9\n')
Esempio n. 13
0
 def test_printo(self):
     origout = sys.stdout
     tmpout = io.BytesIO()
     sys.stdout = tmpout
     utils.printo('é', encoding='ascii')
     utils.printo('é', encoding='utf-8')
     sys.stdout = origout
     self.assertEqual(tmpout.getvalue(), b'?\n\xc3\xa9\n')
Esempio n. 14
0
def clean_ri_without_vn(ris):
    # This will leave RTs, but check_unused_rt will remove
    # them later
    for ri in ris:
        try:
            ri.delete()
            printo('Deleted %s' % ri)
        except ResourceNotFound:
            pass
Esempio n. 15
0
 def add(self, vn, mode, name):
     prop = mode_map[mode]
     if prop not in vn:
         vn[prop] = {'route_target': []}
     if name in vn[prop]['route_target']:
         raise CommandError('RT %s already added' % name)
     vn[prop]['route_target'].append(name)
     vn.save()
     printo('RT %s added to VN' % name)
Esempio n. 16
0
def check_duplicate_public_ips(g):
    """duplicate public ips
    """
    r = g.V().hasLabel(within('floating_ip', 'instance_ip')) \
        .property('ip_address', __.values('floating_ip_address', 'instance_ip_address')).group().by('ip_address').unfold() \
        .filter(lambda: "it.get().value.size() > 1 && it.get().value.findAll{it.label.value == 'floating_ip'} != []").toList()
    if len(r) > 0:
        printo('Found %d %s:' % (len(r), check_duplicate_public_ips.__doc__.strip()))
    return r
Esempio n. 17
0
 def _get_stale_subnets(self, kv, healthy_kv):
     stale_subnets = []
     for _dict in kv:
         key = _dict['key']
         value = _dict['value']
         if not (key in healthy_kv and str(value) == healthy_kv[key]):
             stale_subnets.append(key)
     printo("Current KV store contains %d stale entries." %
            len(stale_subnets))
     return stale_subnets
Esempio n. 18
0
 def _check(self, project):
     if project.fq_name == FQName('default-domain:default-project'):
         return
     keystone_uuid = project.uuid.replace('-', '')
     try:
         self.kclient.tenants.get(keystone_uuid)
     except HttpError as e:
         if e.http_status == 404:
             printo(self.current_path(project))
         else:
             raise
Esempio n. 19
0
 def delete(self, vn, mode, name):
     prop = mode_map[mode]
     rt_list = vn.get(prop, {}).get('route_target', [])
     try:
         rt_list.remove(name)
     except ValueError:
         printo('RT %s not found on VN' % name)
         return
     vn[prop]['route_target'] = rt_list
     vn.save()
     printo('RT %s deleted from VN' % name)
Esempio n. 20
0
 def _clean_lbaas_si(self, si):
     printo('[%s] Cleaning stale lbaas' % si.uuid)
     for pool in si.get('loadbalancer_pool_back_refs', []):
         pool.fetch()
         for vip in pool.get('virtual_ip_back_refs', []):
             vip.fetch()
             vip_vmis = vip.get('virtual_machine_interface_refs', [])
             self._delete_res(si, vip)
             for vmi in vip_vmis:
                 self._delete_res(si, vmi)
         self._remove_back_ref(si, si, pool)
Esempio n. 21
0
 def _remove_vm(self, vmi, parent):
     logger.debug("trying to remove vmi vms")
     # VMs are not linked to the project
     vmi.fetch()
     for vm in vmi.refs.virtual_machine:
         if continue_prompt("Nova VM %s will be deleted" % vm.uuid):
             printo("deleting nova VM %s" % vm.uuid)
             self.nclient.servers.delete(vm.uuid)
         else:
             raise CommandError("Exiting.")
     self._delete(vmi)
Esempio n. 22
0
 def _delete(self, resource, parent=None):
     try:
         logger.debug("trying to delete %s" % self.current_path(resource))
         resource.delete()
         printo("%s deleted" % self.current_path(resource))
     except (ChildrenExists, BackRefsExists) as e:
         logger.debug("failed: %s" % e)
         action = self.actions[e.__class__].get(
             (resource.type, e.resources[0].type), self._delete)
         for r in e.resources:
             action(r, resource)
         self._delete(resource)
     except ResourceNotFound:
         pass
Esempio n. 23
0
 def _check_si(self, si):
     try:
         si.fetch()
     except ResourceNotFound:
         return
     for vm in si.get('virtual_machine_back_refs', []):
         vm.fetch()
         if len(vm.get('virtual_router_back_refs', [])) > 1:
             printo('SI %s VM %s is scheduled on %s' % (
                 si.path,
                 vm.uuid,
                 ", ".join([str(vr.fq_name) for vr in vm['virtual_router_back_refs']])))
             if self.check is not True:
                 self._clean_vm(vm)
Esempio n. 24
0
def check_duplicate_ip_addresses(g):
    """networks with duplicate ip addresses
    """
    r = g.V().hasLabel("virtual_network").as_('vn').flatMap(
        union(
            select('vn'),
            __.in_().hasLabel("instance_ip").has("instance_ip_address").group(
            ).by("instance_ip_address").unfold().filter(
                lambda: "it.get().value.size() > 1")).fold().filter(
                    lambda: "it.get().size() > 1")).toList()
    if len(r) > 0:
        printo('Found %d %s:' %
               (len(r), check_duplicate_ip_addresses.__doc__.strip()))
    for dup in r:
        # FIXME:
        dup[0].label = 'virtual_network'
        # First item is the vn
        r_ = v_to_r(dup[0])
        printo('  - %s/%s - %s' % (r_.type, r_.uuid, r_.fq_name))
        for ips in dup[1:]:
            for ip, iips in ips.items():
                printo("      %s:" % ip)
                for iip in iips:
                    r_ = v_to_r(iip)
                    printo('        - %s/%s - %s' %
                           (r_.type, r_.uuid, r_.fq_name))
    return r
Esempio n. 25
0
    def __call__(self, paths=None, **kwargs):
        super(MigrateSI110221, self).__call__(**kwargs)
        if not paths:
            resources = Collection('service-instance', fetch=True)
        else:
            resources = expand_paths(
                paths, predicate=lambda r: r.type == 'service-instance')
        for si in resources:
            si.fetch()

            try:
                si['service_template_refs'][0]
            except (KeyError, IndexError):
                printo('SI %s has no template, skipping.' % si.uuid)
                continue
            vms = si.get('virtual_machine_back_refs', [])
            if not vms:
                printo('SI %s has no VM, skipping.' % si.uuid)

            if all([si.fq_name[-1] in str(vm.fq_name) for vm in vms]):
                continue

            printo('Found lbaas SI to migrate %s (%s)' % (si.path, si.fq_name))
            if not self.check:
                self._migrate_si(si)
                printo('Done')
Esempio n. 26
0
    def _is_stale_lbaas(self, si):
        """Return True if the lbaas SI is stale.
        """

        if 'loadbalancer_pool_back_refs' not in si:
            printo('[%s] No pool attached to SI' % si.uuid)
            return True

        pool = si['loadbalancer_pool_back_refs'][0]
        pool.fetch()

        if 'virtual_ip_back_refs' not in pool:
            printo('[%s] No VIP attached to pool' % si.uuid)
            return True

        vip = pool['virtual_ip_back_refs'][0]
        vip.fetch()

        if 'virtual_machine_interface_refs' not in vip:
            printo('[%s] No VMI for VIP' % si.uuid)
            return True

        vip_vmi = vip['virtual_machine_interface_refs'][0]
        vip_vmi.fetch()

        if 'instance_ip_back_refs' not in vip_vmi:
            printo('[%s] No IIP found for VIP VMI' % si.uuid)
            return True

        return False
 def chk(self, item):
     vn_uuid, subnet_uuid, subnet = item
     to_add = []
     if self.subnet_uuid is not None and self.subnet_uuid != subnet_uuid:
         return to_add
     subnet_key = self._subnet_key(vn_uuid, subnet)
     try:
         self.session.search_kv_store(subnet_uuid)
     except HttpError:
         printo('Missing key %s for subnet %s' % (subnet_uuid, subnet_uuid))
         to_add.append((subnet_uuid, subnet_key))
     try:
         self.session.search_kv_store(subnet_key)
     except HttpError:
         printo('Missing key %s for subnet %s' % (subnet_key, subnet_uuid))
         to_add.append((subnet_key, subnet_uuid))
     return to_add
Esempio n. 28
0
    def _migrate_iip(self, old_iip, si, itf_type):
        new_fq_name = '%s__%s-%s' % (str(si.parent.fq_name).replace(
            ':', '__'), si.fq_name[-1], itf_type)

        # there is 2 left itf so migrate the iip only once
        if str(old_iip.fq_name) == new_fq_name:
            printo('Already migrated %s. Skip' % old_iip.fq_name)
            return old_iip

        self._delete_res(old_iip)
        iip = copy.deepcopy(old_iip)

        del iip['uuid']
        iip['fq_name'] = FQName([new_fq_name])
        iip['display_name'] = new_fq_name
        iip['name'] = new_fq_name
        self._create_res(iip)

        return iip
Esempio n. 29
0
 def __call__(self, on=False, off=False, **kwargs):
     super(RPF, self).__call__(**kwargs)
     for vn in self.resources:
         vn.fetch()
         if 'virtual_network_properties' not in vn:
             vn['virtual_network_properties'] = {
                 "allow_transit": None,
                 "forwarding_mode": None,
                 "network_id": vn.get('virtual_network_id', None),
                 "vxlan_network_identifier": None
             }
         if off:
             vn['virtual_network_properties']['rpf'] = 'disable'
             vn.save()
         elif on:
             vn['virtual_network_properties']['rpf'] = None
             vn.save()
         else:
             status = 'on' if vn['virtual_network_properties'].get(
                 'rpf') is None else 'off'
             printo("%s : %s" % (self.current_path(vn), status))
Esempio n. 30
0
    def show(self, vn):
        vns = [vn.uuid]
        import_export_list = vn.get('route_target_list',
                                    {}).get('route_target', [])
        import_list = vn.get('import_route_target_list',
                             {}).get('route_target', [])
        export_list = vn.get('export_route_target_list',
                             {}).get('route_target', [])

        table = [
            ["VN", "import/export", "import", "export"],
        ]

        def add_row(vn, import_export_rt, import_rt, export_rt):
            table.append([
                vn if vn else "", import_export_rt if import_export_rt else "",
                import_rt if import_rt else "", export_rt if export_rt else ""
            ])

        map(add_row, vns, import_export_list, import_list, export_list)
        printo(format_table(table))
Esempio n. 31
0
    def _migrate_si(self, old_si):
        si_target = old_si.get('loadbalancer_pool_back_refs', [None])[0]
        if si_target is not None:
            si_target_uuid = si_target.uuid
        elif si_target is None:
            si_target = old_si.get('logical_router_back_refs', [None])[0]
            si_target_uuid = old_si.fq_name[-1]

        if si_target is None:
            printo('No pool or router attached to SI')
            return

        new_si = Resource(
            'service-instance',
            fq_name=list(old_si.parent.fq_name) + [si_target_uuid],
            display_name=si_target_uuid,
            service_instance_properties=old_si['service_instance_properties'],
            parent=old_si.parent)
        new_si['service_template_refs'] = old_si['service_template_refs']

        # SNAT si fq_name doesn't change so we create the same
        # resource and it will conflict
        re_use = False
        try:
            self._create_res(new_si)
        except Conflict:
            new_si = old_si
            re_use = True

        for old_vm in old_si.get('virtual_machine_back_refs', []):
            old_vm.fetch()
            self._remove_back_ref(old_si, old_vm)
            # VM index is the last char of the fq_name
            new_vm = self._migrate_vm(old_vm, str(old_vm.fq_name)[-1], new_si)
            self._add_back_ref(new_si, new_vm)

        if not re_use:
            self._remove_back_ref(old_si, si_target)
            self._delete_res(old_si)
            self._add_back_ref(new_si, si_target)
Esempio n. 32
0
    def test_set_std_printo(self):
        FAKE_STRING_NEW_LINE = b"fake string\n"
        FAKE_STRING_NO_NEW_LINE = "fake string"
        orig_stdout = sys.stdout
        orig_stderr = sys.stdout
        fake_stdout = io.BytesIO()
        fake_stderr = io.BytesIO()
        sys.stdout = fake_stdout
        sys.stderr = fake_stderr

        def clear_buffers():
            fake_stdout.truncate(0)
            fake_stdout.seek(0)
            fake_stderr.truncate(0)
            fake_stderr.seek(0)

        utils.printo(FAKE_STRING_NO_NEW_LINE)
        self.assertEqual(fake_stdout.getvalue(), FAKE_STRING_NEW_LINE)
        self.assertEqual(fake_stderr.getvalue(), b'')
        clear_buffers()

        utils.printo(FAKE_STRING_NO_NEW_LINE, std_type='stdout')
        self.assertEqual(fake_stdout.getvalue(), FAKE_STRING_NEW_LINE)
        self.assertEqual(fake_stderr.getvalue(), b'')
        clear_buffers()

        utils.printo(FAKE_STRING_NO_NEW_LINE, std_type='stderr')
        self.assertEqual(fake_stdout.getvalue(), b'')
        self.assertEqual(fake_stderr.getvalue(), FAKE_STRING_NEW_LINE)
        clear_buffers()

        utils.printo(FAKE_STRING_NO_NEW_LINE, std_type='fake_std')
        self.assertEqual(fake_stdout.getvalue(), FAKE_STRING_NEW_LINE)
        self.assertEqual(fake_stderr.getvalue(), b'')
        clear_buffers()

        sys.stdout = orig_stdout
        sys.stderr = orig_stderr