Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #993 from markdryan/fix-956
Browse files Browse the repository at this point in the history
ciao-down: Update connect and status to handle booting VMs
  • Loading branch information
rbradford authored Jan 6, 2017
2 parents f7af1bd + b809753 commit 837d0a9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
31 changes: 31 additions & 0 deletions testutil/ciao-down/ciao_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"os/exec"
"os/signal"
"syscall"
"time"
)

func init() {
Expand Down Expand Up @@ -257,6 +258,36 @@ func connect(ctx context.Context, errCh chan error) {
return
}

if !vmStarted(ctx, ws.instanceDir) {
errCh <- fmt.Errorf("VM is not running. Try ciao-down start")
return
}

if !sshReady(ctx) {
fmt.Printf("Waiting for VM to boot ")
DONE:
for {
select {
case <-time.After(time.Second):
case <-ctx.Done():
errCh <- fmt.Errorf("Cancelled")
return
}

if !vmStarted(ctx, ws.instanceDir) {
errCh <- fmt.Errorf("VM is not running. Try ciao-down start")
return
}

if sshReady(ctx) {
break DONE
}

fmt.Print(".")
}
fmt.Println()
}

err = syscall.Exec(path, []string{path,
"-q", "-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
Expand Down
39 changes: 32 additions & 7 deletions testutil/ciao-down/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"bufio"
"bytes"
"context"
"fmt"
Expand Down Expand Up @@ -99,16 +100,40 @@ func quitVM(ctx context.Context, instanceDir string) error {
})
}

func statusVM(ctx context.Context, instanceDir, keyPath string) {
status := "ciao down"
ssh := "N/A"
func sshReady(ctx context.Context) bool {
dialer := net.Dialer{}
conn, err := dialer.DialContext(ctx, "tcp", "127.0.0.1:10022")
if err != nil {
return false
}
_ = conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500))
scanner := bufio.NewScanner(conn)
retval := scanner.Scan()
_ = conn.Close()
return retval
}

func vmStarted(ctx context.Context, instanceDir string) bool {
socket := path.Join(instanceDir, "socket")
disconnectedCh := make(chan struct{})
qmp, _, err := qemu.QMPStart(ctx, socket, qemu.QMPConfig{}, disconnectedCh)
if err == nil {
status = "ciao up"
ssh = fmt.Sprintf("ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i %s 127.0.0.1 -p %d", keyPath, 10022)
defer qmp.Shutdown()
if err != nil {
return false
}
qmp.Shutdown()
return true
}

func statusVM(ctx context.Context, instanceDir, keyPath string) {
status := "ciao down"
ssh := "N/A"
if vmStarted(ctx, instanceDir) {
if sshReady(ctx) {
status = "ciao up"
ssh = fmt.Sprintf("ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i %s 127.0.0.1 -p %d", keyPath, 10022)
} else {
status = "ciao up (booting)"
}
}

w := new(tabwriter.Writer)
Expand Down

0 comments on commit 837d0a9

Please sign in to comment.