Skip to content

Commit b3520fd

Browse files
authored
[sshshim] Add and pass ctx parameter for sshshim execution (#1119)
## Summary Title says it. Now that `context.Context` is used, I think it's better to pass this parameter from the top instead of creating it when necessary. ## How was it tested? None.
1 parent ae6932f commit b3520fd

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

internal/boxcli/midcobra/midcobra.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010

1111
"github.com/spf13/cobra"
12+
1213
"go.jetpack.io/devbox/internal/boxcli/usererr"
1314
"go.jetpack.io/devbox/internal/debug"
1415
"go.jetpack.io/devbox/internal/ux"

internal/boxcli/root.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/spf13/cobra"
14+
1415
"go.jetpack.io/devbox/internal/boxcli/midcobra"
1516
"go.jetpack.io/devbox/internal/cloud/openssh/sshshim"
1617
"go.jetpack.io/devbox/internal/debug"
@@ -97,17 +98,18 @@ func Execute(ctx context.Context, args []string) int {
9798
}
9899

99100
func Main() {
101+
ctx := context.Background()
100102
if strings.HasSuffix(os.Args[0], "ssh") ||
101103
strings.HasSuffix(os.Args[0], "scp") {
102-
code := sshshim.Execute(os.Args)
103-
os.Exit(code)
104+
os.Exit(sshshim.Execute(ctx, os.Args))
104105
}
106+
105107
if len(os.Args) > 1 && os.Args[1] == "bug" {
106108
telemetry.ReportErrors()
107109
return
108110
}
109-
code := Execute(context.Background(), os.Args[1:])
110-
os.Exit(code)
111+
112+
os.Exit(Execute(ctx, os.Args[1:]))
111113
}
112114

113115
func listAllCommands(cmd *cobra.Command, indent string) {

internal/cloud/openssh/sshshim/command.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,37 @@
44
package sshshim
55

66
import (
7+
"context"
78
"fmt"
89
"os"
910

1011
"go.jetpack.io/devbox/internal/debug"
1112
"go.jetpack.io/devbox/internal/telemetry"
1213
)
1314

14-
func Execute(args []string) int {
15+
func Execute(ctx context.Context, args []string) int {
1516
defer debug.Recover()
1617
telemetry.Start(telemetry.AppSSHShim)
1718
defer telemetry.Stop()
1819

19-
if err := execute(args); err != nil {
20+
if err := execute(ctx, args); err != nil {
2021
telemetry.Error(err, telemetry.Metadata{})
2122
return 1
2223
}
2324
return 0
2425
}
2526

26-
func execute(args []string) error {
27+
func execute(ctx context.Context, args []string) error {
2728
EnableDebug() // Always enable for now.
2829
debug.Log("os.Args: %v", args)
2930

30-
if alive, err := EnsureLiveVMOrTerminateMutagenSessions(args[1:]); err != nil {
31+
alive, err := EnsureLiveVMOrTerminateMutagenSessions(ctx, args[1:])
32+
if err != nil {
3133
debug.Log("ensureLiveVMOrTerminateMutagenSessions error: %v", err)
3234
fmt.Fprintf(os.Stderr, "%v", err)
3335
return err
34-
} else if !alive {
36+
}
37+
if !alive {
3538
return nil
3639
}
3740

@@ -40,5 +43,6 @@ func execute(args []string) error {
4043
fmt.Fprintf(os.Stderr, "%v", err)
4144
return err
4245
}
46+
4347
return nil
4448
}

internal/cloud/openssh/sshshim/mutagen.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
"time"
1212

1313
"github.com/pkg/errors"
14+
1415
"go.jetpack.io/devbox/internal/cloud/mutagenbox"
1516
"go.jetpack.io/devbox/internal/debug"
1617
)
1718

1819
// EnsureLiveVMOrTerminateMutagenSessions returns true if a liveVM is found, OR sshArgs were connecting to a server that is not a devbox-VM.
1920
// EnsureLiveVMOrTerminateMutagenSessions returns false iff the sshArgs were connecting to a devbox VM AND a deadVM is found.
20-
func EnsureLiveVMOrTerminateMutagenSessions(sshArgs []string) (bool, error) {
21+
func EnsureLiveVMOrTerminateMutagenSessions(ctx context.Context, sshArgs []string) (bool, error) {
2122
vmAddr := vmAddressIfAny(sshArgs)
2223

2324
debug.Log("Found vmAddr: %s", vmAddr)
@@ -28,9 +29,11 @@ func EnsureLiveVMOrTerminateMutagenSessions(sshArgs []string) (bool, error) {
2829
return true, nil
2930
}
3031

31-
if isActive, err := checkActiveVMWithRetries(vmAddr); err != nil {
32+
isActive, err := checkActiveVMWithRetries(ctx, vmAddr)
33+
if err != nil {
3234
return false, errors.WithStack(err)
33-
} else if !isActive {
35+
}
36+
if !isActive {
3437
debug.Log("terminating mutagen session for vm: %s", vmAddr)
3538
// If no vm is active, then we should terminate the running mutagen sessions
3639
return false, terminateMutagenSessions(vmAddr)
@@ -57,25 +60,25 @@ func terminateMutagenSessions(vmAddr string) error {
5760
return mutagenbox.ForwardTerminateByHost(hostname)
5861
}
5962

60-
func checkActiveVMWithRetries(vmAddr string) (bool, error) {
61-
var err error
63+
func checkActiveVMWithRetries(ctx context.Context, vmAddr string) (bool, error) {
64+
var finalErr error
6265

6366
// Try 3 times:
6467
for num := 0; num < 3; num++ {
65-
var isActive bool
66-
isActive, err = checkActiveVM(vmAddr)
68+
isActive, err := checkActiveVM(ctx, vmAddr)
6769
if err == nil && isActive {
6870
// found an active VM
6971
return true, nil
7072
}
73+
finalErr = err
7174
time.Sleep(10 * time.Second)
7275
debug.Log("Try %d failed to find activeVM for %s", num, vmAddr)
7376
}
74-
return false, err
77+
return false, finalErr
7578
}
7679

77-
func checkActiveVM(vmAddr string) (bool, error) {
78-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
80+
func checkActiveVM(ctx context.Context, vmAddr string) (bool, error) {
81+
ctx, cancel := context.WithTimeout(ctx, time.Minute*2)
7982
defer cancel()
8083
cmd := exec.CommandContext(ctx, "ssh", vmAddr, "echo 'alive'")
8184

0 commit comments

Comments
 (0)