2018-01-11 04:57:53 -05:00
|
|
|
package ncloud
|
|
|
|
|
|
|
|
import (
|
2018-01-29 08:41:22 -05:00
|
|
|
"context"
|
2018-01-11 04:57:53 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
|
2018-01-29 08:41:22 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-11 04:57:53 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepCreateServerImage struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
|
|
|
CreateServerImage func(serverInstanceNo string) (*server.MemberServerImage, error)
|
2018-01-11 04:57:53 -05:00
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
Config *Config
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func NewStepCreateServerImage(conn *NcloudAPIClient, ui packer.Ui, config *Config) *StepCreateServerImage {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepCreateServerImage{
|
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
step.CreateServerImage = step.createServerImage
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func (s *StepCreateServerImage) createServerImage(serverInstanceNo string) (*server.MemberServerImage, error) {
|
2018-01-29 05:08:53 -05:00
|
|
|
// Can't create server image when status of server instance is stopping (not stopped)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err := waiterServerInstanceStatus(s.Conn, serverInstanceNo, "NSTOP", 1*time.Minute); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.CreateMemberServerImageRequest)
|
|
|
|
reqParams.MemberServerImageName = &s.Config.ServerImageName
|
|
|
|
reqParams.MemberServerImageDescription = &s.Config.ServerImageDescription
|
|
|
|
reqParams.ServerInstanceNo = &serverInstanceNo
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
memberServerImageList, err := s.Conn.server.V2Api.CreateMemberServerImage(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
2018-01-11 20:15:40 -05:00
|
|
|
return nil, err
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
serverImage := memberServerImageList.MemberServerImageList[0]
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
s.Say(fmt.Sprintf("Server Image[%s:%s] is creating...", *serverImage.MemberServerImageName, *serverImage.MemberServerImageNo))
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
if err := waiterMemberServerImageStatus(s.Conn, *serverImage.MemberServerImageNo, "CREAT", 6*time.Hour); err != nil {
|
2018-01-11 04:57:53 -05:00
|
|
|
return nil, errors.New("TIMEOUT : Server Image is not created")
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
s.Say(fmt.Sprintf("Server Image[%s:%s] is created", *serverImage.MemberServerImageName, *serverImage.MemberServerImageNo))
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
return serverImage, nil
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateServerImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-11 04:57:53 -05:00
|
|
|
s.Say("Create Server Image")
|
|
|
|
|
|
|
|
serverInstanceNo := state.Get("InstanceNo").(string)
|
|
|
|
|
|
|
|
serverImage, err := s.CreateServerImage(serverInstanceNo)
|
|
|
|
if err == nil {
|
|
|
|
state.Put("memberServerImage", serverImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepCreateServerImage) Cleanup(multistep.StateBag) {
|
|
|
|
}
|