예제 #1
0
func GetTaskReport(w http.ResponseWriter, r *http.Request) {
	filename := r.URL.Query().Get("File")
	id := r.URL.Query().Get(":ID")

	db := liboct.GetDefaultDB()
	taskInterface, err := db.Get(liboct.DBTask, id)
	if err != nil {
		logrus.Warnf("Cannot find the test job %v", id)
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("Cannot find the test job " + id))
		return
	}

	task, _ := liboct.TaskFromString(taskInterface.String())
	workingDir := strings.TrimSuffix(task.BundleURL, ".tar.gz")
	//The real working dir is under 'source'
	realURL := path.Join(workingDir, "source", filename)
	file, err := os.Open(realURL)
	defer file.Close()
	if err != nil {
		logrus.Warnf("Cannot open the file %v", filename)
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("Cannot open the file: " + realURL))
		return
	}

	buf := bytes.NewBufferString("")
	buf.ReadFrom(file)

	w.Write([]byte(buf.String()))
}
예제 #2
0
// 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))
}
예제 #3
0
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))
}
예제 #4
0
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)
	}
}