mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
Last little bits to make everything work
Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
@ -5,6 +5,13 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
mrand "math/rand"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
|
|
||||||
"github.com/coder/coder/v2/coderd/audit"
|
"github.com/coder/coder/v2/coderd/audit"
|
||||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||||
"github.com/coder/coder/v2/coderd/database/provisionerjobs"
|
"github.com/coder/coder/v2/coderd/database/provisionerjobs"
|
||||||
@ -12,19 +19,16 @@ import (
|
|||||||
"github.com/coder/coder/v2/coderd/rbac"
|
"github.com/coder/coder/v2/coderd/rbac"
|
||||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||||
"github.com/coder/coder/v2/coderd/wsbuilder"
|
"github.com/coder/coder/v2/coderd/wsbuilder"
|
||||||
"golang.org/x/exp/slices"
|
"github.com/coder/coder/v2/codersdk"
|
||||||
"math"
|
|
||||||
mrand "math/rand"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"cdr.dev/slog"
|
"cdr.dev/slog"
|
||||||
|
|
||||||
"github.com/coder/coder/v2/coderd/database"
|
|
||||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
|
"github.com/coder/coder/v2/coderd/database"
|
||||||
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
@ -395,13 +399,26 @@ func (c Controller) deletePrebuild(ctx context.Context, db database.Store, prebu
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Controller) provision(ctx context.Context, db database.Store, prebuildID uuid.UUID, template database.Template, transition database.WorkspaceTransition, workspace database.Workspace) error {
|
func (c Controller) provision(ctx context.Context, db database.Store, prebuildID uuid.UUID, template database.Template, transition database.WorkspaceTransition, workspace database.Workspace) error {
|
||||||
|
tvp, err := db.GetPresetParametersByTemplateVersionID(ctx, template.ActiveVersionID)
|
||||||
|
if err != nil {
|
||||||
|
return xerrors.Errorf("fetch preset details: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var params []codersdk.WorkspaceBuildParameter
|
||||||
|
for _, param := range tvp {
|
||||||
|
params = append(params, codersdk.WorkspaceBuildParameter{
|
||||||
|
Name: param.Name,
|
||||||
|
Value: param.Value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
builder := wsbuilder.New(workspace, transition).
|
builder := wsbuilder.New(workspace, transition).
|
||||||
Reason(database.BuildReasonInitiator).
|
Reason(database.BuildReasonInitiator).
|
||||||
Initiator(PrebuildOwnerUUID).
|
Initiator(PrebuildOwnerUUID).
|
||||||
ActiveVersion().
|
ActiveVersion().
|
||||||
VersionID(template.ActiveVersionID).
|
VersionID(template.ActiveVersionID).
|
||||||
MarkPrebuild()
|
MarkPrebuild().
|
||||||
// RichParameterValues(req.RichParameterValues) // TODO: fetch preset's params
|
RichParameterValues(params)
|
||||||
|
|
||||||
_, provisionerJob, _, err := builder.Build(
|
_, provisionerJob, _, err := builder.Build(
|
||||||
ctx,
|
ctx,
|
||||||
|
@ -1713,13 +1713,13 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
|
|||||||
|
|
||||||
// If this job was initiated by the prebuilds user and the job is not a prebuild, then it MUST be the claim run.
|
// If this job was initiated by the prebuilds user and the job is not a prebuild, then it MUST be the claim run.
|
||||||
// TODO: maybe add some specific metadata to indicate this rather than imputing it.
|
// TODO: maybe add some specific metadata to indicate this rather than imputing it.
|
||||||
if input.PrebuildClaimByUser != uuid.Nil {
|
if input.PrebuildClaimedByUser != uuid.Nil {
|
||||||
channel := agentsdk.PrebuildClaimedChannel(workspace.ID)
|
channel := agentsdk.PrebuildClaimedChannel(workspace.ID)
|
||||||
s.Logger.Info(ctx, "workspace prebuild successfully claimed by user",
|
s.Logger.Info(ctx, "workspace prebuild successfully claimed by user",
|
||||||
slog.F("user", input.PrebuildClaimByUser.String()),
|
slog.F("user", input.PrebuildClaimedByUser.String()),
|
||||||
slog.F("workspace_id", workspace.ID),
|
slog.F("workspace_id", workspace.ID),
|
||||||
slog.F("channel", channel))
|
slog.F("channel", channel))
|
||||||
if err := s.Pubsub.Publish(channel, []byte(input.PrebuildClaimByUser.String())); err != nil {
|
if err := s.Pubsub.Publish(channel, []byte(input.PrebuildClaimedByUser.String())); err != nil {
|
||||||
s.Logger.Error(ctx, "failed to publish message to workspace agent to pull new manifest", slog.Error(err))
|
s.Logger.Error(ctx, "failed to publish message to workspace agent to pull new manifest", slog.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1881,22 +1881,23 @@ func InsertWorkspacePresetAndParameters(ctx context.Context, db database.Store,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("insert preset parameters: %w", err)
|
return xerrors.Errorf("insert preset parameters: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if protoPreset.Prebuild != nil {
|
||||||
|
_, err := db.InsertPresetPrebuild(ctx, database.InsertPresetPrebuildParams{
|
||||||
|
ID: uuid.New(),
|
||||||
|
PresetID: dbPreset.ID,
|
||||||
|
DesiredInstances: protoPreset.Prebuild.Instances,
|
||||||
|
InvalidateAfterSecs: 0, // TODO: implement cache invalidation
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return xerrors.Errorf("insert preset prebuild: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}, nil)
|
}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("insert preset and parameters: %w", err)
|
return xerrors.Errorf("insert preset and parameters: %w", err)
|
||||||
}
|
}
|
||||||
if protoPreset.Prebuild != nil {
|
|
||||||
_, err := db.InsertPresetPrebuild(ctx, database.InsertPresetPrebuildParams{
|
|
||||||
ID: uuid.New(),
|
|
||||||
PresetID: dbPreset.ID,
|
|
||||||
DesiredInstances: protoPreset.Prebuild.Instances,
|
|
||||||
InvalidateAfterSecs: 0, // TODO: implement cache invalidation
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return xerrors.Errorf("insert preset prebuild: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2383,10 +2384,10 @@ type TemplateVersionImportJob struct {
|
|||||||
|
|
||||||
// WorkspaceProvisionJob is the payload for the "workspace_provision" job type.
|
// WorkspaceProvisionJob is the payload for the "workspace_provision" job type.
|
||||||
type WorkspaceProvisionJob struct {
|
type WorkspaceProvisionJob struct {
|
||||||
WorkspaceBuildID uuid.UUID `json:"workspace_build_id"`
|
WorkspaceBuildID uuid.UUID `json:"workspace_build_id"`
|
||||||
DryRun bool `json:"dry_run"`
|
DryRun bool `json:"dry_run"`
|
||||||
IsPrebuild bool `json:"is_prebuild,omitempty"`
|
IsPrebuild bool `json:"is_prebuild,omitempty"`
|
||||||
PrebuildClaimByUser uuid.UUID `json:"prebuild_claim_by,omitempty"`
|
PrebuildClaimedByUser uuid.UUID `json:"prebuild_claimed_by,omitempty"`
|
||||||
// RunningWorkspaceAgentID is *only* used for prebuilds. We pass it down when we want to rebuild a prebuilt workspace
|
// RunningWorkspaceAgentID is *only* used for prebuilds. We pass it down when we want to rebuild a prebuilt workspace
|
||||||
// but not generate a new agent token. The provisionerdserver will retrieve this token and push it down to
|
// but not generate a new agent token. The provisionerdserver will retrieve this token and push it down to
|
||||||
// the provisioner (and ultimately to the `coder_agent` resource in the Terraform provider) where it will be
|
// the provisioner (and ultimately to the `coder_agent` resource in the Terraform provider) where it will be
|
||||||
|
@ -6,18 +6,20 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/coder/coder/v2/coderd/prebuilds"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/coder/v2/coderd/prebuilds"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"cdr.dev/slog"
|
"cdr.dev/slog"
|
||||||
|
|
||||||
"github.com/coder/coder/v2/agent/proto"
|
"github.com/coder/coder/v2/agent/proto"
|
||||||
"github.com/coder/coder/v2/coderd/audit"
|
"github.com/coder/coder/v2/coderd/audit"
|
||||||
"github.com/coder/coder/v2/coderd/database"
|
"github.com/coder/coder/v2/coderd/database"
|
||||||
@ -729,7 +731,7 @@ func createWorkspace(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if claimedWorkspace != nil {
|
if claimedWorkspace != nil {
|
||||||
builder = builder.MarkPrebuildClaimBy(owner.ID)
|
builder = builder.MarkPrebuildClaimedBy(owner.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
workspaceBuild, provisionerJob, provisionerDaemons, err = builder.Build(
|
workspaceBuild, provisionerJob, provisionerDaemons, err = builder.Build(
|
||||||
|
@ -74,7 +74,7 @@ type Builder struct {
|
|||||||
parameterValues *[]string
|
parameterValues *[]string
|
||||||
|
|
||||||
prebuild bool
|
prebuild bool
|
||||||
prebuildClaimBy uuid.UUID
|
prebuildClaimedBy uuid.UUID
|
||||||
runningWorkspaceAgentID uuid.UUID
|
runningWorkspaceAgentID uuid.UUID
|
||||||
|
|
||||||
verifyNoLegacyParametersOnce bool
|
verifyNoLegacyParametersOnce bool
|
||||||
@ -178,9 +178,9 @@ func (b Builder) MarkPrebuild() Builder {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Builder) MarkPrebuildClaimBy(userID uuid.UUID) Builder {
|
func (b Builder) MarkPrebuildClaimedBy(userID uuid.UUID) Builder {
|
||||||
// nolint: revive
|
// nolint: revive
|
||||||
b.prebuildClaimBy = userID
|
b.prebuildClaimedBy = userID
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,7 +319,7 @@ func (b *Builder) buildTx(authFunc func(action policy.Action, object rbac.Object
|
|||||||
WorkspaceBuildID: workspaceBuildID,
|
WorkspaceBuildID: workspaceBuildID,
|
||||||
LogLevel: b.logLevel,
|
LogLevel: b.logLevel,
|
||||||
IsPrebuild: b.prebuild,
|
IsPrebuild: b.prebuild,
|
||||||
PrebuildClaimByUser: b.prebuildClaimBy,
|
PrebuildClaimedByUser: b.prebuildClaimedBy,
|
||||||
RunningWorkspaceAgentID: b.runningWorkspaceAgentID,
|
RunningWorkspaceAgentID: b.runningWorkspaceAgentID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -613,6 +613,11 @@ func (b *Builder) findNewBuildParameterValue(name string) *codersdk.WorkspaceBui
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) getLastBuildParameters() ([]database.WorkspaceBuildParameter, error) {
|
func (b *Builder) getLastBuildParameters() ([]database.WorkspaceBuildParameter, error) {
|
||||||
|
// TODO: exclude preset params from this list instead of returning nothing?
|
||||||
|
if b.prebuildClaimedBy != uuid.Nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
if b.lastBuildParameters != nil {
|
if b.lastBuildParameters != nil {
|
||||||
return *b.lastBuildParameters, nil
|
return *b.lastBuildParameters, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user