示例#1
0
    def __call__(self, src=None, dst=None, **kwargs):
        super(RescheduleVM, self).__call__(**kwargs)
        src = expand_paths([src],
                           predicate=lambda r: r.type == 'virtual-router')[0]
        dst = expand_paths(dst, predicate=lambda r: r.type == 'virtual-router')

        # get list of VMs to move
        vms = []
        src.fetch()
        for vm in src.refs.virtual_machine:
            vm.fetch()
            if vm.refs.service_instance:
                vms.append((0, vm))

        if not vms:
            return "No VMs on this virtual-router"

        if self.check:
            return "\n".join([text_type(vm.path) for (_, vm) in vms])

        # get all the resources we need
        for vr in dst:
            vr.fetch()
            for vm in vr.refs.virtual_machine:
                vm.fetch()

        printo("Moving %d VMs from %s to %s" %
               (len(vms), src['name'], ", ".join([vr['name'] for vr in dst])))

        for vr in itertools.cycle(dst):
            # no more VMs to process
            if not vms:
                break
            failures, vm = vms.pop(0)
            vm_si = vm.refs.service_instance[0]
            vr_sis = [
                si for vr_vm in vr.refs.virtual_machine
                for si in vr_vm.refs.service_instance
            ]
            logger.debug("Checking VM %s of SI %s" % (vm, vm_si))
            if vm_si in vr_sis:
                logger.debug("%s already has a VM for SI %s" %
                             (vr['name'], vm_si))
                if failures == len(dst):
                    printo(
                        "Unable to move VM %s. Dest vrouter already has a VM of the same SI."
                        % vm)
                else:
                    vms.insert(0, (failures + 1, vm))
            else:
                printo("Moving VM %s on %s" % (vm, vr['name']))
                if not self.dry_run:
                    src.remove_ref(vm)
                    vr.add_ref(vm)
示例#2
0
    def __call__(self, paths=None, nova_api_version=None):
        self.nclient = nclient.Client(nova_api_version,
                                      session=Context().session)

        self.actions = {
            BackRefsExists: {
                # we don't want to delete virtual-router objects
                ('virtual-machine', 'virtual-router'):
                self._remove_back_ref,
                ('virtual-machine-interface', 'instance-ip'):
                self._handle_si_vm,
                ('virtual-network', 'virtual-machine-interface'):
                self._remove_vm,
                ('security-group', 'virtual-machine-interface'):
                self._remove_vm,
            },
            ChildrenExists: {
                ('project', 'virtual-machine-interface'): self._remove_vm,
            }
        }

        resources = expand_paths(paths,
                                 predicate=lambda r: r.type == 'project')

        if not continue_prompt(
                "Do you really want to purge theses projects ? All resources will be destroyed !"
        ):
            return
        for project in resources:
            self._delete(project)
示例#3
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')
示例#4
0
 def __call__(self, paths=None, **kwargs):
     if not paths:
         self.resources = Collection(self.resource_type, fetch=True)
     else:
         self.resources = expand_paths(
             paths, predicate=lambda r: r.type == self.resource_type)
     super(PathCommand, self).__call__(**kwargs)
示例#5
0
    def __call__(self, paths=None, filename_output=None, excludes=[]):
        resources = expand_paths(paths,
                                 predicate=lambda r: isinstance(r, Resource))
        graph = nx.Graph()

        # For each provided resources, a edge is created from this
        # resource to all of its refs, back_refs and parent.
        for r in resources:
            print "%s %s" % (short_name(r.path.name), r.path)
            r.fetch()
            graph.add_node(r.path, _node_rendering(r.path, r))

            paths = [t.path for t in itertools.chain(r.refs, r.back_refs)]
            try:
                paths.append(r.parent.path)
            except ResourceMissing:
                pass
            for p in paths:
                if p.base in excludes:
                    continue
                print "%s   %s" % (short_name(p.name), p)
                graph.add_node(p, _node_rendering(p))
                graph.add_edge(r.path, p)

        print "Dot file written to %s" % filename_output
        write_dot(graph, filename_output)
示例#6
0
def _node_rendering(path, resource=None):
    renderer = rendering_map.get(path.base, default_renderer(path.base))
    if "attributes" in renderer:
        if resource is None:
            resource = expand_paths([path])[0]
            resource.fetch()
        attributes = renderer["attributes"](resource)
    else:
        attributes = []
    label = "%s\n%s\n%s" % (renderer["alias"], short_name(
        path.name), "\n".join(attributes))
    return {"label": label, "style": "filled", "fillcolor": renderer["color"]}
示例#7
0
    def __call__(self, path=None, mode=None, action=None, name=None):
        if not action == 'show' and not name:
            raise CommandError("--name is required")

        vn = expand_paths([path],
                          predicate=lambda r: r.type == 'virtual-network')[0]
        vn.fetch()

        if action == 'show':
            self.show(vn)
        elif action == 'add':
            self.add(vn, mode, name)
        elif action == 'delete':
            self.delete(vn, mode, name)
示例#8
0
    def generate(self, vn_paths):
        result = []
        if vn_paths == []:
            vns = Collection("virtual-network", fetch=True, detail=True)
        else:
            vns = expand_paths(vn_paths)
            for vn in vns:
                vn.fetch()

        for r in vns:
            nid = r["virtual_network_network_id"]
            try:
                zk_data, _ = self.zk_client.get(ZK_BASEPATH + "/" + to_zk_index(nid))
            except kazoo.exceptions.NoNodeError:
                result.append({"reason": "nolock", "nid": nid, "path": r.path, "api-fqname": text_type(r.fq_name), "resource": r})
                continue
            if "%s" % zk_data.decode('utf-8') != "%s" % r.fq_name:
                result.append({"reason": "badlock", "nid": nid, "path": r.path, "api-fqname": text_type(r.fq_name), "zk-fqname": zk_data, "resource": r})
        return result