func (this *Build) DockerBuild(workDir string) { this.UpdateBuildStatus("docker building...") logFile := fmt.Sprintf("%s/%d.log", g.LogDir, this.Id) cmd := exec.Command(g.BuildScript, workDir, this.UserName, this.App, this.Version, g.Registry, logFile) err := cmd.Start() if err != nil { this.UpdateBuildStatus(fmt.Sprintf("error occur when build: %v", err)) log.Printf("start cmd fail: %v when docker build %s/Dockerfile", err, workDir) return } err, timeout := systool.CmdRunWithTimeout(cmd, g.BuildTimeout) if err != nil { log.Printf("docker build %s/Dockerfile fail: %v", workDir, err) } if timeout { log.Printf("docker build %s/Dockerfile timeout", workDir) } if !timeout && err == nil { this.UpdateBuildStatus("successfully:-)") image := fmt.Sprintf("%s/%s/%s:%s", g.Registry, this.UserName, this.App, this.Version) this.UpdateImage(image) f, err := os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY, 0600) if err == nil { defer f.Close() f.WriteString("\ndone:-)\n") } os.RemoveAll(workDir) } else { this.UpdateBuildStatus(fmt.Sprintf("is_timeout: %v, err: %v", timeout, err)) } }
func PluginRun(plugin *Plugin) { timeout := plugin.Cycle*1000 - 500 fpath := filepath.Join(g.Config().Plugin.Dir, plugin.FilePath) if !file.IsExist(fpath) { log.Println("no such plugin:", fpath) return } debug := g.Config().Debug if debug { log.Println(fpath, "running...") } cmd := exec.Command(fpath) var stdout bytes.Buffer cmd.Stdout = &stdout var stderr bytes.Buffer cmd.Stderr = &stderr cmd.Start() err, isTimeout := sys.CmdRunWithTimeout(cmd, time.Duration(timeout)*time.Millisecond) errStr := stderr.String() if errStr != "" { logFile := filepath.Join(g.Config().Plugin.LogDir, plugin.FilePath+".stderr.log") if _, err = file.WriteString(logFile, errStr); err != nil { log.Printf("[ERROR] write log to %s fail, error: %s\n", logFile, err) } } if isTimeout { // has be killed if err == nil && debug { log.Println("[INFO] timeout and kill process", fpath, "successfully") } if err != nil { log.Println("[ERROR] kill process", fpath, "occur error:", err) } return } if err != nil { log.Println("[ERROR] exec plugin", fpath, "fail. error:", err) return } // exec successfully data := stdout.Bytes() if len(data) == 0 { if debug { log.Println("[DEBUG] stdout of", fpath, "is blank") } return } var metrics []*model.MetricValue err = json.Unmarshal(data, &metrics) if err != nil { log.Printf("[ERROR] json.Unmarshal stdout of %s fail. error:%s stdout: \n%s\n", fpath, err, stdout.String()) return } g.SendToTransfer(metrics) }