Skip to content

Commit

Permalink
Merge pull request #11 from JonasProgrammer/added-cloud-init-support
Browse files Browse the repository at this point in the history
integrated cloud-init support (fix #10)
  • Loading branch information
mxschmitt authored Mar 21, 2018
2 parents 8c24ac3 + e42c999 commit 159a8fc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
63 changes: 44 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and pass that to `docker-machine create` with the `--hetzner-api-token` option.

You can find sources and pre-compiled binaries [here](https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases).

```shell
```bash
# Download the binary (this example downloads the binary for linux amd64)
$ wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/1.0.0/docker-machine-driver-hetzner_1.0.0_linux_amd64.tar.gz
$ tar -xvf docker-machine-driver-hetzner_1.0.0_linux_amd64.tar.gz
Expand All @@ -25,31 +25,55 @@ $ cp docker-machine-driver-hetzner /usr/local/bin/

## Usage

$ docker-machine create \
--driver hetzner \
--hetzner-api-token=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
some-machine

```bash
$ docker-machine create \
--driver hetzner \
--hetzner-api-token=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
some-machine
```

### Using environment variables

$ HETZNER_API_TOKEN=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
&& HETZNER_IMAGE=centos-7 \
&& docker-machine create \
--driver hetzner \
some-machine

```bash
$ HETZNER_API_TOKEN=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
&& HETZNER_IMAGE=centos-7 \
&& docker-machine create \
--driver hetzner \
some-machine
```

### Using Cloud-init

```bash
$ CLOUD_INIT_USER_DATA=`cat <<EOF
#cloud-config
write_files:
- path: /test.txt
content: |
Here is a line.
Another line is here.
EOF
`

$ docker-machine create \
--driver hetzner \
--hetzner-api-token=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
--hetzner-user-data="${CLOUD_INIT_USER_DATA}" \
some-machine
```

## Options

- `--hetzner-api-token`: **required**. Your project-specific access token for the Hetzner Cloud API.
- `--hetzner-image`: The name of the Hetzner Cloud image to use, see [Images API](https://docs.hetzner.cloud/#resources-images-get) for how to get a list (defaults to `ubuntu-16.04`).
- `--hetzner-server-type`: The type of the Hetzner Cloud server, see [Server Types API](https://docs.hetzner.cloud/#resources-server-types-get) for how to get a list (defaults to `cx11`).
- `--hetzner-server-location`: The location to create the server in, see [Locations API](https://docs.hetzner.cloud/#resources-locations-get) for how to get a list.
- `--hetzner-api-token`: **required**. Your project-specific access token for the Hetzner Cloud API.
- `--hetzner-image`: The name of the Hetzner Cloud image to use, see [Images API](https://docs.hetzner.cloud/#resources-images-get) for how to get a list (defaults to `ubuntu-16.04`).
- `--hetzner-server-type`: The type of the Hetzner Cloud server, see [Server Types API](https://docs.hetzner.cloud/#resources-server-types-get) for how to get a list (defaults to `cx11`).
- `--hetzner-server-location`: The location to create the server in, see [Locations API](https://docs.hetzner.cloud/#resources-locations-get) for how to get a list.
**NOTICE: Beware that Hetzner does not reject invalid location names at the time of writing this; instead, a seemingly random location is chosen. Double check both the option value's
spelling and the newly created server to make sure the desired location was chosen indeed.**
- `--hetzner-existing-key-path`: Use an existing (local) SSH key instead of generating a new keypair.
- `--hetzner-existing-key-id`: **requires `--hetzner-existing-key-path`**. Use an existing (remote) SSH key instead of uploading the imported key pair,
see [SSH Keys API](https://docs.hetzner.cloud/#resources-ssh-keys-get) for how to get a list
- `--hetzner-existing-key-path`: Use an existing (local) SSH key instead of generating a new keypair.
- `--hetzner-existing-key-id`: **requires `--hetzner-existing-key-path`**. Use an existing (remote) SSH key instead of uploading the imported key pair,
see [SSH Keys API](https://docs.hetzner.cloud/#resources-ssh-keys-get) for how to get a list
- `--hetzner-user-data`: Cloud-init based User data

#### Existing SSH keys

Expand All @@ -76,6 +100,7 @@ was used during creation.
| `--hetzner-server-location` | `HETZNER_LOCATION` | - *(let Hetzner choose)* |
| `--hetzner-existing-key-path` | `HETZNER_EXISTING_KEY_PATH` | - *(generate new keypair)* |
| `--hetzner-existing-key-id` | `HETZNER_EXISTING_KEY_ID` | 0 *(upload new key)* |
| `--hetzner-user-data` | `HETZNER_USER_DATA` | - |


## Building from source
Expand Down
15 changes: 13 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Driver struct {
originalKey string
danglingKey bool
ServerID int
userData string
cachedServer *hcloud.Server
}

Expand All @@ -48,6 +49,7 @@ const (
flagLocation = "hetzner-server-location"
flagExKeyID = "hetzner-existing-key-id"
flagExKeyPath = "hetzner-existing-key-path"
flagUserData = "hetzner-user-data"
)

func NewDriver() *Driver {
Expand Down Expand Up @@ -105,6 +107,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Usage: "Path to existing key (new public key will be created unless --hetzner-existing-key-id is specified)",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "HETZNER_USER_DATA",
Name: flagUserData,
Usage: "Cloud-init based User data",
Value: "",
},
}
}

Expand All @@ -116,13 +124,13 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
d.KeyID = opts.Int(flagExKeyID)
d.IsExistingKey = d.KeyID != 0
d.originalKey = opts.String(flagExKeyPath)
d.userData = opts.String(flagUserData)

d.SetSwarmConfigFromFlags(opts)

if d.AccessToken == "" {
return errors.Errorf("hetzner requires --%v to be set", flagAPIToken)
}

return nil
}

Expand Down Expand Up @@ -208,7 +216,10 @@ func (d *Driver) Create() error {

log.Infof("Creating Hetzner server...")

srvopts := hcloud.ServerCreateOpts{Name: d.GetMachineName()}
srvopts := hcloud.ServerCreateOpts{
Name: d.GetMachineName(),
UserData: d.userData,
}

var err error
if srvopts.Location, err = d.getLocation(); err != nil {
Expand Down

0 comments on commit 159a8fc

Please sign in to comment.