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 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 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 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 ListCases(w http.ResponseWriter, r *http.Request) { //Need better explaination of 'status', currently, only hasReport/isUpdated db := liboct.GetDefaultDB() var query liboct.DBQuery page_string := r.URL.Query().Get("Page") page, err := strconv.Atoi(page_string) if err == nil { query.Page = page } pageSizeString := r.URL.Query().Get("PageSize") pageSize, err := strconv.Atoi(pageSizeString) if err != nil { query.PageSize = pageSize } status := r.URL.Query().Get("Status") if len(status) > 0 { query.Params = make(map[string]string) query.Params["Status"] = status } ids := db.Lookup(liboct.DBCase, query) var caseList []liboct.TestCase for index := 0; index < len(ids); index++ { if val, err := db.Get(liboct.DBCase, ids[index]); err == nil { tc, _ := liboct.CaseFromString(val.String()) caseList = append(caseList, tc) } } liboct.RenderOK(w, fmt.Sprintf("%d cases founded", len(caseList)), caseList) }
func RefreshRepos(w http.ResponseWriter, r *http.Request) { db := liboct.GetDefaultDB() ids := db.Lookup(liboct.DBRepo, liboct.DBQuery{}) for index := 0; index < len(ids); index++ { RefreshRepo(ids[index]) } liboct.RenderOK(w, "", nil) }
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 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 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 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 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 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 ListRepos(w http.ResponseWriter, r *http.Request) { var query liboct.DBQuery page_string := r.URL.Query().Get("Page") page, err := strconv.Atoi(page_string) if err == nil { query.Page = page } pageSizeString := r.URL.Query().Get("PageSize") pageSize, err := strconv.Atoi(pageSizeString) if err == nil { query.PageSize = pageSize } var rl []liboct.DBInterface db := liboct.GetDefaultDB() ids := db.Lookup(liboct.DBRepo, query) for index := 0; index < len(ids); index++ { repo, _ := db.Get(liboct.DBRepo, ids[index]) rl = append(rl, repo) } liboct.RenderOK(w, fmt.Sprintf("%d repos founded", len(rl)), rl) }
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 ListTasks(w http.ResponseWriter, r *http.Request) { //TODO status var query liboct.DBQuery db := liboct.GetDefaultDB() pageStr := r.URL.Query().Get("Page") page, err := strconv.Atoi(pageStr) if err == nil { query.Page = page } pageSizeStr := r.URL.Query().Get("PageSize") pageSize, err := strconv.Atoi(pageSizeStr) if err == nil { query.PageSize = pageSize } ids := db.Lookup(liboct.DBTask, query) var tl []liboct.DBInterface for index := 0; index < len(ids); index++ { task, _ := db.Get(liboct.DBTask, ids[index]) tl = append(tl, task) } liboct.RenderOK(w, fmt.Sprintf("%d tasks founded", len(tl)), tl) }
//TODO: is there any usefull Restful help document lib? func GetHelp(w http.ResponseWriter, r *http.Request) { liboct.RenderOK(w, fmt.Sprintf("The following APIs are supported."), "case, repo and task") }