packer-cn/builder/ncloud/step_get_rootpassword.go

67 lines
1.8 KiB
Go
Raw Normal View History

2018-01-11 04:57:53 -05:00
package ncloud
import (
"context"
"fmt"
2018-01-11 04:57:53 -05:00
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
"github.com/hashicorp/packer/helper/multistep"
2018-01-11 04:57:53 -05:00
"github.com/hashicorp/packer/packer"
)
type StepGetRootPassword struct {
Conn *ncloud.Conn
GetRootPassword func(serverInstanceNo string, privateKey string) (string, error)
Say func(message string)
Error func(e error)
2018-12-21 03:53:52 -05:00
Config *Config
2018-01-11 04:57:53 -05:00
}
2018-12-21 03:53:52 -05:00
func NewStepGetRootPassword(conn *ncloud.Conn, ui packer.Ui, config *Config) *StepGetRootPassword {
2018-01-11 04:57:53 -05:00
var step = &StepGetRootPassword{
2018-12-21 03:53:52 -05:00
Conn: conn,
Say: func(message string) { ui.Say(message) },
Error: func(e error) { ui.Error(e.Error()) },
Config: config,
2018-01-11 04:57:53 -05:00
}
step.GetRootPassword = step.getRootPassword
return step
}
func (s *StepGetRootPassword) getRootPassword(serverInstanceNo string, privateKey string) (string, error) {
reqParams := new(ncloud.RequestGetRootPassword)
reqParams.ServerInstanceNo = serverInstanceNo
reqParams.PrivateKey = privateKey
rootPassword, err := s.Conn.GetRootPassword(reqParams)
if err != nil {
2018-01-11 20:12:47 -05:00
return "", err
2018-01-11 04:57:53 -05:00
}
2018-01-29 06:42:22 -05:00
s.Say(fmt.Sprintf("Root password is %s", rootPassword.RootPassword))
2018-01-11 04:57:53 -05:00
return rootPassword.RootPassword, nil
}
func (s *StepGetRootPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2018-01-11 04:57:53 -05:00
s.Say("Get Root Password")
serverInstanceNo := state.Get("InstanceNo").(string)
loginKey := state.Get("LoginKey").(*LoginKey)
rootPassword, err := s.GetRootPassword(serverInstanceNo, loginKey.PrivateKey)
2018-12-21 03:53:52 -05:00
if s.Config.Comm.Type == "ssh" {
s.Config.Comm.SSHPassword = rootPassword
} else if s.Config.Comm.Type == "winrm" {
s.Config.Comm.WinRMPassword = rootPassword
}
2018-01-11 04:57:53 -05:00
return processStepResult(err, s.Error, state)
}
func (*StepGetRootPassword) Cleanup(multistep.StateBag) {
}