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"
|
|
|
|
"github.com/scaleway/scaleway-cli/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2017-04-06 05:19:17 -04:00
|
|
|
client := state.Get("client").(*api.ScalewayAPI)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
server, err := client.PostServer(api.ScalewayServerDefinition{
|
|
|
|
Name: c.ServerName,
|
|
|
|
Image: &c.Image,
|
|
|
|
Organization: c.Organization,
|
|
|
|
CommercialType: c.CommercialType,
|
2017-04-19 05:10:52 -04:00
|
|
|
Tags: tags,
|
2018-06-29 13:44:56 -04:00
|
|
|
Bootscript: bootscript,
|
2018-09-27 10:27:33 -04:00
|
|
|
BootType: c.BootType,
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
err = client.PostServerAction(server, "poweron")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-11 06:19:28 -04:00
|
|
|
s.serverID = server
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
state.Put("server_id", server)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("client").(*api.ScalewayAPI)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Destroying server...")
|
2017-07-21 06:26:20 -04:00
|
|
|
|
|
|
|
err := client.DeleteServerForce(s.serverID)
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error destroying server. Please destroy it manually: %s", err))
|
|
|
|
}
|
2017-07-21 06:26:20 -04:00
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|