@@ -2,7 +2,9 @@ package provisionerdserver
2
2
3
3
import (
4
4
"context"
5
+ "crypto/sha256"
5
6
"database/sql"
7
+ "encoding/hex"
6
8
"encoding/json"
7
9
"errors"
8
10
"fmt"
@@ -50,6 +52,10 @@ import (
50
52
sdkproto "github.com/coder/coder/v2/provisionersdk/proto"
51
53
)
52
54
55
+ const (
56
+ tarMimeType = "application/x-tar"
57
+ )
58
+
53
59
const (
54
60
// DefaultAcquireJobLongPollDur is the time the (deprecated) AcquireJob rpc waits to try to obtain a job before
55
61
// canceling and returning an empty job.
@@ -1426,11 +1432,59 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
1426
1432
return nil , xerrors .Errorf ("update template version external auth providers: %w" , err )
1427
1433
}
1428
1434
1429
- if len (jobType .TemplateImport .Plan ) > 0 {
1430
- err := s .Database .InsertTemplateVersionTerraformValuesByJobID (ctx , database.InsertTemplateVersionTerraformValuesByJobIDParams {
1431
- JobID : jobID ,
1432
- CachedPlan : jobType .TemplateImport .Plan ,
1433
- UpdatedAt : now ,
1435
+ plan := jobType .TemplateImport .Plan
1436
+ moduleFiles := jobType .TemplateImport .ModuleFiles
1437
+ // If there is a plan, or a module files archive we need to insert a
1438
+ // template_version_terraform_values row.
1439
+ if len (plan ) > 0 || len (moduleFiles ) > 0 {
1440
+ // ...but the plan and the module files archive are both optional! So
1441
+ // we need to fallback to a valid JSON object if the plan was omitted.
1442
+ if len (plan ) == 0 {
1443
+ plan = []byte ("{}" )
1444
+ }
1445
+
1446
+ // ...and we only want to insert a files row if an archive was provided.
1447
+ var fileID uuid.NullUUID
1448
+ if len (moduleFiles ) > 0 {
1449
+ hashBytes := sha256 .Sum256 (moduleFiles )
1450
+ hash := hex .EncodeToString (hashBytes [:])
1451
+
1452
+ // nolint:gocritic // Requires reading "system" files
1453
+ file , err := s .Database .GetFileByHashAndCreator (dbauthz .AsSystemRestricted (ctx ), database.GetFileByHashAndCreatorParams {Hash : hash , CreatedBy : uuid .Nil })
1454
+ switch {
1455
+ case err == nil :
1456
+ // This set of modules is already cached, which means we can reuse them
1457
+ fileID = uuid.NullUUID {
1458
+ Valid : true ,
1459
+ UUID : file .ID ,
1460
+ }
1461
+ case ! xerrors .Is (err , sql .ErrNoRows ):
1462
+ return nil , xerrors .Errorf ("check for cached modules: %w" , err )
1463
+ default :
1464
+ // nolint:gocritic // Requires creating a "system" file
1465
+ file , err = s .Database .InsertFile (dbauthz .AsSystemRestricted (ctx ), database.InsertFileParams {
1466
+ ID : uuid .New (),
1467
+ Hash : hash ,
1468
+ CreatedBy : uuid .Nil ,
1469
+ CreatedAt : dbtime .Now (),
1470
+ Mimetype : tarMimeType ,
1471
+ Data : moduleFiles ,
1472
+ })
1473
+ if err != nil {
1474
+ return nil , xerrors .Errorf ("insert template version terraform modules: %w" , err )
1475
+ }
1476
+ fileID = uuid.NullUUID {
1477
+ Valid : true ,
1478
+ UUID : file .ID ,
1479
+ }
1480
+ }
1481
+ }
1482
+
1483
+ err = s .Database .InsertTemplateVersionTerraformValuesByJobID (ctx , database.InsertTemplateVersionTerraformValuesByJobIDParams {
1484
+ JobID : jobID ,
1485
+ UpdatedAt : now ,
1486
+ CachedPlan : plan ,
1487
+ CachedModuleFiles : fileID ,
1434
1488
})
1435
1489
if err != nil {
1436
1490
return nil , xerrors .Errorf ("insert template version terraform data: %w" , err )
0 commit comments