- Notifications
You must be signed in to change notification settings - Fork907
fix: persist terraform modules during template import#17665
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE template_version_terraform_values DROP COLUMN cached_module_files; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTERTABLE template_version_terraform_values ADD COLUMN cached_module_files uuidreferences files(id); |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,7 +2,9 @@ package provisionerdserver | ||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"database/sql" | ||
"encoding/hex" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
@@ -50,6 +52,10 @@ import ( | ||
sdkproto "github.com/coder/coder/v2/provisionersdk/proto" | ||
) | ||
const ( | ||
tarMimeType = "application/x-tar" | ||
) | ||
const ( | ||
// DefaultAcquireJobLongPollDur is the time the (deprecated) AcquireJob rpc waits to try to obtain a job before | ||
// canceling and returning an empty job. | ||
@@ -1426,11 +1432,59 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob) | ||
return nil, xerrors.Errorf("update template version external auth providers: %w", err) | ||
} | ||
plan := jobType.TemplateImport.Plan | ||
moduleFiles := jobType.TemplateImport.ModuleFiles | ||
aslilac marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// If there is a plan, or a module files archive we need to insert a | ||
// template_version_terraform_values row. | ||
if len(plan) > 0 || len(moduleFiles) > 0 { | ||
// ...but the plan and the module files archive are both optional! So | ||
// we need to fallback to a valid JSON object if the plan was omitted. | ||
if len(plan) == 0 { | ||
plan = []byte("{}") | ||
} | ||
// ...and we only want to insert a files row if an archive was provided. | ||
var fileID uuid.NullUUID | ||
if len(moduleFiles) > 0 { | ||
hashBytes := sha256.Sum256(moduleFiles) | ||
hash := hex.EncodeToString(hashBytes[:]) | ||
// nolint:gocritic // Requires reading "system" files | ||
file, err := s.Database.GetFileByHashAndCreator(dbauthz.AsSystemRestricted(ctx), database.GetFileByHashAndCreatorParams{Hash: hash, CreatedBy: uuid.Nil}) | ||
switch { | ||
case err == nil: | ||
// This set of modules is already cached, which means we can reuse them | ||
fileID = uuid.NullUUID{ | ||
Valid: true, | ||
UUID: file.ID, | ||
} | ||
case !xerrors.Is(err, sql.ErrNoRows): | ||
return nil, xerrors.Errorf("check for cached modules: %w", err) | ||
default: | ||
// nolint:gocritic // Requires creating a "system" file | ||
file, err = s.Database.InsertFile(dbauthz.AsSystemRestricted(ctx), database.InsertFileParams{ | ||
ID: uuid.New(), | ||
Hash: hash, | ||
CreatedBy: uuid.Nil, | ||
CreatedAt: dbtime.Now(), | ||
Mimetype: tarMimeType, | ||
Data: moduleFiles, | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("insert template version terraform modules: %w", err) | ||
} | ||
fileID = uuid.NullUUID{ | ||
Valid: true, | ||
UUID: file.ID, | ||
} | ||
} | ||
} | ||
err = s.Database.InsertTemplateVersionTerraformValuesByJobID(ctx, database.InsertTemplateVersionTerraformValuesByJobIDParams{ | ||
JobID: jobID, | ||
UpdatedAt: now, | ||
CachedPlan: plan, | ||
CachedModuleFiles: fileID, | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("insert template version terraform data: %w", err) | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.