Beispiel #1
0
def run(**kwargs):
    with build_config(os.path.join(repo_root, "config.json"),
                      **kwargs) as config:
        global logger
        logger = config.logger
        set_logger(logger)

        bind_address = config["bind_address"]

        if kwargs.get("alias_file"):
            with open(kwargs["alias_file"], 'r') as alias_file:
                for line in alias_file:
                    alias, doc_root = [x.strip() for x in line.split(',')]
                    config["aliases"].append({
                        'url-path': alias,
                        'local-dir': doc_root,
                    })

        if config["check_subdomains"]:
            check_subdomains(config)

        stash_address = None
        if bind_address:
            stash_address = (config.server_host, get_port(""))
            logger.debug("Going to use port %d for stash" % stash_address[1])

        with stash.StashServer(stash_address, authkey=str(uuid.uuid4())):
            servers = start(config, build_routes(config["aliases"]), **kwargs)

            try:
                while any(item.is_alive() for item in iter_procs(servers)):
                    for item in iter_procs(servers):
                        item.join(1)
            except KeyboardInterrupt:
                logger.info("Shutting down")
Beispiel #2
0
def run(**kwargs):
    config = load_config(os.path.join(repo_root, "config.json"),
                         **kwargs)

    global logger
    logger = config.logger
    set_logger(logger)

    bind_address = config["bind_address"]

    if config["check_subdomains"]:
        check_subdomains(config)

    stash_address = None
    if bind_address:
        stash_address = (config.server_host, get_port(config.server_host))
        logger.debug("Going to use port %d for stash" % stash_address[1])

    with stash.StashServer(stash_address, authkey=str(uuid.uuid4())):
        servers = start(config, config.ssl_env, build_routes(config["aliases"]), **kwargs)

        try:
            while any(item.is_alive() for item in iter_procs(servers)):
                for item in iter_procs(servers):
                    item.join(1)
        except KeyboardInterrupt:
            logger.info("Shutting down")
Beispiel #3
0
def check_subdomains(domains, paths, bind_address, ssl_config, aliases):
    domains = domains.copy()
    host = domains.pop("")
    port = get_port(host)
    logger.debug("Going to use port %d to check subdomains" % port)

    wrapper = ServerProc()
    wrapper.start(start_http_server, host, port, paths, build_routes(aliases), bind_address,
                  None, ssl_config)

    connected = False
    for i in range(10):
        try:
            urllib2.urlopen("http://%s:%d/" % (host, port))
            connected = True
            break
        except urllib2.URLError:
            time.sleep(1)

    if not connected:
        logger.critical("Failed to connect to test server on http://%s:%s. "
                        "You may need to edit /etc/hosts or similar, see README.md." % (host, port))
        sys.exit(1)

    for domain in domains.itervalues():
        try:
            urllib2.urlopen("http://%s:%d/" % (domain, port))
        except Exception as e:
            logger.critical("Failed probing domain %s. "
                            "You may need to edit /etc/hosts or similar, see README.md." % domain)
            sys.exit(1)

    wrapper.wait()
Beispiel #4
0
def run(**kwargs):
    received_signal = threading.Event()

    with build_config(os.path.join(repo_root, "config.json"),
                      **kwargs) as config:
        global logger
        logger = config.logger
        set_logger(logger)
        # Configure the root logger to cover third-party libraries.
        logging.getLogger().setLevel(config.log_level)

        def handle_signal(signum, frame):
            logger.debug("Received signal %s. Shutting down.", signum)
            received_signal.set()

        bind_address = config["bind_address"]

        if kwargs.get("alias_file"):
            with open(kwargs["alias_file"], 'r') as alias_file:
                for line in alias_file:
                    alias, doc_root = [x.strip() for x in line.split(',')]
                    config["aliases"].append({
                        'url-path': alias,
                        'local-dir': doc_root,
                    })

        if config["check_subdomains"]:
            check_subdomains(config)

        stash_address = None
        if bind_address:
            stash_address = (config.server_host, get_port(""))
            logger.debug("Going to use port %d for stash" % stash_address[1])

        with stash.StashServer(stash_address, authkey=str(uuid.uuid4())):
            servers = start(config, build_routes(config["aliases"]), **kwargs)
            signal.signal(signal.SIGTERM, handle_signal)
            signal.signal(signal.SIGINT, handle_signal)

            while (all(subproc.is_alive() for subproc in iter_procs(servers))
                   and not received_signal.is_set()):
                for subproc in iter_procs(servers):
                    subproc.join(1)

            failed_subproc = 0
            for subproc in iter_procs(servers):
                if subproc.is_alive():
                    logger.info('Status of subprocess "%s": running' %
                                subproc.name)
                else:
                    if subproc.exitcode == 0:
                        logger.info(
                            'Status of subprocess "%s": exited correctly' %
                            subproc.name)
                    else:
                        logger.warning(
                            'Status of subprocess "%s": failed. Exit with non-zero status: %d'
                            % (subproc.name, subproc.exitcode))
                        failed_subproc += 1
            return failed_subproc
Beispiel #5
0
def run(**kwargs):
    with build_config(os.path.join(repo_root, "config.json"),
                      **kwargs) as config:
        global logger
        logger = config.logger
        set_logger(logger)

        bind_address = config["bind_address"]

        if kwargs.get("alias_file"):
            with open(kwargs["alias_file"], 'r') as alias_file:
                for line in alias_file:
                    alias, doc_root = [x.strip() for x in line.split(',')]
                    config["aliases"].append({
                        'url-path': alias,
                        'local-dir': doc_root,
                    })

        if config["check_subdomains"]:
            check_subdomains(config)

        stash_address = None
        if bind_address:
            stash_address = (config.server_host, get_port(""))
            logger.debug("Going to use port %d for stash" % stash_address[1])

        with stash.StashServer(stash_address, authkey=str(uuid.uuid4())):
            servers = start(config, build_routes(config["aliases"]), **kwargs)

            try:
                while any(item.is_alive() for item in iter_procs(servers)):
                    for item in iter_procs(servers):
                        item.join(1)
            except KeyboardInterrupt:
                logger.info("Shutting down")
Beispiel #6
0
def run(**kwargs):
    received_signal = threading.Event()

    with build_config(os.path.join(repo_root, "config.json"),
                      **kwargs) as config:
        global logger
        logger = config.logger
        set_logger(logger)
        # Configure the root logger to cover third-party libraries.
        logging.getLogger().setLevel(config.log_level)

        def handle_signal(signum, frame):
            logger.debug("Received signal %s. Shutting down.", signum)
            received_signal.set()

        bind_address = config["bind_address"]

        if kwargs.get("alias_file"):
            with open(kwargs["alias_file"], 'r') as alias_file:
                for line in alias_file:
                    alias, doc_root = [x.strip() for x in line.split(',')]
                    config["aliases"].append({
                        'url-path': alias,
                        'local-dir': doc_root,
                    })

        if config["check_subdomains"]:
            check_subdomains(config)

        stash_address = None
        if bind_address:
            stash_address = (config.server_host, get_port(""))
            logger.debug("Going to use port %d for stash" % stash_address[1])

        with stash.StashServer(stash_address, authkey=str(uuid.uuid4())):
            servers = start(config, build_routes(config["aliases"]), **kwargs)
            signal.signal(signal.SIGTERM, handle_signal)
            signal.signal(signal.SIGINT, handle_signal)

            while (all(item.is_alive() for item in iter_procs(servers))
                   and not received_signal.is_set()):
                for item in iter_procs(servers):
                    item.join(1)
            exited = [
                item for item in iter_procs(servers) if not item.is_alive()
            ]
            subject = "subprocess" if len(exited) == 1 else "subprocesses"

            logger.info("%s %s exited:" % (len(exited), subject))

            for item in iter_procs(servers):
                logger.info("Status of %s:\t%s" %
                            (item.name,
                             "running" if item.is_alive() else "not running"))
Beispiel #7
0
def run(**kwargs):
    with build_config(os.path.join(repo_root, "config.json"),
                      **kwargs) as config:
        global logger
        logger = config.logger
        set_logger(logger)

        bind_address = config["bind_address"]

        # Creating wave specific config if kwargs is_wave = true
        wave_cfg = None
        if kwargs.get("is_wave") is True:
            wave_cfg = {
                "is_wave": kwargs.get("is_wave"),
                "report": kwargs.get("report")
            }


        if kwargs.get("alias_file"):
            with open(kwargs["alias_file"], 'r') as alias_file:
                for line in alias_file:
                    alias, doc_root = [x.strip() for x in line.split(',')]
                    config["aliases"].append({
                        'url-path': alias,
                        'local-dir': doc_root,
                    })

        if config["check_subdomains"]:
            # added wave_cfg to pass on to build_routes to init wave handler
            check_subdomains(config, wave_cfg)

        stash_address = None
        if bind_address:
            stash_address = (config.server_host, get_port(""))
            logger.debug("Going to use port %d for stash" % stash_address[1])

        with stash.StashServer(stash_address, authkey=str(uuid.uuid4())):
            servers = start(config, build_routes(config["aliases"], wave_cfg), **kwargs)

            try:
                while all(item.is_alive() for item in iter_procs(servers)):
                    for item in iter_procs(servers):
                        item.join(1)
                exited = [item for item in iter_procs(servers) if not item.is_alive()]
                subject = "subprocess" if len(exited) == 1 else "subprocesses"

                logger.info("%s %s exited:" % (len(exited), subject))

                for item in iter_procs(servers):
                    logger.info("Status of %s:\t%s" % (item.name, "running" if item.is_alive() else "not running"))
            except KeyboardInterrupt:
                logger.info("Shutting down")
Beispiel #8
0
def check_subdomains(config):
    paths = config.paths
    bind_address = config.bind_address
    ssl_config = config.ssl_config
    aliases = config.aliases

    host = config.server_host
    port = get_port()
    logger.debug("Going to use port %d to check subdomains" % port)

    wrapper = ServerProc()
    wrapper.start(start_http_server, host, port, paths, build_routes(aliases),
                  bind_address, None, ssl_config)

    connected = False
    for i in range(10):
        try:
            urllib.request.urlopen("http://%s:%d/" % (host, port))
            connected = True
            break
        except urllib.error.URLError:
            time.sleep(1)

    if not connected:
        logger.critical(
            "Failed to connect to test server on http://%s:%s. "
            "You may need to edit /etc/hosts or similar, see README.md." %
            (host, port))
        sys.exit(1)

    for domain in config.domains_set:
        if domain == host:
            continue

        try:
            urllib.request.urlopen("http://%s:%d/" % (domain, port))
        except Exception:
            logger.critical(
                "Failed probing domain %s. "
                "You may need to edit /etc/hosts or similar, see README.md." %
                domain)
            sys.exit(1)

    wrapper.wait()
Beispiel #9
0
def check_subdomains(config):
    paths = config.paths
    bind_address = config.bind_address
    ssl_config = config.ssl_config
    aliases = config.aliases

    host = config.server_host
    port = get_port(host)
    logger.debug("Going to use port %d to check subdomains" % port)

    wrapper = ServerProc()
    wrapper.start(start_http_server, host, port, paths, build_routes(aliases), bind_address,
                  None, ssl_config)

    connected = False
    for i in range(10):
        try:
            urllib2.urlopen("http://%s:%d/" % (host, port))
            connected = True
            break
        except urllib2.URLError:
            time.sleep(1)

    if not connected:
        logger.critical("Failed to connect to test server on http://%s:%s. "
                        "You may need to edit /etc/hosts or similar, see README.md." % (host, port))
        sys.exit(1)

    for domain in config.domains_set:
        if domain == host:
            continue

        try:
            urllib2.urlopen("http://%s:%d/" % (domain, port))
        except Exception:
            logger.critical("Failed probing domain %s. "
                            "You may need to edit /etc/hosts or similar, see README.md." % domain)
            sys.exit(1)

    wrapper.wait()
Beispiel #10
0
def check_subdomains(config):
    paths = config.paths
    bind_address = config.bind_address
    aliases = config.aliases

    host = config.server_host
    port = get_port()
    logger.debug("Going to use port %d to check subdomains" % port)

    wrapper = ServerProc()
    wrapper.start(start_http_server, host, port, paths, build_routes(aliases),
                  bind_address, config)

    url = "http://{}:{}/".format(host, port)
    connected = False
    for i in range(10):
        try:
            urllib.request.urlopen(url)
            connected = True
            break
        except urllib.error.URLError:
            time.sleep(1)

    if not connected:
        logger.critical("Failed to connect to test server "
                        "on {}. {}".format(url, EDIT_HOSTS_HELP))
        sys.exit(1)

    for domain in config.domains_set:
        if domain == host:
            continue

        try:
            urllib.request.urlopen("http://%s:%d/" % (domain, port))
        except Exception:
            logger.critical("Failed probing domain {}. {}".format(
                domain, EDIT_HOSTS_HELP))
            sys.exit(1)

    wrapper.wait()
Beispiel #11
0
def check_subdomains(logger, config, routes, mp_context, log_handlers):
    paths = config.paths
    bind_address = config.bind_address

    host = config.server_host
    port = get_port()
    logger.debug("Going to use port %d to check subdomains" % port)

    wrapper = ServerProc(mp_context)
    wrapper.start(start_http_server, host, port, paths, routes, bind_address,
                  config, log_handlers)

    url = f"http://{host}:{port}/"
    connected = False
    for i in range(10):
        try:
            urllib.request.urlopen(url)
            connected = True
            break
        except urllib.error.URLError:
            time.sleep(1)

    if not connected:
        logger.critical("Failed to connect to test server "
                        "on {}. {}".format(url, EDIT_HOSTS_HELP))
        sys.exit(1)

    for domain in config.domains_set:
        if domain == host:
            continue

        try:
            urllib.request.urlopen("http://%s:%d/" % (domain, port))
        except Exception:
            logger.critical(
                f"Failed probing domain {domain}. {EDIT_HOSTS_HELP}")
            sys.exit(1)

    wrapper.stop()
Beispiel #12
0
def check_subdomains(domains, paths, bind_address, ssl_config, aliases):
    domains = domains.copy()
    host = domains.pop("")
    port = get_port(host)
    logger.debug("Going to use port %d to check subdomains" % port)

    wrapper = ServerProc()
    wrapper.start(start_http_server, host, port, paths, build_routes(aliases),
                  bind_address, None, ssl_config)

    connected = False
    for i in range(10):
        try:
            urllib2.urlopen("http://%s:%d/" % (host, port))
            connected = True
            break
        except urllib2.URLError:
            time.sleep(1)

    if not connected:
        logger.critical(
            "Failed to connect to test server on http://%s:%s. "
            "You may need to edit /etc/hosts or similar, see README.md." %
            (host, port))
        sys.exit(1)

    for domain in domains.itervalues():
        try:
            urllib2.urlopen("http://%s:%d/" % (domain, port))
        except Exception as e:
            logger.critical(
                "Failed probing domain %s. "
                "You may need to edit /etc/hosts or similar, see README.md." %
                domain)
            sys.exit(1)

    wrapper.wait()
Beispiel #13
0
def run(config_cls=ConfigBuilder, route_builder=None, mp_context=None, log_handlers=None,
        **kwargs):
    logger = get_logger("INFO", log_handlers)

    if mp_context is None:
        if hasattr(multiprocessing, "get_context"):
            mp_context = multiprocessing.get_context()
        else:
            mp_context = MpContext()

    with build_config(logger,
                      os.path.join(repo_root, "config.json"),
                      config_cls=config_cls,
                      **kwargs) as config:
        # This sets the right log level
        logger = get_logger(config.log_level, log_handlers)

        bind_address = config["bind_address"]

        if kwargs.get("alias_file"):
            with open(kwargs["alias_file"]) as alias_file:
                for line in alias_file:
                    alias, doc_root = (x.strip() for x in line.split(','))
                    config["aliases"].append({
                        'url-path': alias,
                        'local-dir': doc_root,
                    })

        if route_builder is None:
            route_builder = get_route_builder
        routes = route_builder(logger, config.aliases, config).get_routes()

        if config["check_subdomains"]:
            check_subdomains(logger, config, routes, mp_context, log_handlers)

        stash_address = None
        if bind_address:
            stash_address = (config.server_host, get_port(""))
            logger.debug("Going to use port %d for stash" % stash_address[1])

        with stash.StashServer(stash_address, authkey=str(uuid.uuid4())):
            servers = start(logger, config, routes, mp_context, log_handlers, **kwargs)

            if not kwargs.get("exit_after_start"):
                try:
                    # Periodically check if all the servers are alive
                    server_process_exited = False
                    while not server_process_exited:
                        for server in iter_servers(servers):
                            server.proc.join(1)
                            if not server.proc.is_alive():
                                server_process_exited = True
                                break
                except KeyboardInterrupt:
                    pass

            failed_subproc = 0
            for server in iter_servers(servers):
                subproc = server.proc
                if subproc.is_alive():
                    logger.info('Status of subprocess "%s": running', subproc.name)
                    server.stop(timeout=1)

                if server.proc.exitcode == 0:
                    logger.info('Status of subprocess "%s": exited correctly', subproc.name)
                else:
                    logger.warning('Status of subprocess "%s": failed. Exit with non-zero status: %d',
                                   subproc.name, subproc.exitcode)
                    failed_subproc += 1
            return failed_subproc