Skip to content

local cli: support additional ssh arguments #19046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions components/local-app/cmd/workspace-ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ var dryRun bool
var workspaceSSHCmd = &cobra.Command{
Use: "ssh <workspace-id>",
Short: "Connects to a workspace via SSH",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
Example: ` # connect to workspace with current terminal session
$ gitpod workspace ssh <workspace-id>

# Execute with ssh command
$ gitpod workspace ssh <workspace-id> -- ls -la
$ gitpod ws ssh <workspace-id> -- -t watch date

# Get all SSH features with --dry-run
$ $(gitpod workspace ssh <workspace-id> --dry-run) -- ls -la
Copy link
Member

@akosyakov akosyakov Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i still think we should remove it, wait for bugs, and fix them properly, not spread hacks.

$ $(gitpod workspace ssh <workspace-id> --dry-run) -t watch date`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

Expand All @@ -26,7 +36,14 @@ var workspaceSSHCmd = &cobra.Command{
return err
}

return helper.SSHConnectToWorkspace(cmd.Context(), gitpod, workspaceID, dryRun)
dashDashIndex := cmd.ArgsLenAtDash()

sshArgs := []string{}
if dashDashIndex != -1 {
sshArgs = args[dashDashIndex:]
}

return helper.SSHConnectToWorkspace(cmd.Context(), gitpod, workspaceID, dryRun, sshArgs...)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/local-app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect
Expand Down
14 changes: 9 additions & 5 deletions components/local-app/pkg/helper/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func OpenWorkspaceInPreferredEditor(ctx context.Context, clnt *client.Gitpod, wo
}

// SSHConnectToWorkspace connects to the workspace via SSH
func SSHConnectToWorkspace(ctx context.Context, clnt *client.Gitpod, workspaceID string, runDry bool) error {
func SSHConnectToWorkspace(ctx context.Context, clnt *client.Gitpod, workspaceID string, runDry bool, sshArgs ...string) error {
workspace, err := clnt.Workspaces.GetWorkspace(ctx, connect.NewRequest(&v1.GetWorkspaceRequest{WorkspaceId: workspaceID}))
if err != nil {
return err
Expand All @@ -113,13 +113,17 @@ func SSHConnectToWorkspace(ctx context.Context, clnt *client.Gitpod, workspaceID
ownerToken := token.Msg.Token

host := WorkspaceSSHHost(wsInfo)

command := exec.Command("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
if len(sshArgs) > 0 {
slog.Debug("With additional SSH args and command", "with", sshArgs)
command.Args = append(command.Args, sshArgs...)
}
if runDry {
fmt.Println("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
fmt.Println(strings.Join(command.Args, " "))
return nil
}

slog.Debug("Connecting to" + wsInfo.Description)
command := exec.Command("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
slog.Debug("Connecting to", "context", wsInfo.Description)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
Expand Down