Ejemplo n.º 1
0
def get_subnet_create_options(network_id,
                              ip_version=4,
                              gateway='',
                              cidr=None,
                              mask_bits=None,
                              num_subnet=1,
                              gateway_offset=1,
                              cidr_offset=0,
                              **kwargs):
    """When cidr_offset>0 it request only one subnet-options:

        subnet = get_subnet_create_options('abcdefg', 4, num_subnet=4)[3]
        subnet = get_subnet_create_options('abcdefg', 4, cidr_offset=3)
    """

    gateway_not_set = (gateway == '')
    if ip_version == 4:
        cidr = cidr or netaddr.IPNetwork(CONF.network.project_network_cidr)
        mask_bits = mask_bits or CONF.network.project_network_mask_bits
    elif ip_version == 6:
        cidr = (cidr
                or netaddr.IPNetwork(CONF.network.project_network_v6_cidr))
        mask_bits = mask_bits or CONF.network.project_network_v6_mask_bits
    # Find a cidr that is not in use yet and create a subnet with it
    subnet_list = []
    if cidr_offset > 0:
        num_subnet = cidr_offset + 1
    for subnet_cidr in cidr.subnet(mask_bits):
        if gateway_not_set:
            gateway_ip = gateway or (
                str(netaddr.IPAddress(subnet_cidr) + gateway_offset))
        else:
            gateway_ip = gateway
        try:
            subnet_body = dict(network_id=network_id,
                               cidr=str(subnet_cidr),
                               ip_version=ip_version,
                               gateway_ip=gateway_ip,
                               **kwargs)
            if num_subnet <= 1:
                return subnet_body
            subnet_list.append(subnet_body)
            if len(subnet_list) >= num_subnet:
                if cidr_offset > 0:
                    # user request the 'cidr_offset'th of cidr
                    return subnet_list[cidr_offset]
                # user request list of cidr
                return subnet_list
        except exceptions.BadRequest as e:
            is_overlapping_cidr = 'overlaps with another subnet' in str(e)
            if not is_overlapping_cidr:
                raise
    else:
        message = 'Available CIDR for subnet creation could not be found'
        raise exceptions.BuildErrorException(message)
    return {}
Ejemplo n.º 2
0
    def wait_for_server_termination(self, server_id, ignore_error=False):
        """Waits for server to reach termination."""
        build_interval = CONF.compute.build_interval
        while True:
            try:
                rs_client = self._connect_server()
                resp, body = rs_client.get("servers/%s" % str(server_id))
                body = jsonutils.loads(body)
            except lib_exc.NotFound:
                return

            server_status = body['server']['status']
            if server_status == 'ERROR' and not ignore_error:
                raise lib_exc.BuildErrorException(server_id=server_id)

            time.sleep(build_interval)
Ejemplo n.º 3
0
    def wait_for_server_status(self,
                               server_id,
                               status,
                               ready_wait=True,
                               extra_timeout=0,
                               raise_on_error=True):
        """Waits for a server to reach a given status."""
        build_interval = CONF.compute.build_interval
        build_timeout = CONF.compute.build_timeout

        def _get_task_state(body):
            return body.get('OS-EXT-STS:task_state', None)

        rs_client = self._connect_server()
        resp, body = rs_client.get("servers/%s" % str(server_id))
        body = jsonutils.loads(body)
        old_status = server_status = body['server']['status']
        old_task_state = task_state = _get_task_state(body)
        start_time = int(time.time())
        timeout = build_timeout + extra_timeout
        while True:
            if status == 'BUILD' and server_status != 'UNKNOWN':
                return
            if server_status == status:
                if ready_wait:
                    if status == 'BUILD':
                        return
                    if str(task_state) == "None":
                        time.sleep(CONF.compute.ready_wait)
                        return
                else:
                    return

            time.sleep(build_interval)
            resp, body = rs_client.get("servers/%s" % str(server_id))
            body = jsonutils.loads(body)
            server_status = body['server']['status']
            task_state = _get_task_state(body)
            if (server_status != old_status) or (task_state != old_task_state):
                oldstatus = '/'.join((old_status, str(old_task_state)))
                serverstatus = '/'.join((server_status, str(task_state)))
                waitsec = (time.time() - start_time)
                LOG.info(
                    _LI('State transition %(oldstatus)s => %(serverstatus)s '
                        'after %(waitsec)d second wait') % {
                            'oldstatus': oldstatus,
                            'serverstatus': serverstatus,
                            'waitsec': waitsec
                        })
            if (server_status == 'ERROR') and raise_on_error:
                if 'fault' in body:
                    raise lib_exc.BuildErrorException(body['fault'],
                                                      server_id=server_id)
                else:
                    raise lib_exc.BuildErrorException(server_id=server_id)

            timed_out = int(time.time()) - start_time >= timeout

            if timed_out:
                expected_task_state = 'None' if ready_wait else 'n/a'
                message = ('Server %(server_id)s failed to reach %(status)s '
                           'status and task state "%(expected_task_state)s" '
                           'within the required time (%(timeout)s s).' % {
                               'server_id': server_id,
                               'status': status,
                               'expected_task_state': expected_task_state,
                               'timeout': timeout
                           })
                message += ' Current status: %s.' % server_status
                message += ' Current task state: %s.' % task_state
                caller = misc_utils.find_test_caller()
                if caller:
                    message = '(%s) %s' % (caller, message)
                raise lib_exc.TimeoutException(message)
            old_status = server_status
            old_task_state = task_state