// URL /api/container/add/ func addNewContainer(req *Request) (int, interface{}) { type Data struct { Control string `json:"control"` ContainerID string `json:"container_id"` Meta map[string]interface{} `json:"meta"` } data := &Data{} decoder := json.NewDecoder(req.Body) err := decoder.Decode(data) if err != nil { return http.StatusBadRequest, JSON{"message": "wrong JSON format"} } switch data.Control { case "+": if app.Valid(data.ContainerID) { break } logs.Info("API status watch", data.ContainerID) container, err := g.Docker.InspectContainer(data.ContainerID) if err != nil { logs.Info("API status inspect docker failed", err) break } if eruApp := app.NewEruApp(container.ID, container.Name, data.Meta); eruApp != nil { app.Add(eruApp) lenz.Attacher.Attach(&eruApp.Meta) } } return http.StatusOK, JSON{"message": "ok"} }
func monitor() { for event := range events { switch event.Status { case common.STATUS_DIE: logs.Debug("Status", event.Status, event.ID[:12], event.From) app.Remove(event.ID) reportContainerDeath(event.ID) case common.STATUS_START: logs.Debug("Status", event.Status, event.ID[:12], event.From) // if not in watching list, just ignore it if meta := getContainerMeta(event.ID); meta != nil && !app.Valid(event.ID) { container, err := g.Docker.InspectContainer(event.ID) if err != nil { logs.Info("Status inspect docker failed", err) break } eruApp := app.NewEruApp(container, meta) if eruApp == nil { logs.Info("Create EruApp failed") break } lenz.Attacher.Attach(&eruApp.Meta) app.Add(eruApp) reportContainerCure(event.ID) } } } }
func statusWatcher() { conn := g.GetRedisConn() defer g.ReleaseRedisConn(conn) subs := gore.NewSubscriptions(conn) defer subs.Close() subKey := fmt.Sprintf("eru:agent:%s:watcher", g.Config.HostName) logs.Debug("API status subscribe", subKey) subs.Subscribe(subKey) for message := range subs.Message() { if message == nil { logs.Info("API status watcher shutdown") break } command := string(message.Message) logs.Debug("API status watcher get", command) parser := strings.Split(command, "|") if len(parser) != 3 { logs.Info("API status watcher command invaild", command) continue } control, cid, metaString := parser[0], parser[1], parser[2] switch control { case "+": if app.Valid(cid) { break } logs.Info("API status watch", cid[:12]) container, err := g.Docker.InspectContainer(cid) if err != nil { logs.Info("API status inspect docker failed", err) break } var meta map[string]interface{} if err := json.Unmarshal([]byte(metaString), &meta); err != nil { logs.Info("API status load failed", err) break } if eruApp := app.NewEruApp(container, meta); eruApp != nil { lenz.Attacher.Attach(&eruApp.Meta) app.Add(eruApp) } } } }