Example #1
0
def main():
    with open("container_config.json", "r") as f:
        container_config = json.load(f)

    container_type = container_config["container_type"]
    container_name = container_config["container_name"]
    connection_configuration = container_config["connection_configuration"]
    if container_type != "docker":
        raise Exception("Monitoring container type [%s], not yet implemented." % container_type)

    ports_raw = None
    try:
        while True:
            ports_command = docker_util.build_docker_simple_command("port", container_name=container_name, **connection_configuration)
            with tempfile.TemporaryFile() as stdout_file:
                exit_code = subprocess.call(ports_command,
                                            shell=True,
                                            stdout=stdout_file,
                                            preexec_fn=os.setpgrp)
                if exit_code == 0:
                    stdout_file.seek(0)
                    ports_raw = stdout_file.read()
                    break

        if ports_raw is not None:
            with open("container_runtime.json", "w") as f:
                json.dump(docker_util.parse_port_text(ports_raw), f)
        else:
            raise Exception("Failed to recover ports...")
    except Exception as e:
        with open("exception.txt", "w") as f:
            f.write(str(e))
Example #2
0
def main():
    if not os.path.exists("configs"):
        # on Pulsar and in tool working directory instead of job directory
        os.chdir("..")

    with open("configs/container_config.json", "r") as f:
        container_config = json.load(f)

    container_type = container_config["container_type"]
    container_name = container_config["container_name"]
    callback_url = container_config.get("callback_url")
    connection_configuration = container_config["connection_configuration"]
    if container_type != "docker":
        raise Exception(
            "Monitoring container type [%s], not yet implemented." %
            container_type)

    ports_raw = None
    for i in range(10):
        try:
            ports_raw = parse_ports(container_name, connection_configuration)
            if ports_raw is not None:
                try:
                    host_ip = socket.gethostbyname(socket.gethostname())
                except Exception:
                    # doesn't work on OS X
                    host_ip = None
                ports = docker_util.parse_port_text(ports_raw)
                if host_ip is not None:
                    for key in ports:
                        if ports[key]['host'] == '0.0.0.0':
                            ports[key]['host'] = host_ip
                if callback_url:
                    requests.post(callback_url,
                                  json={"container_runtime": ports})
                else:
                    with open("container_runtime.json", "w") as f:
                        json.dump(ports, f)
                break
            else:
                raise Exception("Failed to recover ports...")
        except Exception as e:
            with open("container_monitor_exception.txt", "a") as f:
                f.write(str(e) + "\n\n\n")
        time.sleep(i * 2)
Example #3
0
def main():
    if not os.path.exists("configs"):
        # on Pulsar and in tool working directory instead of job directory
        os.chdir("..")

    with open("configs/container_config.json") as f:
        container_config = json.load(f)

    container_type = container_config["container_type"]
    container_name = container_config["container_name"]
    callback_url = container_config.get("callback_url")
    connection_configuration = container_config["connection_configuration"]
    if container_type != "docker":
        raise Exception(
            "Monitoring container type [%s], not yet implemented." %
            container_type)

    ports_raw = None
    exc_traceback = ""
    for i in range(10):
        try:
            ports_raw = parse_ports(container_name, connection_configuration)
            if ports_raw is not None:
                host_ip = get_ip()
                ports = docker_util.parse_port_text(ports_raw)
                if host_ip is not None:
                    for key in ports:
                        if ports[key]['host'] == '0.0.0.0':
                            ports[key]['host'] = host_ip
                if callback_url:
                    requests.post(callback_url,
                                  json={"container_runtime": ports})
                else:
                    with open("container_runtime.json", "w") as f:
                        json.dump(ports, f)
                break
            else:
                raise Exception("Failed to recover ports...")
        except Exception:
            exc_info = sys.exc_info()
            exc_traceback = "".join(traceback.format_exception(*exc_info))
        time.sleep(i * 2)
    else:
        with open("container_monitor_exception.txt", "w") as f:
            f.write(exc_traceback)
Example #4
0
def main():
    with open("container_config.json", "r") as f:
        container_config = json.load(f)

    container_type = container_config["container_type"]
    container_name = container_config["container_name"]
    connection_configuration = container_config["connection_configuration"]
    if container_type != "docker":
        raise Exception(
            "Monitoring container type [%s], not yet implemented." %
            container_type)

    ports_raw = None
    for i in range(10):
        try:
            ports_raw = parse_ports(container_name, connection_configuration)
            if ports_raw is not None:
                try:
                    host_ip = socket.gethostbyname(socket.gethostname())
                except Exception:
                    # doesn't work on OS X
                    host_ip = None
                with open("container_runtime.json", "w") as f:
                    ports = docker_util.parse_port_text(ports_raw)
                    if host_ip is not None:
                        for key in ports:
                            if ports[key]['host'] == '0.0.0.0':
                                ports[key]['host'] = host_ip
                    json.dump(ports, f)
                break
            else:
                raise Exception("Failed to recover ports...")
        except Exception as e:
            with open("container_monitor_exception.txt", "a") as f:
                f.write(str(e))
        time.sleep(i * 2)