From 23286ed2d6a0c2135054f6a4578f09e65de4e90f Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 13 Jan 2025 15:54:21 +0100 Subject: [PATCH] Added connect command Signed-off-by: Victor Moene Ticket: ENT-5726 --- cf_remote/commands.py | 10 ++++++++++ cf_remote/main.py | 7 +++++++ cf_remote/remote.py | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/cf_remote/commands.py b/cf_remote/commands.py index e859b7f..f9613cc 100644 --- a/cf_remote/commands.py +++ b/cf_remote/commands.py @@ -12,6 +12,7 @@ run_command, transfer_file, deploy_masterfiles, + open_shell, ) from cf_remote.packages import Releases from cf_remote.web import download_package @@ -931,3 +932,12 @@ def agent(hosts, bootstrap=None): print(output) return 0 + + +def connect_cmd(hosts, users=None): + + if len(hosts) > 1: + user_error("You can only connect to one host at a time") + + print("Opening a SSH command shell...") + return open_shell(hosts[0]) diff --git a/cf_remote/main.py b/cf_remote/main.py index 07e1879..550b258 100644 --- a/cf_remote/main.py +++ b/cf_remote/main.py @@ -274,6 +274,11 @@ def _get_arg_parser(): ) sp.add_argument("--bootstrap", "-B", help="Which hub to bootstrap to", type=str) + sp = subp.add_parser("connect", help="Opens interactive ssh shell") + sp.add_argument( + "-hosts", "-H", help="Host to open the shell on", type=str, required=True + ) + return ap @@ -388,6 +393,8 @@ def run_command_with_args(command, args): return commands.deploy(args.hub, args.masterfiles) elif command == "agent": return commands.agent(args.hosts, args.bootstrap) + elif command == "connect": + return commands.connect_cmd(args.hosts, None) else: user_error("Unknown command: '{}'".format(command)) diff --git a/cf_remote/remote.py b/cf_remote/remote.py index 07b3b3c..650709c 100644 --- a/cf_remote/remote.py +++ b/cf_remote/remote.py @@ -632,3 +632,29 @@ def deploy_masterfiles(host, tarball, *, connection=None): return 1 print("Policy set successfully deployed to '%s' 🚀" % host) return 0 + + +@auto_connect +def open_shell(host, connection=None): + print("Enter 'exit' to log out\n") + + cmd = "" + while cmd != "exit": + try: + cmd = input("\033[92m{}\033[00m :~ $ ".format(host)) + result = connection.run(cmd, hide=True) + + if cmd == "exit": + pass + elif result.retcode == 0: + print(result.stdout.strip().strip("\n")) + else: + print(result.stderr.strip("\n")) + except KeyboardInterrupt: + print() + continue + except EOFError: + print() + return 0 + + return 0