github.com/hashicorp/packer@v1.14.3/website/content/docs/plugins/creation/hcp-support.mdx (about) 1 --- 2 description: | 3 You can create or modify custom plugins to support HCP Packer. Add support for HCP Packer to let plugins manage image metadata stored in the HCP Packer registry. 4 page_title: Enable HCP Packer support for custom plugins 5 --- 6 7 # Enable HCP Packer Support for Custom Plugins 8 9 This page explains how to update a custom plugin so that it can publish image metadata to the [HCP Packer registry](/hcp/docs/packer). Refer to [Custom Builders](/packer/docs/plugins/creation/custom-builders) and [Custom Post-Processors](/packer/docs/plugins/creation/custom-post-processors) for details about creating an external Packer plugin. 10 11 Before pushing metadata to the HCP Packer registry, Packer uses the [`par.artifact.metadata` key](https://pkg.go.dev/github.com/hashicorp/packer-plugin-sdk/packer/registry/image#pkg-constants) 12 to query a builder artifact for the image metadata that a particular component would like to have stored in the registry. 13 14 For details and examples of how to manage image metadata, refer to the [HCP Packer GitHub documentation](https://pkg.go.dev/github.com/hashicorp/packer-plugin-sdk/packer/registry/image). 15 16 HCP Packer is under active development, and we suggest plugin maintainers to keep up with the SDK changes for more on HCP Packer support. 17 18 ## Builder Artifact 19 20 To support HCP Packer, changes must be made to the plugin's build artifact. The artifact should keep the appropriate 21 Image metadata in state under the key `par.artifact.metadata`. The expected key is provided by the [image.ArtifactStateURI](https://pkg.go.dev/github.com/hashicorp/packer-plugin-sdk/packer/registry/image#pkg-constants) constant. 22 23 To make HCP Packer support easier, we ship an Image package with the [packer-plugin-sdk](https://pkg.go.dev/github.com/hashicorp/packer-plugin-sdk/packer/registry/image#pkg-overview). 24 We provide the [`FromArtifact`](https://pkg.go.dev/github.com/hashicorp/packer-plugin-sdk/packer/registry/image#FromArtifact) function 25 to easily create an Image with the basic information contained in the Artifact. If customization is necessary, the `FromArtifact` accepts 26 override functions as the second argument, and that can be used to set different values for the metadata. 27 28 The [Image metadata](https://pkg.go.dev/github.com/hashicorp/packer-plugin-sdk/packer/registry/image#Image) must contain every 29 metadata HCP Packer should store about this component. The structure is: 30 ```go 31 // Image represents the metadata for some Artifact in the HCP Packer Registry. 32 type Image struct { 33 // ImageID is a unique reference identifier stored on the HCP Packer registry 34 // that can be used to get back the built artifact of a builder or post-processor. 35 ImageID string 36 // ProviderName represents the name of the top level cloud or service where the built artifact resides. 37 // For example "aws, azure, docker, gcp, and vsphere". 38 ProviderName string 39 // ProviderRegion represents the location of the built artifact. 40 // For cloud providers region usually maps to a cloud region or zone, but for things like the file builder, 41 // S3 bucket or vsphere cluster region can represent a path on the upstream datastore, or cluster. 42 ProviderRegion string 43 // Labels represents additional details about an image that a builder or post-processor may with to provide for a given build. 44 // Any additional metadata will be made available as build labels within a HCP Packer registry iteration. 45 Labels map[string]string 46 // SourceImageID is the cloud image ID of the image that was used as the 47 // source for this image. If set, the HCP Packer registry will be able 48 // link the parent and child images for ancestry visualizations and 49 // dependency tracking. 50 SourceImageID string 51 } 52 ``` 53 54 Where `ImageID`, `ProviderName`, and `SourceImageID` are mandatory fields. 55 56 Examples using the `FromArtifact` method: 57 58 - Simple form: 59 ```go 60 func (a *Artifact) State(name string) interface{} { 61 if name == registryimage.ArtifactStateURI { 62 img, err := registryimage.FromArtifact(a) 63 if err != nil { 64 log.Printf("[DEBUG] error encountered when creating a registry image %v", err) 65 return nil 66 } 67 return img 68 } 69 return a.StateData[name] 70 } 71 ``` 72 73 - Using overrides: 74 ```go 75 func (a *Artifact) State(name string) interface{} { 76 if name == registryimage.ArtifactStateURI { 77 img, err := registryimage.FromArtifact(a, 78 registryimage.WithID(a.Name), 79 registryimage.WithRegion(a.Region.Name()), 80 registryimage.WithProvider("happy-cloud"), 81 registryimage.WithSourceID(a.SourceID), 82 registryimage.SetLabels(a.Labels), 83 ) 84 if err != nil { 85 log.Printf("[DEBUG] error encountered when creating a registry image %v", err) 86 return nil 87 } 88 return img 89 } 90 return a.StateData[name] 91 } 92 ``` 93 94 See [packer-plugin-sdk/packer/registry/image](https://pkg.go.dev/github.com/hashicorp/packer-plugin-sdk/packer/registry/image#pkg-examples) for additional examples. 95 96 ### SDK Version 97 98 - The HCP Packer support is available from packer-plugin-sdk >= v0.2.7 99 100 ## Plugins Example 101 102 The following plugins currently support HCP Packer and are great references for development: 103 104 - [packer-plugin-amazon](https://github.com/hashicorp/packer-plugin-amazon) 105 - [packer-plugin-azure](https://github.com/hashicorp/packer-plugin-azure) 106 - [packer-plugin-vsphere](https://github.com/hashicorp/packer-plugin-vsphere) 107 - [packer-plugin-docker](https://github.com/hashicorp/packer-plugin-docker) 108 - [packer-plugin-googlecompute](https://github.com/hashicorp/packer-plugin-googlecompute) 109 110 ## HCP Packer 111 112 Refer to the following resources to learn about HCP Packer: 113 114 - [HCP Packer documentation](/hcp/docs/packer) 115 - [HCP Packer tutorials](/packer/tutorials/hcp-get-started). 116 - [Log into the HCP portal](https://portal.cloud.hashicorp.com)