Skip to content

Commit 159a8fc

Browse files
authored
Merge pull request #11 from JonasProgrammer/added-cloud-init-support
integrated cloud-init support (fix #10)
2 parents 8c24ac3 + e42c999 commit 159a8fc

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

README.md

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and pass that to `docker-machine create` with the `--hetzner-api-token` option.
1313

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

16-
```shell
16+
```bash
1717
# Download the binary (this example downloads the binary for linux amd64)
1818
$ 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
1919
$ tar -xvf docker-machine-driver-hetzner_1.0.0_linux_amd64.tar.gz
@@ -25,31 +25,55 @@ $ cp docker-machine-driver-hetzner /usr/local/bin/
2525

2626
## Usage
2727

28-
$ docker-machine create \
29-
--driver hetzner \
30-
--hetzner-api-token=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
31-
some-machine
32-
28+
```bash
29+
$ docker-machine create \
30+
--driver hetzner \
31+
--hetzner-api-token=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
32+
some-machine
33+
```
34+
3335
### Using environment variables
3436

35-
$ HETZNER_API_TOKEN=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
36-
&& HETZNER_IMAGE=centos-7 \
37-
&& docker-machine create \
38-
--driver hetzner \
39-
some-machine
40-
37+
```bash
38+
$ HETZNER_API_TOKEN=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
39+
&& HETZNER_IMAGE=centos-7 \
40+
&& docker-machine create \
41+
--driver hetzner \
42+
some-machine
43+
```
44+
45+
### Using Cloud-init
46+
47+
```bash
48+
$ CLOUD_INIT_USER_DATA=`cat <<EOF
49+
#cloud-config
50+
write_files:
51+
- path: /test.txt
52+
content: |
53+
Here is a line.
54+
Another line is here.
55+
EOF
56+
`
57+
58+
$ docker-machine create \
59+
--driver hetzner \
60+
--hetzner-api-token=QJhoRT38JfAUO037PWJ5Zt9iAABIxdxdh4gPqNkUGKIrUMd6I3cPIsfKozI513sy \
61+
--hetzner-user-data="${CLOUD_INIT_USER_DATA}" \
62+
some-machine
63+
```
4164

4265
## Options
4366

44-
- `--hetzner-api-token`: **required**. Your project-specific access token for the Hetzner Cloud API.
45-
- `--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`).
46-
- `--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`).
47-
- `--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.
67+
- `--hetzner-api-token`: **required**. Your project-specific access token for the Hetzner Cloud API.
68+
- `--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`).
69+
- `--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`).
70+
- `--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.
4871
**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
4972
spelling and the newly created server to make sure the desired location was chosen indeed.**
50-
- `--hetzner-existing-key-path`: Use an existing (local) SSH key instead of generating a new keypair.
51-
- `--hetzner-existing-key-id`: **requires `--hetzner-existing-key-path`**. Use an existing (remote) SSH key instead of uploading the imported key pair,
52-
see [SSH Keys API](https://docs.hetzner.cloud/#resources-ssh-keys-get) for how to get a list
73+
- `--hetzner-existing-key-path`: Use an existing (local) SSH key instead of generating a new keypair.
74+
- `--hetzner-existing-key-id`: **requires `--hetzner-existing-key-path`**. Use an existing (remote) SSH key instead of uploading the imported key pair,
75+
see [SSH Keys API](https://docs.hetzner.cloud/#resources-ssh-keys-get) for how to get a list
76+
- `--hetzner-user-data`: Cloud-init based User data
5377

5478
#### Existing SSH keys
5579

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

80105

81106
## Building from source

driver.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Driver struct {
3535
originalKey string
3636
danglingKey bool
3737
ServerID int
38+
userData string
3839
cachedServer *hcloud.Server
3940
}
4041

@@ -48,6 +49,7 @@ const (
4849
flagLocation = "hetzner-server-location"
4950
flagExKeyID = "hetzner-existing-key-id"
5051
flagExKeyPath = "hetzner-existing-key-path"
52+
flagUserData = "hetzner-user-data"
5153
)
5254

5355
func NewDriver() *Driver {
@@ -105,6 +107,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
105107
Usage: "Path to existing key (new public key will be created unless --hetzner-existing-key-id is specified)",
106108
Value: "",
107109
},
110+
mcnflag.StringFlag{
111+
EnvVar: "HETZNER_USER_DATA",
112+
Name: flagUserData,
113+
Usage: "Cloud-init based User data",
114+
Value: "",
115+
},
108116
}
109117
}
110118

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

120129
d.SetSwarmConfigFromFlags(opts)
121130

122131
if d.AccessToken == "" {
123132
return errors.Errorf("hetzner requires --%v to be set", flagAPIToken)
124133
}
125-
126134
return nil
127135
}
128136

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

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

211-
srvopts := hcloud.ServerCreateOpts{Name: d.GetMachineName()}
219+
srvopts := hcloud.ServerCreateOpts{
220+
Name: d.GetMachineName(),
221+
UserData: d.userData,
222+
}
212223

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

0 commit comments

Comments
 (0)