Skip to content
GitLab
Explore
Sign in
Commits on Source (1)
bugfix: Make server location, SSH key optional (closes #110, thanks @avpnusr)
· 711d027d
JonasS
authored
Jul 19, 2023
- reduce overly pedantic null checks - indicate nullability in getter name
711d027d
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
711d027d
...
...
@@ -15,8 +15,8 @@ You can find sources and pre-compiled binaries [here](https://github.com/JonasPr
```
bash
# Download the binary (this example downloads the binary for linux amd64)
$
wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/4.1.
0
/docker-machine-driver-hetzner_4.1.
0
_linux_amd64.tar.gz
$
tar
-xvf
docker-machine-driver-hetzner_4.1.
0
_linux_amd64.tar.gz
$
wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/4.1.
2
/docker-machine-driver-hetzner_4.1.
2
_linux_amd64.tar.gz
$
tar
-xvf
docker-machine-driver-hetzner_4.1.
2
_linux_amd64.tar.gz
# Make it executable and copy the binary in a directory accessible with your $PATH
$
chmod
+x docker-machine-driver-hetzner
...
...
driver/driver.go
View file @
711d027d
...
...
@@ -425,7 +425,7 @@ func (d *Driver) PreCreateCheck() error {
return
errors
.
Wrap
(
err
,
"could not get image"
)
}
if
_
,
err
:=
d
.
getLocation
();
err
!=
nil
{
if
_
,
err
:=
d
.
getLocation
Nullable
();
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"could not get location"
)
}
...
...
@@ -563,7 +563,7 @@ func (d *Driver) Remove() error {
// failure to remove a server-specific key is a hard error
if
!
d
.
IsExistingKey
&&
d
.
KeyID
!=
0
{
key
,
err
:=
d
.
getKey
()
key
,
err
:=
d
.
getKey
Nullable
()
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"could not get ssh key"
)
}
...
...
driver/hetzner_query.go
View file @
711d027d
...
...
@@ -14,10 +14,13 @@ func (d *Driver) getClient() *hcloud.Client {
return
hcloud
.
NewClient
(
hcloud
.
WithToken
(
d
.
AccessToken
),
hcloud
.
WithApplication
(
"docker-machine-driver"
,
d
.
version
))
}
func
(
d
*
Driver
)
getLocation
()
(
*
hcloud
.
Location
,
error
)
{
func
(
d
*
Driver
)
getLocation
Nullable
()
(
*
hcloud
.
Location
,
error
)
{
if
d
.
cachedLocation
!=
nil
{
return
d
.
cachedLocation
,
nil
}
if
d
.
Location
==
""
{
return
nil
,
nil
}
location
,
_
,
err
:=
d
.
getClient
()
.
Location
.
GetByName
(
context
.
Background
(),
d
.
Location
)
if
err
!=
nil
{
...
...
@@ -95,6 +98,17 @@ func (d *Driver) getImageArchitectureForLookup() (hcloud.Architecture, error) {
}
func
(
d
*
Driver
)
getKey
()
(
*
hcloud
.
SSHKey
,
error
)
{
key
,
err
:=
d
.
getKeyNullable
()
if
err
!=
nil
{
return
nil
,
err
}
if
key
==
nil
{
return
nil
,
fmt
.
Errorf
(
"key not found: %v"
,
d
.
KeyID
)
}
return
key
,
err
}
func
(
d
*
Driver
)
getKeyNullable
()
(
*
hcloud
.
SSHKey
,
error
)
{
if
d
.
cachedKey
!=
nil
{
return
d
.
cachedKey
,
nil
}
...
...
@@ -103,14 +117,11 @@ func (d *Driver) getKey() (*hcloud.SSHKey, error) {
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"could not get sshkey by ID"
)
}
if
key
==
nil
{
return
nil
,
fmt
.
Errorf
(
"key not found: %v"
,
d
.
KeyID
)
}
d
.
cachedKey
=
key
return
instrumented
(
key
),
nil
}
func
(
d
*
Driver
)
getRemoteKeyWithSameFingerprint
(
publicKeyBytes
[]
byte
)
(
*
hcloud
.
SSHKey
,
error
)
{
func
(
d
*
Driver
)
getRemoteKeyWithSameFingerprint
Nullable
(
publicKeyBytes
[]
byte
)
(
*
hcloud
.
SSHKey
,
error
)
{
publicKey
,
_
,
_
,
_
,
err
:=
ssh
.
ParseAuthorizedKey
(
publicKeyBytes
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"could not parse ssh public key"
)
...
...
@@ -122,9 +133,6 @@ func (d *Driver) getRemoteKeyWithSameFingerprint(publicKeyBytes []byte) (*hcloud
if
err
!=
nil
{
return
remoteKey
,
errors
.
Wrap
(
err
,
"could not get sshkey by fingerprint"
)
}
if
remoteKey
==
nil
{
return
nil
,
fmt
.
Errorf
(
"key not found by fingerprint: %v"
,
fp
)
}
return
instrumented
(
remoteKey
),
nil
}
...
...
driver/setup.go
View file @
711d027d
...
...
@@ -72,7 +72,7 @@ func (d *Driver) makeCreateServerOptions() (*hcloud.ServerCreateOpts, error) {
}
srvopts
.
Volumes
=
volumes
if
srvopts
.
Location
,
err
=
d
.
getLocation
();
err
!=
nil
{
if
srvopts
.
Location
,
err
=
d
.
getLocation
Nullable
();
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"could not get location"
)
}
if
srvopts
.
ServerType
,
err
=
d
.
getType
();
err
!=
nil
{
...
...
driver/ssh_keys.go
View file @
711d027d
...
...
@@ -70,7 +70,7 @@ func (d *Driver) createRemoteKeys() error {
return
errors
.
Wrap
(
err
,
"could not read ssh public key"
)
}
key
,
err
:=
d
.
getRemoteKeyWithSameFingerprint
(
buf
)
key
,
err
:=
d
.
getRemoteKeyWithSameFingerprint
Nullable
(
buf
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"error retrieving potentially existing key"
)
}
...
...
@@ -89,7 +89,7 @@ func (d *Driver) createRemoteKeys() error {
d
.
KeyID
=
key
.
ID
}
for
i
,
pubkey
:=
range
d
.
AdditionalKeys
{
key
,
err
:=
d
.
getRemoteKeyWithSameFingerprint
([]
byte
(
pubkey
))
key
,
err
:=
d
.
getRemoteKeyWithSameFingerprint
Nullable
([]
byte
(
pubkey
))
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"error checking for existing key for %v"
,
pubkey
)
}
...
...