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)
def check_exclusive_options(**kwargs): """Checks that only one of the provided options is actually not-none. Iterates over all the kwargs passed in and checks that only one of said arguments is not-none, if more than one is not-none then an exception will be raised with the names of those arguments who were not-none. """ if not kwargs: return pretty_keys = kwargs.pop("pretty_keys", True) exclusive_options = {} for (k, v) in kwargs.iteritems(): if v is not None: exclusive_options[k] = True if len(exclusive_options) > 1: # Change the format of the names from pythonic to # something that is more readable. # # Ex: 'the_key' -> 'the key' if pretty_keys: names = [k.replace('_', ' ') for k in kwargs.keys()] else: names = kwargs.keys() names = ", ".join(sorted(names)) msg = (_("May specify only one of %s") % (names)) raise exception.InvalidInput(reason=msg)
def __init__(self, name, loader=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) LOG.error("test host: %(host)s, port: %(port)s", { 'host': self.host, 'port': self.port }) self.workers = (getattr(CONF, '%s_workers' % name, None) or processutils.get_worker_count()) if self.workers and self.workers < 1: worker_name = '%s_workers' % name msg = (_("%(worker_name)s value of %(workers)d is invalid, " "must be greater than 0.") % { 'worker_name': worker_name, 'workers': self.workers }) raise exception.InvalidInput(msg) self.server = wsgi.Server(name, self.app, host=self.host, port=self.port)
def _ftp_copy_volume(self, volume): dev_disk_name = volume['des_dev_name'] disk_format = volume['src_dev_format'] # 1. format disk self.migrate_ssh.format_disk(dev_disk_name, disk_format) # 2. make the same directory as the source vm's disk mounted mount_dir = volume['src_mount_point'][0] self.migrate_ssh.make_dir(mount_dir) # mount disk to the directory volume['disk_name'] = volume['des_dev_name'] volume['disk_format'] = volume['src_dev_format'] self.migrate_ssh.mount_disk(volume, mount_dir) # 3.download data to this disk in the directory remote_host = volume['src_gw_url'] # splite remote gw url, get host ip and port urls = remote_host.split(':') if len(urls) != 2: LOG.error("Input source gw url error: %s", remote_host) msg = "Input source gw url error: " + remote_host raise exception.InvalidInput(reason=msg) host_ip = urls[0] host_port = urls[1] try: # create transformer task and return task id for quering its state task_id = uuidutils.generate_uuid() task_state = task_status.TRANSFORMERING task = transformer.TransformerTask(task_id, task_state=task_state) self.trans_states.add_task(task) # start data transformer task thread args = [host_ip, host_port, mount_dir, mount_dir] thread = AgentThread(self.downLoadDirTree, self.trans_states, task_id, *args) thread.start() return task_id # self.downLoadDirTree(host_ip, host_ip, mount_dir, mount_dir) except Exception as e: LOG.error("DownLoad data error: %s", e) raise exception.DownLoadDataError(error=e)
def _fillp_copy_volume(self, volume, protocol): # 1. get sgent vm info src_disk_name = volume.get('src_dev_name') dev_disk_name = volume.get('des_dev_name') src_vm_url = volume.get('src_gw_url') des_vm_url = volume.get('des_gw_url') mount_dir = volume.get('src_mount_point', None) mount = None if mount_dir: mount = mount_dir[0] des_urls = des_vm_url.split(':') if len(des_urls) != 2: LOG.error("Inpute source gw url error: %s", des_vm_url) msg = "Inpute source gw url error: %s" % des_vm_url raise exception.InvalidInput(reason=msg) des_ip = des_urls[0] # 2. start fillp server trans_port = volume.get('trans_port') agent_driver = self.agents.get(protocol) try: agent_driver.start_fillp_server(des_ip, trans_port, dev_disk_name, protocol) except Exception as e: _msg = "Conveyor agent start fillp server error: %s" % e LOG.error(_msg) raise exception.V2vException(message=_msg) # 3. start data transformer # splite remove gw url, get host ip and port src_urls = src_vm_url.split(':') if len(src_urls) != 2: LOG.error("Input source gw url error: %s", src_vm_url) msg = "Input Source gw url error: %s" % src_vm_url raise exception.InvalidInput(reason=msg) src_vm_ip = src_urls[0] src_vm_port = src_urls[1] try: # create transformer task and return task id for querying it's # state task_id = uuidutils.generate_uuid() task_state = task_status.TRANSFORMERING task = transformer.TransformerTask(task_id, task_state=task_state) self.trans_states.add_task(task) # start data transformer task thread args = [ src_vm_ip, src_vm_port, des_ip, trans_port, src_disk_name, dev_disk_name, protocol, mount ] thread = AgentThread(self._fillp_transformer_data, self.trans_states, task_id, *args) thread.start() return task_id except Exception as e: LOG.error("Download data error: %s", e) raise exception.DownLoadDataError(Error=e)
def __init__(self, name, app, host=None, port=None, pool_size=None, protocol=eventlet.wsgi.HttpProtocol, backlog=128): """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. :returns: None """ # Allow operators to customize http requests max header line size. eventlet.wsgi.MAX_HEADER_LINE = CONF.max_header_line self.client_socket_timeout = CONF.client_socket_timeout or None self.name = name self.app = app self._host = host or "0.0.0.0" self._port = port or 0 self._server = None self._socket = None self._protocol = protocol self.pool_size = pool_size or self.default_pool_size self._pool = eventlet.GreenPool(self.pool_size) self._logger = logging.getLogger("eventlet.wsgi.server") 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 cert_file = CONF.ssl_cert_file key_file = CONF.ssl_key_file ca_file = CONF.ssl_ca_file self._use_ssl = cert_file or key_file if cert_file and not os.path.exists(cert_file): raise RuntimeError(_("Unable to find cert_file : %s") % cert_file) if ca_file and not os.path.exists(ca_file): raise RuntimeError(_("Unable to find ca_file : %s") % ca_file) if key_file and not os.path.exists(key_file): raise RuntimeError(_("Unable to find key_file : %s") % key_file) if self._use_ssl and (not cert_file or not key_file): raise RuntimeError( _("When running server in SSL mode, you " "must specify both a cert_file and " "key_file option value in your " "configuration file.")) retry_until = time.time() + 30 while not self._socket and time.time() < retry_until: try: self._socket = eventlet.listen(bind_addr, backlog=backlog, family=family) except socket.error as err: if err.args[0] != errno.EADDRINUSE: raise eventlet.sleep(0.1) if not self._socket: raise RuntimeError( _("Could not bind to %(host)s:%(port)s " "after trying for 30 seconds") % { 'host': host, 'port': port }) (self._host, self._port) = self._socket.getsockname()[0:2] LOG.info(_LI("%(name)s listening on %(_host)s:%(_port)s"), { 'name': self.name, '_host': self._host, '_port': self._port })