Merge pull request #4 from YuSungDuk/support_ncloud

Support ncloud
This commit is contained in:
Yu SungDuk 2018-01-29 21:49:21 +09:00 committed by GitHub
commit 0cc04889cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 56 additions and 279 deletions

View File

@ -56,8 +56,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
NewStepCreateServerImage(conn, ui, b.config),
NewStepDeleteBlockStorageInstance(conn, ui, b.config),
NewStepTerminateServerInstance(conn, ui),
NewStepDeleteLoginKey(conn, ui),
NewStepDeletePublicIPInstance(conn, ui),
}
} else if b.config.Comm.Type == "Windows" {
steps = []multistep.Step{
@ -84,8 +82,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
NewStepCreateServerImage(conn, ui, b.config),
NewStepDeleteBlockStorageInstance(conn, ui, b.config),
NewStepTerminateServerInstance(conn, ui),
NewStepDeleteLoginKey(conn, ui),
NewStepDeletePublicIPInstance(conn, ui),
}
}

View File

@ -2,6 +2,8 @@ package ncloud
import (
"errors"
"fmt"
"os"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
@ -22,11 +24,10 @@ type Config struct {
ServerImageName string `mapstructure:"server_image_name"`
ServerImageDescription string `mapstructure:"server_image_description"`
UserData string `mapstructure:"user_data"`
UserDataFile string `mapstructure:"user_data_file"`
UserDataFile string `mapstructure:"user_data_file"`
BlockStorageSize int `mapstructure:"block_storage_size"`
Region string `mapstructure:"region"`
AccessControlGroupConfigurationNo string `mapstructure:"access_control_group_configuration_no"`
FeeSystemTypeCode string `mapstructure:"-"`
Comm communicator.Config `mapstructure:",squash"`
ctx *interpolate.Context
@ -92,6 +93,14 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
}
}
if c.UserData != "" && c.UserDataFile != "" {
errs = packer.MultiErrorAppend(errs, errors.New("Only one of user_data or user_data_file can be specified."))
} else if c.UserDataFile != "" {
if _, err := os.Stat(c.UserDataFile); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
}
}
if c.UserData != "" && len(c.UserData) > 21847 {
errs = packer.MultiErrorAppend(errs, errors.New("If user_data field is set, length of UserData should be max 21847"))
}
@ -100,8 +109,6 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs = packer.MultiErrorAppend(errs, errors.New("If Communicator is winrm, access_control_group_configuration_no is required"))
}
c.FeeSystemTypeCode = "MTRAT"
if errs != nil && len(errs.Errors) > 0 {
return nil, warnings, errs
}

View File

@ -57,13 +57,6 @@ func (s *StepCreateLoginKey) Run(state multistep.StateBag) multistep.StepAction
}
func (s *StepCreateLoginKey) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
if loginKey, ok := state.GetOk("LoginKey"); ok {
s.Say("Clean up login key")
s.Conn.DeleteLoginKey(loginKey.(*LoginKey).KeyName)

View File

@ -105,13 +105,6 @@ func (s *StepCreatePublicIPInstance) Run(state multistep.StateBag) multistep.Ste
}
func (s *StepCreatePublicIPInstance) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
publicIPInstance, ok := state.GetOk("PublicIPInstance")
if !ok {
return
@ -148,6 +141,11 @@ func (s *StepCreatePublicIPInstance) waitPublicIPInstanceStatus(publicIPInstance
return
}
if resp.TotalRows == 0 {
c1 <- nil
return
}
instance := resp.PublicIPInstanceList[0]
if instance.PublicIPInstanceStatus.Code == status && instance.PublicIPInstanceOperation.Code == "NULL" {
c1 <- nil

View File

@ -32,7 +32,7 @@ func NewStepCreateServerImage(conn *ncloud.Conn, ui packer.Ui, config *Config) *
}
func (s *StepCreateServerImage) createServerImage(serverInstanceNo string) (*ncloud.ServerImage, error) {
// 서버 인스턴스 상태가 정지 중일 경우에는 서버 이미지 생성할 수 없음.
// Can't create server image when status of server instance is stopping (not stopped)
if err := waiterServerInstanceStatus(s.Conn, serverInstanceNo, "NSTOP", 1*time.Minute); err != nil {
return nil, err
}

View File

@ -3,6 +3,7 @@ package ncloud
import (
"errors"
"fmt"
"io/ioutil"
"log"
"time"
@ -13,7 +14,7 @@ import (
type StepCreateServerInstance struct {
Conn *ncloud.Conn
CreateServerInstance func(loginKeyName string, zoneNo string) (string, error)
CreateServerInstance func(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error)
CheckServerInstanceStatusIsRunning func(serverInstanceNo string) error
Say func(message string)
Error func(e error)
@ -34,7 +35,7 @@ func NewStepCreateServerInstance(conn *ncloud.Conn, ui packer.Ui, config *Config
return step
}
func (s *StepCreateServerInstance) createServerInstance(loginKeyName string, zoneNo string) (string, error) {
func (s *StepCreateServerInstance) createServerInstance(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error) {
reqParams := new(ncloud.RequestCreateServerInstance)
reqParams.ServerProductCode = s.Config.ServerProductCode
reqParams.MemberServerImageNo = s.Config.MemberServerImageNo
@ -43,12 +44,21 @@ func (s *StepCreateServerInstance) createServerInstance(loginKeyName string, zon
}
reqParams.LoginKeyName = loginKeyName
reqParams.ZoneNo = zoneNo
reqParams.FeeSystemTypeCode = s.Config.FeeSystemTypeCode
reqParams.FeeSystemTypeCode = feeSystemTypeCode
if s.Config.UserData != "" {
reqParams.UserData = s.Config.UserData
}
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 = string(contents)
}
if s.Config.AccessControlGroupConfigurationNo != "" {
reqParams.AccessControlGroupConfigurationNoList = []string{s.Config.AccessControlGroupConfigurationNo}
}
@ -77,7 +87,12 @@ func (s *StepCreateServerInstance) Run(state multistep.StateBag) multistep.StepA
var loginKey = state.Get("LoginKey").(*LoginKey)
var zoneNo = state.Get("ZoneNo").(string)
serverInstanceNo, err := s.CreateServerInstance(loginKey.KeyName, zoneNo)
feeSystemTypeCode := "MTRAT"
if _, ok := state.GetOk("FeeSystemTypeCode"); ok {
feeSystemTypeCode = state.Get("FeeSystemTypeCode").(string)
}
serverInstanceNo, err := s.CreateServerInstance(loginKey.KeyName, zoneNo, feeSystemTypeCode)
if err == nil {
state.Put("InstanceNo", serverInstanceNo)
}

View File

@ -8,7 +8,7 @@ import (
func TestStepCreateServerInstanceShouldFailIfOperationCreateFails(t *testing.T) {
var testSubject = &StepCreateServerInstance{
CreateServerInstance: func(loginKeyName string, zoneNo string) (string, error) {
CreateServerInstance: func(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error) {
return "", fmt.Errorf("!! Unit Test FAIL !!")
},
Say: func(message string) {},
@ -30,7 +30,7 @@ func TestStepCreateServerInstanceShouldFailIfOperationCreateFails(t *testing.T)
func TestStepCreateServerInstanceShouldPassIfOperationCreatePasses(t *testing.T) {
var testSubject = &StepCreateServerInstance{
CreateServerInstance: func(loginKeyName string, zoneNo string) (string, error) { return "", nil },
CreateServerInstance: func(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error) { return "", nil },
Say: func(message string) {},
Error: func(e error) {},
}

View File

@ -1,51 +0,0 @@
package ncloud
import (
"fmt"
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
type StepDeleteLoginKey struct {
Conn *ncloud.Conn
DeleteLoginKey func(keyName string) error
Say func(message string)
Error func(e error)
}
func NewStepDeleteLoginKey(conn *ncloud.Conn, ui packer.Ui) *StepDeleteLoginKey {
var step = &StepDeleteLoginKey{
Conn: conn,
Say: func(message string) { ui.Say(message) },
Error: func(e error) { ui.Error(e.Error()) },
}
step.DeleteLoginKey = step.deleteLoginKey
return step
}
func (s *StepDeleteLoginKey) deleteLoginKey(keyName string) error {
_, err := s.Conn.DeleteLoginKey(keyName)
if err != nil {
return err
}
return nil
}
func (s *StepDeleteLoginKey) Run(state multistep.StateBag) multistep.StepAction {
var loginKey = state.Get("LoginKey").(*LoginKey)
err := s.DeleteLoginKey(loginKey.KeyName)
if err == nil {
s.Say(fmt.Sprintf("Login Key[%s] is deleted", loginKey.KeyName))
}
return processStepResult(err, s.Error, state)
}
func (*StepDeleteLoginKey) Cleanup(multistep.StateBag) {
}

View File

@ -1,55 +0,0 @@
package ncloud
import (
"fmt"
"github.com/mitchellh/multistep"
"testing"
)
func TestStepDeleteLoginKeyShouldFailIfOperationDeleteLoginKeyFails(t *testing.T) {
var testSubject = &StepDeleteLoginKey{
DeleteLoginKey: func(keyName string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
Say: func(message string) {},
Error: func(e error) {},
}
stateBag := DeleteTestStateBagStepDeleteLoginKey()
var result = testSubject.Run(stateBag)
if result != multistep.ActionHalt {
t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
}
if _, ok := stateBag.GetOk("Error"); ok == false {
t.Fatal("Expected the step to set stateBag['Error'], but it was not.")
}
}
func TestStepDeleteLoginKeyShouldPassIfOperationDeleteLoginKeyPasses(t *testing.T) {
var testSubject = &StepDeleteLoginKey{
DeleteLoginKey: func(keyName string) error { return nil },
Say: func(message string) {},
Error: func(e error) {},
}
stateBag := DeleteTestStateBagStepDeleteLoginKey()
var result = testSubject.Run(stateBag)
if result != multistep.ActionContinue {
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
}
if _, ok := stateBag.GetOk("Error"); ok == true {
t.Fatalf("Expected the step to not set stateBag['Error'], but it was.")
}
}
func DeleteTestStateBagStepDeleteLoginKey() multistep.StateBag {
stateBag := new(multistep.BasicStateBag)
stateBag.Put("LoginKey", &LoginKey{"a", "b"})
return stateBag
}

View File

@ -1,78 +0,0 @@
package ncloud
import (
"errors"
"fmt"
"log"
"time"
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
type StepDeletePublicIPInstance struct {
Conn *ncloud.Conn
DeletePublicIPInstance func(publicIPInstanceNo string) error
Say func(message string)
Error func(e error)
}
func NewStepDeletePublicIPInstance(conn *ncloud.Conn, ui packer.Ui) *StepDeletePublicIPInstance {
var step = &StepDeletePublicIPInstance{
Conn: conn,
Say: func(message string) { ui.Say(message) },
Error: func(e error) { ui.Error(e.Error()) },
}
step.DeletePublicIPInstance = step.deletePublicIPInstance
return step
}
func (s *StepDeletePublicIPInstance) deletePublicIPInstance(publicIPInstanceNo string) error {
reqParams := new(ncloud.RequestDeletePublicIPInstances)
reqParams.PublicIPInstanceNoList = []string{publicIPInstanceNo}
c1 := make(chan error, 1)
go func() {
for {
resp, err := s.Conn.DeletePublicIPInstances(reqParams)
if err != nil && (resp.ReturnCode == 24073 || resp.ReturnCode == 25032) {
// error code : 24073 : Unable to destroy the server since a public IP is associated with the server. First, please disassociate a public IP from the server.
// error code : 25032 : You may not delete sk since (other) user is changing the target official IP settings.
log.Println(resp.ReturnCode, resp.ReturnMessage)
} else if err != nil {
c1 <- fmt.Errorf("error code: %d, error message: %s", resp.ReturnCode, resp.ReturnMessage)
return
} else if err == nil {
s.Say(fmt.Sprintf("Public IP Instance [%s] is deleted.", publicIPInstanceNo))
c1 <- nil
return
}
time.Sleep(time.Second * 5)
}
}()
select {
case res := <-c1:
return res
case <-time.After(time.Second * 60):
return errors.New("TIMEOUT : Can't delete server instance")
}
}
func (s *StepDeletePublicIPInstance) Run(state multistep.StateBag) multistep.StepAction {
s.Say("Delete Public IP Instance")
publicIPInstance := state.Get("PublicIPInstance").(*ncloud.PublicIPInstance)
err := s.DeletePublicIPInstance(publicIPInstance.PublicIPInstanceNo)
return processStepResult(err, s.Error, state)
}
func (*StepDeletePublicIPInstance) Cleanup(multistep.StateBag) {
}

View File

@ -1,57 +0,0 @@
package ncloud
import (
"fmt"
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
"testing"
"github.com/mitchellh/multistep"
)
func TestStepDeletePublicIPInstanceShouldFailIfOperationDeletePublicIPInstanceFails(t *testing.T) {
var testSubject = &StepDeletePublicIPInstance{
DeletePublicIPInstance: func(publicIPInstanceNo string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
Say: func(message string) {},
Error: func(e error) {},
}
stateBag := createTestStateBagStepDeletePublicIPInstance()
var result = testSubject.Run(stateBag)
if result != multistep.ActionHalt {
t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
}
if _, ok := stateBag.GetOk("Error"); ok == false {
t.Fatal("Expected the step to set stateBag['Error'], but it was not.")
}
}
func TestStepDeletePublicIPInstanceShouldPassIfOperationDeletePublicIPInstancePasses(t *testing.T) {
var testSubject = &StepDeletePublicIPInstance{
DeletePublicIPInstance: func(publicIPInstanceNo string) error { return nil },
Say: func(message string) {},
Error: func(e error) {},
}
stateBag := createTestStateBagStepDeletePublicIPInstance()
var result = testSubject.Run(stateBag)
if result != multistep.ActionContinue {
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
}
if _, ok := stateBag.GetOk("Error"); ok == true {
t.Fatalf("Expected the step to not set stateBag['Error'], but it was.")
}
}
func createTestStateBagStepDeletePublicIPInstance() multistep.StateBag {
stateBag := new(multistep.BasicStateBag)
stateBag.Put("PublicIPInstance", &ncloud.PublicIPInstance{PublicIPInstanceNo: "22"})
return stateBag
}

View File

@ -1,6 +1,8 @@
package ncloud
import (
"fmt"
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
@ -35,6 +37,8 @@ func (s *StepGetRootPassword) getRootPassword(serverInstanceNo string, privateKe
return "", err
}
s.Say(fmt.Sprintf("Root password is %s", rootPassword.RootPassword))
return rootPassword.RootPassword, nil
}

View File

@ -14,13 +14,14 @@ import (
//StepValidateTemplate : struct for Validation a tempalte
type StepValidateTemplate struct {
Conn *ncloud.Conn
Validate func() error
Say func(message string)
Error func(e error)
Config *Config
zoneNo string
regionNo string
Conn *ncloud.Conn
Validate func() error
Say func(message string)
Error func(e error)
Config *Config
zoneNo string
regionNo string
FeeSystemTypeCode string
}
// NewStepValidateTemplate : funciton for Validation a tempalte
@ -168,7 +169,7 @@ func (s *StepValidateTemplate) validateServerImageProduct() error {
}
if strings.Contains(productName, "mssql") {
s.Config.FeeSystemTypeCode = "FXSUM"
s.FeeSystemTypeCode = "FXSUM"
}
return nil
@ -193,7 +194,7 @@ func (s *StepValidateTemplate) validateServerProductCode() error {
if product.ProductCode == productCode {
isExistProductCode = true
if strings.Contains(product.ProductName, "mssql") {
s.Config.FeeSystemTypeCode = "FXSUM"
s.FeeSystemTypeCode = "FXSUM"
}
if product.ProductType.Code == "VDS" {
@ -255,6 +256,10 @@ func (s *StepValidateTemplate) Run(state multistep.StateBag) multistep.StepActio
state.Put("ZoneNo", s.zoneNo)
if s.FeeSystemTypeCode != "" {
state.Put("FeeSystemTypeCode", s.FeeSystemTypeCode)
}
return processStepResult(err, s.Error, state)
}