Skip to content

Commit

Permalink
refactor: add source image id, new hcp example
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Lammler <[email protected]>
  • Loading branch information
apricote and jooola committed Dec 15, 2023
1 parent 242ce34 commit 8280e55
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
20 changes: 10 additions & 10 deletions builder/hcloud/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ func (a *Artifact) State(name string) interface{} {
func (a *Artifact) stateHCPPackerRegistryMetadata() interface{} {
// create labels map
labels := make(map[string]string)

// This label contains the value the user specified in their template
sourceImage, ok := a.StateData["source_image"].(string)
if ok {
labels["source_image"] = sourceImage
}
// This is the canonical ID of the source image that was used, useful for ancestry tracking
sourceImageID, ok := a.StateData["source_image_id"].(int64)

Check failure on line 65 in builder/hcloud/artifact.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to ok (ineffassign)
// get and set region from stateData into labels
region, ok := a.StateData["region"].(string)
if ok {
Expand All @@ -70,17 +74,13 @@ func (a *Artifact) stateHCPPackerRegistryMetadata() interface{} {
labels["server_type"] = serverType
}

image, err := registryimage.FromArtifact(a,
registryimage.WithProvider("hetznercloud"),
registryimage.WithID(strconv.FormatInt(a.snapshotId, 10)),
registryimage.WithSourceID(sourceImage),
registryimage.WithRegion(region))
if err != nil {
log.Printf("[DEBUG] error encountered when creating registry image %s", err)
return nil
return &registryimage.Image{
ProviderName: "hetznercloud",
ImageID: a.Id(),
ProviderRegion: region,
Labels: labels,
SourceImageID: strconv.FormatInt(sourceImageID, 10),
}
image.Labels = labels
return image
}

func (a *Artifact) Destroy() error {
Expand Down
11 changes: 9 additions & 2 deletions builder/hcloud/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
sshKeys = append(sshKeys, sshKey)
}

serverType := state.Get("serverType").(*hcloud.ServerType)
var image *hcloud.Image
if c.Image != "" {
image = &hcloud.Image{Name: c.Image}
var err error
image, _, err = client.Image.GetForArchitecture(ctx, c.Image, serverType.Architecture)
if err != nil {
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
} else {
serverType := state.Get("serverType").(*hcloud.ServerType)
var err error
image, err = getImageWithSelectors(ctx, client, c, serverType)
if err != nil {
Expand All @@ -69,6 +75,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
}
ui.Message(fmt.Sprintf("Using image %s with ID %d", image.Description, image.ID))
}
state.Put("source_image_id", image.ID)

var networks []*hcloud.Network
for _, k := range c.Networks {
Expand Down
50 changes: 50 additions & 0 deletions example/hcp-build.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

packer {
required_plugins {
hcloud = {
version = ">=v1.0.5"
source = "github.com/hashicorp/hcloud"
}
}
}

variable "hcloud_token" {
type = string
default = "${env("HCLOUD_TOKEN")}"
sensitive = true
}

source "hcloud" "example" {
image = "ubuntu-22.04"
location = "hel1"
server_name = "hcloud-example"
server_type = "cx11"
snapshot_labels = {
app = "hcloud-example"
}
snapshot_name = "hcloud-example"
ssh_username = "root"
token = var.hcloud_token
}

build {
hcp_packer_registry {
bucket_name = "hcloud-hcp-test"
description = "A nice test description"
bucket_labels = {
"foo" = "bar"
}
}

sources = ["source.hcloud.example"]

provisioner "shell" {
inline = ["cloud-init status --wait"]
}

provisioner "shell" {
inline = ["echo \"Hello World!\" > /var/log/packer.log"]
}
}

0 comments on commit 8280e55

Please sign in to comment.