From 92a4bdbf143c8fb2823f172c9011ab5b9cd5536c Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Sun, 15 Sep 2019 18:59:08 +0700 Subject: [PATCH] implement ssh wait for a VM Signed-off-by: Chanwit Kaewkasi --- cmd/ignite/run/start.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/cmd/ignite/run/start.go b/cmd/ignite/run/start.go index 7bd5a5775..af4baf3e7 100644 --- a/cmd/ignite/run/start.go +++ b/cmd/ignite/run/start.go @@ -2,6 +2,10 @@ package run import ( "fmt" + "net" + "time" + + "github.com/weaveworks/ignite/pkg/apis/ignite" "github.com/weaveworks/ignite/pkg/operations" "github.com/weaveworks/ignite/pkg/preflight/checkers" @@ -47,9 +51,41 @@ func Start(so *startOptions) error { return err } + if err := waitForSSH(so.vm, 10); err != nil { + return err + } + // If starting interactively, attach after starting if so.Interactive { return Attach(so.attachOptions) } return nil } + +func waitForSSH(vm *ignite.VM, seconds int) error { + // When --ssh is enabled, wait until SSH service started on port 22 at most N seconds + ssh := vm.Spec.SSH + if ssh != nil && ssh.Generate { + if len(vm.Status.IPAddresses) > 0 { + addr := vm.Status.IPAddresses[0].String() + ":22" + var err error + for i := 0; i < seconds*10; i++ { + conn, dialErr := net.DialTimeout("tcp", addr, 100*time.Millisecond) + if conn != nil { + defer conn.Close() + err = nil + break + } + err = dialErr + time.Sleep(100 * time.Millisecond) + } + if err != nil { + if err, ok := err.(*net.OpError); ok && err.Timeout() { + return fmt.Errorf("Tried connecting to SSH but timed out %s", err) + } + } + } + } + + return nil +}