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