Skip to content

Commit 591a9b7

Browse files
Wilken Riveranywilken
authored andcommitted
Update to use registry image from packersdk
1 parent e8e44fd commit 591a9b7

8 files changed

Lines changed: 271 additions & 100 deletions

File tree

builder/file/artifact.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77
"path"
88

9-
"github.com/hashicorp/packer-plugin-sdk/packer/registryimage"
9+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
1010
)
1111

1212
type FileArtifact struct {
@@ -31,10 +31,17 @@ func (a *FileArtifact) String() string {
3131

3232
func (a *FileArtifact) State(name string) interface{} {
3333
if name == registryimage.ArtifactStateURI {
34-
return registryimage.FromArtifact(a,
34+
img, err := registryimage.FromArtifact(a,
3535
registryimage.WithID(path.Base(a.filename)),
3636
registryimage.WithRegion(path.Dir(a.filename)),
3737
)
38+
39+
if err != nil {
40+
log.Printf("[DEBUG] error encountered when creating a registry image %v", err)
41+
return nil
42+
}
43+
44+
return img
3845
}
3946

4047
return nil

go.mod

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ require (
2424
github.com/hashicorp/packer-plugin-alicloud v1.0.0
2525
github.com/hashicorp/packer-plugin-amazon v1.0.1-dev
2626
github.com/hashicorp/packer-plugin-ansible v1.0.0
27-
github.com/hashicorp/packer-plugin-azure v1.0.3-0.20210827204949-2db2f8f742f5
27+
github.com/hashicorp/packer-plugin-azure v1.0.3-0.20210901194000-92cb558280ea
2828
github.com/hashicorp/packer-plugin-chef v1.0.1
2929
github.com/hashicorp/packer-plugin-cloudstack v1.0.0
3030
github.com/hashicorp/packer-plugin-converge v1.0.0
3131
github.com/hashicorp/packer-plugin-digitalocean v1.0.0
3232
github.com/hashicorp/packer-plugin-docker v1.0.1
33-
github.com/hashicorp/packer-plugin-googlecompute v1.0.1-0.20210827170357-eff61b67a6ce
33+
github.com/hashicorp/packer-plugin-googlecompute v1.0.1-0.20210901195800-781d29aa5ab5
3434
github.com/hashicorp/packer-plugin-hcloud v1.0.0
3535
github.com/hashicorp/packer-plugin-hyperone v1.0.0
3636
github.com/hashicorp/packer-plugin-hyperv v1.0.0
@@ -49,7 +49,7 @@ require (
4949
github.com/hashicorp/packer-plugin-puppet v1.0.0
5050
github.com/hashicorp/packer-plugin-qemu v1.0.0
5151
github.com/hashicorp/packer-plugin-scaleway v1.0.1
52-
github.com/hashicorp/packer-plugin-sdk v0.2.4-0.20210824193240-6b55f55dd41b
52+
github.com/hashicorp/packer-plugin-sdk v0.2.5-0.20210902203720-8464b03760f4
5353
github.com/hashicorp/packer-plugin-tencentcloud v1.0.1
5454
github.com/hashicorp/packer-plugin-triton v1.0.0
5555
github.com/hashicorp/packer-plugin-ucloud v1.0.0
@@ -59,27 +59,27 @@ require (
5959
github.com/hashicorp/packer-plugin-vsphere v1.0.0
6060
github.com/hashicorp/packer-plugin-yandex v1.0.0
6161
github.com/klauspost/pgzip v0.0.0-20151221113845-47f36e165cec
62-
github.com/masterzen/winrm v0.0.0-20210504160029-28ed956f5227
62+
github.com/masterzen/winrm v0.0.0-20210623064412-3b76017826b0
6363
github.com/mattn/go-tty v0.0.0-20191112051231-74040eebce08
64-
github.com/mitchellh/cli v1.1.0
64+
github.com/mitchellh/cli v1.1.2
6565
github.com/mitchellh/go-homedir v1.1.0
6666
github.com/mitchellh/mapstructure v1.4.1
6767
github.com/mitchellh/panicwrap v1.0.0
6868
github.com/mitchellh/prefixedio v0.0.0-20151214002211-6e6954073784
69-
github.com/pierrec/lz4 v2.5.2+incompatible
69+
github.com/pierrec/lz4 v2.6.1+incompatible
7070
github.com/posener/complete v1.2.3
7171
github.com/shirou/gopsutil v3.21.1+incompatible
7272
github.com/stretchr/testify v1.7.0
73-
github.com/ulikunitz/xz v0.5.8
74-
github.com/zclconf/go-cty v1.9.0
73+
github.com/ulikunitz/xz v0.5.10
74+
github.com/zclconf/go-cty v1.9.1
7575
github.com/zclconf/go-cty-yaml v1.0.1
76-
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
77-
golang.org/x/mod v0.4.1
78-
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
79-
golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84
76+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
77+
golang.org/x/mod v0.5.0
78+
golang.org/x/net v0.0.0-20210825183410-e898025ed96a
79+
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
8080
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
81-
golang.org/x/tools v0.1.0
82-
google.golang.org/grpc v1.36.1
81+
golang.org/x/tools v0.1.5
82+
google.golang.org/grpc v1.40.0
8383
)
8484

8585
go 1.16

go.sum

Lines changed: 184 additions & 0 deletions
Large diffs are not rendered by default.

internal/packer_registry/types.bucket.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/hashicorp/go-multierror"
1212
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/models"
13+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
1314
"github.com/hashicorp/packer/internal/packer_registry/env"
1415
"google.golang.org/grpc/codes"
1516
)
@@ -127,7 +128,7 @@ func (b *Bucket) CreateInitialBuildForIteration(ctx context.Context, componentTy
127128
RunUUID: b.Iteration.RunUUID,
128129
Status: status,
129130
Labels: make(map[string]string),
130-
Images: make(map[string]Image),
131+
Images: make(map[string]registryimage.Image),
131132
}
132133

133134
log.Println("[TRACE] creating initial build for component", componentType)
@@ -219,7 +220,7 @@ func (b *Bucket) markBuildComplete(ctx context.Context, name string) error {
219220
if providerName == "" {
220221
providerName = image.ProviderName
221222
}
222-
images = append(images, &models.HashicorpCloudPackerImage{ImageID: image.ID, Region: image.ProviderRegion})
223+
images = append(images, &models.HashicorpCloudPackerImage{ImageID: image.ImageID, Region: image.ProviderRegion})
223224
}
224225

225226
buildInput.Updates.CloudProvider = providerName
@@ -236,7 +237,7 @@ func (b *Bucket) markBuildComplete(ctx context.Context, name string) error {
236237
}
237238

238239
// UpdateImageForBuild appends one or more images artifacts to the build referred to by componentType.
239-
func (b *Bucket) UpdateImageForBuild(componentType string, images ...Image) error {
240+
func (b *Bucket) UpdateImageForBuild(componentType string, images ...registryimage.Image) error {
240241
return b.Iteration.AddImageToBuild(componentType, images...)
241242
}
242243

@@ -340,8 +341,8 @@ func (b *Bucket) initializeIteration(ctx context.Context) error {
340341
// potential issue on updating the status of a build that is already DONE. Is this possible?
341342
for _, image := range existing.Images {
342343

343-
err := b.UpdateImageForBuild(existing.ComponentType, Image{
344-
ID: image.ImageID,
344+
err := b.UpdateImageForBuild(existing.ComponentType, registryimage.Image{
345+
ImageID: image.ImageID,
345346
ProviderRegion: image.Region,
346347
})
347348

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package packer_registry
22

33
import (
4-
"fmt"
5-
64
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/models"
5+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
76
)
87

98
// Build represents a build of a given component type for some bucket on the HCP Packer Registry.
@@ -13,17 +12,6 @@ type Build struct {
1312
ComponentType string
1413
RunUUID string
1514
Labels map[string]string
16-
Images map[string]Image
15+
Images map[string]registryimage.Image
1716
Status models.HashicorpCloudPackerBuildStatus
1817
}
19-
20-
// Image represents an artifact on some external provider (e.g AWS, GCP, Azure) that should be tracked
21-
// as the main image artifact for some iteration of a Bucket on the HCP Packer Registry.
22-
type Image struct {
23-
ID string
24-
ProviderName, ProviderRegion string
25-
}
26-
27-
func (i Image) String() string {
28-
return fmt.Sprintf("provider:%s, image:%s, region:%s", i.ProviderName, i.ID, i.ProviderRegion)
29-
}

internal/packer_registry/types.iterations.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88

99
git "github.com/go-git/go-git/v5"
10+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
1011
)
1112

1213
type Iteration struct {
@@ -82,7 +83,7 @@ func GetGitFingerprint(opts IterationOptions) (string, error) {
8283
}
8384

8485
// AddImageToBuild appends one or more images artifacts to the build referred to by buildName.
85-
func (i *Iteration) AddImageToBuild(buildName string, images ...Image) error {
86+
func (i *Iteration) AddImageToBuild(buildName string, images ...registryimage.Image) error {
8687
existingBuild, ok := i.builds.Load(buildName)
8788
if !ok {
8889
return errors.New("no build found for the name " + buildName)
@@ -94,14 +95,25 @@ func (i *Iteration) AddImageToBuild(buildName string, images ...Image) error {
9495
}
9596

9697
if build.Images == nil {
97-
build.Images = make(map[string]Image)
98+
build.Images = make(map[string]registryimage.Image)
9899
}
99100

100101
for _, image := range images {
102+
if err := image.Validate(); err != nil {
103+
return fmt.Errorf("failed to add image to build %q: %v", buildName, err)
104+
}
105+
101106
if build.CloudProvider == "" {
102107
build.CloudProvider = image.ProviderName
103108
}
104109

110+
for k, v := range image.Labels {
111+
if _, ok := build.Labels[k]; ok {
112+
continue
113+
}
114+
build.Labels[k] = v
115+
}
116+
105117
build.Images[image.String()] = image
106118
}
107119

packer/registry_builder.go

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77

88
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/models"
99
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
10-
"github.com/hashicorp/packer-plugin-sdk/packer/registryimage"
10+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
11+
"github.com/hashicorp/packer-plugin-sdk/template/config"
1112
packerregistry "github.com/hashicorp/packer/internal/packer_registry"
12-
"github.com/mitchellh/mapstructure"
1313
)
1414

1515
type RegistryBuilder struct {
@@ -61,36 +61,28 @@ func (b *RegistryBuilder) Run(ctx context.Context, ui packersdk.Ui, hook packers
6161
// close chan to mark completion
6262
close(runCompleted)
6363

64-
// Lets post state
65-
if artifact != nil {
66-
switch state := artifact.State(registryimage.ArtifactStateURI).(type) {
67-
case map[interface{}]interface{}:
68-
var image registryimage.Image
69-
mapstructure.Decode(state, &image)
70-
// TODO handle these error better
71-
err := b.ArtifactMetadataPublisher.UpdateImageForBuild(b.Name, packerregistry.Image{
72-
ProviderName: image.ProviderName,
73-
ProviderRegion: image.ProviderRegion,
74-
ID: image.ImageID,
75-
})
76-
if err != nil {
77-
log.Printf("[TRACE] failed to add image artifact for %q: %s", b.Name, err)
78-
}
79-
case []interface{}:
80-
var images []registryimage.Image
81-
mapstructure.Decode(state, &images)
82-
for _, image := range images {
83-
// TODO handle these error better
84-
err := b.ArtifactMetadataPublisher.UpdateImageForBuild(b.Name, packerregistry.Image{
85-
ProviderName: image.ProviderName,
86-
ProviderRegion: image.ProviderRegion,
87-
ID: image.ImageID,
88-
})
89-
if err != nil {
90-
log.Printf("[TRACE] failed to add image artifact for %q: %s", b.Name, err)
91-
}
92-
}
93-
}
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
if artifact == nil {
69+
return nil, nil
70+
}
71+
72+
switch state := artifact.State(registryimage.ArtifactStateURI).(type) {
73+
case map[interface{}]interface{}:
74+
var image registryimage.Image
75+
config.Decode(&image, &config.DecodeOpts{}, state)
76+
err = b.ArtifactMetadataPublisher.UpdateImageForBuild(b.Name, image)
77+
case []interface{}:
78+
var images []registryimage.Image
79+
config.Decode(&images, &config.DecodeOpts{}, state)
80+
err = b.ArtifactMetadataPublisher.UpdateImageForBuild(b.Name, images...)
9481
}
82+
83+
if err != nil {
84+
log.Printf("[TRACE] failed to add image artifact for %q: %s", b.Name, err)
85+
}
86+
9587
return artifact, err
9688
}

packer/registry_post_processor.go

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"github.com/hashicorp/hcl/v2/hcldec"
99
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/models"
1010
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
11-
"github.com/hashicorp/packer-plugin-sdk/packer/registryimage"
11+
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
12+
"github.com/hashicorp/packer-plugin-sdk/template/config"
1213
packerregistry "github.com/hashicorp/packer/internal/packer_registry"
1314
"github.com/mitchellh/mapstructure"
1415
)
@@ -61,35 +62,21 @@ func (p *RegistryPostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui
6162
return source, false, false, err
6263
}
6364

64-
// Lets post state
65-
if source != nil {
66-
switch state := source.State(registryimage.ArtifactStateURI).(type) {
67-
case map[interface{}]interface{}:
68-
var image registryimage.Image
69-
mapstructure.Decode(state, &image)
70-
err := p.ArtifactMetadataPublisher.UpdateImageForBuild(p.BuilderType, packerregistry.Image{
71-
ProviderName: image.ProviderName,
72-
ProviderRegion: image.ProviderRegion,
73-
ID: image.ImageID,
74-
})
75-
if err != nil {
76-
log.Printf("[TRACE] failed to add image artifact for %q: %s", p.BuilderType, err)
77-
}
78-
case []interface{}:
79-
var images []registryimage.Image
80-
mapstructure.Decode(state, &images)
81-
for _, image := range images {
82-
// TODO handle these error better
83-
err := p.ArtifactMetadataPublisher.UpdateImageForBuild(p.BuilderType, packerregistry.Image{
84-
ProviderName: image.ProviderName,
85-
ProviderRegion: image.ProviderRegion,
86-
ID: image.ImageID,
87-
})
88-
if err != nil {
89-
log.Printf("[TRACE] failed to add image artifact for %q: %s", p.BuilderType, err)
90-
}
91-
}
92-
}
65+
switch state := source.State(registryimage.ArtifactStateURI).(type) {
66+
case map[interface{}]interface{}:
67+
var image registryimage.Image
68+
config.Decode(&image, &config.DecodeOpts{}, state)
69+
err = p.ArtifactMetadataPublisher.UpdateImageForBuild(p.BuilderType, image)
70+
case []interface{}:
71+
var images []registryimage.Image
72+
mapstructure.Decode(state, &images)
73+
config.Decode(&images, &config.DecodeOpts{}, state)
74+
err = p.ArtifactMetadataPublisher.UpdateImageForBuild(p.BuilderType, images...)
9375
}
94-
return source, keep, override, nil
76+
77+
if err != nil {
78+
log.Printf("[TRACE] failed to add image artifact for %q: %s", p.BuilderType, err)
79+
}
80+
81+
return source, keep, override, err
9582
}

0 commit comments

Comments
 (0)