Ejemplo n.º 1
0
def delete_floating_ip(instance_id):
    fl_ips = nova.client().floating_ips.findall(instance_id=instance_id)
    for fl_ip in fl_ips:
        if CONF.use_neutron:
            nova.client().floating_ips.delete(fl_ip.id)
        else:
            nova.client().floating_ips.delete(fl_ip.ip)
Ejemplo n.º 2
0
    def _shutdown_instance(self, instance):
        ctx = context.ctx()

        if instance.node_group.floating_ip_pool:
            try:
                networks.delete_floating_ip(instance.instance_id)
            except nova_exceptions.NotFound:
                LOG.warn("Attempted to delete non-existent floating IP in "
                         "pool %s from instancie %s",
                         instance.node_group.floating_ip_pool,
                         instance.instance_id)

        try:
            volumes.detach_from_instance(instance)
        except Exception:
            LOG.warn("Detaching volumes from instance %s failed",
                     instance.instance_id)

        try:
            nova.client().servers.delete(instance.instance_id)
        except nova_exceptions.NotFound:
            LOG.warn("Attempted to delete non-existent instance %s",
                     instance.instance_id)

        conductor.instance_remove(ctx, instance)
Ejemplo n.º 3
0
    def _shutdown_instance(self, instance):
        ctx = context.ctx()

        if instance.node_group.floating_ip_pool:
            try:
                networks.delete_floating_ip(instance.instance_id)
            except nova_exceptions.NotFound:
                LOG.warn(
                    "Attempted to delete non-existent floating IP in "
                    "pool %s from instancie %s",
                    instance.node_group.floating_ip_pool, instance.instance_id)

        try:
            volumes.detach_from_instance(instance)
        except Exception:
            LOG.warn("Detaching volumes from instance %s failed",
                     instance.instance_id)

        try:
            nova.client().servers.delete(instance.instance_id)
        except nova_exceptions.NotFound:
            LOG.warn("Attempted to delete non-existent instance %s",
                     instance.instance_id)

        conductor.instance_remove(ctx, instance)
Ejemplo n.º 4
0
def delete_floating_ip(instance_id):
    fl_ips = nova.client().floating_ips.findall(instance_id=instance_id)
    for fl_ip in fl_ips:
        if CONF.use_neutron:
            nova.client().floating_ips.delete(fl_ip.id)
        else:
            nova.client().floating_ips.delete(fl_ip.ip)
Ejemplo n.º 5
0
def _shutdown_instance(instance):
    ctx = context.ctx()
    try:
        nova.client().servers.delete(instance.instance_id)
    except nova_exceptions.NotFound:
        #Just ignore non-existing instances
        pass

    conductor.instance_remove(ctx, instance)
Ejemplo n.º 6
0
def _shutdown_instance(instance):
    ctx = context.ctx()
    try:
        nova.client().servers.delete(instance.instance_id)
    except nova_exceptions.NotFound:
        #Just ignore non-existing instances
        pass

    conductor.instance_remove(ctx, instance)
Ejemplo n.º 7
0
def _shutdown_instance(instance):
    session = context.ctx().session
    try:
        nova.client().servers.delete(instance.instance_id)
    except nova_exceptions.NotFound:
        #Just ignore non-existing instances
        pass

    with session.begin():
        session.delete(instance)
Ejemplo n.º 8
0
def _shutdown_instance(instance):
    ctx = context.ctx()
    try:
        if instance.node_group.floating_ip_pool:
            networks.delete_floating_ip(instance.instance_id)
        nova.client().servers.delete(instance.instance_id)
    except nova_exceptions.NotFound:
        #Just ignore non-existing instances
        pass

    conductor.instance_remove(ctx, instance)
Ejemplo n.º 9
0
def _shutdown_instance(instance):
    ctx = context.ctx()
    try:
        if instance.node_group.floating_ip_pool:
            networks.delete_floating_ip(instance.instance_id)
        nova.client().servers.delete(instance.instance_id)
    except nova_exceptions.NotFound:
        #Just ignore non-existing instances
        pass

    conductor.instance_remove(ctx, instance)
Ejemplo n.º 10
0
def _rollback_cluster_creation(cluster, ex):
    """Shutdown all instances and update cluster status."""
    LOG.info("Cluster '%s' creation rollback (reason: %s)", cluster.name, ex)

    session = context.ctx().session
    _shutdown_instances(cluster, True)
    volumes.detach(cluster)
    alive_instances = set([srv.id for srv in nova.client().servers.list()])

    for node_group in cluster.node_groups:
        for instance in node_group.instances:
            if instance.instance_id in alive_instances:
                nova.client().servers.delete(instance.instance_id)
            with session.begin():
                session.delete(instance)
Ejemplo n.º 11
0
def _run_instance(cluster, node_group, idx, aa_groups, userdata):
    """Create instance using nova client and persist them into DB."""
    ctx = context.ctx()
    name = '%s-%s-%03d' % (cluster.name, node_group.name, idx)

    # aa_groups: node process -> instance ids
    aa_ids = []
    for node_process in node_group.node_processes:
        aa_ids += aa_groups.get(node_process) or []

    # create instances only at hosts w/ no instances w/ aa-enabled processes
    hints = {'different_host': list(set(aa_ids))} if aa_ids else None

    nova_instance = nova.client().servers.create(
        name,
        node_group.get_image_id(),
        node_group.flavor_id,
        scheduler_hints=hints,
        userdata=userdata,
        key_name=cluster.user_keypair_id)

    instance_id = conductor.instance_add(ctx, node_group, {
        "instance_id": nova_instance.id,
        "instance_name": name
    })
    # save instance id to aa_groups to support aa feature
    for node_process in node_group.node_processes:
        if node_process in cluster.anti_affinity:
            aa_group_ids = aa_groups.get(node_process, [])
            aa_group_ids.append(nova_instance.id)
            aa_groups[node_process] = aa_group_ids

    return instance_id
Ejemplo n.º 12
0
def _run_instance(cluster, node_group, idx, aa_groups, userdata):
    """Create instance using nova client and persist them into DB."""
    session = context.ctx().session
    name = '%s-%s-%03d' % (cluster.name, node_group.name, idx)

    # aa_groups: node process -> instance ids
    aa_ids = []
    for node_process in node_group.node_processes:
        aa_ids += aa_groups.get(node_process) or []

    # create instances only at hosts w/ no instances w/ aa-enabled processes
    hints = {'different_host': list(set(aa_ids))} if aa_ids else None

    context.model_save(node_group)

    nova_instance = nova.client().servers.create(
        name, node_group.get_image_id(), node_group.flavor_id,
        scheduler_hints=hints, userdata=userdata,
        key_name=cluster.user_keypair_id)

    with session.begin():
        instance = m.Instance(node_group.id, nova_instance.id, name)
        node_group.instances.append(instance)
        session.add(instance)

    # save instance id to aa_groups to support aa feature
    for node_process in node_group.node_processes:
        if node_process in cluster.anti_affinity:
            aa_group_ids = aa_groups.get(node_process, [])
            aa_group_ids.append(nova_instance.id)
            aa_groups[node_process] = aa_group_ids

    return instance
Ejemplo n.º 13
0
def init_instances_ips(instance, server):
    """Extracts internal and management ips.

    As internal ip will be used the first ip from the nova networks CIDRs.
    If use_floating_ip flag is set than management ip will be the first
    non-internal ip.
    """
    ctx = context.ctx()

    if instance.internal_ip and instance.management_ip:
        return True

    management_ip = instance.management_ip
    internal_ip = instance.internal_ip

    for network_label in server.networks:
        nova_network = nova.client().networks.find(label=network_label)
        network = netaddr.IPNetwork(nova_network.cidr)
        for ip in server.networks[network_label]:
            if netaddr.IPAddress(ip) in network:
                internal_ip = instance.internal_ip or ip
            else:
                management_ip = instance.management_ip or ip

    if not CONF.use_floating_ips:
        management_ip = internal_ip

    conductor.instance_update(ctx, instance, {
        "management_ip": management_ip,
        "internal_ip": internal_ip
    })

    return internal_ip and management_ip
Ejemplo n.º 14
0
def init_instances_ips(instance, server):
    """Extracts internal and management ips.

    As internal ip will be used the first ip from the nova networks CIDRs.
    If use_floating_ip flag is set than management ip will be the first
    non-internal ip.
    """
    ctx = context.ctx()

    if instance.internal_ip and instance.management_ip:
        return True

    management_ip = instance.management_ip
    internal_ip = instance.internal_ip

    for network_label in server.networks:
        nova_network = nova.client().networks.find(label=network_label)
        network = netaddr.IPNetwork(nova_network.cidr)
        for ip in server.networks[network_label]:
            if netaddr.IPAddress(ip) in network:
                internal_ip = instance.internal_ip or ip
            else:
                management_ip = instance.management_ip or ip

    if not CONF.use_floating_ips:
        management_ip = internal_ip

    conductor.instance_update(ctx, instance, {"management_ip": management_ip,
                                              "internal_ip": internal_ip})

    return internal_ip and management_ip
Ejemplo n.º 15
0
def _create_attach_volume(instance, size, device_path, display_name=None,
                          volume_type=None):
    volume = cinder.client().volumes.create(size=size,
                                            display_name=display_name,
                                            volume_type=volume_type)
    instance.volumes.append(volume.id)

    while volume.status != 'available':
        volume = cinder.get_volume(volume.id)
        if volume.status == 'error':
            raise RuntimeError("Volume %s has error status" % volume.id)

        context.sleep(1)

    nova.client().volumes.create_server_volume(instance.instance_id,
                                               volume.id, device_path)
Ejemplo n.º 16
0
def _create_attach_volume(ctx, instance, size, display_name=None,
                          volume_type=None):
    volume = cinder.client().volumes.create(size=size,
                                            display_name=display_name,
                                            volume_type=volume_type)
    conductor.append_volume(ctx, instance, volume.id)

    while volume.status != 'available':
        volume = cinder.get_volume(volume.id)
        if volume.status == 'error':
            raise RuntimeError("Volume %s has error status" % volume.id)

        context.sleep(1)

    nova.client().volumes.create_server_volume(instance.instance_id,
                                               volume.id, None)
Ejemplo n.º 17
0
    def user_keypair(self):
        """Extract user keypair object from nova.

        It contains 'public_key' and 'fingerprint' fields.
        """
        if not hasattr(self, '_user_kp'):
            try:
                self._user_kp = nova.client().keypairs.get(
                    self.user_keypair_id)
            except nova_ex.NotFound:
                self._user_kp = None
        return self._user_kp
Ejemplo n.º 18
0
    def _run_instance(self, cluster, node_group, idx, aa_groups, userdata):
        """Create instance using nova client and persist them into DB."""
        ctx = context.ctx()
        name = '%s-%s-%03d' % (cluster.name, node_group.name, idx)

        # aa_groups: node process -> instance ids
        aa_ids = []
        for node_process in node_group.node_processes:
            aa_ids += aa_groups.get(node_process) or []

        # create instances only at hosts w/ no instances
        # w/ aa-enabled processes
        hints = {'different_host': list(set(aa_ids))} if aa_ids else None

        if CONF.use_neutron:
            net_id = cluster.neutron_management_network
            nics = [{"net-id": net_id, "v4-fixed-ip": ""}]

            nova_instance = nova.client().servers.create(
                name, node_group.get_image_id(), node_group.flavor_id,
                scheduler_hints=hints, userdata=userdata,
                key_name=cluster.user_keypair_id,
                nics=nics)
        else:
            nova_instance = nova.client().servers.create(
                name, node_group.get_image_id(), node_group.flavor_id,
                scheduler_hints=hints, userdata=userdata,
                key_name=cluster.user_keypair_id)

        instance_id = conductor.instance_add(ctx, node_group,
                                             {"instance_id": nova_instance.id,
                                              "instance_name": name})
        # save instance id to aa_groups to support aa feature
        for node_process in node_group.node_processes:
            if node_process in cluster.anti_affinity:
                aa_group_ids = aa_groups.get(node_process, [])
                aa_group_ids.append(nova_instance.id)
                aa_groups[node_process] = aa_group_ids

        return instance_id
Ejemplo n.º 19
0
def init_neutron_ips(instance, server):
    ctx = context.ctx()

    net_id = instance.node_group.cluster.neutron_management_network
    net_name = nova.client().networks.get(net_id).label

    internal_ip = server.networks.get(net_name, [None])[0]
    management_ip = internal_ip

    conductor.instance_update(ctx, instance, {"management_ip": management_ip,
                                              "internal_ip": internal_ip})

    return internal_ip
Ejemplo n.º 20
0
def generate_topology_map(cluster, is_node_awareness):
    mapping = _read_compute_topology()
    nova_client = nova.client()
    topology_mapping = {}
    for ng in cluster.node_groups:
        for i in ng.instances:
            #TODO(alazarev) get all servers info with one request
            ni = nova_client.servers.get(i.instance_id)
            hostId = ni.hostId
            if hostId not in mapping:
                raise ex.NotFoundException(
                    i.instance_id, "Was not able to find compute node "
                    "topology for VM %s")
            rack = mapping[hostId]
            if is_node_awareness:
                rack += "/" + hostId

            topology_mapping[i.instance_name] = rack
            topology_mapping[i.management_ip] = rack
            topology_mapping[i.internal_ip] = rack

    topology_mapping.update(_read_swift_topology())

    return topology_mapping
Ejemplo n.º 21
0
def generate_topology_map(cluster, is_node_awareness):
    mapping = _read_compute_topology()
    nova_client = nova.client()
    topology_mapping = {}
    for ng in cluster.node_groups:
        for i in ng.instances:
            #TODO(alazarev) get all servers info with one request
            ni = nova_client.servers.get(i.instance_id)
            hostId = ni.hostId
            if hostId not in mapping:
                raise ex.NotFoundException(
                    i.instance_id, "Was not able to find compute node "
                                   "topology for VM %s")
            rack = mapping[hostId]
            if is_node_awareness:
                rack += "/" + hostId

            topology_mapping[i.instance_name] = rack
            topology_mapping[i.management_ip] = rack
            topology_mapping[i.internal_ip] = rack

    topology_mapping.update(_read_swift_topology())

    return topology_mapping
Ejemplo n.º 22
0
def register_image(image_id, username, description=None):
    client = nova.client()
    client.images.set_description(image_id, username, description)
    return client.images.get(image_id)
Ejemplo n.º 23
0
 def get_node_group_image_username(self, node_group):
     image_id = node_group.get_image_id()
     return nova.client().images.get(image_id).username
Ejemplo n.º 24
0
def assign_floating_ip(instance_id, pool):
    ip = nova.client().floating_ips.create(pool)
    nova.client().servers.get(instance_id).add_floating_ip(ip)
Ejemplo n.º 25
0
 def nova_info(self):
     """Returns info from nova about instance."""
     return nova.client().servers.get(self.instance_id)
Ejemplo n.º 26
0
 def username(self):
     if not hasattr(self, '_username'):
         image_id = self.get_image_id()
         self._username = nova.client().images.get(image_id).username
     return self._username
Ejemplo n.º 27
0
def add_image_tags(image_id, tags):
    client = nova.client()
    client.images.tag(image_id, tags)
    return client.images.get(image_id)
Ejemplo n.º 28
0
def check_flavor_exists(flavor_id):
    try:
        nova.client().flavors.get(flavor_id)
    except nova_ex.NotFound:
        raise ex.InvalidException("Requested flavor '%s' not found" %
                                  flavor_id)
Ejemplo n.º 29
0
def check_flavor_exists(flavor_id):
    flavor_list = nova.client().flavors.list()
    if flavor_id not in [flavor.id for flavor in flavor_list]:
        raise ex.InvalidException("Requested flavor '%s' not found" %
                                  flavor_id)
Ejemplo n.º 30
0
def get_images(tags):
    return nova.client().images.list_registered(tags)
Ejemplo n.º 31
0
def check_keypair_exists(keypair):
    try:
        nova.client().keypairs.get(keypair)
    except nova_ex.NotFound:
        raise ex.InvalidException("Requested keypair '%s' not found" % keypair)
Ejemplo n.º 32
0
def check_keypair_exists(keypair):
    try:
        nova.client().keypairs.get(keypair)
    except nova_ex.NotFound:
        raise ex.InvalidException("Requested keypair '%s' not found" % keypair)
Ejemplo n.º 33
0
def check_image_registered(image_id):
    if image_id not in [i.id for i in nova.client().images.list_registered()]:
        raise ex.InvalidException("Requested image '%s' is not registered" %
                                  image_id)
Ejemplo n.º 34
0
def get_image(**kwargs):
    if len(kwargs) == 1 and 'id' in kwargs:
        return nova.client().images.get(kwargs['id'])
    else:
        return nova.client().images.find(**kwargs)
Ejemplo n.º 35
0
def delete_floating_ip(instance_id):
    fl_ips = nova.client().floating_ips.findall(instance_id=instance_id)
    for fl_ip in fl_ips:
        nova.client().floating_ips.delete(fl_ip.id)
Ejemplo n.º 36
0
def unregister_image(image_id):
    client = nova.client()
    client.images.unset_description(image_id)
    return client.images.get(image_id)
Ejemplo n.º 37
0
def delete_floating_ip(instance_id):
    fl_ips = nova.client().floating_ips.findall(instance_id=instance_id)
    for fl_ip in fl_ips:
        nova.client().floating_ips.delete(fl_ip.id)
Ejemplo n.º 38
0
def remove_image_tags(image_id, tags):
    client = nova.client()
    client.images.untag(image_id, tags)
    return client.images.get(image_id)
Ejemplo n.º 39
0
def _get_node_group_image_username(node_group):
    image_id = node_group.get_image_id()
    return nova.client().images.get(image_id).username
Ejemplo n.º 40
0
def check_flavor_exists(flavor_id):
    try:
        nova.client().flavors.get(flavor_id)
    except nova_ex.NotFound:
        raise ex.InvalidException("Requested flavor '%s' not found"
                                  % flavor_id)
Ejemplo n.º 41
0
def assign_floating_ip(instance_id, pool):
    ip = nova.client().floating_ips.create(pool)
    nova.client().servers.get(instance_id).add_floating_ip(ip)
Ejemplo n.º 42
0
def check_image_registered(image_id):
    if image_id not in [i.id for i in nova.client().images.list_registered()]:
        raise ex.InvalidException("Requested image '%s' is not registered"
                                  % image_id)
Ejemplo n.º 43
0
def check_flavor_exists(flavor_id):
    flavor_list = nova.client().flavors.list()
    if flavor_id not in [flavor.id for flavor in flavor_list]:
        raise ex.InvalidException("Requested flavor '%s' not found"
                                  % flavor_id)