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

Commit

Permalink
exoscale: add ability to specify customer user data for cloud-init
Browse files Browse the repository at this point in the history
CoreOS user are likely to want this capacity as it enables the
configuration of the etcd cluster. Moreover, we remove some of the
default options as they are not supported in CoreOS and are useless on
Debian/Ubuntu:

 - Resizing of rootfs is already done on all images by default.
 - Docker Machine will set the hostname as part of the provisioning.

Signed-off-by: Vincent Bernat <Vincent.Bernat@exoscale.ch>
  • Loading branch information
vincentbernat committed Mar 21, 2016
1 parent 6a831d4 commit 97b2359
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
2 changes: 2 additions & 0 deletions docs/drivers/exoscale.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options:
- `--exoscale-security-group`: Security group. It will be created if it doesn't exist.
- `--exoscale-availability-zone`: Exoscale availability zone.
- `--exoscale-ssh-user`: SSH username, which must match the default SSH user for the used image.
- `--exoscale-userdata`: Path to file containing user data for cloud-init.

If a custom security group is provided, you need to ensure that you allow TCP ports 22 and 2376 in an ingress rule. Moreover, if you want to use Swarm, also add TCP port 3376.

Expand All @@ -43,3 +44,4 @@ Environment variables and default values:
| `--exoscale-security-group` | `EXOSCALE_SECURITY_GROUP` | `docker-machine` |
| `--exoscale-availability-zone` | `EXOSCALE_AVAILABILITY_ZONE` | `ch-gva-2` |
| `--exoscale-ssh-user` | `EXOSCALE_SSH_USER` | `ubuntu` |
| `--exoscale-userdata` | `EXOSCALE_USERDATA` | - |
55 changes: 33 additions & 22 deletions drivers/exoscale/exoscale.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package exoscale

import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"text/template"
"time"

"github.com/docker/machine/libmachine/drivers"
Expand All @@ -29,6 +28,7 @@ type Driver struct {
AvailabilityZone string
KeyPair string
PublicKey string
UserDataFile string
ID string `json:"Id"`
}

Expand Down Expand Up @@ -95,6 +95,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Value: defaultSSHUser,
Usage: "Set the name of the ssh user",
},
mcnflag.StringFlag{
EnvVar: "EXOSCALE_USERDATA",
Name: "exoscale-userdata",
Usage: "path to file with cloud-init user-data",
},
}
}

Expand Down Expand Up @@ -142,6 +147,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.SecurityGroup = strings.Join(securityGroups, ",")
d.AvailabilityZone = flags.String("exoscale-availability-zone")
d.SSHUser = flags.String("exoscale-ssh-user")
d.UserDataFile = flags.String("exoscale-userdata")
d.SetSwarmConfigFromFlags(flags)

if d.URL == "" {
Expand All @@ -154,6 +160,16 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}

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)
}
}

return nil
}

func (d *Driver) GetURL() (string, error) {
if err := drivers.MustBeRunning(d); err != nil {
return "", err
Expand Down Expand Up @@ -238,6 +254,11 @@ func (d *Driver) createDefaultSecurityGroup(client *egoscale.Client, group strin
}

func (d *Driver) Create() error {
userdata, err := d.getCloudInit()
if err != nil {
return err
}

log.Infof("Querying exoscale for the requested parameters...")
client := egoscale.NewClient(d.URL, d.APIKey, d.APISecretKey)
topology, err := client.GetTopology()
Expand Down Expand Up @@ -303,11 +324,6 @@ func (d *Driver) Create() error {
d.KeyPair = keypairName

log.Infof("Spawn exoscale host...")

userdata, err := d.getCloudInit()
if err != nil {
return err
}
log.Debugf("Using the following cloud-init file:")
log.Debugf("%s", userdata)

Expand Down Expand Up @@ -434,20 +450,15 @@ func (d *Driver) waitForVM(client *egoscale.Client, jobid string) (*egoscale.Dep
// Build a cloud-init user data string that will install and run
// docker.
func (d *Driver) getCloudInit() (string, error) {
const tpl = `#cloud-config
manage_etc_hosts: true
fqdn: {{ .MachineName }}
resize_rootfs: true
`
var buffer bytes.Buffer

tmpl, err := template.New("cloud-init").Parse(tpl)
if err != nil {
return "", err
}
err = tmpl.Execute(&buffer, d)
if err != nil {
return "", err
if d.UserDataFile != "" {
buf, err := ioutil.ReadFile(d.UserDataFile)
if err != nil {
return "", err
}
return string(buf), nil
}
return buffer.String(), nil

return `#cloud-config
manage_etc_hosts: true
`, nil
}

0 comments on commit 97b2359

Please sign in to comment.