func ReceiveTaskCommand(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() id := r.URL.Query().Get(":ID") sInterface, err := db.Get(liboct.DBScheduler, id) if err != nil { liboct.RenderError(w, err) return } s, _ := liboct.SchedulerFromString(sInterface.String()) result, _ := ioutil.ReadAll(r.Body) logrus.Debugf("Receive task Command %s", string(result)) r.Body.Close() /* Donnot use this now FIXME var cmd liboct.TestActionCommand json.Unmarshal([]byte(result), &cmd) */ action, err := liboct.TestActionFromString(string(result)) if err != nil { liboct.RenderError(w, err) return } err = s.Command(action) db.Update(liboct.DBScheduler, id, s) if err != nil { liboct.RenderError(w, err) } else { liboct.RenderOK(w, "", nil) } }
func GetTaskReport(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() id := r.URL.Query().Get(":ID") sInterface, err := db.Get(liboct.DBScheduler, id) if err != nil { liboct.RenderError(w, err) return } s, _ := liboct.SchedulerFromString(sInterface.String()) //Send the collect command to the octd if err := s.Command(liboct.TestActionCollect); err != nil { db.Update(liboct.DBScheduler, id, s) liboct.RenderError(w, err) return } else { db.Update(liboct.DBScheduler, id, s) } //Tar the reports in the cacheDir reportURL := path.Join(strings.TrimSuffix(s.Case.GetBundleURL(), ".tar.gz"), "source", "reports.tar.gz") found := false logrus.Debugf("Get reportURL %s", reportURL) _, err = os.Stat(reportURL) if err != nil { logrus.Debugf("Regenerate the report") var reports []string for index := 0; index < len(s.Case.Units); index++ { if len(s.Case.Units[index].ReportURL) > 0 { reports = append(reports, s.Case.Units[index].ReportURL) } //Add log reports = append(reports, fmt.Sprintf("%s.log", s.Case.Units[index].Name)) } tarDir := path.Join(strings.TrimSuffix(s.Case.GetBundleURL(), ".tar.gz"), "source") reportURL, found = liboct.TarFileList(reports, tarDir, "reports") } if !found { liboct.RenderError(w, err) return } file, err := os.Open(reportURL) defer file.Close() if err != nil { liboct.RenderError(w, err) } else { buf := bytes.NewBufferString("") buf.ReadFrom(file) //Different write back, not json w.Write([]byte(buf.String())) } }
// Since the sheduler ID is got after receiving the test files // we need to move it to a better place // /tmp/.../A.tar.gz --> /tmp/.../id/A.tar.gz func updateSchedulerBundle(id string, oldURL string) { db := liboct.GetDefaultDB() sInterface, _ := db.Get(liboct.DBScheduler, id) s, _ := liboct.SchedulerFromString(sInterface.String()) dir := path.Dir(path.Dir(oldURL)) newURL := fmt.Sprintf("%s/%s", path.Join(dir, id), path.Base(oldURL)) liboct.PreparePath(path.Join(dir, id), "") logrus.Debugf("Old URL %s, New URL %s", oldURL, newURL) os.Rename(strings.TrimSuffix(oldURL, ".tar.gz"), strings.TrimSuffix(newURL, ".tar.gz")) os.Rename(oldURL, newURL) //bundleURL is the directory of the bundle s.Case.SetBundleURL(strings.TrimSuffix(newURL, ".tar.gz")) db.Update(liboct.DBScheduler, id, s) os.RemoveAll(path.Dir(oldURL)) }