File: step_get_rootpassword.go

package info (click to toggle)
packer 1.3.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,324 kB
  • sloc: python: 619; sh: 557; makefile: 111
file content (66 lines) | stat: -rw-r--r-- 1,808 bytes parent folder | download
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
66
package ncloud

import (
	"context"
	"fmt"

	ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
	"github.com/hashicorp/packer/helper/multistep"
	"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)
	Config          *Config
}

func NewStepGetRootPassword(conn *ncloud.Conn, ui packer.Ui, config *Config) *StepGetRootPassword {
	var step = &StepGetRootPassword{
		Conn:   conn,
		Say:    func(message string) { ui.Say(message) },
		Error:  func(e error) { ui.Error(e.Error()) },
		Config: config,
	}

	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 {
		return "", err
	}

	s.Say(fmt.Sprintf("Root password is %s", rootPassword.RootPassword))

	return rootPassword.RootPassword, nil
}

func (s *StepGetRootPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
	s.Say("Get Root Password")

	serverInstanceNo := state.Get("InstanceNo").(string)
	loginKey := state.Get("LoginKey").(*LoginKey)

	rootPassword, err := s.GetRootPassword(serverInstanceNo, loginKey.PrivateKey)

	if s.Config.Comm.Type == "ssh" {
		s.Config.Comm.SSHPassword = rootPassword
	} else if s.Config.Comm.Type == "winrm" {
		s.Config.Comm.WinRMPassword = rootPassword
	}

	return processStepResult(err, s.Error, state)
}

func (*StepGetRootPassword) Cleanup(multistep.StateBag) {
}