func ModifyRepo(w http.ResponseWriter, r *http.Request) { repoID := r.URL.Query().Get(":ID") action := r.URL.Query().Get("Action") db := liboct.GetDefaultDB() val, err := db.Get(liboct.DBRepo, repoID) if err != nil { liboct.RenderError(w, err) return } if action == "Modify" { var newRepo liboct.TestCaseRepo result, _ := ioutil.ReadAll(r.Body) r.Body.Close() err := json.Unmarshal([]byte(result), &newRepo) if err != nil { liboct.RenderError(w, err) } else { oldRepo, _ := liboct.RepoFromString(val.String()) oldRepo.Modify(newRepo) db.Update(liboct.DBRepo, repoID, oldRepo) RefreshRepo(repoID) liboct.RenderOK(w, "", nil) } } else if action == "Refresh" { RefreshRepo(repoID) liboct.RenderOK(w, "", nil) } else { liboct.RenderErrorf(w, "The action in ModifyRepo is limited to Add/Refresh") } }
// redirect to local to test func GetTaskReport(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() id := r.URL.Query().Get(":TaskID") taskInterface, err := db.Get(liboct.DBTask, id) if err != nil { liboct.RenderError(w, err) return } task, _ := liboct.TaskFromString(taskInterface.String()) // Here the task.PostURL is: http://ip:port/task/id getURL := fmt.Sprintf("%s/report", task.PostURL) logrus.Debugf("Get url ", getURL) resp, err := http.Get(getURL) if err != nil { liboct.RenderError(w, err) return } defer resp.Body.Close() resp_body, err := ioutil.ReadAll(resp.Body) if err != nil { liboct.RenderError(w, err) return } w.Write([]byte(resp_body)) }
func AddRepo(w http.ResponseWriter, r *http.Request) { action := r.URL.Query().Get("Action") db := liboct.GetDefaultDB() //Add and refresh if action == "Add" { var repo liboct.TestCaseRepo result, _ := ioutil.ReadAll(r.Body) r.Body.Close() err := json.Unmarshal([]byte(result), &repo) if err != nil { liboct.RenderError(w, err) } else { if err := repo.IsValid(); err == nil { if id, e := db.Add(liboct.DBRepo, repo); e != nil { liboct.RenderError(w, err) } else { RefreshRepo(id) liboct.RenderOK(w, "", nil) } } else { liboct.RenderError(w, err) } } } else if action == "Refresh" { ids := db.Lookup(liboct.DBRepo, liboct.DBQuery{}) for index := 0; index < len(ids); index++ { RefreshRepo(ids[index]) } liboct.RenderOK(w, "", nil) } else { liboct.RenderErrorf(w, "The action in AddRepo is limited to Add/Refresh") } }
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 ReceiveTask(w http.ResponseWriter, r *http.Request) { logrus.Debugf("ReceiveTask begin") var tc liboct.TestCase db := liboct.GetDefaultDB() realURL, _ := liboct.ReceiveFile(w, r, liboct.SchedulerCacheDir) tc, err := liboct.CaseFromTar(realURL, "") if err != nil { liboct.RenderError(w, err) return } s, _ := liboct.SchedulerNew(tc) err = s.Command(liboct.TestActionApply) if err == nil { if id, e := db.Add(liboct.DBScheduler, s); e == nil { updateSchedulerBundle(id, realURL) liboct.RenderOK(w, id, nil) } else { liboct.RenderError(w, e) } } else { liboct.RenderError(w, err) } }
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())) } }
func GetRepo(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get(":ID") db := liboct.GetDefaultDB() if repo, err := db.Get(liboct.DBRepo, id); err != nil { liboct.RenderError(w, err) } else { liboct.RenderOK(w, "", repo) } }
func GetTaskStatus(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() id := r.URL.Query().Get(":TaskID") if taskInterface, err := db.Get(liboct.DBTask, id); err != nil { liboct.RenderError(w, err) } else { liboct.RenderOK(w, "", taskInterface) } }
func PostTaskAction(w http.ResponseWriter, r *http.Request) { result, _ := ioutil.ReadAll(r.Body) logrus.Debugf("Post task action %v", string(result)) r.Body.Close() action, err := liboct.ActionCommandFromString(string(result)) if err != nil { liboct.RenderError(w, err) return } id := r.URL.Query().Get(":ID") db := liboct.GetDefaultDB() taskInterface, err := db.Get(liboct.DBTask, id) if err != nil { liboct.RenderError(w, err) return } task, _ := liboct.TaskFromString(taskInterface.String()) workingDir := path.Join(strings.TrimSuffix(task.BundleURL, ".tar.gz"), "source") if _, err := os.Stat(workingDir); err != nil { //Create in the case which has no 'source' files os.MkdirAll(workingDir, 0777) } switch action.Action { case liboct.TestActionDeploy: fallthrough case liboct.TestActionRun: val, err := RunCommand(action, workingDir) //Save the logs logFile := fmt.Sprintf("%s/%s.log", workingDir, task.Name) ioutil.WriteFile(logFile, val, 0644) if err != nil { liboct.RenderErrorf(w, fmt.Sprintf("Failed in run command: %s", string(result))) } else { liboct.RenderOK(w, "", string(val)) } return } liboct.RenderErrorf(w, fmt.Sprintf("Action %s is not support yet", action.Action)) }
func PostResource(w http.ResponseWriter, r *http.Request) { var res liboct.Resource db := liboct.GetDefaultDB() result, _ := ioutil.ReadAll(r.Body) r.Body.Close() logrus.Debugf(string(result)) json.Unmarshal([]byte(result), &res) if err := res.IsValid(); err != nil { liboct.RenderError(w, err) } else { lock.Lock() if id, err := db.Add(liboct.DBResource, res); err != nil { liboct.RenderError(w, err) } else { liboct.RenderOK(w, fmt.Sprintf("Success in adding the resource: %s ", id), nil) } lock.Unlock() } }
func DeleteResource(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() id := r.URL.Query().Get("ID") lock.Lock() if err := db.Remove(liboct.DBResource, id); err != nil { liboct.RenderError(w, err) } else { liboct.RenderOK(w, "Success in remove the resource", nil) } lock.Unlock() }
func AddTask(w http.ResponseWriter, r *http.Request) { result, _ := ioutil.ReadAll(r.Body) r.Body.Close() db := liboct.GetDefaultDB() caseID := string(result) caseInterface, err := db.Get(liboct.DBCase, caseID) if err != nil { liboct.RenderError(w, err) return } tc, _ := liboct.CaseFromString(caseInterface.String()) bundleURL := tc.GetBundleTarURL() postURL := pubConfig.SchedulerURL if task, err := liboct.TestTaskNew(postURL, bundleURL, liboct.SchedulerDefaultPrio); err != nil { liboct.RenderError(w, err) } else { id, _ := db.Add(liboct.DBTask, task) liboct.RenderOK(w, id, nil) } }
func PostTaskAction(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() result, _ := ioutil.ReadAll(r.Body) r.Body.Close() action, err := liboct.TestActionFromString(string(result)) if err != nil { liboct.RenderError(w, err) return } id := r.URL.Query().Get(":TaskID") taskInterface, err := db.Get(liboct.DBTask, id) if err != nil { liboct.RenderError(w, err) return } task, _ := liboct.TaskFromString(taskInterface.String()) if e := task.Command(action); e != nil { liboct.RenderError(w, err) } else { liboct.RenderOK(w, "", nil) } }
func GetCaseReport(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() id := r.URL.Query().Get(":ID") if val, err := db.Get(liboct.DBCase, id); err != nil { liboct.RenderError(w, err) } else { tc, _ := liboct.CaseFromString(val.String()) content := tc.GetReportContent() if len(content) > 0 { liboct.RenderOK(w, "", content) } else { liboct.RenderErrorf(w, "Case report is empty.") } } }
func GetCase(w http.ResponseWriter, r *http.Request) { //TODO: support another query method : repo/group/name db := liboct.GetDefaultDB() id := r.URL.Query().Get(":ID") if val, err := db.Get(liboct.DBCase, id); err != nil { liboct.RenderError(w, err) } else { tc, _ := liboct.CaseFromString(val.String()) value := tc.GetBundleContent() if len(value) > 0 { w.Write([]byte(value)) } else { liboct.RenderErrorf(w, "Case is empty.") } } }