@@ -257,7 +257,7 @@ func (cfg *PackerConfig) getCoreBuildProvisioner(source SourceUseBlock, pb *Prov
257
257
258
258
// getCoreBuildProvisioners takes a list of post processor block, starts
259
259
// according provisioners and sends parsed HCL2 over to it.
260
- func (cfg * PackerConfig ) getCoreBuildPostProcessors (source SourceUseBlock , blocksList [][]* PostProcessorBlock , ectx * hcl.EvalContext , exceptMatches * int ) ([][]packer.CoreBuildPostProcessor , hcl.Diagnostics ) {
260
+ func (cfg * PackerConfig ) getCoreBuildPostProcessors (source SourceUseBlock , blocksList [][]* PostProcessorBlock , ectx * hcl.EvalContext ) ([][]packer.CoreBuildPostProcessor , hcl.Diagnostics ) {
261
261
var diags hcl.Diagnostics
262
262
res := [][]packer.CoreBuildPostProcessor {}
263
263
for _ , blocks := range blocksList {
@@ -276,7 +276,6 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, block
276
276
for _ , exceptGlob := range cfg .except {
277
277
if exceptGlob .Match (name ) {
278
278
exclude = true
279
- * exceptMatches = * exceptMatches + 1
280
279
break
281
280
}
282
281
}
@@ -308,175 +307,54 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, block
308
307
return res , diags
309
308
}
310
309
311
- // GetBuilds returns a list of packer Build based on the HCL2 parsed build
312
- // blocks. All Builders, Provisioners and Post Processors will be started and
313
- // configured.
314
310
func (cfg * PackerConfig ) GetBuilds (opts packer.GetBuildsOptions ) ([]packersdk.Build , hcl.Diagnostics ) {
315
- res := []packersdk.Build {}
311
+ var allBuilds []packersdk.Build
316
312
var diags hcl.Diagnostics
317
- possibleBuildNames := []string {}
318
-
319
- cfg .debug = opts .Debug
320
- cfg .force = opts .Force
321
- cfg .onError = opts .OnError
322
313
323
314
if len (cfg .Builds ) == 0 {
324
- return res , append (diags , & hcl.Diagnostic {
315
+ return nil , append (diags , & hcl.Diagnostic {
325
316
Summary : "Missing build block" ,
326
317
Detail : "A build block with one or more sources is required for executing a build." ,
327
318
Severity : hcl .DiagError ,
328
319
})
329
320
}
330
321
331
- for _ , build := range cfg .Builds {
332
- for _ , srcUsage := range build .Sources {
333
- src , found := cfg .Sources [srcUsage .SourceRef ]
334
- if ! found {
335
- diags = append (diags , & hcl.Diagnostic {
336
- Summary : "Unknown " + sourceLabel + " " + srcUsage .String (),
337
- Subject : build .HCL2Ref .DefRange .Ptr (),
338
- Severity : hcl .DiagError ,
339
- Detail : fmt .Sprintf ("Known: %v" , cfg .Sources ),
340
- })
341
- continue
342
- }
343
-
344
- pcb := & packer.CoreBuild {
345
- BuildName : build .Name ,
346
- Type : srcUsage .String (),
347
- }
348
-
349
- pcb .SetDebug (cfg .debug )
350
- pcb .SetForce (cfg .force )
351
- pcb .SetOnError (cfg .onError )
352
-
353
- // Apply the -only and -except command-line options to exclude matching builds.
354
- buildName := pcb .Name ()
355
- possibleBuildNames = append (possibleBuildNames , buildName )
356
- // -only
357
- if len (opts .Only ) > 0 {
358
- onlyGlobs , diags := convertFilterOption (opts .Only , "only" )
359
- if diags .HasErrors () {
360
- return nil , diags
361
- }
362
- cfg .only = onlyGlobs
363
- include := false
364
- for _ , onlyGlob := range onlyGlobs {
365
- if onlyGlob .Match (buildName ) {
366
- include = true
367
- break
368
- }
369
- }
370
- if ! include {
371
- continue
372
- }
373
- opts .OnlyMatches ++
374
- }
375
-
376
- // -except
377
- if len (opts .Except ) > 0 {
378
- exceptGlobs , diags := convertFilterOption (opts .Except , "except" )
379
- if diags .HasErrors () {
380
- return nil , diags
381
- }
382
- cfg .except = exceptGlobs
383
- exclude := false
384
- for _ , exceptGlob := range exceptGlobs {
385
- if exceptGlob .Match (buildName ) {
386
- exclude = true
387
- break
388
- }
389
- }
390
- if exclude {
391
- opts .ExceptMatches ++
392
- continue
393
- }
394
- }
395
-
396
- builder , moreDiags , generatedVars := cfg .startBuilder (srcUsage , cfg .EvalContext (BuildContext , nil ))
397
- diags = append (diags , moreDiags ... )
398
- if moreDiags .HasErrors () {
399
- continue
400
- }
401
-
402
- decoded , _ := decodeHCL2Spec (srcUsage .Body , cfg .EvalContext (BuildContext , nil ), builder )
403
- pcb .HCLConfig = decoded
404
-
405
- // If the builder has provided a list of to-be-generated variables that
406
- // should be made accessible to provisioners, pass that list into
407
- // the provisioner prepare() so that the provisioner can appropriately
408
- // validate user input against what will become available. Otherwise,
409
- // only pass the default variables, using the basic placeholder data.
410
- unknownBuildValues := map [string ]cty.Value {}
411
- for _ , k := range append (packer .BuilderDataCommonKeys , generatedVars ... ) {
412
- unknownBuildValues [k ] = cty .StringVal ("<unknown>" )
413
- }
414
- unknownBuildValues ["name" ] = cty .StringVal (build .Name )
415
-
416
- variables := map [string ]cty.Value {
417
- sourcesAccessor : cty .ObjectVal (srcUsage .ctyValues ()),
418
- buildAccessor : cty .ObjectVal (unknownBuildValues ),
419
- }
322
+ var convertDiags hcl.Diagnostics
323
+ cfg .debug = opts .Debug
324
+ cfg .except , convertDiags = convertFilterOption (opts .Except , "except" )
325
+ diags = diags .Extend (convertDiags )
326
+ cfg .only , convertDiags = convertFilterOption (opts .Only , "only" )
327
+ diags = diags .Extend (convertDiags )
328
+ cfg .force = opts .Force
329
+ cfg .onError = opts .OnError
420
330
421
- provisioners , moreDiags := cfg .getCoreBuildProvisioners (srcUsage , build .ProvisionerBlocks , cfg .EvalContext (BuildContext , variables ))
422
- diags = append (diags , moreDiags ... )
423
- if moreDiags .HasErrors () {
424
- continue
425
- }
426
- pps , moreDiags := cfg .getCoreBuildPostProcessors (srcUsage , build .PostProcessorsLists , cfg .EvalContext (BuildContext , variables ), & opts .ExceptMatches )
427
- diags = append (diags , moreDiags ... )
428
- if moreDiags .HasErrors () {
429
- continue
430
- }
331
+ for _ , build := range cfg .Builds {
332
+ cbs , cbDiags := build .ToCoreBuilds (cfg )
333
+ diags = diags .Extend (cbDiags )
431
334
432
- if build .ErrorCleanupProvisionerBlock != nil &&
433
- ! build .ErrorCleanupProvisionerBlock .OnlyExcept .Skip (srcUsage .String ()) {
434
- errorCleanupProv , moreDiags := cfg .getCoreBuildProvisioner (srcUsage , build .ErrorCleanupProvisionerBlock , cfg .EvalContext (BuildContext , variables ))
435
- diags = append (diags , moreDiags ... )
436
- if moreDiags .HasErrors () {
437
- continue
438
- }
439
- pcb .CleanupProvisioner = errorCleanupProv
440
- }
335
+ for _ , cb := range cbs {
336
+ cb .SetDebug (opts .Debug )
337
+ cb .SetForce (opts .Force )
338
+ cb .SetOnError (opts .OnError )
441
339
442
- pcb .Builder = builder
443
- pcb .Provisioners = provisioners
444
- pcb .PostProcessors = pps
445
- pcb .Prepared = true
340
+ cb .Prepared = true
446
341
447
342
// Prepare just sets the "prepareCalled" flag on CoreBuild, since
448
343
// we did all the prep here.
449
- _ , err := pcb .Prepare ()
344
+ _ , err := cb .Prepare ()
450
345
if err != nil {
451
346
diags = append (diags , & hcl.Diagnostic {
452
347
Severity : hcl .DiagError ,
453
- Summary : fmt .Sprintf ("Preparing packer core build %s failed" , src . Ref (). String ()),
348
+ Summary : fmt .Sprintf ("Preparing packer core build %s failed" , cb . Name ()),
454
349
Detail : err .Error (),
455
- Subject : build .HCL2Ref .DefRange .Ptr (),
456
350
})
457
- continue
458
351
}
459
352
460
- res = append (res , pcb )
353
+ allBuilds = append (allBuilds , cb )
461
354
}
462
355
}
463
- if len (opts .Only ) > opts .OnlyMatches {
464
- diags = append (diags , & hcl.Diagnostic {
465
- Severity : hcl .DiagWarning ,
466
- Summary : "an 'only' option was passed, but not all matches were found for the given build." ,
467
- Detail : fmt .Sprintf ("Possible build names: %v.\n " +
468
- "These could also be matched with a glob pattern like: 'happycloud.*'" , possibleBuildNames ),
469
- })
470
- }
471
- if len (opts .Except ) > opts .ExceptMatches {
472
- diags = append (diags , & hcl.Diagnostic {
473
- Severity : hcl .DiagWarning ,
474
- Summary : "an 'except' option was passed, but did not match any build." ,
475
- Detail : fmt .Sprintf ("Possible build names: %v.\n " +
476
- "These could also be matched with a glob pattern like: 'happycloud.*'" , possibleBuildNames ),
477
- })
478
- }
479
- return res , diags
356
+
357
+ return allBuilds , diags
480
358
}
481
359
482
360
var PackerConsoleHelp = strings .TrimSpace (`
0 commit comments