def check_task(self, t_name): session = get_session() with session.begin(): res = False hostname = socket.gethostname() now = time.time() tasks = session.query(models.RegitsterTask).filter_by( task_name=t_name).with_lockmode('update').all() if len(tasks) != 1: log.error(_LE("unsuported task type:%s, please check it"), t_name) return False if tasks[0].update_time is None or (now - time.mktime( time.strptime(str(tasks[0].update_time), '%Y-%m-%d %H:%M:%S'))) > 600: tasks[0].host_name = hostname tasks[0].update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now)) res = True else: if tasks[0].host_name == hostname: tasks[0].update_time = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(now)) res = True else: res = False return res
def get_method(self, request, action, content_type, body): """Look up the action-specific method and its extensions.""" # Look up the method try: if not self.controller: meth = getattr(self, action) else: meth = getattr(self.controller, action) except AttributeError as e: with excutils.save_and_reraise_exception(e) as ctxt: if (not self.wsgi_actions or action not in ['action', 'create', 'delete', 'update']): LOG.exception(_LE('Get method error.')) else: ctxt.reraise = False else: return meth, self.wsgi_extensions.get(action, []) if action == 'action': # OK, it's an action; figure out which action... mtype = _MEDIA_TYPE_MAP.get(content_type) action_name = self.action_peek[mtype](body) LOG.debug("Action body: %s", body) else: action_name = action # Look up the action method return (self.wsgi_actions[action_name], self.wsgi_action_extensions.get(action_name, []))
def start(self): """Start serving a WSGI application. :returns: None :raises: venus.exception.InvalidInput """ # The server socket object will be closed after server exits, # but the underlying file descriptor will remain open, and will # give bad file descriptor error. So duplicating the socket object, # to keep file descriptor usable. dup_socket = self._socket.dup() dup_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # NOTE(praneshp): Call set_tcp_keepalive in oslo to set # tcp keepalive parameters. Sockets can hang around forever # without keepalive netutils.set_tcp_keepalive(dup_socket, CONF.tcp_keepalive, CONF.tcp_keepidle, CONF.tcp_keepalive_count, CONF.tcp_keepalive_interval) if self._use_ssl: try: ssl_kwargs = { 'server_side': True, 'certfile': CONF.ssl_cert_file, 'keyfile': CONF.ssl_key_file, 'cert_reqs': ssl.CERT_NONE, } if CONF.ssl_ca_file: ssl_kwargs['ca_certs'] = CONF.ssl_ca_file ssl_kwargs['cert_reqs'] = ssl.CERT_REQUIRED dup_socket = ssl.wrap_socket(dup_socket, **ssl_kwargs) except Exception: with excutils.save_and_reraise_exception(): LOG.error( _LE("Failed to start %(name)s on %(_host)s: " "%(_port)s with SSL " "support."), self.__dict__) wsgi_kwargs = { 'func': eventlet.wsgi.server, 'sock': dup_socket, 'site': self.app, 'protocol': self._protocol, 'custom_pool': self._pool, 'log': self._logger, 'socket_timeout': self.client_socket_timeout, 'keepalive': CONF.wsgi_keep_alive } self._server = eventlet.spawn(**wsgi_kwargs)
def delete_es_history_index(self): len_d = self.custom_sql.get_config("es_index_length") if len_d is None: LOG.error(_LE("es_index_length no exist")) return today = time.strftime('%Y-%m-%d') url = self.elasticsearch_url + '/_cat/indices/*log-*' status, indexes = utils.request_es(url, "GET") if status != 200: LOG.error(_LE("failed to get es indexes")) return indexes_array = indexes.split('\n') for index in indexes_array: index_name = index.split(' ')[2] index_day = index_name.split('-')[1] diff_day = datetime.datetime.strptime(today, "%Y-%m-%d") - \ datetime.datetime.strptime(index_day, '%Y.%m.%d') if diff_day.days >= int(len_d): self.delete_index(index_name)
def start_task(self): try: LOG.info(_LI("delete es index task started")) ret = self.task_sql.check_task(TASK_NAME) if ret is not True: LOG.info(_LI("delete es index task not need execute")) return if CONF.elasticsearch.url == "": LOG.info(_LI("not deploy es and not need execute")) return try: self.delete_es_history_index() except Exception as e: LOG.error(_LE("delete es index, catch exception:%s"), six.text_type(e)) LOG.info(_LI("delete es index task done")) except Exception as e: LOG.error(_LE("delete es index task, catch exception:%s"), six.text_type(e))
def __init__(self, message=None, **kwargs): self.kwargs = kwargs self.kwargs['message'] = message if 'code' not in self.kwargs: try: self.kwargs['code'] = self.code except AttributeError: pass for k, v in self.kwargs.items(): if isinstance(v, Exception): self.kwargs[k] = six.text_type(v) if self._should_format(): try: message = self.message % kwargs except Exception: exc_info = sys.exc_info() # kwargs doesn't match a variable in the message # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in kwargs.items(): LOG.error(_LE("%(name)s: %(value)s"), { 'name': name, 'value': value }) if CONF.fatal_exception_format_errors: six.reraise(*exc_info) # at least get the core message out if something happened message = self.message elif isinstance(message, Exception): message = six.text_type(message) # NOTE(luisg): We put the actual message in 'msg' so that we can access # it, because if we try to access the message via 'message' it will be # overshadowed by the class' message attribute self.msg = message super(VenusException, self).__init__(message)
def load_app(self, name): """Return the paste URLMap wrapped WSGI application. :param name: Name of the application to load. :returns: Paste URLMap object wrapping the requested application. :raises: `venus.exception.PasteAppNotFound` """ try: return deploy.loadapp("config:%s" % self.config_path, name=name) except LookupError: LOG.exception(_LE("Error loading app %s"), name) raise exception.PasteAppNotFound(name=name, path=self.config_path)
def _check_extension(self, extension): """Checks for required methods in extension objects.""" try: LOG.debug('Ext name: %s', extension.name) LOG.debug('Ext alias: %s', extension.alias) LOG.debug('Ext description: %s', ' '.join(extension.__doc__.strip().split())) LOG.debug('Ext namespace: %s', extension.namespace) LOG.debug('Ext updated: %s', extension.updated) except AttributeError: LOG.exception(_LE("Exception loading extension.")) return False return True
def request_es(url, method, data=None): http = urllib3.PoolManager(timeout=30.0) try: if method == "GET" or method == "DELETE": resp = http.request(method, url=url) elif method == "POST": resp = http.request(method, url=url, body=json.dumps(data)) else: return 0, None return resp.status, resp.data.strip() except Exception as e: LOG.error(_LE("request es, catch exception:%s"), six.text_type(e)) return 0, None
def __exit__(self, ex_type, ex_value, ex_traceback): if not ex_value: return True if isinstance(ex_value, exception.NotAuthorized): raise Fault(webob.exc.HTTPForbidden(explanation=ex_value.msg)) elif isinstance(ex_value, exception.Invalid): raise Fault(exception.ConvertedException( code=ex_value.code, explanation=ex_value.msg)) elif isinstance(ex_value, TypeError): exc_info = (ex_type, ex_value, ex_traceback) LOG.error(_LE( 'Exception handling resource: %s'), ex_value, exc_info=exc_info) raise Fault(webob.exc.HTTPBadRequest()) elif isinstance(ex_value, Fault): LOG.info(_LI("Fault thrown: %s"), ex_value) raise ex_value elif isinstance(ex_value, webob.exc.HTTPException): LOG.info(_LI("HTTP exception thrown: %s"), ex_value) raise Fault(ex_value) # We didn't handle the exception return False
def delete_index(self, name): url = self.elasticsearch_url + '/' + name status, text = utils.request_es(url, "DELETE") if status != 200: LOG.error(_LE("failed to delete es index")) return