Esempio n. 1
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)
Esempio n. 2
0
def main():
    """
    Top-level function for Telepresence
    """

    with crash_reporting():
        ########################################
        # Preliminaries: No changes to the machine or the cluster, no cleanup
        # Capture environment info

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

        runner = Runner(args.logfile, args.verbose)
        span = runner.span()
        runner.add_cleanup("Stop time tracking", span.end)
        set_kube_command(runner, args)

    with runner.cleanup_handling(), crash_reporting(runner):
        ########################################
        # Intent: Fast, user prompts here, cleanup available
        # Capture the user's intent

        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)

        ########################################
        # Action: Perform the user's intended operation(s)
        # Now it's okay to change things

        # 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, pod_info = get_remote_env(runner, ssh, 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, pod_info
        )

        runner.wait_for_exit(user_process)
Esempio n. 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",
            [
                "sh", "-c", 'exec sudo NOTIFY_SOCKET="$NOTIFY_SOCKET" "$@"',
                "--", "teleproxy", "-mode", "intercept"
            ],
            killer=kill_intercept,
            keep_session=True,  # Avoid trouble with interactive sudo
            notify=True,
        )
        runner.launch(
            "teleproxy bridge",
            [
                "teleproxy", "-mode", "bridge", "-context",
                runner.kubectl.context, "-namespace", runner.kubectl.namespace
            ],
            notify=True,
        )
        runner.show("Outbound is running. Press Ctrl-C/Ctrl-Break to quit.")
        user_process = Popen(["cat"], stdout=DEVNULL)
        runner.wait_for_exit(user_process)
Esempio n. 4
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)
Esempio n. 5
0
def command_main(args):
    """
    Top-level function for Telepresence when executing subcommands
    """

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

        args.operation = args.command
        args.method = "teleproxy"
        call_scout(runner, args)

    if args.command == "outbound":
        return outbound.command(runner)

    raise runner.fail("Not implemented!")
Esempio n. 6
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)
Esempio n. 7
0
def command(runner, args):
    with runner.cleanup_handling(), crash_reporting(runner):
        # Process arguments
        name = args.name or runner.session_id
        local_port = args.port
        deployment = args.deployment
        patterns = [
            dict(name=header, regex_match=pattern)
            for header, pattern in args.match
        ]

        # Inform the user
        runner.show("Setting up intercept session {}".format(name))
        runner.show("Intercepting requests to {}".format(deployment))
        runner.show("and redirecting them to localhost:{}".format(local_port))
        runner.show("when the following headers match:")
        for obj in patterns:
            runner.show("  {name}: {regex_match}".format(**obj))

        # Check the deployment exists and has the sidecar
        # FIXME: implement

        # Connect to the proxy
        runner.show("Connecting to the Telepresence Proxy")
        proxy_name = "telepresence-proxy"
        remote_info = get_remote_info(runner, proxy_name, "deployment")

        old_chatty, runner.chatty = runner.chatty, False
        _, ssh = connect(runner, remote_info, False, PortMapping())
        runner.chatty = old_chatty

        # Forward local port to the proxy's API server
        api_server_port = find_free_port()
        forward_args = [
            "-L127.0.0.1:{}:127.0.0.1:8081".format(api_server_port)
        ]
        runner.launch("SSH port forward (api server)",
                      ssh.bg_command(forward_args))
        url = "http://127.0.0.1:{}/intercept/{}".format(
            api_server_port, deployment)
        runner.write("Proxy URL is {}".format(url))

        # Start the intercept, get the remote port on the proxy
        data = json.dumps(dict(name=name, patterns=patterns))
        response = proxy_request(runner, url, data, "POST")
        try:
            remote_port = int(response)
        except ValueError:
            raise runner.fail("Unexpected response from the proxy")

        # Forward remote proxy port to the local port. This is how the
        # intercepted requests will get from the proxy to the user's code.
        forward_args = ["-R{}:127.0.0.1:{}".format(remote_port, local_port)]
        runner.launch("SSH port forward (proxy to user code)",
                      ssh.bg_command(forward_args))

        runner.add_cleanup("Delete intercept", proxy_request, runner, url,
                           str(remote_port), "DELETE")

        runner.show("Intercept is running. Press Ctrl-C/Ctrl-Break to quit.")
        user_process = Popen(["cat"], stdout=DEVNULL)
        runner.wait_for_exit(user_process)