Пример #1
0
func (c *ClusterManager) getInfo() *NodeInfo {
	var info = NodeInfo{}
	s := systemutils.New()

	info.Cpu, _, _ = s.CpuUsage()
	info.Memory = s.MemUsage()
	info.Luns = s.Luns()
	info.NodeId = c.config.NodeId
	info.Ip, _ = externalIp()
	info.Status = StatusOk

	return &info
}
Пример #2
0
func (c *ClusterManager) getCurrentState() *api.Node {
	node := c.getSelf()
	s := systemutils.New()

	node.Cpu, _, _ = s.CpuUsage()
	node.Memory = s.MemUsage()
	node.Luns = s.Luns()

	node.Timestamp = time.Now()

	// Get containers running on this system.
	node.Containers, _ = c.docker.ListContainers(true, true, "")

	return node
}
Пример #3
0
func (c *ClusterManager) getCurrentState() *api.Node {
	c.selfNode.Timestamp = time.Now()
	s := systemutils.New()

	c.selfNode.Cpu, _, _ = s.CpuUsage()
	c.selfNode.Memory = s.MemUsage()
	c.selfNode.Luns = s.Luns()

	c.selfNode.Timestamp = time.Now()

	// Get containers running on this system.
	c.selfNode.Containers, _ = c.docker.ListContainers(docker.ListContainersOptions{All: true})

	return &c.selfNode
}
Пример #4
0
func (c *ClusterManager) Start() error {
	logrus.Info("Cluster manager starting...")

	c.gEnabled = true
	c.selfNode = api.Node{}
	c.selfNode.GenNumber = uint64(time.Now().UnixNano())
	c.selfNode.Id = c.config.NodeId
	c.selfNode.Status = api.StatusOk
	c.selfNode.Ip, _ = externalIp(&c.config)
	c.selfNode.NodeData = make(map[string]interface{})
	c.system = systemutils.New()

	// Start the gossip protocol.
	// XXX Make the port configurable.
	gob.Register(api.Node{})
	c.g = gossip.New("0.0.0.0:9002", types.NodeId(c.config.NodeId),
		c.selfNode.GenNumber)
	c.g.SetGossipInterval(2 * time.Second)

	kvdb := kvdb.Instance()
	kvlock, err := kvdb.Lock("cluster/lock", 60)
	if err != nil {
		logrus.Panic("Fatal, Unable to obtain cluster lock.", err)
	}
	defer kvdb.Unlock(kvlock)

	db, err := readDatabase()
	if err != nil {
		logrus.Panic(err)
	}

	if db.Status == api.StatusInit {
		logrus.Info("Will initialize a new cluster.")

		c.status = api.StatusOk
		db.Status = api.StatusOk
		self, _ := c.initNode(&db)

		err = c.initCluster(&db, self, false)
		if err != nil {
			logrus.Error("Failed to initialize the cluster.", err)
			return err
		}

		// Update the new state of the cluster in the KV Database
		err := writeDatabase(&db)
		if err != nil {
			logrus.Error("Failed to save the database.", err)
			return err
		}

	} else if db.Status&api.StatusOk > 0 {
		logrus.Info("Cluster state is OK... Joining the cluster.")

		c.status = api.StatusOk
		self, exist := c.initNode(&db)

		err = c.joinCluster(&db, self, exist)
		if err != nil {
			logrus.Error("Failed to join cluster.", err)
			return err
		}

		err := writeDatabase(&db)
		if err != nil {
			return err
		}

	} else {
		return errors.New("Fatal, Cluster is in an unexpected state.")
	}

	// Start heartbeating to other nodes.
	c.g.Start()
	go c.heartBeat()

	return nil
}