Skip to content

Commit cadbe7d

Browse files
authored
feat: added network and volume integration (fix: #33) (#34)
1 parent 466c8e0 commit cadbe7d

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

README.md

Lines changed: 6 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/1.3.0/docker-machine-driver-hetzner_1.3.0_linux_amd64.tar.gz
19-
$ tar -xvf docker-machine-driver-hetzner_1.3.0_linux_amd64.tar.gz
18+
$ wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/1.4.0/docker-machine-driver-hetzner_1.4.0_linux_amd64.tar.gz
19+
$ tar -xvf docker-machine-driver-hetzner_1.4.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
@@ -99,6 +99,8 @@ $ docker-machine create \
9999
- `--hetzner-existing-key-id`: **requires `--hetzner-existing-key-path`**. Use an existing (remote) SSH key instead of uploading the imported key pair,
100100
see [SSH Keys API](https://docs.hetzner.cloud/#resources-ssh-keys-get) for how to get a list
101101
- `--hetzner-user-data`: Cloud-init based User data
102+
- `--hetzner-volumes`: Volume IDs or names which should be attached to the server
103+
- `--hetzner-networks`: Network IDs or names which should be attached to the server private network interface
102104

103105
#### Existing SSH keys
104106

@@ -127,6 +129,8 @@ was used during creation.
127129
| `--hetzner-existing-key-path` | `HETZNER_EXISTING_KEY_PATH` | - *(generate new keypair)* |
128130
| `--hetzner-existing-key-id` | `HETZNER_EXISTING_KEY_ID` | 0 *(upload new key)* |
129131
| `--hetzner-user-data` | `HETZNER_USER_DATA` | - |
132+
| `--hetzner-networks` | `HETZNER_NETWORKS` | - |
133+
| `--hetzner-volumes` | `HETZNER_VOLUMES` | - |
130134

131135

132136
## Building from source

driver.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Driver struct {
3737
danglingKey bool
3838
ServerID int
3939
userData string
40+
volumes []string
41+
networks []string
4042
cachedServer *hcloud.Server
4143
}
4244

@@ -52,6 +54,8 @@ const (
5254
flagExKeyID = "hetzner-existing-key-id"
5355
flagExKeyPath = "hetzner-existing-key-path"
5456
flagUserData = "hetzner-user-data"
57+
flagVolumes = "hetzner-volumes"
58+
flagNetworks = "hetzner-networks"
5559
)
5660

5761
func NewDriver() *Driver {
@@ -120,6 +124,18 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
120124
Usage: "Cloud-init based User data",
121125
Value: "",
122126
},
127+
mcnflag.StringSliceFlag{
128+
EnvVar: "HETZNER_VOLUMES",
129+
Name: flagVolumes,
130+
Usage: "Volume IDs or names which should be attached to the server",
131+
Value: []string{},
132+
},
133+
mcnflag.StringSliceFlag{
134+
EnvVar: "HETZNER_NETWORKS",
135+
Name: flagNetworks,
136+
Usage: "Network IDs or names which should be attached to the server private network interface",
137+
Value: []string{},
138+
},
123139
}
124140
}
125141

@@ -133,6 +149,8 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
133149
d.IsExistingKey = d.KeyID != 0
134150
d.originalKey = opts.String(flagExKeyPath)
135151
d.userData = opts.String(flagUserData)
152+
d.volumes = opts.StringSlice(flagVolumes)
153+
d.networks = opts.StringSlice(flagNetworks)
136154

137155
d.SetSwarmConfigFromFlags(opts)
138156

@@ -233,6 +251,25 @@ func (d *Driver) Create() error {
233251
Name: d.GetMachineName(),
234252
UserData: d.userData,
235253
}
254+
networks := []*hcloud.Network{}
255+
for _, networkIDorName := range d.networks {
256+
network, _, err := d.getClient().Network.Get(context.Background(), networkIDorName)
257+
if err != nil {
258+
return errors.Wrap(err, "could not get network by ID or name")
259+
}
260+
networks = append(networks, network)
261+
}
262+
srvopts.Networks = networks
263+
264+
volumes := []*hcloud.Volume{}
265+
for _, volumeIDorName := range d.volumes {
266+
volume, _, err := d.getClient().Volume.Get(context.Background(), volumeIDorName)
267+
if err != nil {
268+
return errors.Wrap(err, "could not get volume by ID or name")
269+
}
270+
volumes = append(volumes, volume)
271+
}
272+
srvopts.Volumes = volumes
236273

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

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ require (
77
github.com/docker/docker v0.0.0-20181018193557-f7e5154f37a4 // indirect
88
github.com/docker/machine v0.16.1
99
github.com/google/go-cmp v0.3.0 // indirect
10-
github.com/hetznercloud/hcloud-go v1.13.0
10+
github.com/hetznercloud/hcloud-go v1.14.0
1111
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1212
github.com/pkg/errors v0.8.1
1313
github.com/sirupsen/logrus v1.4.2 // indirect
1414
github.com/stretchr/testify v1.3.0 // indirect
15-
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f
16-
golang.org/x/sys v0.0.0-20190516110030-61b9204099cb // indirect
15+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
16+
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
1717
gotest.tools v2.2.0+incompatible // indirect
1818
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/docker/machine v0.16.1 h1:zrgroZounGVkxLmBqMyc1uT2GgapXVjIWHCfBf0udrA
99
github.com/docker/machine v0.16.1/go.mod h1:I8mPNDeK1uH+JTcUU7X0ZW8KiYz0jyAgNaeSJ1rCfDI=
1010
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
1111
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
12-
github.com/hetznercloud/hcloud-go v1.13.0 h1:8K60QdD50ZX0vG+eNkJtmEzR+rj1mn/NBUNWtVgi8II=
13-
github.com/hetznercloud/hcloud-go v1.13.0/go.mod h1:g5pff0YNAZywQaivY/CmhUYFVp7oP0nu3MiODC2W4Hw=
12+
github.com/hetznercloud/hcloud-go v1.14.0 h1:6IdF0Vox/6j1pyEdUCbFPIzEH/K9xZZzVuSFro8Y2vw=
13+
github.com/hetznercloud/hcloud-go v1.14.0/go.mod h1:8lR3yHBHZWy2uGcUi9Ibt4UOoop2wrVdERJgCtxsF3Q=
1414
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
1515
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1616
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
@@ -28,14 +28,14 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
2828
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
2929
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3030
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
31-
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo=
32-
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
31+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
32+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
3333
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
3434
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3535
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3636
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
37-
golang.org/x/sys v0.0.0-20190516110030-61b9204099cb h1:k07iPOt0d6nEnwXF+kHB+iEg+WSuKe/SOQuFM2QoD+E=
38-
golang.org/x/sys v0.0.0-20190516110030-61b9204099cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
37+
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
38+
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3939
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4040
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
4141
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

0 commit comments

Comments
 (0)