github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/scaleway/step_create_server.go (about) 1 package scaleway 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 "github.com/hashicorp/packer/packer" 10 "github.com/scaleway/scaleway-cli/pkg/api" 11 ) 12 13 type stepCreateServer struct { 14 serverID string 15 } 16 17 func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 18 client := state.Get("client").(*api.ScalewayAPI) 19 ui := state.Get("ui").(packer.Ui) 20 c := state.Get("config").(Config) 21 sshPubKey := state.Get("ssh_pubkey").(string) 22 tags := []string{} 23 var bootscript *string 24 25 ui.Say("Creating server...") 26 27 if c.Bootscript != "" { 28 bootscript = &c.Bootscript 29 } 30 31 if sshPubKey != "" { 32 tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))} 33 } 34 35 server, err := client.PostServer(api.ScalewayServerDefinition{ 36 Name: c.ServerName, 37 Image: &c.Image, 38 Organization: c.Organization, 39 CommercialType: c.CommercialType, 40 Tags: tags, 41 Bootscript: bootscript, 42 }) 43 44 if err != nil { 45 err := fmt.Errorf("Error creating server: %s", err) 46 state.Put("error", err) 47 ui.Error(err.Error()) 48 return multistep.ActionHalt 49 } 50 51 err = client.PostServerAction(server, "poweron") 52 53 if err != nil { 54 err := fmt.Errorf("Error starting server: %s", err) 55 state.Put("error", err) 56 ui.Error(err.Error()) 57 return multistep.ActionHalt 58 } 59 60 s.serverID = server 61 62 state.Put("server_id", server) 63 64 return multistep.ActionContinue 65 } 66 67 func (s *stepCreateServer) Cleanup(state multistep.StateBag) { 68 if s.serverID == "" { 69 return 70 } 71 72 client := state.Get("client").(*api.ScalewayAPI) 73 ui := state.Get("ui").(packer.Ui) 74 75 ui.Say("Destroying server...") 76 77 err := client.DeleteServerForce(s.serverID) 78 79 if err != nil { 80 ui.Error(fmt.Sprintf( 81 "Error destroying server. Please destroy it manually: %s", err)) 82 } 83 84 }