Beispiel #1
0
        def _validate(context, body, gid):
            if not self.is_valid_body(body, 'keypair'):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            self._uuid_check(gid)
            values = body["keypair"]
            name = values.get("name")
            is_default = values.get("is_default")

            if is_default:
                try:
                    is_default = strutils.bool_from_string(is_default,
                                                           strict=True)
                    keypairs = db.keypair_get_all(context,
                                                  gid,
                                                  filters={"is_default": True})
                    if keypairs:
                        msg = _("Default keypair already exists in the "
                                "group %s" % gid)
                        raise exception.InvalidInput(reason=msg)
                except ValueError:
                    msg = _("is_default must be a boolean")
                    raise exception.InvalidInput(reason=msg)
            else:
                is_default = False

            valid_values = {}
            valid_values["gid"] = gid
            valid_values["display_name"] = name
            valid_values["is_default"] = is_default
            return valid_values
Beispiel #2
0
def check_string_length(value, name, min_length=0, max_length=None):
    """Check the length of specified string
    :param value: the value of the string
    :param name: the name of the string
    :param min_length: the min_length of the string
    :param max_length: the max_length of the string
    """
    if not isinstance(value, six.string_types):
        msg = _("%s is not a string or unicode") % name
        raise exception.InvalidInput(message=msg)

    if len(value) < min_length:
        msg = _("%(name)s has a minimum character requirement of "
                "%(min_length)s.") % {
                    'name': name,
                    'min_length': min_length
                }
        raise exception.InvalidInput(message=msg)

    if max_length and len(value) > max_length:
        msg = _("%(name)s has more than %(max_length)s "
                "characters.") % {
                    'name': name,
                    'max_length': max_length
                }
        raise exception.InvalidInput(message=msg)
Beispiel #3
0
        def _validate(body):
            if not self.is_valid_body(body, 'group'):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            values = body["group"]
            name = values.get("name")
            description = values.get("description")

            if not name:
                msg = _("Group name is required")
                raise exception.InvalidInput(reason=msg)

            if isinstance(name, six.string_types):
                name = name.strip()
            utils.check_string_length(name,
                                      'name',
                                      min_length=1,
                                      max_length=255)

            if description:
                utils.check_string_length(description,
                                          'description',
                                          min_length=0,
                                          max_length=255)

            valid_values = {}
            valid_values["display_name"] = name
            valid_values["display_description"] = description
            return valid_values
Beispiel #4
0
        def _validate(context, body, gid):
            self._uuid_check(gid)
            process = db.process_get_all(context,
                                         gid,
                                         filters={"is_proxy": True})
            if not process:
                msg = _("Proxy process does not exist in the group %s" % gid)
                raise exception.InvalidInput(reason=msg)

            if not self.is_valid_body(body, 'proxy'):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            values = body["proxy"]
            app_status = values.get("app_status")
            ipc_endpoint = values.get("ipc_endpoint")
            shm_endpoint = values.get("shm_endpoint")
            fs_endpoint = values.get("fs_endpoint")

            valid_values = {}
            if app_status:
                utils.check_string_length(app_status,
                                          'app_status',
                                          min_length=1,
                                          max_length=255)
                valid_values["app_status"] = app_status
            if ipc_endpoint:
                utils.check_string_length(ipc_endpoint,
                                          'ipc_endpoint',
                                          min_length=1,
                                          max_length=255)
                valid_values["ipc_endpoint"] = ipc_endpoint
            if shm_endpoint:
                utils.check_string_length(shm_endpoint,
                                          'shm_endpoint',
                                          min_length=1,
                                          max_length=255)
                valid_values["shm_endpoint"] = shm_endpoint
            if fs_endpoint:
                utils.check_string_length(fs_endpoint,
                                          'fs_endpoint',
                                          min_length=1,
                                          max_length=255)
                valid_values["fs_endpoint"] = fs_endpoint

            if not valid_values:
                msg = _("No keyword is provided.")
                raise exception.InvalidInput(reason=msg)

            return process[0]["pid"], valid_values
Beispiel #5
0
        def _validate(context, body, gid):
            self._uuid_check(gid)
            if not self.is_valid_body(body, "network"):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            values = body.get("network")

            cidr = values.get("cidr")
            name = values.get("name")

            is_admin = values.get("is_admin")
            if is_admin:
                try:
                    is_admin = strutils.bool_from_string(is_admin, strict=True)
                except ValueError:
                    msg = _("is_admin must be a boolean")
                    raise exception.InvalidInput(reason=msg)
            else:
                is_admin = False

            gateway = values.get("gateway")
            ext_router = values.get("ext_router_id")
            dns_nameservers = values.get("dns_nameservers")
            if dns_nameservers is not None and not isinstance(
                    dns_nameservers, list):
                msg = _("dns_nameservers must be a list")
                raise exception.InvalidInput(reason=msg)

            valid_values = {}
            valid_values["gid"] = gid
            valid_values["network_id"] = unicode(uuid.uuid4())
            if not name:
                name = "network-" + valid_values["network_id"]
            valid_values["display_name"] = name
            valid_values["cidr"] = cidr
            valid_values["is_admin"] = is_admin
            valid_values["gateway"] = gateway
            valid_values["ext_router"] = ext_router
            valid_values["dns_nameservers"] = dns_nameservers

            network_values = {}
            network_values["name"] = name
            network_values["cidr"] = cidr
            network_values["gateway"] = gateway
            network_values["ext_router"] = ext_router
            network_values["dns_nameservers"] = dns_nameservers

            return valid_values, network_values
Beispiel #6
0
    def __init__(self, name, loader=None, use_ssl=False, max_url_len=None):
        """Initialize, but do not start the WSGI server.

        :param name: The name of the WSGI server given to the loader.
        :param loader: Loads the WSGI application using the given name.
        :returns: None

        """
        self.name = name
        self.manager = self._get_manager()
        self.loader = loader or wsgi.Loader()
        self.app = self.loader.load_app(name)
        self.host = getattr(CONF, '%s_listen' % name, "0.0.0.0")
        self.port = getattr(CONF, '%s_listen_port' % name, 0)
        self.workers = (getattr(CONF, '%s_workers' % name, None)
                        or utils.cpu_count())
        if self.workers and self.workers < 1:
            worker_name = '%s_workers' % name
            msg = (_("%(worker_name)s value of %(workers)s is invalid, "
                     "must be greater than 0") % {
                         'worker_name': worker_name,
                         'workers': str(self.workers)
                     })
            raise exception.InvalidInput(msg)
        self.use_ssl = use_ssl
        self.server = wsgi.Server(name,
                                  self.app,
                                  host=self.host,
                                  port=self.port,
                                  use_ssl=self.use_ssl,
                                  max_url_len=max_url_len)
        # Pull back actual port used
        self.port = self.server.port
        self.backdoor_port = None
Beispiel #7
0
        def _validate(body, gid, pid):
            self._uuid_check(gid, pid)

            if not self.is_valid_body(body, 'process'):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            values = body["process"]
            app_status = values.get("app_status")

            if not app_status:
                msg = _("app_status is required")
                raise exception.InvalidInput(reason=msg)

            valid_values = {"app_status": app_status}

            return valid_values
Beispiel #8
0
    def __init__(self, name, app, host='0.0.0.0', port=0, pool_size=None,
                 protocol=eventlet.wsgi.HttpProtocol, backlog=128,
                 use_ssl=False, max_url_len=None):
        """Initialize, but do not start, a WSGI server.

        :param name: Pretty name for logging.
        :param app: The WSGI application to serve.
        :param host: IP address to serve the application.
        :param port: Port number to server the application.
        :param pool_size: Maximum number of eventlets to spawn concurrently.
        :param backlog: Maximum number of queued connections.
        :param max_url_len: Maximum length of permitted URLs.
        :returns: None
        :raises: rack.exception.InvalidInput
        """
        # Allow operators to customize http requests max header line size.
        eventlet.wsgi.MAX_HEADER_LINE = CONF.max_header_line
        self.name = name
        self.app = app
        self._server = None
        self._protocol = protocol
        self._pool = eventlet.GreenPool(pool_size or self.default_pool_size)
        self._logger = logging.getLogger("rack.%s.wsgi.server" % self.name)
        self._wsgi_logger = logging.WritableLogger(self._logger)
        self._use_ssl = use_ssl
        self._max_url_len = max_url_len

        if backlog < 1:
            raise exception.InvalidInput(
                reason='The backlog must be more than 1')

        bind_addr = (host, port)
        # TODO(dims): eventlet's green dns/socket module does not actually
        # support IPv6 in getaddrinfo(). We need to get around this in the
        # future or monitor upstream for a fix
        try:
            info = socket.getaddrinfo(bind_addr[0],
                                      bind_addr[1],
                                      socket.AF_UNSPEC,
                                      socket.SOCK_STREAM)[0]
            family = info[0]
            bind_addr = info[-1]
        except Exception:
            family = socket.AF_INET

        try:
            self._socket = eventlet.listen(bind_addr, family, backlog=backlog)
        except EnvironmentError:
            LOG.error(_("Could not bind to %(host)s:%(port)s"),
                      {'host': host, 'port': port})
            raise

        (self.host, self.port) = self._socket.getsockname()[0:2]
        LOG.info(_("%(name)s listening on %(host)s:%(port)s") % self.__dict__)
Beispiel #9
0
        def _validate(body, gid, keypair_id):
            if not self.is_valid_body(body, 'keypair'):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            self._uuid_check(gid, keypair_id)
            values = body["keypair"]
            is_default = values.get("is_default")

            if is_default is not None:
                try:
                    is_default = strutils.bool_from_string(is_default,
                                                           strict=True)
                except ValueError:
                    msg = _("is_default must be a boolean")
                    raise exception.InvalidInput(reason=msg)
            else:
                msg = _("is_default is required")
                raise exception.InvalidInput(reason=msg)

            valid_values = {"is_default": is_default}
            return valid_values
Beispiel #10
0
        def _validate(body, gid):
            if not self.is_valid_body(body, 'group'):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            values = body["group"]
            name = values.get("name")
            description = values.get("description")

            if not uuidutils.is_uuid_like(gid):
                raise exception.GroupNotFound(gid=gid)

            if name is None and description is None:
                msg = _("Group name or description is required")
                raise exception.InvalidInput(reason=msg)

            if name is not None:
                if isinstance(name, six.string_types):
                    name = name.strip()
                utils.check_string_length(name,
                                          'name',
                                          min_length=1,
                                          max_length=255)

            if description is not None:
                utils.check_string_length(description,
                                          'description',
                                          min_length=0,
                                          max_length=255)

            valid_values = {}
            if name:
                valid_values["display_name"] = name
            # allow blank string to clear description
            if description is not None:
                valid_values["display_description"] = description
            valid_values["gid"] = gid
            return valid_values
Beispiel #11
0
def validate_integer(value, name, min_value=None, max_value=None):
    """Make sure that value is a valid integer, potentially within range."""
    try:
        value = int(str(value))
    except (ValueError, UnicodeEncodeError):
        msg = _('%(value_name)s must be an integer')
        raise exception.InvalidInput(reason=(msg % {'value_name': name}))

    if min_value is not None:
        if value < min_value:
            msg = _('%(value_name)s must be >= %(min_value)d')
            raise exception.InvalidInput(reason=(msg % {
                'value_name': name,
                'min_value': min_value
            }))
    if max_value is not None:
        if value > max_value:
            msg = _('%(value_name)s must be <= %(max_value)d')
            raise exception.InvalidInput(reason=(msg % {
                'value_name': name,
                'max_value': max_value
            }))
    return value
Beispiel #12
0
def process_create(context, values, network_ids, securitygroup_ids):
    session = get_session()
    with session.begin():
        process_ref = models.Process(**values)
        session.add(process_ref)

        try:
            if network_ids:
                for network_id in network_ids:
                    network_ref = session.query(models.Network)\
                        .filter_by(deleted=0)\
                        .filter_by(gid=values["gid"])\
                        .filter_by(network_id=network_id)\
                        .first()
                    if network_ref is None:
                        raise exception.NetworkNotFound(network_id=network_id)
                    session.add(
                        models.ProcessNetwork(
                            pid=values["pid"],
                            network_id=network_ref.network_id))

            if securitygroup_ids:
                for securitygroup_id in securitygroup_ids:
                    securitygroup_ref = session.query(models.Securitygroup)\
                        .filter_by(deleted=0)\
                        .filter_by(gid=values["gid"])\
                        .filter_by(securitygroup_id=securitygroup_id)\
                        .first()
                    if securitygroup_ref is None:
                        raise exception.SecuritygroupNotFound(
                            securitygroup_id=securitygroup_id)
                    session.add(
                        models.ProcessSecuritygroup(
                            pid=values["pid"],
                            securitygroup_id=securitygroup_ref.securitygroup_id
                        ))

            session.flush()
        except db_exc.DBDuplicateEntry:
            msg = _("securitygroup or network is duplicated")
            raise exception.InvalidInput(reason=msg)

    return _get_process_dict(process_ref)
Beispiel #13
0
        def _validate(context, body, gid, is_proxy=False):
            proxy = db.process_get_all(context,
                                       gid,
                                       filters={"is_proxy": True})
            if is_proxy:
                if len(proxy) > 0:
                    msg = _("Proxy process already exists in the group %s" %
                            gid)
                    raise exception.InvalidInput(reason=msg)
            else:
                if len(proxy) != 1:
                    msg = _("Proxy process does not exist in the group %s" %
                            gid)
                    raise webob.exc.HTTPBadRequest(explanation=msg)

            keyname = "proxy" if is_proxy else "process"
            if not self.is_valid_body(body, keyname):
                msg = _("Invalid request body")
                raise exception.InvalidInput(reason=msg)

            values = body[keyname]
            ppid = values.get("ppid")
            name = values.get("name")
            keypair_id = values.get("keypair_id")
            securitygroup_ids = values.get("securitygroup_ids")
            glance_image_id = values.get("glance_image_id")
            nova_flavor_id = values.get("nova_flavor_id")
            userdata = values.get("userdata")
            args = values.get("args")

            self._uuid_check(gid, ppid, keypair_id)

            pid = unicode(uuid.uuid4())
            if not name:
                prefix = "proxy-" if is_proxy else "process-"
                name = prefix + pid

            if ppid:
                parent_process = db.process_get_by_pid(context, gid, ppid)

            nova_keypair_id = None
            if keypair_id:
                keypair = db.keypair_get_by_keypair_id(context, gid,
                                                       keypair_id)
                nova_keypair_id = keypair["nova_keypair_id"]
            elif ppid:
                keypair_id = parent_process.get("keypair_id")
                if keypair_id:
                    keypair = db.keypair_get_by_keypair_id(
                        context, gid, keypair_id)
                    nova_keypair_id = keypair["nova_keypair_id"]
            else:
                default_keypair = db.keypair_get_all(
                    context, gid, filters={"is_default": True})
                if default_keypair:
                    keypair_id = default_keypair[0]["keypair_id"]
                    nova_keypair_id = default_keypair[0]["nova_keypair_id"]

            if securitygroup_ids is not None and\
                    not isinstance(securitygroup_ids, list):
                msg = _("securitygroupids must be a list")
                raise exception.InvalidInput(reason=msg)
            elif securitygroup_ids:
                neutron_securitygroup_ids = []
                for id in securitygroup_ids:
                    self._uuid_check(securitygroup_id=id)
                    securitygroup = db.securitygroup_get_by_securitygroup_id(
                        context, gid, id)
                    neutron_securitygroup_ids.append(
                        securitygroup["neutron_securitygroup_id"])
            elif ppid:
                securitygroups = parent_process.get("securitygroups")
                securitygroup_ids =\
                    [securitygroup["securitygroup_id"]
                        for securitygroup in securitygroups]
                neutron_securitygroup_ids =\
                    [securitygroup["neutron_securitygroup_id"]
                        for securitygroup in securitygroups]
            else:
                default_securitygroups = db.securitygroup_get_all(
                    context, gid, filters={"is_default": True})
                if default_securitygroups:
                    securitygroup_ids =\
                        [securitygroup["securitygroup_id"]
                            for securitygroup in default_securitygroups]
                    neutron_securitygroup_ids =\
                        [securitygroup["neutron_securitygroup_id"]
                            for securitygroup in default_securitygroups]
                else:
                    msg = _("securitygroup_ids is required. Default \
                            securitygroup_ids are not registered.")
                    raise exception.InvalidInput(reason=msg)

            if not glance_image_id and ppid:
                glance_image_id = parent_process.get("glance_image_id")

            if not nova_flavor_id and ppid:
                nova_flavor_id = parent_process.get("nova_flavor_id")

            if userdata:
                try:
                    base64.b64decode(userdata)
                except TypeError:
                    msg = _("userdadta must be a base64 encoded value.")
                    raise exception.InvalidInput(reason=msg)

            networks = db.network_get_all(context, gid)
            if not networks:
                msg = _("Netwoks does not exist in the group %s" % gid)
                raise webob.exc.HTTPBadRequest(explanation=msg)

            network_ids =\
                [network["network_id"] for network in networks]
            neutron_network_ids =\
                [network["neutron_network_id"] for network in networks]
            nics = []
            for id in neutron_network_ids:
                nics.append({"net-id": id})

            if args is None:
                args = {}
            elif args is not None and\
                    not isinstance(args, dict):
                msg = _("args must be a dict.")
                raise exception.InvalidInput(reason=msg)
            else:
                for key in args.keys():
                    args[key] = str(args[key])

            default_args = {
                "gid": gid,
                "pid": pid,
            }
            if ppid:
                default_args["ppid"] = ppid

            if is_proxy:
                default_args["rackapi_ip"] = cfg.CONF.my_ip
                default_args["os_username"] = cfg.CONF.os_username
                default_args["os_password"] = cfg.CONF.os_password
                default_args["os_tenant_name"] = cfg.CONF.os_tenant_name
                default_args["os_auth_url"] = cfg.CONF.os_auth_url
                default_args["os_region_name"] = cfg.CONF.os_region_name
            else:
                proxy_instance_id = proxy[0]["nova_instance_id"]
                default_args["proxy_ip"] = self.manager.get_process_address(
                    context, proxy_instance_id)
            args.update(default_args)

            valid_values = {}
            valid_values["gid"] = gid
            valid_values["ppid"] = ppid
            valid_values["pid"] = pid
            valid_values["display_name"] = name
            valid_values["keypair_id"] = keypair_id
            valid_values["securitygroup_ids"] = securitygroup_ids
            valid_values["glance_image_id"] = glance_image_id
            valid_values["nova_flavor_id"] = nova_flavor_id
            valid_values["userdata"] = userdata
            valid_values["args"] = json.dumps(args)
            valid_values["is_proxy"] = True if is_proxy else False
            valid_values["network_ids"] = network_ids

            if is_proxy:
                ipc_endpoint = values.get("ipc_endpoint")
                shm_endpoint = values.get("shm_endpoint")
                fs_endpoint = values.get("fs_endpoint")
                if ipc_endpoint:
                    utils.check_string_length(ipc_endpoint,
                                              'ipc_endpoint',
                                              min_length=1,
                                              max_length=255)
                if shm_endpoint:
                    utils.check_string_length(shm_endpoint,
                                              'shm_endpoint',
                                              min_length=1,
                                              max_length=255)
                if fs_endpoint:
                    utils.check_string_length(fs_endpoint,
                                              'fs_endpoint',
                                              min_length=1,
                                              max_length=255)
                valid_values["ipc_endpoint"] = ipc_endpoint
                valid_values["shm_endpoint"] = shm_endpoint
                valid_values["fs_endpoint"] = fs_endpoint

            boot_values = {}
            boot_values["name"] = name
            boot_values["key_name"] = nova_keypair_id
            boot_values["security_groups"] = neutron_securitygroup_ids
            boot_values["image"] = glance_image_id
            boot_values["flavor"] = nova_flavor_id
            boot_values["userdata"] = userdata
            boot_values["meta"] = args
            boot_values["nics"] = nics

            return valid_values, boot_values