packer-cn/builder/ncloud/step_create_server_instance.go

281 lines
8.8 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"
2021-03-23 03:35:36 -04:00
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/vserver"
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"
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
)
2021-03-23 03:35:36 -04:00
const (
ServerInstanceStatusStopped = "NSTOP"
ServerInstanceStatusTerminated = "TERMT"
ServerInstanceStatusRunning = "RUN"
)
2018-01-11 04:57:53 -05:00
type StepCreateServerInstance struct {
2021-03-23 03:35:36 -04:00
Conn *NcloudAPIClient
CreateServerInstance func(loginKeyName string, feeSystemTypeCode string, state multistep.StateBag) (string, error)
GetServerInstance func() (string, string, error)
WaiterServerInstanceStatus func(conn *NcloudAPIClient, serverInstanceNo string, status string, timeout time.Duration) error
Say func(message string)
Error func(e error)
Config *Config
serverInstanceNo string
2018-01-11 04:57:53 -05:00
}
func NewStepCreateServerInstance(conn *NcloudAPIClient, ui packersdk.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,
}
2021-03-23 03:35:36 -04:00
if config.SupportVPC {
step.CreateServerInstance = step.createVpcServerInstance
step.WaiterServerInstanceStatus = waiterVpcServerInstanceStatus
step.GetServerInstance = step.getVpcServerInstance
} else {
step.CreateServerInstance = step.createClassicServerInstance
step.WaiterServerInstanceStatus = waiterClassicServerInstanceStatus
step.GetServerInstance = step.getClassicServerInstance
}
2018-01-11 04:57:53 -05:00
return step
}
2021-03-23 03:35:36 -04:00
func (s *StepCreateServerInstance) createClassicServerInstance(loginKeyName string, feeSystemTypeCode string, state multistep.StateBag) (string, error) {
var zoneNo = state.Get("zone_no").(string)
reqParams := &server.CreateServerInstancesRequest{
ServerProductCode: &s.Config.ServerProductCode,
MemberServerImageNo: &s.Config.MemberServerImageNo,
LoginKeyName: &loginKeyName,
ZoneNo: &zoneNo,
FeeSystemTypeCode: &feeSystemTypeCode,
}
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
}
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])
2021-03-23 03:35:36 -04:00
if err := s.WaiterServerInstanceStatus(s.Conn, s.serverInstanceNo, ServerInstanceStatusRunning, 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) createVpcServerInstance(loginKeyName string, feeSystemTypeCode string, state multistep.StateBag) (string, error) {
var initScriptNo string
var acgNo string
var err error
if v, ok := state.GetOk("init_script_no"); ok {
initScriptNo = v.(string)
}
if s.Config.AccessControlGroupConfigurationNo != "" {
acgNo = s.Config.AccessControlGroupConfigurationNo
} else {
acgNo, err = s.getDefaultAccessControlGroup(s.Config.VpcNo)
}
if err != nil {
return "", err
}
reqParams := &vserver.CreateServerInstancesRequest{
RegionCode: &s.Config.RegionCode,
ServerProductCode: &s.Config.ServerProductCode,
MemberServerImageInstanceNo: &s.Config.MemberServerImageNo,
LoginKeyName: &loginKeyName,
FeeSystemTypeCode: &feeSystemTypeCode,
InitScriptNo: &initScriptNo,
VpcNo: &s.Config.VpcNo,
SubnetNo: &s.Config.SubnetNo,
NetworkInterfaceList: []*vserver.NetworkInterfaceParameter{{
NetworkInterfaceOrder: ncloud.Int32(0),
AccessControlGroupNoList: []*string{ncloud.String(acgNo)}},
},
}
if s.Config.MemberServerImageNo == "" {
reqParams.ServerImageProductCode = &s.Config.ServerImageProductCode
}
serverInstanceList, err := s.Conn.vserver.V2Api.CreateServerInstances(reqParams)
if err != nil {
return "", err
}
s.serverInstanceNo = *serverInstanceList.ServerInstanceList[0].ServerInstanceNo
s.Say(fmt.Sprintf("Server Instance is creating. Server InstanceNo is %s", s.serverInstanceNo))
log.Println("Server Instance information : ", serverInstanceList.ServerInstanceList[0])
if err := s.WaiterServerInstanceStatus(s.Conn, s.serverInstanceNo, ServerInstanceStatusRunning, 30*time.Minute); err != nil {
2018-01-11 04:57:53 -05:00
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
}
2021-03-23 03:35:36 -04:00
func (s *StepCreateServerInstance) getDefaultAccessControlGroup(id string) (string, error) {
reqParams := &vserver.GetAccessControlGroupListRequest{
RegionCode: &s.Config.RegionCode,
VpcNo: ncloud.String(id),
}
resp, err := s.Conn.vserver.V2Api.GetAccessControlGroupList(reqParams)
if err != nil {
return "", err
}
if resp == nil || len(resp.AccessControlGroupList) == 0 {
return "", fmt.Errorf("no matching Access Control Group found")
}
for _, i := range resp.AccessControlGroupList {
if *i.IsDefault {
return *i.AccessControlGroupNo, nil
}
}
return "", fmt.Errorf("no matching default Access Control Group found")
}
func (s *StepCreateServerInstance) getClassicServerInstance() (string, string, error) {
reqParams := &server.GetServerInstanceListRequest{
ServerInstanceNoList: []*string{&s.serverInstanceNo},
}
resp, err := s.Conn.server.V2Api.GetServerInstanceList(reqParams)
if err != nil {
return "", "", err
}
if *resp.TotalRows > 0 {
return *resp.ServerInstanceList[0].ServerInstanceNo, *resp.ServerInstanceList[0].ServerInstanceStatus.Code, nil
} else {
return "", "", nil
}
}
func (s *StepCreateServerInstance) getVpcServerInstance() (string, string, error) {
reqParams := &vserver.GetServerInstanceDetailRequest{
ServerInstanceNo: &s.serverInstanceNo,
}
resp, err := s.Conn.vserver.V2Api.GetServerInstanceDetail(reqParams)
if err != nil {
return "", "", err
}
if *resp.TotalRows > 0 {
return *resp.ServerInstanceList[0].ServerInstanceNo, *resp.ServerInstanceList[0].ServerInstanceStatus.Code, nil
} else {
return "", "", 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")
2021-03-23 03:35:36 -04:00
var loginKey = state.Get("login_key").(*LoginKey)
2018-01-11 04:57:53 -05:00
feeSystemTypeCode := "MTRAT"
2021-03-23 03:35:36 -04:00
if _, ok := state.GetOk("fee_system_type_code"); ok {
feeSystemTypeCode = state.Get("fee_system_type_code").(string)
}
2021-03-23 03:35:36 -04:00
serverInstanceNo, err := s.CreateServerInstance(loginKey.KeyName, feeSystemTypeCode, state)
2018-01-11 04:57:53 -05:00
if err == nil {
2021-03-23 03:35:36 -04:00
state.Put("instance_no", 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)
2021-03-23 03:35:36 -04:00
ui := state.Get("ui").(packersdk.Ui)
2018-01-11 04:57:53 -05:00
if !cancelled && !halted {
return
}
if s.serverInstanceNo == "" {
return
}
2021-03-23 03:35:36 -04:00
_, status, err := s.GetServerInstance()
2018-01-11 04:57:53 -05:00
2021-03-23 03:35:36 -04:00
if err != nil || len(status) == 0 {
2018-01-11 04:57:53 -05:00
return
}
s.Say("Clean up Server Instance")
// stop server instance
2021-03-23 03:35:36 -04:00
if status != ServerInstanceStatusStopped && status != ServerInstanceStatusTerminated {
stepStopServerInstance := NewStepStopServerInstance(s.Conn, ui, s.Config)
err := stepStopServerInstance.StopServerInstance(s.serverInstanceNo)
if err != nil {
s.Error(err)
return
}
2018-01-11 04:57:53 -05:00
}
// terminate server instance
2021-03-23 03:35:36 -04:00
if status != ServerInstanceStatusTerminated {
stepStopServerInstance := NewStepTerminateServerInstance(s.Conn, ui, s.Config)
err := stepStopServerInstance.TerminateServerInstance(s.serverInstanceNo)
if err != nil {
s.Error(err)
return
}
2018-01-11 04:57:53 -05:00
}
}