diff options
| author | xengineering <me@xengineering.eu> | 2024-07-28 13:18:23 +0200 | 
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2024-07-28 18:37:11 +0200 | 
| commit | 419fe88d38145973ef9aa387e877be402c5e7e28 (patch) | |
| tree | 024341fdecedc2e8b481379b3bd68d4ce22974ba | |
| parent | 1b78d4af89791791b68b51a72cbcf0775038108d (diff) | |
| download | craft-419fe88d38145973ef9aa387e877be402c5e7e28.tar craft-419fe88d38145973ef9aa387e877be402c5e7e28.tar.zst craft-419fe88d38145973ef9aa387e877be402c5e7e28.zip  | |
Wait for VM to boot
This waits for a little delay and then tries to run `true` via SSH on
the virtual machine. If this does not work for a maximum amount of
attempts a fatal error is raised and an appropriate log message is
written.
| -rw-r--r-- | main.go | 48 | 
1 files changed, 44 insertions, 4 deletions
@@ -9,17 +9,20 @@ import (  	"os/exec"  	"path/filepath"  	"strings" +	"time"  )  var ( -	repo = "" +	key    = "" +	repo   = ""  	commit = "" -	task = "" -	image = "" -	port = "" +	task   = "" +	image  = "" +	port   = ""  )  func init() { +	flag.StringVar(&key, "key", "", "Path to SSH private key")  	flag.StringVar(&repo, "repo", "", "Source code as valid Git URL")  	flag.StringVar(&commit, "commit", "", "Commit or commit-ish reference for checkout")  	flag.StringVar(&task, "task", "", "Shell code to execute for the build") @@ -47,6 +50,8 @@ func main() {  		}  	}() +	waitBoot() +  	workbench := prepareWorkbench(repo, commit)  	defer os.RemoveAll(workbench)  	craft(workbench) @@ -88,6 +93,41 @@ func qemu() *exec.Cmd {  	)  } +func sshCommand(name string, arg ...string) *exec.Cmd { +	return exec.Command( +		"ssh", +		append([]string{ +			"-p", +			port, +			"-o", +			"UserKnownHostsFile=/dev/null", +			"-o", +			"StrictHostKeyChecking=no", +			"-i", +			key, +			"root@localhost", +			name, +		}, arg...)..., +	) +} + +func waitBoot() { +	retries := 5 +	initialDelay := 1 * time.Second + +	log.Println("Waiting for VM to boot") +	time.Sleep(initialDelay) // without that `ssh` returns too quickly +	for range retries { +		cmd := sshCommand("true") +		err := cmd.Run() +		if err == nil { +			log.Println("VM booted") +			return +		} +	} +	log.Fatalf("Could not reach VM %d times - giving up", retries) +} +  func craft(workbench string) {  	script := fmt.Sprintf(`#!/bin/sh  | 
