// 我收了个小弟 // 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) } }
// 结拜了个兄弟 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) }
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() }