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"
|
2017-04-11 06:19:28 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-02-05 19:50:32 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-06 05:19:17 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-09-09 04:27:48 -04:00
|
|
|
"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 {
|
2017-04-11 06:19:28 -04:00
|
|
|
serverID string
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-09-09 04:27:48 -04:00
|
|
|
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client))
|
2017-04-06 05:19:17 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-09-27 06:04:04 -04:00
|
|
|
c := state.Get("config").(*Config)
|
2017-04-19 05:10:52 -04:00
|
|
|
tags := []string{}
|
2018-06-29 13:44:56 -04:00
|
|
|
var bootscript *string
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
ui.Say("Creating server...")
|
|
|
|
|
2018-06-29 13:44:56 -04:00
|
|
|
if c.Bootscript != "" {
|
|
|
|
bootscript = &c.Bootscript
|
|
|
|
}
|
|
|
|
|
2018-09-27 06:04:04 -04:00
|
|
|
if c.Comm.SSHPublicKey != nil {
|
|
|
|
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.Replace(strings.TrimSpace(string(c.Comm.SSHPublicKey)), " ", "_", -1))}
|
2017-04-19 05:10:52 -04:00
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
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,
|
2020-09-09 04:27:48 -04:00
|
|
|
Name: c.ServerName,
|
|
|
|
Image: c.Image,
|
2017-04-19 05:10:52 -04:00
|
|
|
Tags: tags,
|
2017-04-06 05:19:17 -04:00
|
|
|
})
|
2017-07-11 10:15:00 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
_, err = instanceAPI.ServerAction(&instance.ServerActionRequest{
|
|
|
|
Action: instance.ServerActionPoweron,
|
|
|
|
ServerID: createServerResp.Server.ID,
|
|
|
|
})
|
2017-04-06 05:19:17 -04:00
|
|
|
if err != nil {
|
2017-07-11 10:15:00 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
s.serverID = createServerResp.Server.ID
|
2017-04-06 05:19:17 -04:00
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
state.Put("server_id", createServerResp.Server.ID)
|
2019-12-13 14:57:01 -05:00
|
|
|
// 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) {
|
2017-07-11 10:15:00 -04:00
|
|
|
if s.serverID == "" {
|
2017-04-06 05:19:17 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
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
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
err := instanceAPI.DeleteServer(&instance.DeleteServerRequest{
|
|
|
|
ServerID: s.serverID,
|
|
|
|
})
|
2017-04-06 05:19:17 -04:00
|
|
|
if err != nil {
|
2020-09-09 04:27:48 -04:00
|
|
|
_, 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
|
|
|
}
|
|
|
|
}
|