Skip to content

Commit 1b4dea2

Browse files
authored
feat: added option for using private network (#44)
1 parent 694686d commit 1b4dea2

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ $ docker-machine create \
101101
- `--hetzner-user-data`: Cloud-init based User data
102102
- `--hetzner-volumes`: Volume IDs or names which should be attached to the server
103103
- `--hetzner-networks`: Network IDs or names which should be attached to the server private network interface
104+
- `--hetzner-use-private-network`: Use private network
104105

105106
#### Existing SSH keys
106107

@@ -131,6 +132,7 @@ was used during creation.
131132
| `--hetzner-user-data` | `HETZNER_USER_DATA` | - |
132133
| `--hetzner-networks` | `HETZNER_NETWORKS` | - |
133134
| `--hetzner-volumes` | `HETZNER_VOLUMES` | - |
135+
| `--hetzner-use-private-network` | `HETZNER_USE_PRIVATE_NETWORK` | false |
134136

135137

136138
## Building from source

driver.go

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,42 @@ import (
2222
type Driver struct {
2323
*drivers.BaseDriver
2424

25-
AccessToken string
26-
Image string
27-
ImageID int
28-
cachedImage *hcloud.Image
29-
Type string
30-
cachedType *hcloud.ServerType
31-
Location string
32-
cachedLocation *hcloud.Location
33-
KeyID int
34-
cachedKey *hcloud.SSHKey
35-
IsExistingKey bool
36-
originalKey string
37-
danglingKey bool
38-
ServerID int
39-
userData string
40-
volumes []string
41-
networks []string
42-
cachedServer *hcloud.Server
25+
AccessToken string
26+
Image string
27+
ImageID int
28+
cachedImage *hcloud.Image
29+
Type string
30+
cachedType *hcloud.ServerType
31+
Location string
32+
cachedLocation *hcloud.Location
33+
KeyID int
34+
cachedKey *hcloud.SSHKey
35+
IsExistingKey bool
36+
originalKey string
37+
danglingKey bool
38+
ServerID int
39+
userData string
40+
volumes []string
41+
networks []string
42+
UsePrivateNetwork bool
43+
cachedServer *hcloud.Server
4344
}
4445

4546
const (
4647
defaultImage = "ubuntu-18.04"
4748
defaultType = "cx11"
4849

49-
flagAPIToken = "hetzner-api-token"
50-
flagImage = "hetzner-image"
51-
flagImageID = "hetzner-image-id"
52-
flagType = "hetzner-server-type"
53-
flagLocation = "hetzner-server-location"
54-
flagExKeyID = "hetzner-existing-key-id"
55-
flagExKeyPath = "hetzner-existing-key-path"
56-
flagUserData = "hetzner-user-data"
57-
flagVolumes = "hetzner-volumes"
58-
flagNetworks = "hetzner-networks"
50+
flagAPIToken = "hetzner-api-token"
51+
flagImage = "hetzner-image"
52+
flagImageID = "hetzner-image-id"
53+
flagType = "hetzner-server-type"
54+
flagLocation = "hetzner-server-location"
55+
flagExKeyID = "hetzner-existing-key-id"
56+
flagExKeyPath = "hetzner-existing-key-path"
57+
flagUserData = "hetzner-user-data"
58+
flagVolumes = "hetzner-volumes"
59+
flagNetworks = "hetzner-networks"
60+
flagUsePrivateNetwork = "hetzner-use-private-network"
5961
)
6062

6163
func NewDriver() *Driver {
@@ -136,6 +138,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
136138
Usage: "Network IDs or names which should be attached to the server private network interface",
137139
Value: []string{},
138140
},
141+
mcnflag.BoolFlag{
142+
EnvVar: "HETZNER_USE_PRIVATE_NETWORK",
143+
Name: flagUsePrivateNetwork,
144+
Usage: "Use private network",
145+
},
139146
}
140147
}
141148

@@ -151,6 +158,7 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
151158
d.userData = opts.String(flagUserData)
152159
d.volumes = opts.StringSlice(flagVolumes)
153160
d.networks = opts.StringSlice(flagNetworks)
161+
d.UsePrivateNetwork = opts.Bool(flagUsePrivateNetwork)
154162

155163
d.SetSwarmConfigFromFlags(opts)
156164

@@ -205,6 +213,10 @@ func (d *Driver) PreCreateCheck() error {
205213
return errors.Wrap(err, "could not get location")
206214
}
207215

216+
if d.UsePrivateNetwork && len(d.networks) == 0 {
217+
return errors.Errorf("No private network attached.")
218+
}
219+
208220
return nil
209221
}
210222

@@ -319,9 +331,26 @@ func (d *Driver) Create() error {
319331
time.Sleep(1 * time.Second)
320332
}
321333

322-
log.Debugf(" -> Server %s[%d] ready", srv.Server.Name, srv.Server.ID)
323-
d.IPAddress = srv.Server.PublicNet.IPv4.IP.String()
334+
if d.UsePrivateNetwork {
335+
for {
336+
// we need to wait until network is attached
337+
log.Infof("Wait until private network attached ...")
338+
server, _, err := d.getClient().Server.GetByID(context.Background(), srv.Server.ID)
339+
if err != nil {
340+
return errors.Wrapf(err, "could not get newly created server [%d]", srv.Server.ID)
341+
}
342+
if server.PrivateNet != nil {
343+
d.IPAddress = server.PrivateNet[0].IP.String()
344+
break
345+
}
346+
time.Sleep(1 * time.Second)
347+
}
348+
} else {
349+
log.Infof("Using public network ...")
350+
d.IPAddress = srv.Server.PublicNet.IPv4.IP.String()
351+
}
324352

353+
log.Infof(" -> Server %s[%d] ready. Ip %s", srv.Server.Name, srv.Server.ID, d.IPAddress)
325354
d.danglingKey = false
326355

327356
return nil

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ go 1.12
55
require (
66
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
77
github.com/docker/docker v0.0.0-20181018193557-f7e5154f37a4 // indirect
8-
github.com/docker/machine v0.16.1
8+
github.com/docker/machine v0.16.2
99
github.com/google/go-cmp v0.3.0 // indirect
10-
github.com/hetznercloud/hcloud-go v1.14.0
10+
github.com/hetznercloud/hcloud-go v1.17.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

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ github.com/docker/docker v0.0.0-20181018193557-f7e5154f37a4 h1:u7P9ul4ElF92ZjxTw
77
github.com/docker/docker v0.0.0-20181018193557-f7e5154f37a4/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
88
github.com/docker/machine v0.16.1 h1:zrgroZounGVkxLmBqMyc1uT2GgapXVjIWHCfBf0udrA=
99
github.com/docker/machine v0.16.1/go.mod h1:I8mPNDeK1uH+JTcUU7X0ZW8KiYz0jyAgNaeSJ1rCfDI=
10+
github.com/docker/machine v0.16.2 h1:jyF9k3Zg+oIGxxSdYKPScyj3HqFZ6FjgA/3sblcASiU=
11+
github.com/docker/machine v0.16.2/go.mod h1:I8mPNDeK1uH+JTcUU7X0ZW8KiYz0jyAgNaeSJ1rCfDI=
1012
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
1113
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
1214
github.com/hetznercloud/hcloud-go v1.14.0 h1:6IdF0Vox/6j1pyEdUCbFPIzEH/K9xZZzVuSFro8Y2vw=
1315
github.com/hetznercloud/hcloud-go v1.14.0/go.mod h1:8lR3yHBHZWy2uGcUi9Ibt4UOoop2wrVdERJgCtxsF3Q=
16+
github.com/hetznercloud/hcloud-go v1.17.0 h1:IKH0GLLoTEfgMuBY+GaaVTwjYChecrHFVo4/t0sIkGU=
17+
github.com/hetznercloud/hcloud-go v1.17.0/go.mod h1:8lR3yHBHZWy2uGcUi9Ibt4UOoop2wrVdERJgCtxsF3Q=
1418
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
1519
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1620
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=

0 commit comments

Comments
 (0)