|
|
@ -39,20 +39,22 @@ func main() {
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
// check if there is an instance running
|
|
|
|
// check if there is an instance running
|
|
|
|
|
|
|
|
startServer := true
|
|
|
|
b, err := ioutil.ReadFile(addrFile)
|
|
|
|
b, err := ioutil.ReadFile(addrFile)
|
|
|
|
switch {
|
|
|
|
if err == nil {
|
|
|
|
// existing instance but no command to run
|
|
|
|
|
|
|
|
case err == nil && len(flag.Args()) == 0:
|
|
|
|
|
|
|
|
log.Println("porter already running on", string(b))
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// existing instance with command to run
|
|
|
|
|
|
|
|
case err == nil:
|
|
|
|
|
|
|
|
addr = string(b)
|
|
|
|
addr = string(b)
|
|
|
|
log.Println("re-using porter instance on", addr)
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
log.Println("found running porter instance at", addr)
|
|
|
|
|
|
|
|
startServer = false
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.Printf("found dead porter instance at %s, will take over", addr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// new instance
|
|
|
|
args := flag.Args()
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
if startServer {
|
|
|
|
if err := ioutil.WriteFile(addrFile, []byte(addr), 0644); err != nil {
|
|
|
|
if err := ioutil.WriteFile(addrFile, []byte(addr), 0644); err != nil {
|
|
|
|
log.Fatalf("Cannot write %s: %s", addrFile, err)
|
|
|
|
log.Fatalf("Cannot write %s: %s", addrFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -63,9 +65,12 @@ func main() {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
|
|
|
log.Println("no command and existing porter instance found, exiting")
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
args := flag.Args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// no command to run: wait for CTRL-C
|
|
|
|
// no command to run: wait for CTRL-C
|
|
|
|
if len(args) == 0 {
|
|
|
|
if len(args) == 0 {
|
|
|
|