func monitorHosts() { ctx, err := getaredis.Init(*configFileName) if ctx != nil { defer ctx.Close() } errLogger := log.New(os.Stderr, "MonitorHosts ", log.LstdFlags) outLogger := log.New(os.Stdout, "MonitorHosts ", log.LstdFlags) outLogger.Println("Started") defer outLogger.Println("Finished") if err != nil { errLogger.Println("Error :" + err.Error()) return } started, deleted, err := getaredis.MonitorHosts(ctx) if err != nil { errLogger.Println("Error :" + err.Error()) return } if started { outLogger.Println("A new host has started!") } if deleted != nil && len(deleted) > 0 { outLogger.Printf("Hosts %v have been removed.", deleted) } }
func cleanRedisInstances() { ctx, err := getaredis.Init(*configFileName) if ctx != nil { defer ctx.Close() } errLogger := log.New(os.Stderr, "CleanRedisInstances ", log.LstdFlags) outLogger := log.New(os.Stdout, "CleanRedisInstances ", log.LstdFlags) outLogger.Println("Started") defer outLogger.Println("Finished") if err != nil { errLogger.Println("Error :" + err.Error()) return } cleanedInstances := getaredis.CleanRedisInstances(ctx) if cleanedInstances != nil && len(cleanedInstances) > 0 { outLogger.Printf("Containers %v have been removed.", cleanedInstances) } }
func main() { configFileName := flag.String("config", "", "Configuration file path") port := flag.Int("port", 8080, "Server listening port") flag.Parse() if *configFileName == "" { log.Fatal("A configuration file must be provided.") } ctx, err := getaredis.Init(*configFileName) if err != nil { fmt.Println(err) return } m := martini.Classic() m.Use(render.Renderer()) m.Get("/", func(r render.Render) { count, err := ctx.CountContainers(true) if err != nil { count = -1 } r.HTML(200, "index", count) }) m.Post("/instance", func(res http.ResponseWriter, req *http.Request) (int, string) { requesterIP := getIP(req) err := getaredis.CheckInstanceLimit(ctx, requesterIP) if err != nil { return 403, err.Error() } instance, err := ctx.NewInstance(requesterIP) if err != nil { return 500, err.Error() } return 200, fmt.Sprintf("{\"IP\": \"%v\", \"port\": \"%v\", \"password\": \"%v\"}", instance.HostedAtIP, instance.HostedAtPort, instance.Password) }) m.RunOnAddr("127.0.0.1:" + strconv.Itoa(*port)) }