Support a headless server mode

Add a -headless flag which runs only the debugger server until
a SIGINT is received.
This commit is contained in:
Dan Mace 2015-04-17 16:49:49 -04:00 committed by Derek Parker
parent ca2508af6b
commit 064462e5b5
2 changed files with 24 additions and 13 deletions

@ -3,11 +3,13 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
sys "golang.org/x/sys/unix"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -36,10 +38,12 @@ func main() {
var printv, printhelp bool var printv, printhelp bool
var addr string var addr string
var logEnabled bool var logEnabled bool
var headless bool
flag.BoolVar(&printv, "version", false, "Print version number and exit.") flag.BoolVar(&printv, "version", false, "Print version number and exit.")
flag.StringVar(&addr, "addr", "localhost:0", "Debugging server listen address.") flag.StringVar(&addr, "addr", "localhost:0", "Debugging server listen address.")
flag.BoolVar(&logEnabled, "log", false, "Enable debugging server logging.") flag.BoolVar(&logEnabled, "log", false, "Enable debugging server logging.")
flag.BoolVar(&headless, "headless", false, "Run in headless mode.")
flag.Parse() flag.Parse()
if flag.NFlag() == 0 && len(flag.Args()) == 0 { if flag.NFlag() == 0 && len(flag.Args()) == 0 {
@ -120,15 +124,22 @@ func main() {
}) })
go server.Run() go server.Run()
// Create and start a terminal status := 0
client := rest.NewClient(listener.Addr().String()) if !headless {
term := terminal.New(client) // Create and start a terminal
err, status := term.Run() client := rest.NewClient(listener.Addr().String())
term := terminal.New(client)
err, status = term.Run()
} else {
ch := make(chan os.Signal)
signal.Notify(ch, sys.SIGINT)
<-ch
err = server.Stop(true)
}
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
// Clean up and exit
fmt.Println("[Hope I was of service hunting your bug!]") fmt.Println("[Hope I was of service hunting your bug!]")
os.Exit(status) os.Exit(status)
} }

@ -98,8 +98,13 @@ func (s *RESTServer) Run() error {
return http.Serve(s.listener, container) return http.Serve(s.listener, container)
} }
// Stop detaches from the debugger and waits for it to stop.
func (s *RESTServer) Stop(kill bool) error { func (s *RESTServer) Stop(kill bool) error {
return s.debugger.Detach(kill) err := s.debugger.Detach(kill)
if err != nil {
return err
}
return <-s.debuggerStopped
} }
// writeError writes a simple error response. // writeError writes a simple error response.
@ -117,17 +122,12 @@ func (s *RESTServer) detach(request *restful.Request, response *restful.Response
return return
} }
err = s.debugger.Detach(kill) err = s.Stop(kill)
if err != nil { if err != nil {
writeError(response, http.StatusInternalServerError, err.Error()) writeError(response, http.StatusInternalServerError, err.Error())
return return
} }
err = <-s.debuggerStopped
if err != nil {
writeError(response, http.StatusInternalServerError, err.Error())
return
}
response.WriteHeader(http.StatusOK) response.WriteHeader(http.StatusOK)
} }