-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
config.go
65 lines (51 loc) · 1.81 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package null
import (
"fmt"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
CommConfig communicator.Config `mapstructure:",squash"`
}
func NewConfig(raws ...interface{}) (*Config, []string, error) {
var c Config
err := config.Decode(&c, &config.DecodeOpts{
Interpolate: true,
InterpolateFilter: &interpolate.RenderFilter{},
}, raws...)
if err != nil {
return nil, nil, err
}
var errs *packer.MultiError
if es := c.CommConfig.Prepare(nil); len(es) > 0 {
errs = packer.MultiErrorAppend(errs, es...)
}
if c.CommConfig.Type != "none" {
if c.CommConfig.Host() == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("a Host must be specified, please reference your communicator documentation"))
}
if c.CommConfig.User() == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("a Username must be specified, please reference your communicator documentation"))
}
if !c.CommConfig.SSHAgentAuth && c.CommConfig.Password() == "" && c.CommConfig.SSHPrivateKey == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("one authentication method must be specified, please reference your communicator documentation"))
}
if (c.CommConfig.SSHAgentAuth &&
(c.CommConfig.SSHPassword != "" || c.CommConfig.SSHPrivateKey != "")) ||
(c.CommConfig.SSHPassword != "" && c.CommConfig.SSHPrivateKey != "") {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("only one of ssh_agent_auth, ssh_password, and ssh_private_key_file must be specified"))
}
}
if errs != nil && len(errs.Errors) > 0 {
return nil, nil, errs
}
return &c, nil, nil
}