- Notifications
You must be signed in to change notification settings - Fork1k
feat: addcoder ping
#6161
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
feat: addcoder ping
#6161
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletionscli/ping.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package cli | ||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/sloghuman" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
func ping() *cobra.Command { | ||
var ( | ||
pingNum int | ||
pingTimeout time.Duration | ||
pingWait time.Duration | ||
verbose bool | ||
) | ||
cmd := &cobra.Command{ | ||
Annotations: workspaceCommand, | ||
Use: "ping <workspace>", | ||
Short: "Ping a workspace", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx, cancel := context.WithCancel(cmd.Context()) | ||
defer cancel() | ||
client, err := CreateClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
workspaceName := args[0] | ||
_, workspaceAgent, err := getWorkspaceAndAgent(ctx, cmd, client, codersdk.Me, workspaceName, false) | ||
if err != nil { | ||
return err | ||
} | ||
var logger slog.Logger | ||
if verbose { | ||
logger = slog.Make(sloghuman.Sink(cmd.OutOrStdout())).Leveled(slog.LevelDebug) | ||
} | ||
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, &codersdk.DialWorkspaceAgentOptions{Logger: logger}) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
derpMap := conn.DERPMap() | ||
_ = derpMap | ||
n := 0 | ||
didP2p := false | ||
start := time.Now() | ||
for { | ||
if n > 0 { | ||
time.Sleep(time.Second) | ||
} | ||
n++ | ||
ctx, cancel := context.WithTimeout(ctx, pingTimeout) | ||
dur, p2p, pong, err := conn.Ping(ctx) | ||
cancel() | ||
if err != nil { | ||
if xerrors.Is(err, context.DeadlineExceeded) { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "ping to %q timed out \n", workspaceName) | ||
if n == pingNum { | ||
return nil | ||
} | ||
continue | ||
} | ||
if xerrors.Is(err, context.Canceled) { | ||
return nil | ||
} | ||
if err.Error() == "no matching peer" { | ||
continue | ||
} | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "ping to %q failed %s\n", workspaceName, err.Error()) | ||
if n == pingNum { | ||
return nil | ||
} | ||
continue | ||
} | ||
dur = dur.Round(time.Millisecond) | ||
var via string | ||
if p2p { | ||
if !didP2p { | ||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "p2p connection established in", | ||
cliui.Styles.DateTimeStamp.Render(time.Since(start).Round(time.Millisecond).String()), | ||
) | ||
} | ||
didP2p = true | ||
via = fmt.Sprintf("%s via %s", | ||
cliui.Styles.Fuchsia.Render("p2p"), | ||
cliui.Styles.Code.Render(pong.Endpoint), | ||
) | ||
} else { | ||
derpName := "unknown" | ||
derpRegion, ok := derpMap.Regions[pong.DERPRegionID] | ||
if ok { | ||
derpName = derpRegion.RegionName | ||
} | ||
via = fmt.Sprintf("%s via %s", | ||
cliui.Styles.Fuchsia.Render("proxied"), | ||
cliui.Styles.Code.Render(fmt.Sprintf("DERP(%s)", derpName)), | ||
) | ||
} | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "pong from %s %s in %s\n", | ||
cliui.Styles.Keyword.Render(workspaceName), | ||
via, | ||
cliui.Styles.DateTimeStamp.Render(dur.String()), | ||
) | ||
if n == pingNum { | ||
return nil | ||
} | ||
} | ||
}, | ||
} | ||
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enables verbose logging.") | ||
cmd.Flags().DurationVarP(&pingWait, "wait", "", time.Second, "Specifies how long to wait between pings.") | ||
cmd.Flags().DurationVarP(&pingTimeout, "timeout", "t", 5*time.Second, "Specifies how long to wait for a ping to complete.") | ||
cmd.Flags().IntVarP(&pingNum, "num", "n", 10, "Specifies the number of pings to perform.") | ||
return cmd | ||
} |
54 changes: 54 additions & 0 deletionscli/ping_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cli_test | ||
import ( | ||
"context" | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"cdr.dev/slog/sloggers/slogtest" | ||
"github.com/coder/coder/agent" | ||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/codersdk/agentsdk" | ||
"github.com/coder/coder/pty/ptytest" | ||
"github.com/coder/coder/testutil" | ||
) | ||
func TestPing(t *testing.T) { | ||
t.Parallel() | ||
t.Run("OK", func(t *testing.T) { | ||
t.Parallel() | ||
client, workspace, agentToken := setupWorkspaceForAgent(t, nil) | ||
cmd, root := clitest.New(t, "ping", workspace.Name) | ||
clitest.SetupConfig(t, client, root) | ||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetErr(pty.Output()) | ||
cmd.SetOut(pty.Output()) | ||
agentClient := agentsdk.New(client.URL) | ||
agentClient.SetSessionToken(agentToken) | ||
agentCloser := agent.New(agent.Options{ | ||
Client: agentClient, | ||
Logger: slogtest.Make(t, nil).Named("agent"), | ||
}) | ||
defer func() { | ||
_ = agentCloser.Close() | ||
}() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
cmdDone := tGo(t, func() { | ||
err := cmd.ExecuteContext(ctx) | ||
assert.NoError(t, err) | ||
}) | ||
pty.ExpectMatch("pong from " + workspace.Name) | ||
cancel() | ||
<-cmdDone | ||
}) | ||
} |
3 changes: 2 additions & 1 deletioncli/root.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioncli/speedtest.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscli/testdata/coder_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletionscli/testdata/coder_ping_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Ping a workspace | ||
Usage: | ||
coder ping <workspace> [flags] | ||
Flags: | ||
-h, --help help for ping | ||
-n, --num int Specifies the number of pings to perform. (default 10) | ||
-t, --timeout duration Specifies how long to wait for a ping to complete. (default 5s) | ||
-v, --verbose Enables verbose logging. | ||
--wait duration Specifies how long to wait between pings. (default 1s) | ||
Global Flags: | ||
--global-config coder Path to the global coder config directory. | ||
Consumes $CODER_CONFIG_DIR (default "~/.config/coderv2") | ||
--header stringArray HTTP headers added to all requests. Provide as "Key=Value". | ||
Consumes $CODER_HEADER | ||
--no-feature-warning Suppress warnings about unlicensed features. | ||
Consumes $CODER_NO_FEATURE_WARNING | ||
--no-version-warning Suppress warning when client and server versions do not match. | ||
Consumes $CODER_NO_VERSION_WARNING | ||
--token string Specify an authentication token. For security reasons setting | ||
CODER_SESSION_TOKEN is preferred. | ||
Consumes $CODER_SESSION_TOKEN | ||
--url string URL to a deployment. | ||
Consumes $CODER_URL |
2 changes: 1 addition & 1 deletioncli/vscodessh.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletioncodersdk/workspaceagentconn.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsdocs/cli/coder.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletionsdocs/cli/coder_ping.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
## coder ping | ||
Ping a workspace | ||
``` | ||
coder ping <workspace> [flags] | ||
``` | ||
### Options | ||
``` | ||
-h, --help help for ping | ||
-n, --num int Specifies the number of pings to perform. (default 10) | ||
-t, --timeout duration Specifies how long to wait for a ping to complete. (default 5s) | ||
-v, --verbose Enables verbose logging. | ||
--wait duration Specifies how long to wait between pings. (default 1s) | ||
``` | ||
### Options inherited from parent commands | ||
``` | ||
--global-config coder Path to the global coder config directory. | ||
Consumes $CODER_CONFIG_DIR (default "~/.config/coderv2") | ||
--header stringArray HTTP headers added to all requests. Provide as "Key=Value". | ||
Consumes $CODER_HEADER | ||
--no-feature-warning Suppress warnings about unlicensed features. | ||
Consumes $CODER_NO_FEATURE_WARNING | ||
--no-version-warning Suppress warning when client and server versions do not match. | ||
Consumes $CODER_NO_VERSION_WARNING | ||
--token string Specify an authentication token. For security reasons setting CODER_SESSION_TOKEN is preferred. | ||
Consumes $CODER_SESSION_TOKEN | ||
--url string URL to a deployment. | ||
Consumes $CODER_URL | ||
``` | ||
### SEE ALSO | ||
- [coder](coder.md) - |
4 changes: 4 additions & 0 deletionsdocs/manifest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionsenterprise/coderd/replicas_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionscaletest/agentconn/run.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.