func BuildDockerImageStartByHTTPReq(worker, imageName string, dockerfileTarReader io.Reader, tag string) { dockerClient, _ := NewDockerClient(worker, nil) buildImageConfig := &BuildImage{ Context: dockerfileTarReader, RepoName: imageName, SuppressOutput: true, } reader, err := dockerClient.BuildImage(buildImageConfig) if err != nil { log.Println("[ErrorInfo]", err.Error()) } buf := make([]byte, 4096) for { n, err := reader.Read(buf) if err != nil && err != io.EOF { panic(err) } if strings.Contains(string(buf[:n]), `"stream":"Successfully built`) { dockerClient.PushImage(buildImageConfig) } if 0 == n { err = models.PushMsgToList("buildLog:"+tag, "bye") if err != nil { log.Println("[ErrorInfo]", err.Error()) } err = models.PublishMsg("buildLog:"+tag, "bye") if err != nil { log.Println("[ErrorInfo]", err.Error()) } finishJob(worker) break } err = models.PushMsgToList("buildLog:"+tag, string(buf[:n])) if err != nil { log.Println("[ErrorInfo]", err.Error()) } err = models.PublishMsg("buildLog:"+tag, string(buf[:n])) if err != nil { log.Println("[ErrorInfo]", err.Error()) } } }
func BuildDockerImageStartByHTTPReq(worker string, job *Job) { dockerClient, _ := NewDockerClient(worker, nil) reader, err := dockerClient.BuildImage(&(job.ImageConfig)) if err != nil { log.Println("[ErrorInfo]", err.Error()) } else if reader != nil { buf := make([]byte, 4096) for { n, err := reader.Read(buf) if err != nil && err != io.EOF { panic(err) } dockerClient.PushImage(&(job.ImageConfig)) if 0 == n { err = models.PushMsgToList("buildLog:"+job.Tag, "bye") if err != nil { log.Println("[ErrorInfo]", err.Error()) } err = models.PublishMsg("buildLog:"+job.Tag, "bye") if err != nil { log.Println("[ErrorInfo]", err.Error()) } finishJob(worker) break } err = models.PushMsgToList("buildLog:"+job.Tag, string(buf[:n])) if err != nil { log.Println("[ErrorInfo]", err.Error()) } err = models.PublishMsg("buildLog:"+job.Tag, string(buf[:n])) if err != nil { log.Println("[ErrorInfo]", err.Error()) } } } else { log.Println("[ErrorInfo]", err.Error()) } }
// handle job when has a free docker machine and an unhandle job func handleJob() { for { worker := <-freeWorkerList models.MoveFromListByValue("FreeWorkerList", worker, 0) job := <-unhandleJobList jobStr, err := json.Marshal(job) if err != nil { log.Println("[ErrorInfo]", err) } models.MoveFromListByValue("DockerJobList", string(jobStr), 0) busyWorkerList = append(busyWorkerList, worker) models.PushMsgToList("BusyWorkerList", worker) var in io.Reader if job.Mode == "dockerfile" { in = tarDockerFile(job.Context) } else if job.Mode == "archive" { in = readArchive(job.Context) } else { log.Printf("[ErrorInfo] : %v\n", errors.New("Wrong mode, required mode exactly.")) } job.ImageConfig.Context = in go BuildDockerImageStartByHTTPReq(worker, job) } }
func saveToRedis(list BuilderList) { for _, v := range list.Dockers { err := models.PushMsgToList("DockerList", v.IP+":"+v.PORT) if err != nil { log.Println("[ErrorInfo]", err.Error()) } } }
// add a job to job list func addJob(job *Job) { msg, err := json.Marshal(job) if err != nil { log.Println("[ErrorInfo]", err.Error()) } models.PushMsgToList("DockerJobList", string(msg)) unhandleJobList <- job }
// handle job when has a free docker machine and an unhandle job func handleJob() { for { worker := <-freeWorkerList models.MoveFromListByValue("FreeWorkerList", worker, 0) job := <-unhandleJobList jobStr, err := json.Marshal(job) if err != nil { log.Println("[ErrorInfo]", err) } models.MoveFromListByValue("DockerJobList", string(jobStr), 0) busyWorkerList = append(busyWorkerList, worker) models.PushMsgToList("BusyWorkerList", worker) go BuildDockerImageStartByHTTPReq(worker, job.Name, tarDockerFile(job.DockerFile), job.Tag) } }
// add a docker machine to worker list func addWorker(addr string) { models.PushMsgToList("FreeWorkerList", addr) freeWorkerList <- addr }