Skip to content

Testing plugin section in Extending - Packer docs is not valid anymore #13280

Closed
@lzap

Description

@lzap

I believe this is no longer correct:

https://developer.hashicorp.com/packer/docs/plugins/creation#testing-plugins

I did research the code logic around loading plugins and it seems that the statement in docs that one can simply load plugins from a current directory by overriding PACKER_PLUGIN_PATH is not correct.

If you want to load a custom build of a plugin the following conditions must match:

  • Plugin name must now contain the API version, OS architecture and name and SHA sum.
  • There must be a SHASUM file next to the plugin file with a suffix
  • Must not be in a root directory that was passed via PACKER_PLUGIN_PATH

Here is an example of a Makefile that builds a plugin and prepares the environment in a way that one can execute it via make start:

NAME=image-builder
ROOT_DIR:=$(dir $(realpath $(lastword $(MAKEFILE_LIST))))
BUILD_DIR=build
PLUGIN_DIR=${BUILD_DIR}/plugins
BINARY=packer-plugin-${NAME}_v0.0.0_x5.0_linux_amd64
# https://github.com/hashicorp/packer-plugin-sdk/issues/187
HASHICORP_PACKER_PLUGIN_SDK_VERSION?="v0.5.2"
PLUGIN_FQN=$(shell grep -E '^module' <go.mod | sed -E 's/module \s*//')
PLUGIN_PATH=./cmd/plugin

.PHONY: build
build:
	@mkdir -p ${PLUGIN_DIR}
	@go build -ldflags="-X '${PLUGIN_FQN}/main.Version=$(shell git describe --tags --abbrev=0)'" -o ${PLUGIN_DIR}/${BINARY} ${PLUGIN_PATH}
	@sha256sum < ${PLUGIN_DIR}/${BINARY} > ${PLUGIN_DIR}/${BINARY}_SHA256SUM

.PHONY: clean
clean:
	@rm -rf ${BUILD_DIR}

.PHONY: start
start: build
	PACKER_PLUGIN_PATH=${ROOT_DIR}${BUILD_DIR} PACKER_LOG=1 PACKER_LOG_PATH=packer.log packer build empty.pkr.hcl

The important trick is to create subdirectory ./build/plugins and build the executable there into correct form with underscores and then creating SHA256SUM suffixed file as well. Only then packer appears to load it.

There was a thread about this bug but the strict rules did not allowed me to post more than 2 links in a single post, while I was trying to figure it out it blocked the whole thread and marked as spam: https://discuss.hashicorp.com/t/plugin-not-being-loaded-via-packer-plugin-path-from-root/72865

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions