github.com/hashicorp/packer@v1.14.3/internal/hcp/registry/json.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package registry
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"log"
    10  	"path/filepath"
    11  
    12  	"github.com/hashicorp/hcl/v2"
    13  	hcpPackerModels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2023-01-01/models"
    14  	sdkpacker "github.com/hashicorp/packer-plugin-sdk/packer"
    15  	"github.com/hashicorp/packer/packer"
    16  )
    17  
    18  // JSONRegistry is a HCP handler made to process legacy JSON templates
    19  type JSONRegistry struct {
    20  	configuration *packer.Core
    21  	bucket        *Bucket
    22  	ui            sdkpacker.Ui
    23  	metadata      *MetadataStore
    24  }
    25  
    26  func NewJSONRegistry(config *packer.Core, ui sdkpacker.Ui) (*JSONRegistry, hcl.Diagnostics) {
    27  	bucket, diags := createConfiguredBucket(
    28  		filepath.Dir(config.Template.Path),
    29  		withPackerEnvConfiguration,
    30  	)
    31  
    32  	if diags.HasErrors() {
    33  		return nil, diags
    34  	}
    35  
    36  	for _, b := range config.Template.Builders {
    37  		buildName := b.Name
    38  
    39  		// By default, if the name is unspecified, it will be assigned the type
    40  		//
    41  		// If the two are different, we can compose the HCP build name from both
    42  		if b.Name != b.Type {
    43  			buildName = fmt.Sprintf("%s.%s", b.Type, b.Name)
    44  		}
    45  
    46  		// Get all builds slated within config ignoring any only or exclude flags.
    47  		bucket.RegisterBuildForComponent(buildName)
    48  	}
    49  
    50  	ui.Say(fmt.Sprintf("Tracking build on HCP Packer with fingerprint %q", bucket.Version.Fingerprint))
    51  
    52  	return &JSONRegistry{
    53  		configuration: config,
    54  		bucket:        bucket,
    55  		ui:            ui,
    56  		metadata:      &MetadataStore{},
    57  	}, nil
    58  }
    59  
    60  // PopulateVersion creates the metadata in HCP Packer Registry for a build
    61  func (h *JSONRegistry) PopulateVersion(ctx context.Context) error {
    62  	err := h.bucket.Validate()
    63  	if err != nil {
    64  		return err
    65  	}
    66  	err = h.bucket.Initialize(ctx, hcpPackerModels.HashicorpCloudPacker20230101TemplateTypeJSON)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	err = h.bucket.populateVersion(ctx)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	sha, err := getGitSHA(h.configuration.Template.Path)
    77  	if err != nil {
    78  		log.Printf("failed to get GIT SHA from environment, won't set as build labels")
    79  	} else {
    80  		h.bucket.Version.AddSHAToBuildLabels(sha)
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  // StartBuild is invoked when one build for the configuration is starting to be processed
    87  func (h *JSONRegistry) StartBuild(ctx context.Context, build *packer.CoreBuild) error {
    88  	return h.bucket.startBuild(ctx, build.Name())
    89  }
    90  
    91  // CompleteBuild is invoked when one build for the configuration has finished
    92  func (h *JSONRegistry) CompleteBuild(
    93  	ctx context.Context,
    94  	build *packer.CoreBuild,
    95  	artifacts []sdkpacker.Artifact,
    96  	buildErr error,
    97  ) ([]sdkpacker.Artifact, error) {
    98  	buildName := build.Name()
    99  	buildMetadata, envMetadata := build.GetMetadata(), h.metadata
   100  	err := h.bucket.Version.AddMetadataToBuild(ctx, buildName, buildMetadata, envMetadata)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
   105  }
   106  
   107  // VersionStatusSummary prints a status report in the UI if the version is not yet done
   108  func (h *JSONRegistry) VersionStatusSummary() {
   109  	h.bucket.Version.statusSummary(h.ui)
   110  }
   111  
   112  // Metadata gets the global metadata object that registers global settings
   113  func (h *JSONRegistry) Metadata() Metadata {
   114  	return h.metadata
   115  }