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") } }
func ReceiveTask(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() realURL, params := liboct.ReceiveFile(w, r, OCTDCacheDir) logrus.Debugf("ReceiveTask %v", realURL) if id, ok := params["id"]; ok { if strings.HasSuffix(realURL, ".tar.gz") { liboct.UntarFile(realURL, strings.TrimSuffix(realURL, ".tar.gz")) } var task liboct.TestTask task.ID = id task.BundleURL = realURL if name, ok := params["name"]; ok { task.Name = name } else { task.Name = id logrus.Warnf("Cannot find the name of the task.") } db.Update(liboct.DBTask, id, task) liboct.RenderOK(w, "", nil) } else { liboct.RenderErrorf(w, fmt.Sprintf("Cannot find the task id: %d", id)) } }
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 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 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 GetResource(w http.ResponseWriter, r *http.Request) { query := GetResourceQuery(r) db := liboct.GetDefaultDB() ids := db.Lookup(liboct.DBResource, query) if len(ids) == 0 { liboct.RenderErrorf(w, "Cannot find the avaliable resource") } else { var rss []liboct.DBInterface for index := 0; index < len(ids); index++ { res, _ := db.Get(liboct.DBResource, ids[index]) rss = append(rss, res) } liboct.RenderOK(w, "Find the avaliable resource", rss) } }
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.") } } }