cmd/buildlet: reboot solaris VMs per build

They reboot quickly. Make sure they don't leave cruft behind.

Change-Id: Ibab22676c90a87f276e456673e479e16f5f60f4f
Reviewed-on: https://go-review.googlesource.com/37494
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/cmd/buildlet/buildlet.go b/cmd/buildlet/buildlet.go
index 5018294..7540b08 100644
--- a/cmd/buildlet/buildlet.go
+++ b/cmd/buildlet/buildlet.go
@@ -49,6 +49,7 @@
 
 var (
 	haltEntireOS = flag.Bool("halt", true, "halt OS in /halt handler. If false, the buildlet process just ends.")
+	rebootOnHalt = flag.Bool("reboot", false, "reboot system in /halt handler.")
 	workDir      = flag.String("workdir", "", "Temporary directory to use. The contents of this directory may be deleted at any time. If empty, TempDir is used to create one.")
 	listenAddr   = flag.String("listen", "AUTO", "address to listen on. Unused in reverse mode. Warning: this service is inherently insecure and offers no protection of its own. Do not expose this port to the world.")
 	reverse      = flag.String("reverse", "", "if non-empty, go into reverse mode where the buildlet dials the coordinator instead of listening for connections. The value is a comma-separated list of modes, e.g. 'darwin-arm,darwin-amd64-race'")
@@ -108,6 +109,13 @@
 	log.Printf("buildlet starting.")
 	flag.Parse()
 
+	if *reverse == "solaris-amd64-smartosbuildlet" {
+		// These machines were setup without GO_BUILDER_ENV
+		// set in their base image, so do init work here after
+		// flag parsing instead of at top.
+		*rebootOnHalt = true
+	}
+
 	// Optimize emphemeral filesystems. Prefer speed over safety, since these machines
 	// will be gone soon.
 	switch runtime.GOOS {
@@ -973,12 +981,25 @@
 		http.Error(w, "requires POST method", http.StatusBadRequest)
 		return
 	}
+
+	// Do the halt in 1 second, to give the HTTP response time to
+	// complete.
+	//
+	// TODO(bradfitz): maybe prevent any (unlikely) future HTTP
+	// requests from doing anything from this point on in the
+	// remaining second.
 	log.Printf("Halting in 1 second.")
-	// do the halt in 1 second, to give the HTTP response time to complete:
-	time.AfterFunc(1*time.Second, haltMachine)
+	time.AfterFunc(1*time.Second, doHalt)
 }
 
-func haltMachine() {
+func doHalt() {
+	if *rebootOnHalt {
+		if err := exec.Command("reboot").Run(); err != nil {
+			log.Printf("Error running reboot: %v", err)
+		}
+		os.Exit(0)
+
+	}
 	if !*haltEntireOS {
 		log.Printf("Ending buildlet process due to halt.")
 		os.Exit(0)