Skip to content

Commit 8565344

Browse files
3.2.0
- added --hetzner-server-label option, closes #56
1 parent dcb2120 commit 8565344

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ You can find sources and pre-compiled binaries [here](https://github.com/JonasPr
1515

1616
```bash
1717
# Download the binary (this example downloads the binary for linux amd64)
18-
$ wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/3.1.1/docker-machine-driver-hetzner_3.1.1_linux_amd64.tar.gz
19-
$ tar -xvf docker-machine-driver-hetzner_3.1.1_linux_amd64.tar.gz
18+
$ wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/3.2.0/docker-machine-driver-hetzner_3.2.0_linux_amd64.tar.gz
19+
$ tar -xvf docker-machine-driver-hetzner_3.2.0_linux_amd64.tar.gz
2020

2121
# Make it executable and copy the binary in a directory accessible with your $PATH
2222
$ chmod +x docker-machine-driver-hetzner
@@ -103,6 +103,7 @@ $ docker-machine create \
103103
- `--hetzner-volumes`: Volume IDs or names which should be attached to the server
104104
- `--hetzner-networks`: Network IDs or names which should be attached to the server private network interface
105105
- `--hetzner-use-private-network`: Use private network
106+
- `--hetzner-server-label`: `key=value` pairs of additional metadata to assign to the server.
106107

107108
#### Existing SSH keys
108109

@@ -135,6 +136,7 @@ was used during creation.
135136
| `--hetzner-networks` | `HETZNER_NETWORKS` | - |
136137
| `--hetzner-volumes` | `HETZNER_VOLUMES` | - |
137138
| `--hetzner-use-private-network` | `HETZNER_USE_PRIVATE_NETWORK` | false |
139+
| `--hetzner-server-label` | `HETZNER_SERVER_LABELS` | `[]` |
138140

139141

140142
## Building from source

driver.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"net"
88
"os"
9+
"strings"
910
"time"
1011

1112
"github.com/docker/machine/libmachine/drivers"
@@ -41,6 +42,7 @@ type Driver struct {
4142
networks []string
4243
UsePrivateNetwork bool
4344
cachedServer *hcloud.Server
45+
serverLabels map[string]string
4446

4547
additionalKeys []string
4648
AdditionalKeyIDs []int
@@ -63,6 +65,7 @@ const (
6365
flagNetworks = "hetzner-networks"
6466
flagUsePrivateNetwork = "hetzner-use-private-network"
6567
flagAdditionalKeys = "hetzner-additional-key"
68+
flagServerLabel = "hetzner-server-label"
6669
)
6770

6871
func NewDriver() *Driver {
@@ -154,6 +157,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
154157
Usage: "Additional public keys to be attached to the server",
155158
Value: []string{},
156159
},
160+
mcnflag.StringSliceFlag{
161+
EnvVar: "HETZNER_SERVER_LABELS",
162+
Name: flagServerLabel,
163+
Usage: "Key value pairs of additional labels to assign to the server",
164+
Value: []string{},
165+
},
157166
}
158167
}
159168

@@ -172,6 +181,11 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
172181
d.UsePrivateNetwork = opts.Bool(flagUsePrivateNetwork)
173182
d.additionalKeys = opts.StringSlice(flagAdditionalKeys)
174183

184+
err := d.setLabelsFromFlags(opts)
185+
if err != nil {
186+
return err
187+
}
188+
175189
d.SetSwarmConfigFromFlags(opts)
176190

177191
if d.AccessToken == "" {
@@ -185,6 +199,18 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
185199
return nil
186200
}
187201

202+
func (d *Driver) setLabelsFromFlags(opts drivers.DriverOptions) error {
203+
d.serverLabels = make(map[string]string)
204+
for _, label := range opts.StringSlice(flagServerLabel) {
205+
split := strings.SplitN(label, "=", 2)
206+
if len(split) != 2 {
207+
return errors.Errorf("server label %v is not in key=value format", label)
208+
}
209+
d.serverLabels[split[0]] = split[1]
210+
}
211+
return nil
212+
}
213+
188214
func (d *Driver) PreCreateCheck() error {
189215
if d.IsExistingKey {
190216
if d.originalKey == "" {
@@ -299,6 +325,7 @@ func (d *Driver) Create() error {
299325
srvopts := hcloud.ServerCreateOpts{
300326
Name: d.GetMachineName(),
301327
UserData: d.userData,
328+
Labels: d.serverLabels,
302329
}
303330
networks := []*hcloud.Network{}
304331
for _, networkIDorName := range d.networks {

0 commit comments

Comments
 (0)