packer-cn/builder/scaleway/step_create_server.go

85 lines
1.8 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"
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 {
serverID string
2017-04-06 05:19:17 -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)
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))}
}
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,
Tags: tags,
Bootscript: bootscript,
BootType: c.BootType,
2017-04-06 05:19:17 -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 {
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 = server
2017-04-06 05:19:17 -04:00
state.Put("server_id", server)
return multistep.ActionContinue
}
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
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
}