mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-21 15:31:41 -04:00
* Add working registry pkg * Add custom error for handling the loading of PAR environment variables * Working Publish to Build, with proper error handling for bucket names * Update hcp-sdk-go to use branch instead of mod replace directive * Update Packer build status configuration * Add support for HCP_PACKER_BUILD_FINGERPRINT env * Add support for publishing one or more PARtifacts from a single build * add git shas to this branch * Add ability to set provider name if available * Add working RegistryBuilder type * Add RegistryPostProcessor as wrapper post-processor * When in PAR mode a empty RegistryPostProcessor is added to the end of the post-processor list to publish all final image data. * Add support for updating a build from PAR that is not in a DONE state * Fix a small issue with creation the initial builds for an empty iteration. * Add PAR URL to post-processor display * Implement hcp_packer_registry block (#11168) * Update vendored Amazon plugin to v1.0.1-dev * Fix panic when running a Packer registry build in a clean directory * Remove the publishing of post-processor metadata from the registry post-processor. * Remove metadata add from registry_builder * Update registry builder to skip a build that was found to be DONE Co-authored-by: Megan Marsh <[email protected]> Co-authored-by: Sylvia Moss <[email protected]>
96 lines
3.2 KiB
Go
96 lines
3.2 KiB
Go
package packer
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/models"
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
packerregistry "github.com/hashicorp/packer/internal/packer_registry"
|
|
)
|
|
|
|
type RegistryBuilder struct {
|
|
Name string
|
|
ArtifactMetadataPublisher *packerregistry.Bucket
|
|
packersdk.Builder
|
|
}
|
|
|
|
func (b *RegistryBuilder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
|
return b.Builder.Prepare(raws...)
|
|
}
|
|
|
|
// Run is where the actual build should take place. It takes a Build and a Ui.
|
|
func (b *RegistryBuilder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
|
|
|
if !b.ArtifactMetadataPublisher.IsExpectingBuildForComponent(b.Name) {
|
|
ui.Error(fmt.Sprintf("The build for %q in iteration %q has already been marked as DONE; Skipping build to prevent drift.", b.Name, b.ArtifactMetadataPublisher.Iteration.ID))
|
|
return nil, nil
|
|
}
|
|
|
|
runCompleted := make(chan struct{})
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Printf("[TRACE] marking build %q as cancelled in HCP Packer registry", b.Name)
|
|
if err := b.ArtifactMetadataPublisher.UpdateBuildStatus(context.TODO(), b.Name, models.HashicorpCloudPackerBuildStatusCANCELLED); err != nil {
|
|
log.Printf("[TRACE] failed to update HCP Packer registry status for %q: %s", b.Name, err)
|
|
}
|
|
return
|
|
case <-runCompleted:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err := b.ArtifactMetadataPublisher.UpdateBuildStatus(ctx, b.Name, models.HashicorpCloudPackerBuildStatusRUNNING); err != nil {
|
|
log.Printf("[TRACE] failed to update HCP Packer registry status for %q: %s", b.Name, err)
|
|
}
|
|
|
|
ui.Say(fmt.Sprintf("Publishing build details for %s to the HCP Packer registry", b.Name))
|
|
artifact, err := b.Builder.Run(ctx, ui, hook)
|
|
if err != nil {
|
|
if parErr := b.ArtifactMetadataPublisher.UpdateBuildStatus(ctx, b.Name, models.HashicorpCloudPackerBuildStatusFAILED); parErr != nil {
|
|
log.Printf("[TRACE] failed to update HCP Packer registry status for %q: %s", b.Name, parErr)
|
|
}
|
|
}
|
|
|
|
// close chan to mark completion
|
|
close(runCompleted)
|
|
|
|
// Lets post state
|
|
if artifact != nil {
|
|
switch state := artifact.State("par.artifact.metadata").(type) {
|
|
case map[interface{}]interface{}:
|
|
m := make(map[string]string)
|
|
for k, v := range state {
|
|
m[k.(string)] = v.(string)
|
|
}
|
|
// TODO handle these error better
|
|
err := b.ArtifactMetadataPublisher.UpdateImageForBuild(b.Name, packerregistry.Image{
|
|
ProviderName: m["ProviderName"],
|
|
ProviderRegion: m["ProviderRegion"],
|
|
ID: m["ImageID"],
|
|
})
|
|
if err != nil {
|
|
log.Printf("[TRACE] failed to add image artifact for %q: %s", b.Name, err)
|
|
}
|
|
case []interface{}:
|
|
for _, d := range state {
|
|
d := d.(map[interface{}]interface{})
|
|
err := b.ArtifactMetadataPublisher.UpdateImageForBuild(b.Name, packerregistry.Image{
|
|
ProviderName: d["ProviderName"].(string),
|
|
ProviderRegion: d["ProviderRegion"].(string),
|
|
ID: d["ImageID"].(string),
|
|
})
|
|
if err != nil {
|
|
log.Printf("[TRACE] failed to add image artifact for %q: %s", b.Name, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return artifact, err
|
|
}
|