Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Add provisionerdaemon to coderd #141

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
Feb 3, 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
Next Next commit
The full end-to-end operation works
  • Loading branch information
kylecarbs committed Feb 2, 2022
commit e87f31db81b1f72d1550d561b68b1d1d5883be9b
1 change: 0 additions & 1 deletion .github/workflows/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ jobs:
- run: go install gotest.tools/gotestsum@latest

- uses: hashicorp/setup-terraform@v1
if: runner.os == 'Linux'
with:
terraform_version: 1.1.2
terraform_wrapper: false
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"coderdtest",
"codersdk",
"drpc",
"drpcconn",
"drpcmux",
"drpcserver",
"goleak",
Expand All @@ -44,6 +45,8 @@
"retrier",
"sdkproto",
"stretchr",
"tfexec",
"tfstate",
"unconvert",
"xerrors",
"yamux"
Expand Down
2 changes: 2 additions & 0 deletions coderd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"github.com/coder/coder/coderd"
"github.com/coder/coder/database"
"github.com/coder/coder/database/databasefake"
)

Expand All @@ -24,6 +25,7 @@ func Root() *cobra.Command {
handler := coderd.New(&coderd.Options{
Logger: slog.Make(sloghuman.Sink(os.Stderr)),
Database: databasefake.New(),
Pubsub: database.NewPubsubInMemory(),
})

listener, err := net.Listen("tcp", address)
Expand Down
11 changes: 9 additions & 2 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ func (s *Server) AddProvisionerd(t *testing.T) io.Closer {
ServeOptions: &provisionersdk.ServeOptions{
Listener: tfServer,
},
Logger: slogtest.Make(t, nil).Named("terraform-provisioner").Leveled(slog.LevelDebug),
})
require.NoError(t, err)
}()

closer := provisionerd.New(s.Client.ProvisionerDaemonClient, &provisionerd.Options{
Logger: slogtest.Make(t, nil).Named("provisionerd").Leveled(slog.LevelInfo),
PollInterval: 50 * time.Millisecond,
Logger: slogtest.Make(t, nil).Named("provisionerd").Leveled(slog.LevelInfo),
PollInterval: 50 * time.Millisecond,
UpdateInterval: 50 * time.Millisecond,
Provisioners: provisionerd.Provisioners{
string(database.ProvisionerTypeTerraform): proto.NewDRPCProvisionerClient(provisionersdk.Conn(tfClient)),
},
Expand All @@ -101,6 +103,7 @@ func (s *Server) AddProvisionerd(t *testing.T) io.Closer {
func New(t *testing.T) Server {
// This can be hotswapped for a live database instance.
db := databasefake.New()
pubsub := database.NewPubsubInMemory()
if os.Getenv("DB") != "" {
connectionURL, close, err := postgres.Open()
require.NoError(t, err)
Expand All @@ -113,11 +116,15 @@ func New(t *testing.T) Server {
err = database.Migrate(sqlDB)
require.NoError(t, err)
db = database.New(sqlDB)

pubsub, err = database.NewPubsub(context.Background(), sqlDB, connectionURL)
require.NoError(t, err)
}

handler := coderd.New(&coderd.Options{
Logger: slogtest.Make(t, nil),
Database: db,
Pubsub: pubsub,
})
srv := httptest.NewServer(handler)
serverURL, err := url.Parse(srv.URL)
Expand Down
17 changes: 10 additions & 7 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (api *api) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
daemons, err := api.Database.GetProvisionerDaemons(r.Context())
if errors.Is(err, sql.ErrNoRows) {
err = nil
daemons = []database.ProvisionerDaemon{}
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Expand Down Expand Up @@ -76,9 +77,10 @@ func (api *api) provisionerDaemonsServe(rw http.ResponseWriter, r *http.Request)
}
mux := drpcmux.New()
err = proto.DRPCRegisterProvisionerDaemon(mux, &provisionerdServer{
ID: daemon.ID,
Database: api.Database,
Pubsub: api.Pubsub,
ID: daemon.ID,
Database: api.Database,
Pubsub: api.Pubsub,
Provisioners: daemon.Provisioners,
})
if err != nil {
_ = conn.Close(websocket.StatusInternalError, fmt.Sprintf("drpc register provisioner daemon: %s", err))
Expand All @@ -103,9 +105,10 @@ type projectImportJob struct {

// Implementation of the provisioner daemon protobuf server.
type provisionerdServer struct {
ID uuid.UUID
Database database.Store
Pubsub database.Pubsub
ID uuid.UUID
Provisioners []database.ProvisionerType
Database database.Store
Pubsub database.Pubsub
}

// AcquireJob queries the database to lock a job.
Expand All @@ -120,7 +123,7 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
UUID: server.ID,
Valid: true,
},
Types: []database.ProvisionerType{database.ProvisionerTypeTerraform},
Types: server.Provisioners,
})
if errors.Is(err, sql.ErrNoRows) {
// The provisioner daemon assumes no jobs are available if
Expand Down
8 changes: 7 additions & 1 deletion coderd/provisioners.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
ProvisionerJobStatusPending ProvisionerJobStatus = "pending"
ProvisionerJobStatusRunning ProvisionerJobStatus = "running"
ProvisionerJobStatusSucceeded ProvisionerJobStatus = "succeeded"
ProvisionerJobStatusCancelled ProvisionerJobStatus = "canceled"
ProvisionerJobStatusFailed ProvisionerJobStatus = "failed"
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be a state for cancelled?

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose I'm not sure. I felt like failed would represent cancelled, but I suppose that's a different state. I'm going to add it in!

)

Expand All @@ -41,6 +42,7 @@ func convertProvisionerJob(provisionerJob database.ProvisionerJob) ProvisionerJo
Error: provisionerJob.Error.String,
Provisioner: provisionerJob.Provisioner,
}
// Applying values optional to the struct.
if provisionerJob.StartedAt.Valid {
job.StartedAt = &provisionerJob.StartedAt.Time
}
Expand All @@ -56,7 +58,7 @@ func convertProvisionerJob(provisionerJob database.ProvisionerJob) ProvisionerJo

switch {
case provisionerJob.CancelledAt.Valid:
job.Status = ProvisionerJobStatusFailed
job.Status = ProvisionerJobStatusCancelled
case !provisionerJob.StartedAt.Valid:
job.Status = ProvisionerJobStatusPending
case provisionerJob.CompletedAt.Valid:
Expand All @@ -68,5 +70,9 @@ func convertProvisionerJob(provisionerJob database.ProvisionerJob) ProvisionerJo
job.Status = ProvisionerJobStatusRunning
}

if job.Error != "" {
job.Status = ProvisionerJobStatusFailed
}

return job
}
4 changes: 3 additions & 1 deletion coderd/workspacehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type WorkspaceHistory struct {
ProjectHistoryID uuid.UUID `json:"project_history_id"`
BeforeID uuid.UUID `json:"before_id"`
AfterID uuid.UUID `json:"after_id"`
Name string `json:"name"`
Transition database.WorkspaceTransition `json:"transition"`
Initiator string `json:"initiator"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Not an issue with this PR, but I worry that this schema will make it difficult for a user to change names. That username-change will have to percolate through all these places we're storing their name.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a user ID. It's not very clear, so I should add a comment.

We can't use the uuid.UUID type, because we have to support v1 users.

Provision ProvisionerJob `json:"provision"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, looks like this type w/ Provision now has most of the stuff I need to power the workspaces timeline in my mock!

image

Expand Down Expand Up @@ -144,7 +145,7 @@ func (api *api) postWorkspaceHistoryByUser(rw http.ResponseWriter, r *http.Reque
}

workspaceHistory, err = db.InsertWorkspaceHistory(r.Context(), database.InsertWorkspaceHistoryParams{
ID: uuid.New(),
ID: workspaceHistoryID,
CreatedAt: database.Now(),
UpdatedAt: database.Now(),
WorkspaceID: workspace.ID,
Expand Down Expand Up @@ -242,6 +243,7 @@ func convertWorkspaceHistory(workspaceHistory database.WorkspaceHistory, provisi
ProjectHistoryID: workspaceHistory.ProjectHistoryID,
BeforeID: workspaceHistory.BeforeID.UUID,
AfterID: workspaceHistory.AfterID.UUID,
Name: workspaceHistory.Name,
Transition: workspaceHistory.Transition,
Initiator: workspaceHistory.Initiator,
Provision: convertProvisionerJob(provisionerJob),
Expand Down
42 changes: 32 additions & 10 deletions coderd/workspacehistory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ func TestWorkspaceHistory(t *testing.T) {
setupProjectHistory := func(t *testing.T, client *codersdk.Client, user coderd.CreateInitialUserRequest, project coderd.Project, files map[string]string) coderd.ProjectHistory {
var buffer bytes.Buffer
writer := tar.NewWriter(&buffer)
err := writer.WriteHeader(&tar.Header{
Name: "file",
Size: 1 << 10,
})
require.NoError(t, err)
_, err = writer.Write(make([]byte, 1<<10))
for path, content := range files {
err := writer.WriteHeader(&tar.Header{
Name: path,
Size: int64(len(content)),
})
require.NoError(t, err)
_, err = writer.Write([]byte(content))
require.NoError(t, err)
}
err := writer.Flush()
require.NoError(t, err)

projectHistory, err := client.CreateProjectHistory(context.Background(), user.Organization, project.Name, coderd.CreateProjectHistoryRequest{
StorageMethod: database.ProjectStorageMethodInlineArchive,
StorageSource: buffer.Bytes(),
Expand All @@ -65,7 +70,9 @@ func TestWorkspaceHistory(t *testing.T) {
history, err := server.Client.ListWorkspaceHistory(context.Background(), "", workspace.Name)
require.NoError(t, err)
require.Len(t, history, 0)
projectVersion := setupProjectHistory(t, server.Client, user, project, map[string]string{})
projectVersion := setupProjectHistory(t, server.Client, user, project, map[string]string{
"example": "file",
})
_, err = server.Client.CreateWorkspaceHistory(context.Background(), "", workspace.Name, coderd.CreateWorkspaceHistoryRequest{
ProjectHistoryID: projectVersion.ID,
Transition: database.WorkspaceTransitionCreate,
Expand All @@ -84,7 +91,9 @@ func TestWorkspaceHistory(t *testing.T) {
project, workspace := setupProjectAndWorkspace(t, server.Client, user)
_, err := server.Client.WorkspaceHistory(context.Background(), "", workspace.Name, "")
require.Error(t, err)
projectHistory := setupProjectHistory(t, server.Client, user, project, map[string]string{})
projectHistory := setupProjectHistory(t, server.Client, user, project, map[string]string{
"some": "file",
})
_, err = server.Client.CreateWorkspaceHistory(context.Background(), "", workspace.Name, coderd.CreateWorkspaceHistoryRequest{
ProjectHistoryID: projectHistory.ID,
Transition: database.WorkspaceTransitionCreate,
Expand All @@ -100,12 +109,23 @@ func TestWorkspaceHistory(t *testing.T) {
_ = server.AddProvisionerd(t)
user := server.RandomInitialUser(t)
project, workspace := setupProjectAndWorkspace(t, server.Client, user)
projectHistory := setupProjectHistory(t, server.Client, user, project, map[string]string{})
projectHistory := setupProjectHistory(t, server.Client, user, project, map[string]string{
"main.tf": `resource "null_resource" "example" {}`,
})
_, err := server.Client.CreateWorkspaceHistory(context.Background(), "", workspace.Name, coderd.CreateWorkspaceHistoryRequest{
ProjectHistoryID: projectHistory.ID,
Transition: database.WorkspaceTransitionCreate,
})
require.NoError(t, err)

var workspaceHistory coderd.WorkspaceHistory
require.Eventually(t, func() bool {
workspaceHistory, err = server.Client.WorkspaceHistory(context.Background(), "", workspace.Name, "")
require.NoError(t, err)
return workspaceHistory.Provision.Status.Completed()
}, 5*time.Second, 50*time.Millisecond)
require.Equal(t, "", workspaceHistory.Provision.Error)
require.Equal(t, coderd.ProvisionerJobStatusSucceeded, workspaceHistory.Provision.Status)
})

t.Run("CreateHistoryAlreadyInProgress", func(t *testing.T) {
Expand All @@ -114,7 +134,9 @@ func TestWorkspaceHistory(t *testing.T) {
_ = server.AddProvisionerd(t)
user := server.RandomInitialUser(t)
project, workspace := setupProjectAndWorkspace(t, server.Client, user)
projectHistory := setupProjectHistory(t, server.Client, user, project, map[string]string{})
projectHistory := setupProjectHistory(t, server.Client, user, project, map[string]string{
"some": "content",
})

_, err := server.Client.CreateWorkspaceHistory(context.Background(), "", workspace.Name, coderd.CreateWorkspaceHistoryRequest{
ProjectHistoryID: projectHistory.ID,
Expand Down
2 changes: 1 addition & 1 deletion codersdk/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestProjects(t *testing.T) {
require.NoError(t, err)
})

t.Run("UnauthenticatedHistorys", func(t *testing.T) {
t.Run("UnauthenticatedHistory", func(t *testing.T) {
t.Parallel()
server := coderdtest.New(t)
_, err := server.Client.ListProjectHistory(context.Background(), "org", "project")
Expand Down
Loading