blob: e7baeffc79d69890d0ae13f600cf04f4c0fcceed [file] [log] [blame]
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.16
// +build go1.16
// Binary runqemubuildlet runs a single VM-based buildlet in a loop.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"time"
"golang.org/x/build/internal"
)
var (
// Common flags
guestOS = flag.String("guest-os", "windows", "Guest OS to run (one of: windows)")
healthzURL = flag.String("buildlet-healthz-url", "http://localhost:8080/healthz", "URL to buildlet /healthz endpoint.")
// -guest-os=windows flags
windows10Path = flag.String("windows-10-path", defaultWindowsDir(), "Path to Windows image and QEMU dependencies.")
)
func main() {
flag.Parse()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
for ctx.Err() == nil {
var cmd *exec.Cmd
switch *guestOS {
case "windows":
cmd = windows10Cmd(*windows10Path)
default:
log.Fatalf("Unknown guest OS %q", *guestOS)
}
if err := runGuest(ctx, cmd); err != nil {
log.Printf("runWindows10() = %v. Retrying in 10 seconds.", err)
time.Sleep(10 * time.Second)
continue
}
}
}
func runGuest(ctx context.Context, cmd *exec.Cmd) error {
log.Printf("Starting VM: %s", cmd.String())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return fmt.Errorf("cmd.Start() = %w", err)
}
ctx, cancel := heartbeatContext(ctx, 30*time.Second, 10*time.Minute, func(ctx context.Context) error {
return checkBuildletHealth(ctx, *healthzURL)
})
defer cancel()
if err := internal.WaitOrStop(ctx, cmd, os.Interrupt, time.Minute); err != nil {
return fmt.Errorf("WaitOrStop(_, %v, %v, %v) = %w", cmd, os.Interrupt, time.Minute, err)
}
return nil
}