Skip to content

Commit

Permalink
Adds ssh command, with option to pass a command to run on the worksta…
Browse files Browse the repository at this point in the history
…tion (#7)

* Adds ssh command, with option to pass a command to run on the workstation

* linting

* linting
  • Loading branch information
nkpart authored Feb 14, 2025
1 parent ed12ce6 commit d8e683a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/workstation/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from .crud import create, delete, list, list_configs, logs, start, stop, sync
from .crud import create, delete, list, list_configs, logs, ssh, start, stop, sync

try:
from block.clitools.clock import group as base_group
Expand Down Expand Up @@ -33,3 +33,4 @@ def cli(context: click.Context):
cli.add_command(delete)
cli.add_command(sync)
cli.add_command(logs)
cli.add_command(ssh)
51 changes: 51 additions & 0 deletions src/workstation/cli/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,54 @@ def logs(name: str, project: str, **kwargs):
return
console.print(f"Logs for instance: {instance.get('instance_name')} opening")
webbrowser.open(instance.get("logs_url"))


@command()
@click.option(
"-n",
"--name",
help="Name of the workstation to SSH into.",
type=str,
metavar="<str>",
required=True,
)
@click.option(
"-c",
"--command",
help="Command to run in the SSH session.",
type=str,
metavar="<str>",
)
@click.pass_context
def ssh(context: click.Context, name: str, command: str = None, **kwargs):
"""SSH into a workstation. Optionally run a command in the session."""
# Make sure the user is authenticated
check_gcloud_auth()

workstation_details = config_manager.read_configuration(name)
user = getpass.getuser()

# Construct and execute the gcloud ssh command
import subprocess

ssh_cmd = [
"gcloud",
"workstations",
"ssh",
f"--project={workstation_details['project']}",
f"--cluster={workstation_details['cluster']}",
f"--config={workstation_details['config']}",
f"--region={workstation_details['location']}",
f"--user={user}",
name,
]

# Add command if specified
if command:
# Always use -t for all commands to ensure proper terminal handling
ssh_cmd.extend(["--", "-t", command])
console.print(f"Running command '{command}' on workstation {name}...")
else:
console.print(f"Connecting to workstation {name}...")

subprocess.run(ssh_cmd)

0 comments on commit d8e683a

Please sign in to comment.