Files

68 lines
2.0 KiB
Go
Raw Permalink Normal View History

//go:generate packer-sdc mapstructure-to-hcl2 -type Config
2019-10-14 16:43:59 +02:00
2014-03-24 11:20:32 +01:00
package null
import (
"fmt"
2015-06-13 14:07:43 -04:00
2020-12-17 13:29:25 -08:00
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
2014-03-24 11:20:32 +01:00
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
2015-06-13 17:42:43 -04:00
CommConfig communicator.Config `mapstructure:",squash"`
2014-03-24 11:20:32 +01:00
}
2019-12-17 11:25:56 +01:00
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
2014-03-24 11:20:32 +01:00
2020-01-15 14:27:55 -08:00
err := config.Decode(c, &config.DecodeOpts{
PluginType: BuilderId,
2015-06-13 17:42:43 -04:00
Interpolate: true,
InterpolateFilter: &interpolate.RenderFilter{},
2015-05-27 12:59:14 -07:00
}, raws...)
2014-03-24 11:20:32 +01:00
if err != nil {
2019-12-17 11:25:56 +01:00
return nil, err
2014-03-24 11:20:32 +01:00
}
var errs *packersdk.MultiError
2015-06-13 17:42:43 -04:00
if es := c.CommConfig.Prepare(nil); len(es) > 0 {
errs = packersdk.MultiErrorAppend(errs, es...)
2015-06-13 17:42:43 -04:00
}
2014-03-24 11:20:32 +01:00
if c.CommConfig.Type != "none" {
if c.CommConfig.Host() == "" {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("a Host must be specified, please reference your communicator documentation"))
}
2014-03-24 11:20:32 +01:00
if c.CommConfig.User() == "" {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("a Username must be specified, please reference your communicator documentation"))
}
2018-08-23 16:35:07 +02:00
if !c.CommConfig.SSHAgentAuth && c.CommConfig.Password() == "" && c.CommConfig.SSHPrivateKeyFile == "" {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("one authentication method must be specified, please reference your communicator documentation"))
}
if (c.CommConfig.SSHAgentAuth &&
2018-08-23 16:35:07 +02:00
(c.CommConfig.SSHPassword != "" || c.CommConfig.SSHPrivateKeyFile != "")) ||
(c.CommConfig.SSHPassword != "" && c.CommConfig.SSHPrivateKeyFile != "") {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("only one of ssh_agent_auth, ssh_password, and ssh_private_key_file must be specified"))
2014-03-24 11:20:32 +01:00
}
2014-03-24 11:20:32 +01:00
}
if errs != nil && len(errs.Errors) > 0 {
2019-12-17 11:25:56 +01:00
return nil, errs
2014-03-24 11:20:32 +01:00
}
2019-12-17 11:25:56 +01:00
return nil, nil
2014-03-24 11:20:32 +01:00
}