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"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-06-13 10:03:10 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"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 {
|
|
|
|
// Credentials
|
|
|
|
ClientID string `mapstructure:"client_id"`
|
|
|
|
APIKey string `mapstructure:"api_key"`
|
|
|
|
|
|
|
|
RegionID uint `mapstructure:"region_id"`
|
|
|
|
SizeID uint `mapstructure:"size_id"`
|
|
|
|
ImageID uint `mapstructure:"image_id"`
|
|
|
|
SSHUsername string `mapstructure:"ssh_username"`
|
|
|
|
SSHPort uint `mapstructure:"ssh_port"`
|
|
|
|
|
|
|
|
// Configuration for the image being built
|
|
|
|
SnapshotName string `mapstructure:"snapshot_name"`
|
|
|
|
|
|
|
|
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
2013-06-13 11:58:06 -04:00
|
|
|
SSHTimeout time.Duration
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raw interface{}) error {
|
|
|
|
if err := mapstructure.Decode(raw, &b.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optional configuration with defaults
|
|
|
|
//
|
|
|
|
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 {
|
|
|
|
// Default to base image "Ubuntu 12.04 x64 Server"
|
|
|
|
b.config.ImageID = 2676
|
|
|
|
}
|
|
|
|
|
|
|
|
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.SnapshotName == "" {
|
|
|
|
// Default to packer-{{ unix timestamp (utc) }}
|
|
|
|
b.config.SnapshotName = "packer-{{.CreateTime}}"
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RawSSHTimeout == "" {
|
|
|
|
// Default to 1 minute timeouts
|
|
|
|
b.config.RawSSHTimeout = "1m"
|
|
|
|
}
|
|
|
|
|
|
|
|
// A list of errors on the configuration
|
|
|
|
errs := make([]error, 0)
|
|
|
|
|
|
|
|
// Required configurations that will display errors if not set
|
|
|
|
//
|
2013-06-13 11:58:06 -04:00
|
|
|
if b.config.ClientID == "" {
|
2013-06-13 10:03:10 -04:00
|
|
|
errs = append(errs, errors.New("a client_id must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.APIKey == "" {
|
|
|
|
errs = append(errs, errors.New("an api_key must be specified"))
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
timeout, err := time.ParseDuration(b.config.RawSSHTimeout)
|
2013-06-13 10:03:10 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
b.config.SSHTimeout = timeout
|
2013-06-13 10:03:10 -04:00
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &packer.MultiError{errs}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
new(stepConnectSSH),
|
|
|
|
new(stepProvision),
|
|
|
|
new(stepPowerOff),
|
|
|
|
new(stepSnapshot),
|
|
|
|
new(stepDestroyDroplet),
|
|
|
|
new(stepDestroySSHKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
|
|
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
b.runner.Run(state)
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|