packer-cn/builder/ncloud/step_create_server_instance.go

150 lines
4.9 KiB
Go
Raw Normal View History

2018-01-11 04:57:53 -05:00
package ncloud
import (
"context"
2018-01-11 04:57:53 -05:00
"errors"
"fmt"
2018-01-29 04:09:14 -05:00
"io/ioutil"
2018-01-11 04:57:53 -05:00
"log"
"time"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
"github.com/hashicorp/packer/helper/multistep"
2018-01-11 04:57:53 -05:00
"github.com/hashicorp/packer/packer"
)
type StepCreateServerInstance struct {
Conn *NcloudAPIClient
CreateServerInstance func(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error)
2018-01-11 04:57:53 -05:00
CheckServerInstanceStatusIsRunning func(serverInstanceNo string) error
Say func(message string)
Error func(e error)
Config *Config
serverInstanceNo string
}
func NewStepCreateServerInstance(conn *NcloudAPIClient, ui packer.Ui, config *Config) *StepCreateServerInstance {
2018-01-11 04:57:53 -05:00
var step = &StepCreateServerInstance{
Conn: conn,
Say: func(message string) { ui.Say(message) },
Error: func(e error) { ui.Error(e.Error()) },
Config: config,
}
step.CreateServerInstance = step.createServerInstance
return step
}
func (s *StepCreateServerInstance) createServerInstance(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error) {
reqParams := new(server.CreateServerInstancesRequest)
reqParams.ServerProductCode = &s.Config.ServerProductCode
reqParams.MemberServerImageNo = &s.Config.MemberServerImageNo
2018-01-11 04:57:53 -05:00
if s.Config.MemberServerImageNo == "" {
reqParams.ServerImageProductCode = &s.Config.ServerImageProductCode
2018-01-11 04:57:53 -05:00
}
reqParams.LoginKeyName = &loginKeyName
reqParams.ZoneNo = &zoneNo
reqParams.FeeSystemTypeCode = &feeSystemTypeCode
2018-01-11 04:57:53 -05:00
if s.Config.UserData != "" {
reqParams.UserData = &s.Config.UserData
2018-01-11 04:57:53 -05:00
}
2018-01-29 04:09:14 -05:00
if s.Config.UserDataFile != "" {
contents, err := ioutil.ReadFile(s.Config.UserDataFile)
if err != nil {
return "", fmt.Errorf("Problem reading user data file: %s", err)
}
reqParams.UserData = ncloud.String(string(contents))
2018-01-29 04:09:14 -05:00
}
2018-01-11 04:57:53 -05:00
if s.Config.AccessControlGroupConfigurationNo != "" {
reqParams.AccessControlGroupConfigurationNoList = []*string{&s.Config.AccessControlGroupConfigurationNo}
2018-01-11 04:57:53 -05:00
}
serverInstanceList, err := s.Conn.server.V2Api.CreateServerInstances(reqParams)
2018-01-11 04:57:53 -05:00
if err != nil {
2018-01-11 20:15:40 -05:00
return "", err
2018-01-11 04:57:53 -05:00
}
s.serverInstanceNo = *serverInstanceList.ServerInstanceList[0].ServerInstanceNo
2018-01-11 04:57:53 -05:00
s.Say(fmt.Sprintf("Server Instance is creating. Server InstanceNo is %s", s.serverInstanceNo))
log.Println("Server Instance information : ", serverInstanceList.ServerInstanceList[0])
if err := waiterServerInstanceStatus(s.Conn, s.serverInstanceNo, "RUN", 30*time.Minute); err != nil {
return "", errors.New("TIMEOUT : server instance status is not running")
}
s.Say(fmt.Sprintf("Server Instance is created. Server InstanceNo is %s", s.serverInstanceNo))
return s.serverInstanceNo, nil
}
func (s *StepCreateServerInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2018-01-11 04:57:53 -05:00
s.Say("Create Server Instance")
var loginKey = state.Get("LoginKey").(*LoginKey)
var zoneNo = state.Get("ZoneNo").(string)
feeSystemTypeCode := "MTRAT"
if _, ok := state.GetOk("FeeSystemTypeCode"); ok {
feeSystemTypeCode = state.Get("FeeSystemTypeCode").(string)
}
serverInstanceNo, err := s.CreateServerInstance(loginKey.KeyName, zoneNo, feeSystemTypeCode)
2018-01-11 04:57:53 -05:00
if err == nil {
state.Put("InstanceNo", serverInstanceNo)
// instance_id is the generic term used so that users can have access to the
// instance id inside of the provisioners, used in step_provision.
state.Put("instance_id", serverInstanceNo)
2018-01-11 04:57:53 -05:00
}
return processStepResult(err, s.Error, state)
}
func (s *StepCreateServerInstance) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
if s.serverInstanceNo == "" {
return
}
reqParams := new(server.GetServerInstanceListRequest)
reqParams.ServerInstanceNoList = []*string{&s.serverInstanceNo}
2018-01-11 04:57:53 -05:00
serverInstanceList, err := s.Conn.server.V2Api.GetServerInstanceList(reqParams)
if err != nil || *serverInstanceList.TotalRows == 0 {
2018-01-11 04:57:53 -05:00
return
}
s.Say("Clean up Server Instance")
serverInstance := serverInstanceList.ServerInstanceList[0]
// stop server instance
if *serverInstance.ServerInstanceStatus.Code != "NSTOP" && *serverInstance.ServerInstanceStatus.Code != "TERMT" {
reqParams := new(server.StopServerInstancesRequest)
reqParams.ServerInstanceNoList = []*string{&s.serverInstanceNo}
2018-01-11 04:57:53 -05:00
log.Println("Stop Server Instance")
s.Conn.server.V2Api.StopServerInstances(reqParams)
2018-01-11 04:57:53 -05:00
waiterServerInstanceStatus(s.Conn, s.serverInstanceNo, "NSTOP", time.Minute)
}
// terminate server instance
if *serverInstance.ServerInstanceStatus.Code != "TERMT" {
reqParams := new(server.TerminateServerInstancesRequest)
reqParams.ServerInstanceNoList = []*string{&s.serverInstanceNo}
2018-01-11 04:57:53 -05:00
log.Println("Terminate Server Instance")
s.Conn.server.V2Api.TerminateServerInstances(reqParams)
2018-01-11 04:57:53 -05:00
}
}