Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Digitalocean driver: Support for creating Droplets with Cloud-init User Data #2141

Merged
merged 1 commit into from
Dec 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/drivers/digital-ocean.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Options:
- `--digitalocean-ipv6`: Enable IPv6 support for the droplet.
- `--digitalocean-private-networking`: Enable private networking support for the droplet.
- `--digitalocean-backups`: Enable Digital Oceans backups for the droplet.
- `--digitalocean-userdata`: Path to file containing User Data for the droplet.

The DigitalOcean driver will use `ubuntu-14-04-x64` as the default image.

Expand All @@ -40,3 +41,4 @@ Environment variables and default values:
| `--digitalocean-ipv6` | `DIGITALOCEAN_IPV6` | `false` |
| `--digitalocean-private-networking` | `DIGITALOCEAN_PRIVATE_NETWORKING` | `false` |
| `--digitalocean-backups` | `DIGITALOCEAN_BACKUPS` | `false` |
| `--digitalocean-userdata` | `DIGITALOCEAN_USERDATA` | - |
24 changes: 24 additions & 0 deletions drivers/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"net"
"os"
"time"

"github.com/digitalocean/godo"
Expand All @@ -27,6 +28,7 @@ type Driver struct {
IPv6 bool
Backups bool
PrivateNetworking bool
UserDataFile string
}

const (
Expand Down Expand Up @@ -83,6 +85,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Name: "digitalocean-backups",
Usage: "enable backups for droplet",
},
mcnflag.StringFlag{
EnvVar: "DIGITALOCEAN_USERDATA",
Name: "digitalocean-userdata",
Usage: "path to file with cloud-init user-data",
},
}
}

Expand Down Expand Up @@ -115,6 +122,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.IPv6 = flags.Bool("digitalocean-ipv6")
d.PrivateNetworking = flags.Bool("digitalocean-private-networking")
d.Backups = flags.Bool("digitalocean-backups")
d.UserDataFile = flags.String("digitalocean-userdata")
d.SwarmMaster = flags.Bool("swarm-master")
d.SwarmHost = flags.String("swarm-host")
d.SwarmDiscovery = flags.String("swarm-discovery")
Expand All @@ -129,6 +137,12 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
}

func (d *Driver) PreCreateCheck() error {
if d.UserDataFile != "" {
if _, err := os.Stat(d.UserDataFile); os.IsNotExist(err) {
return fmt.Errorf("user-data file %s could not be found", d.UserDataFile)
}
}

client := d.getClient()
regions, _, err := client.Regions.List(nil)
if err != nil {
Expand All @@ -144,6 +158,15 @@ func (d *Driver) PreCreateCheck() error {
}

func (d *Driver) Create() error {
var userdata string
if d.UserDataFile != "" {
buf, err := ioutil.ReadFile(d.UserDataFile)
if err != nil {
return err
}
userdata = string(buf)
}

log.Infof("Creating SSH key...")

key, err := d.createSSHKey()
Expand All @@ -165,6 +188,7 @@ func (d *Driver) Create() error {
IPv6: d.IPv6,
PrivateNetworking: d.PrivateNetworking,
Backups: d.Backups,
UserData: userdata,
SSHKeys: []godo.DropletCreateSSHKey{{ID: d.SSHKeyID}},
}

Expand Down