github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/step_create_server_instance.go (about)

     1  package ncloud
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"log"
     9  	"time"
    10  
    11  	ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
    12  	"github.com/hashicorp/packer/helper/multistep"
    13  	"github.com/hashicorp/packer/packer"
    14  )
    15  
    16  type StepCreateServerInstance struct {
    17  	Conn                               *ncloud.Conn
    18  	CreateServerInstance               func(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error)
    19  	CheckServerInstanceStatusIsRunning func(serverInstanceNo string) error
    20  	Say                                func(message string)
    21  	Error                              func(e error)
    22  	Config                             *Config
    23  	serverInstanceNo                   string
    24  }
    25  
    26  func NewStepCreateServerInstance(conn *ncloud.Conn, ui packer.Ui, config *Config) *StepCreateServerInstance {
    27  	var step = &StepCreateServerInstance{
    28  		Conn:   conn,
    29  		Say:    func(message string) { ui.Say(message) },
    30  		Error:  func(e error) { ui.Error(e.Error()) },
    31  		Config: config,
    32  	}
    33  
    34  	step.CreateServerInstance = step.createServerInstance
    35  
    36  	return step
    37  }
    38  
    39  func (s *StepCreateServerInstance) createServerInstance(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error) {
    40  	reqParams := new(ncloud.RequestCreateServerInstance)
    41  	reqParams.ServerProductCode = s.Config.ServerProductCode
    42  	reqParams.MemberServerImageNo = s.Config.MemberServerImageNo
    43  	if s.Config.MemberServerImageNo == "" {
    44  		reqParams.ServerImageProductCode = s.Config.ServerImageProductCode
    45  	}
    46  	reqParams.LoginKeyName = loginKeyName
    47  	reqParams.ZoneNo = zoneNo
    48  	reqParams.FeeSystemTypeCode = feeSystemTypeCode
    49  
    50  	if s.Config.UserData != "" {
    51  		reqParams.UserData = s.Config.UserData
    52  	}
    53  
    54  	if s.Config.UserDataFile != "" {
    55  		contents, err := ioutil.ReadFile(s.Config.UserDataFile)
    56  		if err != nil {
    57  			return "", fmt.Errorf("Problem reading user data file: %s", err)
    58  		}
    59  
    60  		reqParams.UserData = string(contents)
    61  	}
    62  
    63  	if s.Config.AccessControlGroupConfigurationNo != "" {
    64  		reqParams.AccessControlGroupConfigurationNoList = []string{s.Config.AccessControlGroupConfigurationNo}
    65  	}
    66  
    67  	serverInstanceList, err := s.Conn.CreateServerInstances(reqParams)
    68  	if err != nil {
    69  		return "", err
    70  	}
    71  
    72  	s.serverInstanceNo = serverInstanceList.ServerInstanceList[0].ServerInstanceNo
    73  	s.Say(fmt.Sprintf("Server Instance is creating. Server InstanceNo is %s", s.serverInstanceNo))
    74  	log.Println("Server Instance information : ", serverInstanceList.ServerInstanceList[0])
    75  
    76  	if err := waiterServerInstanceStatus(s.Conn, s.serverInstanceNo, "RUN", 30*time.Minute); err != nil {
    77  		return "", errors.New("TIMEOUT : server instance status is not running")
    78  	}
    79  
    80  	s.Say(fmt.Sprintf("Server Instance is created. Server InstanceNo is %s", s.serverInstanceNo))
    81  
    82  	return s.serverInstanceNo, nil
    83  }
    84  
    85  func (s *StepCreateServerInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    86  	s.Say("Create Server Instance")
    87  
    88  	var loginKey = state.Get("LoginKey").(*LoginKey)
    89  	var zoneNo = state.Get("ZoneNo").(string)
    90  
    91  	feeSystemTypeCode := "MTRAT"
    92  	if _, ok := state.GetOk("FeeSystemTypeCode"); ok {
    93  		feeSystemTypeCode = state.Get("FeeSystemTypeCode").(string)
    94  	}
    95  
    96  	serverInstanceNo, err := s.CreateServerInstance(loginKey.KeyName, zoneNo, feeSystemTypeCode)
    97  	if err == nil {
    98  		state.Put("InstanceNo", serverInstanceNo)
    99  	}
   100  
   101  	return processStepResult(err, s.Error, state)
   102  }
   103  
   104  func (s *StepCreateServerInstance) Cleanup(state multistep.StateBag) {
   105  	_, cancelled := state.GetOk(multistep.StateCancelled)
   106  	_, halted := state.GetOk(multistep.StateHalted)
   107  
   108  	if !cancelled && !halted {
   109  		return
   110  	}
   111  
   112  	if s.serverInstanceNo == "" {
   113  		return
   114  	}
   115  
   116  	reqParams := new(ncloud.RequestGetServerInstanceList)
   117  	reqParams.ServerInstanceNoList = []string{s.serverInstanceNo}
   118  
   119  	serverInstanceList, err := s.Conn.GetServerInstanceList(reqParams)
   120  	if err != nil || serverInstanceList.TotalRows == 0 {
   121  		return
   122  	}
   123  
   124  	s.Say("Clean up Server Instance")
   125  
   126  	serverInstance := serverInstanceList.ServerInstanceList[0]
   127  	// stop server instance
   128  	if serverInstance.ServerInstanceStatus.Code != "NSTOP" && serverInstance.ServerInstanceStatus.Code != "TERMT" {
   129  		reqParams := new(ncloud.RequestStopServerInstances)
   130  		reqParams.ServerInstanceNoList = []string{s.serverInstanceNo}
   131  
   132  		log.Println("Stop Server Instance")
   133  		s.Conn.StopServerInstances(reqParams)
   134  		waiterServerInstanceStatus(s.Conn, s.serverInstanceNo, "NSTOP", time.Minute)
   135  	}
   136  
   137  	// terminate server instance
   138  	if serverInstance.ServerInstanceStatus.Code != "TERMT" {
   139  		reqParams := new(ncloud.RequestTerminateServerInstances)
   140  		reqParams.ServerInstanceNoList = []string{s.serverInstanceNo}
   141  
   142  		log.Println("Terminate Server Instance")
   143  		s.Conn.TerminateServerInstances(reqParams)
   144  	}
   145  }