-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
registry_builder.go
124 lines (104 loc) · 3.68 KB
/
registry_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package packer
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/models"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
packerregistry "github.com/hashicorp/packer/internal/registry"
"github.com/mitchellh/mapstructure"
)
// HeartbeatPeriod dictates how often a heartbeat is sent to HCP to signal a
// build is still alive.
const HeartbeatPeriod = 2 * time.Minute
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)
}
go func() {
log.Printf("[TRACE] starting heartbeats for %q", b.Name)
tick := time.NewTicker(HeartbeatPeriod)
for {
select {
case <-ctx.Done():
tick.Stop()
return
case <-tick.C:
err := b.ArtifactMetadataPublisher.UpdateBuildStatus(
ctx,
b.Name,
models.HashicorpCloudPackerBuildStatusRUNNING,
)
if err != nil {
log.Printf("[ERROR] failed to send heartbeat for build %q: %s", b.Name, err)
} else {
log.Printf("[TRACE] sent heartbeat for %q", b.Name)
}
}
}
}()
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)
if err != nil {
return nil, err
}
if artifact == nil {
return nil, nil
}
var images []registryimage.Image
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &images,
WeaklyTypedInput: true,
ErrorUnused: false,
})
if err != nil {
return artifact, fmt.Errorf("failed to create decoder for HCP Packer registry image: %w", err)
}
state := artifact.State(registryimage.ArtifactStateURI)
err = decoder.Decode(state)
if err != nil {
return artifact, fmt.Errorf("failed to obtain HCP Packer registry image from build artifact: %w", err)
}
err = b.ArtifactMetadataPublisher.UpdateImageForBuild(b.Name, images...)
if err != nil {
return artifact, fmt.Errorf("failed to add image artifact for %q: %s", b.Name, err)
}
return artifact, nil
}