packer-cn/packer/cmd_post_processor.go
Adrien Delorme ed091163be
HCL2 Parse packer.required_plugins block + packer init (#10304)
This adds the new `required_plugins` block to be nested under the packer block.

Example:
```hcl
packer {
  required_plugins {
    aws = {
      version = ">= 2.7.0"
      source = "azr/aws"
    }
    azure = ">= 2.7.0"
  }
}
```

For example on darwin_amd64 Packer will install those under :
* "${PACKER_HOME_DIR}/plugin/github.com/azr/amazon/packer-plugin-amazon_2.7.0_x5.0_darwin_amd64"
* "${PACKER_HOME_DIR}/plugin/github.com/hashicorp/azure/packer-plugin-azure_2.7.0_x5.0_darwin_amd64_x5"

+ docs
+ tests
2021-02-02 18:05:04 +01:00

50 lines
949 B
Go

package packer
import (
"context"
"log"
"github.com/hashicorp/hcl/v2/hcldec"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
type cmdPostProcessor struct {
p packersdk.PostProcessor
client *PluginClient
}
func (b *cmdPostProcessor) ConfigSpec() hcldec.ObjectSpec {
defer func() {
r := recover()
b.checkExit(r, nil)
}()
return b.p.ConfigSpec()
}
func (c *cmdPostProcessor) Configure(config ...interface{}) error {
defer func() {
r := recover()
c.checkExit(r, nil)
}()
return c.p.Configure(config...)
}
func (c *cmdPostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, a packersdk.Artifact) (packersdk.Artifact, bool, bool, error) {
defer func() {
r := recover()
c.checkExit(r, nil)
}()
return c.p.PostProcess(ctx, ui, a)
}
func (c *cmdPostProcessor) checkExit(p interface{}, cb func()) {
if c.client.Exited() && cb != nil {
cb()
} else if p != nil && !Killed {
log.Panic(p)
}
}