Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
implement ssh wait for a VM
Browse files Browse the repository at this point in the history
Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
  • Loading branch information
chanwit committed Sep 15, 2019
1 parent e8a2996 commit 223df3a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/ignite/run/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package run

import (
"fmt"
"github.com/weaveworks/ignite/pkg/apis/ignite"
"net"
"time"

"github.com/weaveworks/ignite/pkg/operations"
"github.com/weaveworks/ignite/pkg/preflight/checkers"
Expand Down Expand Up @@ -47,9 +50,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
}

0 comments on commit 223df3a

Please sign in to comment.