Пример #1
0
def proxy(config: dict):
    """Start sshuttle proxy to Kubernetes."""
    port = config["port"]
    if "ip" in config:
        # Typically host is macOS:
        ip = config["ip"]
    else:
        # Typically host is Linux, use default route:
        ip = None
        route_output = str(check_output(["route", "-n"]), "ascii")
        for line in route_output.splitlines():
            parts = line.split()
            if parts[0] == "default" or parts[0] == "0.0.0.0":
                ip = parts[1]
                break
        assert ip is not None, route_output
    cidrs = config["cidrs"]
    expose_ports = config["expose_ports"]

    # Start the sshuttle VPN-like thing:
    # XXX duplicates code in telepresence, remove duplication
    main_process = Popen([
        "sshuttle-telepresence", "-v", "--dns", "--method", "nat", "-e", (
            "ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null " +
            "-F /dev/null"
        ), "--to-ns", "127.0.0.1:9053", "-r",
        "telepresence@{}:{}".format(ip, port)
    ] + cidrs)
    # Start the SSH tunnels to expose local services:
    runner = Runner.open("-", "kubectl", False)
    ssh = SSH(runner, port, ip)
    expose_local_services(runner, ssh, expose_ports)

    # Wait for everything to exit:
    wait_for_exit(runner, main_process)
Пример #2
0
def main():
    """
    Top-level function for Telepresence
    """

    ########################################
    # Preliminaries: No changes to the machine or the cluster, no cleanup
    # Capture environment info and the user's intent

    # Check for a subcommand
    with crash_reporting():
        args = command_parse_args(None, only_for_commands=True)
    if args is not None:
        command_main(args)

    with crash_reporting():
        args = parse_args()  # tab-completion stuff goes here

        runner = Runner(Output(args.logfile), None, args.verbose)
        span = runner.span()
        runner.add_cleanup("Stop time tracking", span.end)
        runner.kubectl = KubeInfo(runner, args)

        start_proxy = proxy.setup(runner, args)
        do_connect = connect.setup(runner, args)
        get_remote_env, write_env_files = remote_env.setup(runner, args)
        launch = outbound.setup(runner, args)
        mount_remote = mount.setup(runner, args)

        final_checks(runner, args)

        # Usage tracking
        call_scout(runner, args)

    ########################################
    # Now it's okay to change things

    with runner.cleanup_handling(), crash_reporting(runner):
        # Set up the proxy pod (operation -> pod name)
        remote_info = start_proxy(runner)

        # Connect to the proxy (pod name -> ssh object)
        socks_port, ssh = do_connect(runner, remote_info)

        # Capture remote environment information (ssh object -> env info)
        env = get_remote_env(runner, remote_info)

        # Handle filesystem stuff
        mount_dir = mount_remote(runner, env, ssh)

        # Maybe write environment files
        write_env_files(runner, env)

        # Set up outbound networking (pod name, ssh object)
        # Launch user command with the correct environment (...)
        user_process = launch(
            runner, remote_info, env, socks_port, ssh, mount_dir
        )

        wait_for_exit(runner, user_process)
Пример #3
0
def command(runner):
    with runner.cleanup_handling(), crash_reporting(runner):
        runner.require_sudo()
        runner.show("Setting up outbound connectivity...")
        runner.launch(
            "teleproxy intercept",
            ["sudo", "teleproxy", "-mode", "intercept"],
            killer=kill_intercept,
        )
        runner.launch(
            "teleproxy bridge",
            [
                "teleproxy", "-mode", "bridge", "-context",
                runner.kubectl.context, "-namespace", runner.kubectl.namespace
            ],
        )
        runner.show("Outbound is running. Press Ctrl-C/Ctrl-Break to quit.")
        user_process = Popen(["cat"], stdout=DEVNULL)
        wait_for_exit(runner, user_process)
Пример #4
0
def main(session):
    """
    Top-level function for Telepresence
    """

    ########################################
    # Preliminaries: No changes to the machine or the cluster, no cleanup

    with crash_reporting():
        session.args = parse_args()  # tab-completion stuff goes here

        session.output = Output(session.args.logfile)
        del session.args.logfile

        session.kube_info, session.runner = analyze_args(session)

        span = session.runner.span()
        session.runner.add_cleanup("Stop time tracking", span.end)

        # Usage tracking
        call_scout(session)

    ########################################
    # Now it's okay to change things

    with session.runner.cleanup_handling(), crash_reporting(session.runner):
        runner = session.runner
        args = session.args

        # Set up the proxy pod (operation -> pod name)
        remote_info = start_proxy(runner, args)

        # Connect to the proxy (pod name -> ssh object)
        socks_port, ssh = connect(runner, remote_info, args)

        # Capture remote environment information (ssh object -> env info)
        env = get_remote_env(runner, args, remote_info)

        # Used by mount_remote
        session.ssh = ssh
        session.remote_info = remote_info
        session.env = env

        # Handle filesystem stuff (pod name, ssh object)
        mount_dir = mount_remote(session)

        # Maybe write environment files
        write_env_files(session)

        # Set up outbound networking (pod name, ssh object)
        # Launch user command with the correct environment (...)
        if args.method == "container":
            user_process = run_docker_command(
                runner,
                remote_info,
                args,
                env,
                ssh,
                mount_dir,
            )
        else:
            user_process = run_local_command(runner, remote_info, args, env,
                                             socks_port, ssh)

        wait_for_exit(runner, user_process)