github.phpd.cn/hashicorp/packer@v1.3.2/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 tags := []string{} 22 var bootscript *string 23 24 ui.Say("Creating server...") 25 26 if c.Bootscript != "" { 27 bootscript = &c.Bootscript 28 } 29 30 if c.Comm.SSHPublicKey != nil { 31 tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.Replace(strings.TrimSpace(string(c.Comm.SSHPublicKey)), " ", "_", -1))} 32 } 33 34 server, err := client.PostServer(api.ScalewayServerDefinition{ 35 Name: c.ServerName, 36 Image: &c.Image, 37 Organization: c.Organization, 38 CommercialType: c.CommercialType, 39 Tags: tags, 40 Bootscript: bootscript, 41 BootType: c.BootType, 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 }