def main(): logutils.configure_logging() cmd = ["uwsgi"] autoreload = int(config.get("worker_autoreload", "0")) if autoreload > 0: cmd.extend(["--py-autoreload", "{}".format(autoreload)]) serve_results = config.get("worker_serve_results", default=None) if serve_results is not None: for dname in serve_results.split(":"): cmd.extend(["--static-map", "{}={}".format(dname, dname)]) swagger_yaml = Path( config.get("worker_services_dir", default="/code/services"), "swagger.yaml" ) if swagger_yaml.exists(): cmd.extend(["--static-map", "/services/swagger.yaml={}".format(swagger_yaml)]) swagger_ui = Path( config.get("worker_swagger_ui_path", default="/usr/share/nginx/html") ) if swagger_yaml.exists(): cmd.extend(["--static-map", "/docs={}".format(swagger_ui)]) cmd.extend(["--static-index", "index.html"]) try: static_assets = config.get("worker_static_map") except Exception: pass else: cmd.extend(["--static-map", static_assets]) cmd.append( config.get( "worker_uwsgi_config_file", default=str(Path(config.__file__, "..", "uwsgi.ini").resolve()), ) ) os.environ.setdefault( "SERVICELIB_WORKER_NUM_PROCESSES", config.get("worker_num_processes", str(psutil.cpu_count())), ) os.environ.setdefault( "SERVICELIB_WORKER_NUM_THREADS", config.get("worker_num_threads", "1") ) os.environ.setdefault("SERVICELIB_WORKER_PORT", config.get("worker_port", "8000")) log = logutils.get_logger("servicelib-worker") log.info("Running: %s", " ".join(cmd)) os.execlp(cmd[0], *cmd[0:])
def main(): parser = argparse.ArgumentParser() parser.add_argument("--config-url", metavar="URL", help="URL of the config server", default=None) parser.add_argument("--verbose", action="store_true", help="verbose operation", default=False) subparsers = parser.add_subparsers(help="commands") get_p = subparsers.add_parser("get", help="get the value of a config key") get_p.add_argument("key", metavar="<key>") get_p.set_defaults(func=_get) set_p = subparsers.add_parser("set", help="set the value of a config key") set_p.add_argument("key", metavar="<key>") set_p.add_argument("value", metavar="<value>") set_p.set_defaults(func=_set) del_p = subparsers.add_parser( "delete", help="remove a setting value from the config source") del_p.add_argument("key", metavar="<key>") del_p.set_defaults(func=_delete) dump_p = subparsers.add_parser("dump", help="Dump config in JSON format to stdout") dump_p.set_defaults(func=_dump) diff_p = subparsers.add_parser("diff", help="compare config from two sources") diff_p.add_argument("src", metavar="<source url>") diff_p.add_argument("dest", metavar="<dest url>") diff_p.set_defaults(func=_diff) args = parser.parse_args() logutils.configure_logging(level=args.verbose and "DEBUG" or "WARN") c = client.instance(url=args.config_url) return args.func(c, args)
from __future__ import absolute_import, unicode_literals import os import falcon from servicelib import logutils from servicelib.config.server import ConfigServer from servicelib.falcon import HealthResource __all__ = [ "application", ] logutils.configure_logging( level=os.environ.get("SERVICELIB_LOG_LEVEL", "DEBUG").upper(), log_type=os.environ.get("SERVICELIB_LOG_TYPE", "text"), ) class ConfigServerMiddleware(object): log = logutils.get_logger("config-server") def __init__(self, prefix): self.prefix = prefix def process_request(self, req, resp): """'Translate the key in the HTTP path, so that slashes get transformed in dots. This is a workaround for a Falcon limitation:
import os import falcon from servicelib import config, inventory, logutils from servicelib.falcon import HealthResource, StatsResource, WorkerResource __all__ = [ "application", ] # On y va! logutils.configure_logging( level=config.get("log_level", default="debug").upper(), log_type=config.get("log_type", default="text"), ) services = inventory.instance().load_services() application = falcon.API(media_type=falcon.MEDIA_JSON) application.add_route("/services/{service}", WorkerResource(services)) # Now that routes for services have been set up, we are ready to # handle requests. Let Kubernetes know (or whoever may be sending # health check probes) by enabling the health check route. application.add_route("/health", HealthResource()) application.add_route("/stats", StatsResource()) os.umask(0o22)
import signal import subprocess import tempfile import pytest import requests from servicelib import errors, logutils, utils from servicelib.compat import Path, open __all__ = [ "servicelib_ini", "worker", ] logutils.configure_logging() @pytest.fixture def servicelib_ini(request, monkeypatch): monkeypatch.setenv( "SERVICELIB_CONFIG_FILE", os.path.join(os.path.dirname(__file__), "sample-servicelib.ini"), ) UWSGI_INI_TEMPLATE = """ [uwsgi] chdir = {services_dir} daemonize = {log_file} die-on-term = true
def main(): logutils.configure_logging() cmdline_config = cmdline.parse_args( "worker.autoreload", "worker.hostname", "worker.load_workers", "worker.num_processes", "worker.num_threads", "worker.port", "worker.services_dir", ) for k, v in cmdline_config.items(): if isinstance(v, list): v = json.dumps(v) else: v = str(v) os.environ[env_var(k)] = v cmd = ["uwsgi", "--req-logger", "file:/dev/null"] autoreload = int(config.get("worker.autoreload", "0")) if autoreload > 0: cmd.extend(["--py-autoreload", "{}".format(autoreload)]) # pragma: no cover serve_results = config.get("worker.serve_results", default=None) if serve_results is not None: for dname in serve_results.split(":"): cmd.extend(["--static-map", "{}={}".format(dname, dname)]) swagger_yaml = Path( config.get("worker.services_dir", default="/code/services"), "swagger.yaml") if swagger_yaml.exists(): cmd.extend( ["--static-map", "/services/swagger.yaml={}".format(swagger_yaml)]) swagger_ui = Path( config.get("worker.swagger_ui_path", default="/usr/share/nginx/html")) if swagger_yaml.exists(): cmd.extend(["--static-map", "/docs={}".format(swagger_ui)]) cmd.extend(["--static-index", "index.html"]) static_assets = config.get("worker.static_map", default=None) if static_assets is not None: cmd.extend(["--static-map", static_assets]) cmd.append( config.get( "worker.uwsgi_config_file", default=str(Path(logutils.__file__, "..", "uwsgi.ini").resolve()), )) os.environ.setdefault( env_var("worker.num_processes"), config.get("worker.num_processes", str(psutil.cpu_count())), ) os.environ.setdefault(env_var("worker.num_threads"), str(config.get("worker.num_threads", 1))) os.environ.setdefault(env_var("worker.port"), str(config.get("worker.port", 8000))) log = logutils.get_logger("servicelib-worker") log.info("Environment: %s", os.environ) log.info("Running: %s", " ".join(cmd)) # If we're running under `pytest-cov`, call `pytest_cov.embed.cleanup()` # before exec of uWSGI, so that we do not lose coverage info for this # Python module. if os.environ.get("COV_CORE_DATAFILE"): from pytest_cov.embed import cleanup cleanup() os.execlp(cmd[0], *cmd[0:])