packer-cn/builder/scaleway/step_create_server.go

97 lines
2.5 KiB
Go
Raw Normal View History

2017-04-06 05:19:17 -04:00
package scaleway
import (
2018-02-05 19:50:32 -05:00
"context"
2017-04-06 05:19:17 -04:00
"fmt"
"strings"
2017-04-06 05:19:17 -04:00
"github.com/hashicorp/packer/packer"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
2017-04-06 05:19:17 -04:00
)
type stepCreateServer struct {
serverID string
2017-04-06 05:19:17 -04:00
}
func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
2017-04-06 05:19:17 -04:00
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
tags := []string{}
var bootscript *string
2017-04-06 05:19:17 -04:00
ui.Say("Creating server...")
if c.Bootscript != "" {
bootscript = &c.Bootscript
}
if c.Comm.SSHPublicKey != nil {
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.Replace(strings.TrimSpace(string(c.Comm.SSHPublicKey)), " ", "_", -1))}
}
bootType := instance.BootType(c.BootType)
createServerResp, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
BootType: &bootType,
Bootscript: bootscript,
2017-04-06 05:19:17 -04:00
CommercialType: c.CommercialType,
Name: c.ServerName,
Image: c.Image,
Tags: tags,
}, scw.WithContext(ctx))
if err != nil {
err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
_, err = instanceAPI.ServerAction(&instance.ServerActionRequest{
Action: instance.ServerActionPoweron,
ServerID: createServerResp.Server.ID,
}, scw.WithContext(ctx))
2017-04-06 05:19:17 -04:00
if err != nil {
err := fmt.Errorf("Error starting server: %s", err)
2017-04-06 05:19:17 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.serverID = createServerResp.Server.ID
2017-04-06 05:19:17 -04:00
state.Put("server_id", createServerResp.Server.ID)
// 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", s.serverID)
2017-04-06 05:19:17 -04:00
return multistep.ActionContinue
}
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
if s.serverID == "" {
2017-04-06 05:19:17 -04:00
return
}
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
2017-04-06 05:19:17 -04:00
ui := state.Get("ui").(packer.Ui)
ui.Say("Destroying server...")
2017-07-21 06:26:20 -04:00
err := instanceAPI.DeleteServer(&instance.DeleteServerRequest{
ServerID: s.serverID,
})
2017-04-06 05:19:17 -04:00
if err != nil {
_, err = instanceAPI.ServerAction(&instance.ServerActionRequest{
Action: instance.ServerActionTerminate,
ServerID: s.serverID,
})
if err != nil {
ui.Error(fmt.Sprintf(
"Error destroying server. Please destroy it manually: %s", err))
}
2017-04-06 05:19:17 -04:00
}
}