d2vm/run: hetzner: use tcp to wait for the server to be ready, do not store server key in UserKnownHostsFile

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-08-06 10:40:51 +02:00
parent 9f702e5071
commit dd1b5006cb
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF
1 changed files with 21 additions and 23 deletions

View File

@ -5,9 +5,9 @@ import (
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"time"
"github.com/dustin/go-humanize"
@ -242,21 +242,10 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
if err := <-errs; err != nil {
return err
}
logrus.Infof("server created")
remove = false
args := []string{"-o", "StrictHostKeyChecking=no"}
if hetznerSSHKeyPath != "" {
args = append(args, "-i", hetznerSSHKeyPath)
}
args = append(args, fmt.Sprintf("%s@%s", hetznerSSHUser, sres.Server.PublicNet.IPv4.IP.String()))
makeCmd := func() *exec.Cmd {
cmd := exec.CommandContext(ctx, "ssh", args...)
cmd.Stdin = stdin
cmd.Stderr = stderr
cmd.Stdout = stdout
return cmd
}
logrus.Infof("waiting for server to be ready")
t := time.NewTimer(time.Minute)
wait:
for {
select {
case <-t.C:
@ -264,16 +253,25 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
case <-ctx.Done():
return ctx.Err()
default:
cmd := makeCmd()
if err := cmd.Run(); err != nil {
if strings.Contains(err.Error(), "exit status 255") {
time.Sleep(time.Second)
continue
}
return err
} else {
return nil
conn, err := net.Dial("tcp", fmt.Sprintf("%s:22", sres.Server.PublicNet.IPv4.IP.String()))
if err == nil {
conn.Close()
break wait
}
time.Sleep(time.Second)
}
}
args := []string{"-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null"}
if hetznerSSHKeyPath != "" {
args = append(args, "-i", hetznerSSHKeyPath)
}
args = append(args, fmt.Sprintf("%s@%s", hetznerSSHUser, sres.Server.PublicNet.IPv4.IP.String()))
cmd := exec.CommandContext(ctx, "ssh", args...)
cmd.Stdin = stdin
cmd.Stderr = stderr
cmd.Stdout = stdout
if err := cmd.Run(); err != nil {
return err
}
return nil
}