Ejemplo n.º 1
0
func load() {
	containers, err := g.Docker.ListContainers(docker.ListContainersOptions{All: true})
	if err != nil {
		logs.Assert(err, "List containers")
	}

	conn := g.GetRedisConn()
	defer g.ReleaseRedisConn(conn)
	containersKey := fmt.Sprintf("eru:agent:%s:containers:meta", g.Config.HostName)
	logs.Debug("Status get targets from", containersKey)
	rep, err := gore.NewCommand("HGETALL", containersKey).Run(conn)
	if err != nil {
		logs.Assert(err, "Status get targets")
	}

	if rep.IsNil() {
		return
	}

	targets, err := rep.Map()
	if err != nil {
		logs.Assert(err, "Status load targets")
	}

	logs.Debug("Status targets:", targets)
	logs.Info("Status load container")
	for _, container := range containers {
		if _, ok := targets[container.ID]; !ok {
			continue
		}

		status := getStatus(container.Status)
		if status != common.STATUS_START {
			reportContainerDeath(container.ID)
			continue
		}
		var meta map[string]interface{}
		if err := json.Unmarshal([]byte(targets[container.ID]), &meta); err != nil {
			logs.Info("Status load failed", err)
			continue
		}

		c, err := g.Docker.InspectContainer(container.ID)
		if err != nil {
			logs.Info("Status inspect docker failed", err)
			continue
		}

		if eruApp := app.NewEruApp(c, meta); eruApp != nil {
			lenz.Attacher.Attach(&eruApp.Meta)
			app.Add(eruApp)
			reportContainerCure(container.ID)
		}
	}
}
Ejemplo n.º 2
0
func load(configPath string) {
	if _, err := os.Stat(configPath); err != nil {
		logs.Assert(err, "config file invaild")
	}

	b, err := ioutil.ReadFile(configPath)
	if err != nil {
		logs.Assert(err, "Read config file failed")
	}

	if err := yaml.Unmarshal(b, &Config); err != nil {
		logs.Assert(err, "Load config file failed")
	}
	logs.Debug("Configure:", Config)
}
Ejemplo n.º 3
0
func HTTPServe() {
	restfulAPIServer := pat.New()

	handlers := map[string]map[string]func(*Request) (int, interface{}){
		"GET": {
			"/profile/":      profile,
			"/version/":      version,
			"/api/app/list/": listEruApps,
		},
		"POST": {
			"/api/container/add/":                    addNewContainer,
			"/api/container/:container_id/addvlan/":  addVlanForContainer,
			"/api/container/:container_id/setroute/": setRouteForContainer,
		},
	}

	for method, routes := range handlers {
		for route, handler := range routes {
			restfulAPIServer.Add(method, route, http.HandlerFunc(JSONWrapper(handler)))
		}
	}

	http.Handle("/", restfulAPIServer)
	logs.Info("API http server start at", g.Config.API.Addr)
	err := http.ListenAndServe(g.Config.API.Addr, nil)
	if err != nil {
		logs.Assert(err, "ListenAndServe: ")
	}
}
Ejemplo n.º 4
0
func GetRedisConn() *gore.Conn {
	conn, err := Rds.Acquire()
	if err != nil || conn == nil {
		logs.Assert(err, "Get redis conn")
	}
	return conn
}
Ejemplo n.º 5
0
func ping() {
	ticker := time.Tick(time.Duration(g.Config.Docker.Health) * time.Second)
	for _ = range ticker {
		if err := g.Docker.Ping(); err != nil {
			url := fmt.Sprintf("%s/api/host/%s/down", g.Config.Eru.Endpoint, g.Config.HostName)
			client := &http.Client{}
			req, _ := http.NewRequest("PUT", url, nil)
			client.Do(req)
			logs.Assert(err, "Docker exit")
		}
	}
}
Ejemplo n.º 6
0
func InitialConn() {
	var err error
	Rds = &gore.Pool{
		InitialConn: Config.Redis.Min,
		MaximumConn: Config.Redis.Max,
	}

	redisHost := fmt.Sprintf("%s:%d", Config.Redis.Host, Config.Redis.Port)
	if err := Rds.Dial(redisHost); err != nil {
		logs.Assert(err, "Redis init failed")
	}

	if Docker, err = defines.NewDocker(
		Config.Docker.Endpoint,
		Config.Docker.Cert,
		Config.Docker.Key,
		Config.Docker.Ca,
	); err != nil {
		logs.Assert(err, "Docker")
	}
	logs.Info("Global connections initiated")
}
Ejemplo n.º 7
0
func InitLenz() {
	Attacher = NewAttachManager(g.Docker)
	Router = NewRouteManager(Attacher, g.Config.Lenz.Stdout)
	Routefs = RouteFileStore(g.Config.Lenz.Routes)
	if len(g.Config.Lenz.Forwards) > 0 {
		logs.Debug("Lenz Routing all to", g.Config.Lenz.Forwards)
		target := defines.Target{Addrs: g.Config.Lenz.Forwards}
		route := defines.Route{ID: "lenz_default", Target: &target}
		route.LoadBackends()
		Router.Add(&route)
	}
	if _, err := os.Stat(g.Config.Lenz.Routes); err == nil {
		logs.Debug("Loading and persisting routes in", g.Config.Lenz.Routes)
		logs.Assert(Router.Load(Routefs), "persistor")
	}
	logs.Info("Lenz initiated")
}
Ejemplo n.º 8
0
func WritePid(path string) {
	if err := ioutil.WriteFile(path, []byte(strconv.Itoa(os.Getpid())), 0755); err != nil {
		logs.Assert(err, "Save pid file failed")
	}
}
Ejemplo n.º 9
0
func InitStatus() {
	logs.Assert(g.Docker.AddEventListener(events), "Attacher")
	logs.Info("Status initiated")
}