packer-cn/builder/digitalocean/step_create_droplet.go

108 lines
2.7 KiB
Go
Raw Normal View History

package digitalocean
import (
2017-04-08 15:52:57 -04:00
"context"
"fmt"
"log"
"strconv"
2017-03-28 21:47:10 -04:00
"io/ioutil"
"github.com/digitalocean/godo"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepCreateDroplet struct {
dropletId int
}
func (s *stepCreateDroplet) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*godo.Client)
ui := state.Get("ui").(packersdk.Ui)
c := state.Get("config").(*Config)
sshKeyId := state.Get("ssh_key_id").(int)
// Create the droplet based on configuration
ui.Say("Creating droplet...")
userData := c.UserData
if c.UserDataFile != "" {
contents, err := ioutil.ReadFile(c.UserDataFile)
if err != nil {
state.Put("error", fmt.Errorf("Problem reading user data file: %s", err))
return multistep.ActionHalt
}
userData = string(contents)
}
2020-05-13 13:29:22 -04:00
createImage := getImageType(c.Image)
dropletCreateReq := &godo.DropletCreateRequest{
Name: c.DropletName,
Region: c.Region,
Size: c.Size,
Image: createImage,
SSHKeys: []godo.DropletCreateSSHKey{
2017-03-28 21:47:10 -04:00
{ID: sshKeyId},
},
PrivateNetworking: c.PrivateNetworking,
2017-04-08 15:52:57 -04:00
Monitoring: c.Monitoring,
IPv6: c.IPv6,
UserData: userData,
2018-07-30 09:55:06 -04:00
Tags: c.Tags,
2020-10-13 11:57:57 -04:00
VPCUUID: c.VPCUUID,
}
log.Printf("[DEBUG] Droplet create paramaters: %s", godo.Stringify(dropletCreateReq))
droplet, _, err := client.Droplets.Create(context.TODO(), dropletCreateReq)
if err != nil {
err := fmt.Errorf("Error creating droplet: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// We use this in cleanup
s.dropletId = droplet.ID
// Store the droplet id for later
state.Put("droplet_id", droplet.ID)
// 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", droplet.ID)
return multistep.ActionContinue
}
func (s *stepCreateDroplet) Cleanup(state multistep.StateBag) {
// If the dropletid isn't there, we probably never created it
if s.dropletId == 0 {
return
}
client := state.Get("client").(*godo.Client)
ui := state.Get("ui").(packersdk.Ui)
// Destroy the droplet we just created
ui.Say("Destroying droplet...")
2017-04-09 14:33:05 -04:00
_, err := client.Droplets.Delete(context.TODO(), s.dropletId)
if err != nil {
ui.Error(fmt.Sprintf(
"Error destroying droplet. Please destroy it manually: %s", err))
}
}
2020-05-13 13:29:22 -04:00
func getImageType(image string) godo.DropletCreateImage {
createImage := godo.DropletCreateImage{Slug: image}
imageId, err := strconv.Atoi(image)
if err == nil {
createImage = godo.DropletCreateImage{ID: imageId}
}
return createImage
}