Example #1
0
// 注册资料
func dockerGreetings(c *utils.Connection, data []byte) {
	c.Src = string(data)
	config.Dockers[string(data)] = time.Now().Unix()
	log.Println("Docker:", string(data), "is online.")
	log.Println("Dockers:", config.Dockers)
	c.SendCommandString("docker_greetings_reply", config.ClusterAddress)
}
Example #2
0
// 从连接中读取数据,解析并调用相应handler响应
func serve(conn net.Conn) {
	var (
		n          int
		ok         bool
		err        error
		cmd        string
		data       []byte
		payload    []byte
		handler    HandlerFunc
		connection *utils.Connection
	)

	connection = &utils.Connection{Conn: conn}
	ClusterSwitcher.register <- connection

	defer func() {
		ClusterSwitcher.unregister <- connection
		conn.Close()
	}()

	for {
		if n, data, err = connection.Read(); err != nil {
			break
		}
		cmd, payload = utils.CmdDecode(n, data)

		log.Printf("Controller receive cmd:%s", cmd)

		if handler, ok = ClusterSwitcher.handlers[cmd]; ok {
			handler(connection, payload)
		} else {
			log.Printf("Command[%s] does not exist", cmd)
		}
	}
}
Example #3
0
// docker连接到controller,保持着
func connectController(address string) {

	var (
		length     int
		exist      bool
		err        error
		cmd        string
		data       []byte
		payload    []byte
		conn       net.Conn
		handler    HandlerFunc
		connection *utils.Connection
	)

	defer func() { connCloseCh <- address }()

	log.Printf("Connect controller %s", address)

	conn, err = net.Dial("tcp", address)
	if err != nil {
		log.Println(err)
		waitGroup.Done()
		return
	}

	connection = &utils.Connection{
		Conn: conn,
		Src:  address,
	}
	ClusterSwitcher.register <- connection

	defer func() {
		conn.Close()
		ClusterSwitcher.unregister <- connection
	}()

	waitGroup.Done()
	connection.SendCommandString("docker_greetings", config.ClusterAddress)

	for {
		if length, data, err = connection.Read(); err != nil {
			break
		}
		cmd, payload = utils.CmdDecode(length, data)

		log.Printf("Cmd:%s", cmd)

		if handler, exist = ClusterSwitcher.handlers[cmd]; exist {
			handler(connection, payload)
		} else {
			log.Printf("Command[%s] is not exist", cmd)
		}
	}
	log.Printf("Controller %s is disconnect", address)
}
Example #4
0
// 我收了个小弟
// todo:是否要审核
// 由于其知道我的名字我默认相信他
func dockerJoin(c *utils.Connection, data []byte) {
	// 向集群结构配置里面添加新成员
	config.Dockers[string(data)] = time.Now().Unix()
	// 返回组织中领导层所有人姓名以便小弟有事时着他们
	b, err := json.Marshal(config.Controllers)
	if err != nil {
		// 我收集的资料有误
		c.SendCommandString("docker_join", "")
	} else {
		// 告知其组织的领导层所有人员姓名
		c.SendCommandBytes("docker_join", b)
	}
}
Example #5
0
// 结拜了个兄弟
func controllerJoin(c *utils.Connection, data []byte) {
	address := string(data)
	// 把他名字记下来
	config.Controllers[address] = time.Now().Unix()
	// 把我以前结拜的所有兄弟告诉他,让他们也认识一下
	b, err := json.Marshal(config.Controllers)
	if err != nil {
		log.Println(err)
		c.SendCommandString("controller_join", "")
	} else {
		c.SendCommandBytes("controller_join", b)
	}
	// 先断开连接,以免其收到广播我发给小弟的通知
	c.Conn.Close()
	// 告知我的所有小弟,我认了个兄弟,以后的进贡也要给他们一份

	message := fmt.Sprintf("%s %s", address, "controller_join_to_docker")
	ClusterSwitcher.Broadcast(utils.PacketString(message))
	log.Println("Controllers", config.Controllers)
}
Example #6
0
func reportImagesAndContainers(c *utils.Connection) error {
	// 读取镜像列表
	log.Println("Report images start.")
	imageJob := Eng.Job("images")
	imageJob.Setenv("filter", "")
	imageJob.Setenv("all", "0")
	imageSrc, err := imageJob.Stdout.AddPipe()
	if err != nil {
		log.Fatalf("Create images receive pipe error:%s", err)
	}
	// 从管道读取事件数据并广播给所有controller
	go func() {
		imagesBytes, err := ioutil.ReadAll(imageSrc)
		if err != nil {
			log.Println("Read data error from pipe:", err)
		}
		c.SendCommandBytes("docker_images", imagesBytes)
	}()
	if err := imageJob.Run(); err != nil {
		return err
	}
	// 读取容器列表
	log.Println("Report containers start")
	containerJob := Eng.Job("containers")
	containerJob.Setenv("all", "1")
	containerSrc, err := containerJob.Stdout.AddPipe()
	if err != nil {
		log.Fatalf("Create containers receive pipe error:%s", err)
	}
	// 从管道读取事件数据并广播给所有controller
	go func() {
		containersBytes, err := ioutil.ReadAll(containerSrc)
		if err != nil {
			log.Println("Read data error from pipe:", err)
		}
		c.SendCommandBytes("docker_containers", containersBytes)
	}()

	return containerJob.Run()
}
Example #7
0
// 心跳
func heartbeat(c *utils.Connection, data []byte) {
	c.SendCommandString("heartbeat", config.ClusterAddress)
}