Reuse library function for SSH provisioner config

This commit is contained in:
Vladislav Rassokhin 2019-07-08 18:40:01 +03:00
parent 1c7ccb833c
commit a72ec1ae8d
5 changed files with 3 additions and 44 deletions

View File

@ -62,7 +62,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&communicator.StepConnect{
Config: &b.config.Comm,
Host: common.CommHost(b.config.Comm.SSHHost),
SSHConfig: common.SshConfig,
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&packerCommon.StepProvision{},
&common.StepShutdown{

View File

@ -1,12 +1,7 @@
package common
import (
"fmt"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"golang.org/x/crypto/ssh"
"io/ioutil"
)
func CommHost(host string) func(multistep.StateBag) (string, error) {
@ -18,37 +13,3 @@ func CommHost(host string) func(multistep.StateBag) (string, error) {
}
}
}
func SshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
comm := state.Get("comm").(*communicator.Config)
var auth []ssh.AuthMethod
if comm.SSHPrivateKeyFile != "" {
privateKey, err := ioutil.ReadFile(comm.SSHPrivateKeyFile)
if err != nil {
return nil, fmt.Errorf("Error loading configured private key file: %s", err)
}
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
} else {
auth = []ssh.AuthMethod{
ssh.Password(comm.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(comm.SSHPassword)),
}
}
clientConfig := &ssh.ClientConfig{
User: comm.SSHUsername,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
clientConfig.Auth = auth
return clientConfig, nil
}

1
go.mod
View File

@ -3,6 +3,5 @@ module github.com/jetbrains-infra/packer-builder-vsphere
require (
github.com/hashicorp/packer v1.4.2
github.com/vmware/govmomi v0.20.0
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/mobile v0.0.0-20190607214518-6fa95d984e88
)

3
go.sum
View File

@ -348,9 +348,8 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d h1:adrbvkTDn9rGnXg2IJDKozEpXXLZN89pdIA+Syt4/u0=
golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=

View File

@ -105,7 +105,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&communicator.StepConnect{
Config: &b.config.Comm,
Host: common.CommHost(b.config.Comm.SSHHost),
SSHConfig: common.SshConfig,
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&packerCommon.StepProvision{},
&common.StepShutdown{