2013-06-13 12:48:19 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-04-08 15:52:57 -04:00
|
|
|
"context"
|
2013-06-13 12:48:19 -04:00
|
|
|
"fmt"
|
2020-05-12 12:38:51 -04:00
|
|
|
"log"
|
2020-05-12 11:46:58 -04:00
|
|
|
"strconv"
|
2014-08-28 14:24:31 -04:00
|
|
|
|
2017-03-28 21:47:10 -04:00
|
|
|
"io/ioutil"
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2013-06-13 12:48:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateDroplet struct {
|
2015-06-10 17:02:06 -04:00
|
|
|
dropletId int
|
2013-06-13 12:48:19 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateDroplet) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-09-18 05:36:21 -04:00
|
|
|
c := state.Get("config").(*Config)
|
2015-06-10 17:02:06 -04:00
|
|
|
sshKeyId := state.Get("ssh_key_id").(int)
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
// Create the droplet based on configuration
|
2015-06-10 17:02:06 -04:00
|
|
|
ui.Say("Creating droplet...")
|
2016-09-28 09:00:57 -04:00
|
|
|
|
|
|
|
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)
|
2020-05-12 11:46:58 -04:00
|
|
|
|
2020-05-12 12:38:51 -04:00
|
|
|
dropletCreateReq := &godo.DropletCreateRequest{
|
2015-06-10 17:02:06 -04:00
|
|
|
Name: c.DropletName,
|
|
|
|
Region: c.Region,
|
|
|
|
Size: c.Size,
|
2020-05-12 11:46:58 -04:00
|
|
|
Image: createImage,
|
2015-06-10 17:02:06 -04:00
|
|
|
SSHKeys: []godo.DropletCreateSSHKey{
|
2017-03-28 21:47:10 -04:00
|
|
|
{ID: sshKeyId},
|
2015-06-10 17:02:06 -04:00
|
|
|
},
|
|
|
|
PrivateNetworking: c.PrivateNetworking,
|
2017-04-08 15:52:57 -04:00
|
|
|
Monitoring: c.Monitoring,
|
2017-11-01 14:43:08 -04:00
|
|
|
IPv6: c.IPv6,
|
2016-09-28 09:00:57 -04:00
|
|
|
UserData: userData,
|
2018-07-30 09:55:06 -04:00
|
|
|
Tags: c.Tags,
|
2020-10-13 11:57:57 -04:00
|
|
|
VPCUUID: c.VPCUUID,
|
2020-05-12 12:38:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Droplet create paramaters: %s", godo.Stringify(dropletCreateReq))
|
|
|
|
|
|
|
|
droplet, _, err := client.Droplets.Create(context.TODO(), dropletCreateReq)
|
2013-06-13 12:48:19 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error creating droplet: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-13 12:48:19 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use this in cleanup
|
2015-06-10 17:02:06 -04:00
|
|
|
s.dropletId = droplet.ID
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
// Store the droplet id for later
|
2015-06-10 17:02:06 -04:00
|
|
|
state.Put("droplet_id", droplet.ID)
|
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", droplet.ID)
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepCreateDroplet) Cleanup(state multistep.StateBag) {
|
2013-06-13 12:48:19 -04:00
|
|
|
// If the dropletid isn't there, we probably never created it
|
|
|
|
if s.dropletId == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
// 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)
|
2013-06-13 12:48:19 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
2015-06-10 17:02:06 -04:00
|
|
|
"Error destroying droplet. Please destroy it manually: %s", err))
|
2013-06-13 12:48:19 -04:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|