2013-06-13 10:03:10 -04:00
|
|
|
// The digitalocean package contains a packer.Builder implementation
|
|
|
|
// that builds DigitalOcean images (snapshots).
|
|
|
|
|
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-13 11:58:06 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-06-13 10:03:10 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-08-01 15:11:54 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2013-06-13 10:03:10 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
2013-07-11 05:31:09 -04:00
|
|
|
"os"
|
2013-06-13 10:03:10 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The unique id for the builder
|
|
|
|
const BuilderId = "pearkes.digitalocean"
|
|
|
|
|
|
|
|
// Configuration tells the builder the credentials
|
|
|
|
// to use while communicating with DO and describes the image
|
|
|
|
// you are creating
|
|
|
|
type config struct {
|
2013-07-16 00:28:49 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
ClientID string `mapstructure:"client_id"`
|
|
|
|
APIKey string `mapstructure:"api_key"`
|
2013-06-15 14:09:26 -04:00
|
|
|
RegionID uint `mapstructure:"region_id"`
|
|
|
|
SizeID uint `mapstructure:"size_id"`
|
|
|
|
ImageID uint `mapstructure:"image_id"`
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-08-08 18:42:19 -04:00
|
|
|
SnapshotName string `mapstructure:"snapshot_name"`
|
2013-06-15 14:09:26 -04:00
|
|
|
SSHUsername string `mapstructure:"ssh_username"`
|
|
|
|
SSHPort uint `mapstructure:"ssh_port"`
|
|
|
|
|
2013-06-17 08:21:15 -04:00
|
|
|
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
|
|
|
RawEventDelay string `mapstructure:"event_delay"`
|
2013-06-23 06:51:51 -04:00
|
|
|
RawStateTimeout string `mapstructure:"state_timeout"`
|
2013-07-14 08:20:29 -04:00
|
|
|
|
|
|
|
// These are unexported since they're set by other fields
|
|
|
|
// being set.
|
|
|
|
sshTimeout time.Duration
|
|
|
|
eventDelay time.Duration
|
|
|
|
stateTimeout time.Duration
|
2013-08-08 18:42:19 -04:00
|
|
|
|
|
|
|
tpl *common.Template
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:06:39 -04:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) error {
|
2013-07-19 15:00:32 -04:00
|
|
|
md, err := common.DecodeConfig(&b.config, raws...)
|
2013-07-13 20:28:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-08 18:42:19 -04:00
|
|
|
b.config.tpl, err = common.NewTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-09 17:21:31 -04:00
|
|
|
b.config.tpl.UserVars = b.config.PackerUserVars
|
2013-08-08 18:42:19 -04:00
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
// Accumulate any errors
|
2013-07-19 19:08:25 -04:00
|
|
|
errs := common.CheckUnusedConfig(md)
|
2013-07-13 20:28:56 -04:00
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
// Optional configuration with defaults
|
2013-07-11 05:31:09 -04:00
|
|
|
if b.config.APIKey == "" {
|
|
|
|
// Default to environment variable for api_key, if it exists
|
|
|
|
b.config.APIKey = os.Getenv("DIGITALOCEAN_API_KEY")
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ClientID == "" {
|
|
|
|
// Default to environment variable for client_id, if it exists
|
|
|
|
b.config.ClientID = os.Getenv("DIGITALOCEAN_CLIENT_ID")
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
if b.config.RegionID == 0 {
|
|
|
|
// Default to Region "New York"
|
|
|
|
b.config.RegionID = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SizeID == 0 {
|
|
|
|
// Default to 512mb, the smallest droplet size
|
|
|
|
b.config.SizeID = 66
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ImageID == 0 {
|
2013-06-14 09:26:03 -04:00
|
|
|
// Default to base image "Ubuntu 12.04 x64 Server (id: 284203)"
|
|
|
|
b.config.ImageID = 284203
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
2013-08-08 18:42:19 -04:00
|
|
|
if b.config.SnapshotName == "" {
|
|
|
|
// Default to packer-{{ unix timestamp (utc) }}
|
|
|
|
b.config.SnapshotName = "packer-{{timestamp}}"
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
if b.config.SSHUsername == "" {
|
|
|
|
// Default to "root". You can override this if your
|
|
|
|
// SourceImage has a different user account then the DO default
|
|
|
|
b.config.SSHUsername = "root"
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SSHPort == 0 {
|
|
|
|
// Default to port 22 per DO default
|
|
|
|
b.config.SSHPort = 22
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RawSSHTimeout == "" {
|
|
|
|
// Default to 1 minute timeouts
|
|
|
|
b.config.RawSSHTimeout = "1m"
|
|
|
|
}
|
|
|
|
|
2013-06-17 07:28:21 -04:00
|
|
|
if b.config.RawEventDelay == "" {
|
|
|
|
// Default to 5 second delays after creating events
|
|
|
|
// to allow DO to process
|
|
|
|
b.config.RawEventDelay = "5s"
|
|
|
|
}
|
|
|
|
|
2013-06-23 06:51:51 -04:00
|
|
|
if b.config.RawStateTimeout == "" {
|
2013-06-24 03:07:15 -04:00
|
|
|
// Default to 6 minute timeouts waiting for
|
2013-06-23 06:51:51 -04:00
|
|
|
// desired state. i.e waiting for droplet to become active
|
2013-06-24 03:02:55 -04:00
|
|
|
b.config.RawStateTimeout = "6m"
|
2013-06-23 06:51:51 -04:00
|
|
|
}
|
|
|
|
|
2013-08-08 18:42:19 -04:00
|
|
|
templates := map[string]*string{
|
|
|
|
"client_id": &b.config.ClientID,
|
|
|
|
"api_key": &b.config.APIKey,
|
|
|
|
"snapshot_name": &b.config.SnapshotName,
|
|
|
|
"ssh_username": &b.config.SSHUsername,
|
|
|
|
"ssh_timeout": &b.config.RawSSHTimeout,
|
|
|
|
"event_delay": &b.config.RawEventDelay,
|
|
|
|
"state_timeout": &b.config.RawStateTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = b.config.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:03:10 -04:00
|
|
|
// Required configurations that will display errors if not set
|
2013-06-13 11:58:06 -04:00
|
|
|
if b.config.ClientID == "" {
|
2013-07-19 19:08:25 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a client_id must be specified"))
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.APIKey == "" {
|
2013-07-19 19:08:25 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("an api_key must be specified"))
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
2013-06-17 07:28:21 -04:00
|
|
|
|
2013-06-23 06:51:51 -04:00
|
|
|
sshTimeout, err := time.ParseDuration(b.config.RawSSHTimeout)
|
2013-06-13 10:03:10 -04:00
|
|
|
if err != nil {
|
2013-07-19 19:08:25 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
2013-07-14 08:20:29 -04:00
|
|
|
b.config.sshTimeout = sshTimeout
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-06-23 06:51:51 -04:00
|
|
|
eventDelay, err := time.ParseDuration(b.config.RawEventDelay)
|
2013-06-17 07:28:21 -04:00
|
|
|
if err != nil {
|
2013-07-19 19:08:25 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing event_delay: %s", err))
|
2013-06-17 07:28:21 -04:00
|
|
|
}
|
2013-07-14 08:20:29 -04:00
|
|
|
b.config.eventDelay = eventDelay
|
2013-06-23 06:51:51 -04:00
|
|
|
|
|
|
|
stateTimeout, err := time.ParseDuration(b.config.RawStateTimeout)
|
|
|
|
if err != nil {
|
2013-07-19 19:08:25 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing state_timeout: %s", err))
|
2013-06-23 06:51:51 -04:00
|
|
|
}
|
2013-07-14 08:20:29 -04:00
|
|
|
b.config.stateTimeout = stateTimeout
|
2013-06-17 07:28:21 -04:00
|
|
|
|
2013-07-19 19:08:25 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Config: %+v", b.config)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2013-06-13 11:58:06 -04:00
|
|
|
// Initialize the DO API client
|
|
|
|
client := DigitalOceanClient{}.New(b.config.ClientID, b.config.APIKey)
|
|
|
|
|
|
|
|
// Set up the state
|
|
|
|
state := make(map[string]interface{})
|
|
|
|
state["config"] = b.config
|
|
|
|
state["client"] = client
|
|
|
|
state["hook"] = hook
|
|
|
|
state["ui"] = ui
|
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
|
|
|
new(stepCreateSSHKey),
|
|
|
|
new(stepCreateDroplet),
|
2013-06-13 12:48:19 -04:00
|
|
|
new(stepDropletInfo),
|
2013-07-15 01:14:10 -04:00
|
|
|
&common.StepConnectSSH{
|
|
|
|
SSHAddress: sshAddress,
|
|
|
|
SSHConfig: sshConfig,
|
|
|
|
SSHWaitTimeout: 5 * time.Minute,
|
|
|
|
},
|
2013-07-16 02:44:41 -04:00
|
|
|
new(common.StepProvision),
|
2013-06-13 11:58:06 -04:00
|
|
|
new(stepPowerOff),
|
|
|
|
new(stepSnapshot),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
2013-06-15 14:09:26 -04:00
|
|
|
if b.config.PackerDebug {
|
|
|
|
b.runner = &multistep.DebugRunner{
|
|
|
|
Steps: steps,
|
|
|
|
PauseFn: common.MultistepDebugFn(ui),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
}
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
b.runner.Run(state)
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-06-20 00:00:51 -04:00
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state["error"]; ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2013-06-19 00:54:15 -04:00
|
|
|
if _, ok := state["snapshot_name"]; !ok {
|
|
|
|
log.Println("Failed to find snapshot_name in state. Bug?")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
|
|
|
snapshotName: state["snapshot_name"].(string),
|
2013-06-19 01:02:09 -04:00
|
|
|
snapshotId: state["snapshot_image_id"].(uint),
|
2013-06-19 00:54:15 -04:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|