2014-07-18 18:09:59 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-02-10 13:38:21 -07:00
|
|
|
"path"
|
2014-07-20 23:03:53 -06:00
|
|
|
"time"
|
2014-07-18 18:09:59 -06:00
|
|
|
)
|
|
|
|
|
2014-07-23 21:15:04 -06:00
|
|
|
var running bool = true
|
2014-10-24 22:21:53 -06:00
|
|
|
var maxlogsize uint
|
2014-07-23 21:15:04 -06:00
|
|
|
|
2015-02-10 13:38:21 -07:00
|
|
|
func exists(filename string) bool {
|
|
|
|
_, err := os.Stat(filename); if err != nil {
|
2014-08-12 23:21:19 -06:00
|
|
|
return false
|
|
|
|
}
|
2015-02-10 13:38:21 -07:00
|
|
|
return true
|
2014-07-18 18:09:59 -06:00
|
|
|
}
|
|
|
|
|
2015-02-10 13:38:21 -07:00
|
|
|
func runsvdir(dirname string) {
|
|
|
|
services := make(map[string]*Network)
|
|
|
|
|
|
|
|
dir, err := os.Open(dirname)
|
2014-07-23 21:15:04 -06:00
|
|
|
if err != nil {
|
2015-02-10 13:38:21 -07:00
|
|
|
log.Fatal(err)
|
2014-07-20 23:03:53 -06:00
|
|
|
}
|
2015-02-10 13:38:21 -07:00
|
|
|
defer dir.Close()
|
|
|
|
|
2014-07-20 23:03:53 -06:00
|
|
|
for running {
|
2015-02-10 13:38:21 -07:00
|
|
|
dn, err := dir.Readdirnames(0); if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := make(map[string]bool)
|
|
|
|
for _, fn := range dn {
|
|
|
|
fpath := path.Join(dirname, fn)
|
|
|
|
if exists(path.Join(fpath, "down")) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := services[fpath]; ! ok {
|
|
|
|
if ! exists(path.Join(fpath, "servers")) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newnet := NewNetwork(fpath)
|
|
|
|
services[fpath] = newnet
|
|
|
|
go newnet.Connect()
|
|
|
|
}
|
|
|
|
found[fpath] = true
|
2014-07-20 23:03:53 -06:00
|
|
|
}
|
2015-02-10 13:38:21 -07:00
|
|
|
|
|
|
|
// If anything vanished, disconnect it
|
|
|
|
for fpath, nw := range services {
|
|
|
|
if _, ok := found[fpath]; ! ok {
|
|
|
|
nw.Close()
|
2014-07-20 23:03:53 -06:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 13:38:21 -07:00
|
|
|
|
|
|
|
_, _ = dir.Seek(0, 0)
|
|
|
|
time.Sleep(20 * time.Second)
|
2014-07-20 23:03:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:09:59 -06:00
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] HOST:PORT\n", os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2014-10-29 17:47:04 -06:00
|
|
|
flag.UintVar(&maxlogsize, "logsize", 1000, "Log entries before rotating")
|
2014-07-18 18:09:59 -06:00
|
|
|
flag.Parse()
|
2014-07-20 23:03:53 -06:00
|
|
|
running = false
|
2014-07-18 18:09:59 -06:00
|
|
|
}
|