2018-01-11 04:57:53 -05:00
|
|
|
package ncloud
|
|
|
|
|
|
|
|
import (
|
2018-01-29 08:41:22 -05:00
|
|
|
"context"
|
2018-01-29 07:47:58 -05:00
|
|
|
"fmt"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2018-01-11 04:57:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepGetRootPassword struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
2018-01-11 04:57:53 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func NewStepGetRootPassword(conn *NcloudAPIClient, ui packersdk.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) {
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.GetRootPasswordRequest)
|
|
|
|
reqParams.ServerInstanceNo = &serverInstanceNo
|
|
|
|
reqParams.PrivateKey = &privateKey
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
rootPassword, err := s.Conn.server.V2Api.GetRootPassword(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
2018-01-11 20:12:47 -05:00
|
|
|
return "", err
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
s.Say(fmt.Sprintf("Root password is %s", *rootPassword.RootPassword))
|
2018-01-29 06:42:22 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
return *rootPassword.RootPassword, nil
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepGetRootPassword) Run(ctx 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) {
|
|
|
|
}
|