예제 #1
0
def update_backends_disk_templates(backends):
    """
    Update the backends' disk templates.
    """
    for b in backends:
        if not b.disk_templates:
            backend_mod.update_backend_disk_templates(b)
예제 #2
0
def update_cluster(msg):
    operation = msg.get("operation")
    clustername = msg.get("cluster")
    if clustername is None:
        return
    if operation != "OP_CLUSTER_SET_PARAMS":
        return
    backend = Backend.objects.select_for_update().get(clustername=clustername)
    backend_mod.update_backend_disk_templates(backend)
    backend_mod.update_backend_resources(backend)
예제 #3
0
def get_available_backends(flavor):
    """Get the list of available backends that can host a new VM of a flavor.

    The list contains the backends that are online and that have enabled
    the disk_template of the new VM.

    Also, if the new VM will be automatically connected to a public network,
    the backends that do not have an available public IPv4 address are
    excluded.

    """
    disk_template = flavor.disk_template
    # Ganeti knows only the 'ext' disk template, but the flavors disk template
    # includes the provider.
    if disk_template.startswith("ext_"):
        disk_template = "ext"

    backends = Backend.objects.select_for_update().filter(offline=False,
                                                          drained=False)
    # Update the disk_templates if there are empty.
    [backend_mod.update_backend_disk_templates(b)
     for b in backends if not b.disk_templates]
    backends = filter(lambda b: disk_template in b.disk_templates,
                      list(backends))

    # Update the backend stats if it is needed
    refresh_backends_stats(backends)

    return backends
예제 #4
0
def get_available_backends(flavor):
    """Get the list of available backends that can host a new VM of a flavor.

    The list contains the backends that are online and that have enabled
    the disk_template of the new VM.

    Also, if the new VM will be automatically connected to a public network,
    the backends that do not have an available public IPv4 address are
    excluded.

    """
    disk_template = flavor.volume_type.disk_template
    # Ganeti knows only the 'ext' disk template, but the flavors disk template
    # includes the provider.
    if disk_template.startswith("ext_"):
        disk_template = "ext"

    backends = Backend.objects.select_for_update().filter(offline=False,
                                                          drained=False)
    # Update the disk_templates if there are empty.
    [backend_mod.update_backend_disk_templates(b)
     for b in backends if not b.disk_templates]
    backends = filter(lambda b: disk_template in b.disk_templates,
                      list(backends))

    # Update the backend stats if it is needed
    refresh_backends_stats(backends)

    return backends
예제 #5
0
def update_backends_disk_templates(backends, flavor):
    """Update the backends' disk templates and filter those
    that don't have the required disk_template.

    """
    disk_template = flavor.volume_type.disk_template
    # Ganeti knows only the 'ext' disk template, but the flavors disk template
    # includes the provider.
    if disk_template.startswith("ext_"):
        disk_template = "ext"

    [backend_mod.update_backend_disk_templates(b)
     for b in backends if not b.disk_templates]

    backends = filter(lambda b: disk_template in b.disk_templates,
                      list(backends))

    return backends
예제 #6
0
    def create_backend(self, clustername, port, username, password, public,
                       hypervisor=None, initialize=True):
            kw = {"clustername": clustername,
                  "port": port,
                  "username": username,
                  "password": password,
                  "public": public,
                  "drained": True}

            if hypervisor:
                kw["hypervisor"] = hypervisor

            # Create the new backend in database
            try:
                backend = Backend.objects.create(**kw)
            except IntegrityError as e:
                raise CommandError("Cannot create backend: %s\n" % e)

            self.stderr.write("Successfully created backend with id %d\n"
                              % backend.id)

            if not initialize:
                return

            self.stderr.write("Retrieving backend resources:\n")
            resources = backend_mod.get_physical_resources(backend)
            attr = ['mfree', 'mtotal', 'dfree',
                    'dtotal', 'pinst_cnt', 'ctotal']

            table = [[str(resources[x]) for x in attr]]
            pprint_table(self.stdout, table, attr)

            backend_mod.update_backend_resources(backend, resources)
            backend_mod.update_backend_disk_templates(backend)

            networks = Network.objects.filter(deleted=False, public=True)
            if not networks:
                return

            self.stderr.write("Creating the following public:\n")
            headers = ("ID", "Name", 'IPv4 Subnet',
                       "IPv6 Subnet", 'Mac Prefix')
            table = []

            for net in networks:
                subnet4 = net.subnet4.cidr if net.subnet4 else None
                subnet6 = net.subnet6.cidr if net.subnet6 else None
                table.append((net.id, net.backend_id, subnet4,
                              subnet6, str(net.mac_prefix)))
            pprint_table(self.stdout, table, headers)

            for net in networks:
                net.create_backend_network(backend)
                result = backend_mod.create_network_synced(net, backend)
                if result[0] != "success":
                    self.stderr.write('\nError Creating Network %s: %s\n'
                                      % (net.backend_id, result[1]))
                else:
                    self.stderr.write('Successfully created Network: %s\n'
                                      % net.backend_id)
                result = backend_mod.connect_network_synced(network=net,
                                                            backend=backend)
                if result[0] != "success":
                    self.stderr.write('\nError Connecting Network %s: %s\n'
                                      % (net.backend_id, result[1]))
                else:
                    self.stderr.write('Successfully connected Network: %s\n'
                                      % net.backend_id)
예제 #7
0
 def handle(self, **options):
     for backend in Backend.objects.select_for_update()\
                                   .filter(offline=False):
         backend_mod.update_backend_disk_templates(backend)
         backend_mod.update_backend_resources(backend)
         self.stdout.write("Successfully updated backend '%s'\n" % backend)
예제 #8
0
 def handle(self, **options):
     for backend in Backend.objects.select_for_update()\
                                   .filter(offline=False):
         backend_mod.update_backend_disk_templates(backend)
         backend_mod.update_backend_resources(backend)
         self.stdout.write("Successfully updated backend '%s'\n" % backend)
예제 #9
0
    def create_backend(self,
                       clustername,
                       port,
                       username,
                       password,
                       hypervisor=None,
                       initialize=True):
        kw = {
            "clustername": clustername,
            "port": port,
            "username": username,
            "password": password,
            "drained": True
        }

        if hypervisor:
            kw["hypervisor"] = hypervisor

        # Create the new backend in database
        try:
            backend = Backend.objects.create(**kw)
        except IntegrityError as e:
            raise CommandError("Cannot create backend: %s\n" % e)

        self.stderr.write("Successfully created backend with id %d\n" %
                          backend.id)

        if not initialize:
            return

        self.stderr.write("Retrieving backend resources:\n")
        resources = backend_mod.get_physical_resources(backend)
        attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']

        table = [[str(resources[x]) for x in attr]]
        pprint_table(self.stdout, table, attr)

        backend_mod.update_backend_resources(backend, resources)
        backend_mod.update_backend_disk_templates(backend)

        networks = Network.objects.filter(deleted=False, public=True)
        if not networks:
            return

        self.stderr.write("Creating the following public:\n")
        headers = ("ID", "Name", 'IPv4 Subnet', "IPv6 Subnet", 'Mac Prefix')
        table = []

        for net in networks:
            subnet4 = net.subnet4.cidr if net.subnet4 else None
            subnet6 = net.subnet6.cidr if net.subnet6 else None
            table.append((net.id, net.backend_id, subnet4, subnet6,
                          str(net.mac_prefix)))
        pprint_table(self.stdout, table, headers)

        for net in networks:
            net.create_backend_network(backend)
            result = backend_mod.create_network_synced(net, backend)
            if result[0] != "success":
                self.stderr.write('\nError Creating Network %s: %s\n' %
                                  (net.backend_id, result[1]))
            else:
                self.stderr.write('Successfully created Network: %s\n' %
                                  net.backend_id)
            result = backend_mod.connect_network_synced(network=net,
                                                        backend=backend)
            if result[0] != "success":
                self.stderr.write('\nError Connecting Network %s: %s\n' %
                                  (net.backend_id, result[1]))
            else:
                self.stderr.write('Successfully connected Network: %s\n' %
                                  net.backend_id)