Skip to content

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

Merged
merged 14 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix requested changes
  • Loading branch information
kylecarbs committed Mar 29, 2022
commit 342b03d0f74e57e8510f990a3334731f328c542d
8 changes: 8 additions & 0 deletions cli/cliui/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cliui
import (
"context"
"fmt"
"sync"
"time"

"github.com/briandowns/spinner"
Expand All @@ -19,13 +20,15 @@ type AgentOptions struct {
WarnInterval time.Duration
}

// Agent displays a spinning indicator that waits for a workspace agent to connect.
func Agent(cmd *cobra.Command, opts AgentOptions) error {
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, certainly should.

if opts.FetchInterval == 0 {
opts.FetchInterval = 500 * time.Millisecond
}
if opts.WarnInterval == 0 {
opts.WarnInterval = 30 * time.Second
}
var resourceMutex sync.Mutex
resource, err := opts.Fetch(cmd.Context())
if err != nil {
return xerrors.Errorf("fetch: %w", err)
Expand All @@ -52,6 +55,8 @@ func Agent(cmd *cobra.Command, opts AgentOptions) error {
return
case <-timer.C:
}
resourceMutex.Lock()
defer resourceMutex.Unlock()
message := "Don't panic, your workspace is booting up!"
if resource.Agent.Status == codersdk.WorkspaceAgentDisconnected {
Copy link
Member

Choose a reason for hiding this comment

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

This will fail --race because there is another for loop in the main thread touching this.

We should use atomic or just a mutex to protect that

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I didn't even have a test for this, and now I shall add one.

message = "The workspace agent lost connection! Wait for it to reconnect or run: " + Styles.Code.Render("coder workspaces rebuild "+opts.WorkspaceName)
Expand All @@ -67,13 +72,16 @@ func Agent(cmd *cobra.Command, opts AgentOptions) error {
return cmd.Context().Err()
case <-ticker.C:
}
resourceMutex.Lock()
resource, err = opts.Fetch(cmd.Context())
if err != nil {
return xerrors.Errorf("fetch: %w", err)
}
if resource.Agent.Status != codersdk.WorkspaceAgentConnected {
resourceMutex.Unlock()
continue
}
resourceMutex.Unlock()
return nil
}
}
53 changes: 53 additions & 0 deletions cli/cliui/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cliui_test

import (
"context"
"testing"
"time"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"

"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
)

func TestAgent(t *testing.T) {
t.Parallel()
var disconnected atomic.Bool
ptty := ptytest.New(t)
cmd := &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
err := cliui.Agent(cmd, cliui.AgentOptions{
WorkspaceName: "example",
Fetch: func(ctx context.Context) (codersdk.WorkspaceResource, error) {
resource := codersdk.WorkspaceResource{
Agent: &codersdk.WorkspaceAgent{
Status: codersdk.WorkspaceAgentDisconnected,
},
}
if disconnected.Load() {
resource.Agent.Status = codersdk.WorkspaceAgentConnected
}
return resource, nil
},
FetchInterval: time.Millisecond,
WarnInterval: 10 * time.Millisecond,
})
return err
},
}
cmd.SetOutput(ptty.Output())
cmd.SetIn(ptty.Input())
done := make(chan struct{})
go func() {
defer close(done)
err := cmd.Execute()
require.NoError(t, err)
}()
ptty.ExpectMatch("lost connection")
disconnected.Store(true)
<-done
}
15 changes: 8 additions & 7 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ import (
"path/filepath"
"time"

"github.com/briandowns/spinner"
"github.com/coreos/go-systemd/daemon"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"github.com/briandowns/spinner"
"github.com/coder/coder/cli/cliflag"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/cli/config"
Expand All @@ -31,12 +38,6 @@ import (
"github.com/coder/coder/provisionerd"
"github.com/coder/coder/provisionersdk"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coreos/go-systemd/daemon"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func start() *cobra.Command {
Expand Down
3 changes: 2 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/go-chi/chi/v5"
"google.golang.org/api/idtoken"

chitrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go-chi/chi.v5"

"cdr.dev/slog"
"github.com/coder/coder/coderd/awsidentity"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
"github.com/coder/coder/site"
chitrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go-chi/chi.v5"
)

// Options are requires parameters for Coder to start.
Expand Down
1 change: 0 additions & 1 deletion coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ func AwaitWorkspaceAgents(t *testing.T, client *codersdk.Client, build uuid.UUID
if resource.Agent == nil {
continue
}
// fmt.Printf("resources: %+v\n", resource.Agent)
if resource.Agent.FirstConnectedAt == nil {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions examples/gcp-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ terraform {
}

variable "service_account" {
description = <<EOT
description = <<EOF
Coder requires a Google Cloud Service Account to provision workspaces.

1. Create a service account:
Expand All @@ -19,7 +19,7 @@ Coder requires a Google Cloud Service Account to provision workspaces.
3. Click on the created key, and navigate to the "Keys" tab.
4. Click "Add key", then "Create new key".
5. Generate a JSON private key, and paste the contents in \'\' quotes below.
EOT
EOF
sensitive = true
}

Expand Down
4 changes: 2 additions & 2 deletions examples/gcp-windows/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ terraform {
}

variable "service_account" {
description = <<EOT
description = <<EOF
Coder requires a Google Cloud Service Account to provision workspaces.

1. Create a service account:
Expand All @@ -19,7 +19,7 @@ Coder requires a Google Cloud Service Account to provision workspaces.
3. Click on the created key, and navigate to the "Keys" tab.
4. Click "Add key", then "Create new key".
5. Generate a JSON private key, and paste the contents in \'\' quotes below.
EOT
EOF
sensitive = true
}

Expand Down