-
Notifications
You must be signed in to change notification settings - Fork 901
feat: Add UI for awaiting agent connections #578
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
Changes from 1 commit
7ed283c
048f29a
2152758
e774dd7
172d4fe
82f60db
6bff973
3654022
aef12e1
15cae15
fb9fbb6
c91b3cc
9ad326b
342b03d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cliui | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
type AgentOptions struct { | ||
WorkspaceName string | ||
Fetch func(context.Context) (codersdk.WorkspaceResource, error) | ||
FetchInterval time.Duration | ||
WarnInterval time.Duration | ||
} | ||
|
||
func Agent(cmd *cobra.Command, opts AgentOptions) error { | ||
if opts.FetchInterval == 0 { | ||
opts.FetchInterval = 500 * time.Millisecond | ||
} | ||
if opts.WarnInterval == 0 { | ||
opts.WarnInterval = 30 * time.Second | ||
} | ||
resource, err := opts.Fetch(cmd.Context()) | ||
if err != nil { | ||
return xerrors.Errorf("fetch: %w", err) | ||
} | ||
if resource.Agent.Status == codersdk.WorkspaceAgentConnected { | ||
return nil | ||
} | ||
if resource.Agent.Status == codersdk.WorkspaceAgentDisconnected { | ||
opts.WarnInterval = 0 | ||
} | ||
spin := spinner.New(spinner.CharSets[78], 100*time.Millisecond, spinner.WithColor("fgHiGreen")) | ||
spin.Writer = cmd.OutOrStdout() | ||
spin.Suffix = " Waiting for connection from " + Styles.Field.Render(resource.Type+"."+resource.Name) + "..." | ||
spin.Start() | ||
defer spin.Stop() | ||
|
||
ticker := time.NewTicker(opts.FetchInterval) | ||
defer ticker.Stop() | ||
timer := time.NewTimer(opts.WarnInterval) | ||
defer timer.Stop() | ||
for { | ||
select { | ||
case <-cmd.Context().Done(): | ||
return cmd.Context().Err() | ||
case <-timer.C: | ||
message := "Don't panic, your workspace is booting up!" | ||
if resource.Agent.Status == codersdk.WorkspaceAgentDisconnected { | ||
message = "The workspace agent lost connection! Wait for it to reconnect or run: " + Styles.Code.Render("coder workspaces rebuild "+opts.WorkspaceName) | ||
} | ||
// This saves the cursor position, then defers clearing from the cursor | ||
// position to the end of the screen. | ||
fmt.Fprintf(cmd.OutOrStdout(), "\033[s\r\033[2K%s\n\n", Styles.Paragraph.Render(Styles.Prompt.String()+message)) | ||
defer fmt.Fprintf(cmd.OutOrStdout(), "\033[u\033[J") | ||
case <-ticker.C: | ||
} | ||
resource, err = opts.Fetch(cmd.Context()) | ||
if err != nil { | ||
return xerrors.Errorf("fetch: %w", err) | ||
} | ||
if resource.Agent.Status != codersdk.WorkspaceAgentConnected { | ||
continue | ||
} | ||
return nil | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pion/webrtc/v3" | ||
"github.com/spf13/cobra" | ||
gossh "golang.org/x/crypto/ssh" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
@@ -21,6 +25,12 @@ func ssh() *cobra.Command { | |
if err != nil { | ||
return err | ||
} | ||
if workspace.LatestBuild.Job.CompletedAt == nil { | ||
err = cliui.WorkspaceBuild(cmd, client, workspace.LatestBuild.ID, workspace.CreatedAt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we log some message that their workspace was offline and is being restarted? I feel like this being a silent side effect could be confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I need to revamp the SSH command. |
||
if err != nil { | ||
return err | ||
} | ||
} | ||
if workspace.LatestBuild.Transition == database.WorkspaceTransitionDelete { | ||
return xerrors.New("workspace is deleting...") | ||
} | ||
|
@@ -57,11 +67,19 @@ func ssh() *cobra.Command { | |
} | ||
return xerrors.Errorf("no sshable agent with address %q: %+v", resourceAddress, resourceKeys) | ||
} | ||
if resource.Agent.LastConnectedAt == nil { | ||
return xerrors.Errorf("agent hasn't connected yet") | ||
err = cliui.Agent(cmd, cliui.AgentOptions{ | ||
WorkspaceName: workspace.Name, | ||
Fetch: func(ctx context.Context) (codersdk.WorkspaceResource, error) { | ||
return client.WorkspaceResource(ctx, resource.ID) | ||
}, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("await agent: %w", err) | ||
} | ||
|
||
conn, err := client.DialWorkspaceAgent(cmd.Context(), resource.ID, nil, nil) | ||
conn, err := client.DialWorkspaceAgent(cmd.Context(), resource.ID, []webrtc.ICEServer{{ | ||
URLs: []string{"stun:stun.l.google.com:19302"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For customers with more restrictive firewalls, can they use TURN? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't be hardcoded for long. Just a temporary hack! |
||
}}, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we comment what this function is doing? With lack of returns except
error
, it's a bit unclear.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, certainly should.