示例#1
0
文件: manager.go 项目: qzan9/wharf
/*function:create task from the user command.
	we will create the task and store it in Tasks
param:
	r: r.Body include the string of the user command
	w:	w.Body include the result of the create.response will be marshal to w.
*/
func CreateTaskHandler(w http.ResponseWriter, r *http.Request) {
	var response util.HttpResponse
	var thisTask Task
	response.Status = util.INVALID_INPUT

	var contents []byte
	contents = make([]byte, 1000)
	length, err := r.Body.Read(contents)
	if err != nil && err != io.EOF {
		fmt.Fprintf(os.Stderr, "CreateHandler: can not read from http resposeWriter\n")
		fmt.Fprintf(os.Stderr, "%s", err)
		response.Warnings = []string{"CreateHandler: can not read from http resposeWriter\n" + err.Error()}
		io.WriteString(w, response.String())
		return
	}
	var res util.SendCmd
	if *FlagDebug {
		fmt.Println("contents:", string(contents))
	}
	//make sure the char in contents should not include '\0'
	contentsRight := contents[:length]
	errunmarshal := json.Unmarshal(contentsRight, &res)
	if errunmarshal != nil {
		fmt.Fprintf(os.Stderr, "CreateHandler: Unmarshal failed for contents: ")
		fmt.Fprintf(os.Stderr, "%s", errunmarshal)
		response.Warnings = []string{"CreateHandler: Unmarshal failed for contents: " + errunmarshal.Error()}
		io.WriteString(w, response.String())
		return
	} else {
		//now we will create our task here: filter, allocator, (image server) ,scheduler
		var userRequest CreateRequest
		userRequest.Init()
		for thisflag, flagvalue := range res.Data {
			switch {
			case strings.EqualFold(thisflag, "i"):
				userRequest.ImageName = flagvalue
			case strings.EqualFold(thisflag, "t"):
				userRequest.TypeName = flagvalue
				if !strings.EqualFold(flagvalue, "mpi") && !strings.EqualFold(flagvalue, "single") {
					fmt.Fprintf(os.Stderr, `the type of the task is not supported yet. Only "single" and "mpi" is supported.`)
					response.Warnings = []string{`the type of the task is not supported yet. Only "single" and "mpi" is supported.`}
					io.WriteString(w, response.String())
					return
				}
			case strings.EqualFold(thisflag, "n"):
				userRequest.TaskName = flagvalue
			case strings.EqualFold(thisflag, "s"):
				userRequest.Stratergy = flagvalue
				if !strings.EqualFold(thisflag, "MEM") && !strings.EqualFold(thisflag, "COM") {
					response.Warnings = []string{`Only MEM and COM are valid for -s flag`}
					io.WriteString(w, response.String())
					return
				}
			case strings.EqualFold(thisflag, "c"):
				userRequest.TotalCpuNum, _ = strconv.Atoi(flagvalue)
			case strings.EqualFold(thisflag, "C"):
				userRequest.ContainerNumMax, _ = strconv.Atoi(flagvalue)
			case strings.EqualFold(thisflag, "l"):
				value, _ := strconv.ParseFloat(flagvalue, 32)
				userRequest.OverLoadMax = float32(value)
			case strings.EqualFold(thisflag, "f"):
				filename := flagvalue
				readerme, openerr := os.Open(filename)
				if openerr != nil {
					fmt.Fprintf(os.Stderr, "CreateHandler:%s", openerr)
					response.Warnings = []string{"CreateHandler" + openerr.Error()}
					io.WriteString(w, response.String())
					return
				}
				unmarshalerr := util.UnmarshalReader(readerme, &(userRequest.ResNode))
				if unmarshalerr != nil {
					response.Warnings = []string{unmarshalerr.Error()}
					io.WriteString(w, response.String())
				}
			default:
				fmt.Fprintf(os.Stderr, "CreateHandler: %s flag invalid", thisflag)
				response.Warnings = []string{"CreateHandler: " + thisflag + "flag invalid"}
				io.WriteString(w, response.String())
				return
			}
		}

		var err error
		endpoint := []string{"http://" + MasterConfig.EtcdNode.Ip + ":" + MasterConfig.EtcdNode.Port}
		err = UpdateEtcdForUse(endpoint, MasterConfig.EtcdNode.Key, true, true)
		if err != nil {
			if *FlagDebug {
				util.PrintErr("Failded to Etcd data!")
			}
			response.Warnings = []string{err.Error()}
			io.WriteString(w, response.String())
			return
		}
		//Debug
		if *FlagDebug {
			util.PrintErr("Etcd data has been updated!")
		}

		err = UpdateGres()
		if err != nil {
			response.Warnings = []string{err.Error()}
			io.WriteString(w, response.String())
			return
		}
		//Debug
		if *FlagDebug {
			util.PrintErr("Gres has been updated!With ", len(Gres), " terms left.")
			if len(Gres) == 0 {
				util.PrintErr("Error: ", "0 node can be used in Gres")
			}
		}

		err = Filter(userRequest)
		//Debug
		if *FlagDebug {
			util.PrintErr("Rres has been Filtered!, with ", len(Rres), " terms left")
		}

		err = Allocate(userRequest)
		if err != nil {
			io.WriteString(w, err.Error())
			io.WriteString(w, response.String())
			return
		}
		//Debug
		if *FlagDebug {
			util.PrintErr("Allocate complished for the create task! ", len(Ares), " containers will be created!")
		}

		thisTask.Init(userRequest, len(Ares))

		err = ImageTransport(&thisTask)
		if err != nil {
			io.WriteString(w, err.Error())
			io.WriteString(w, response.String())
			return
		}

		//create container,start it,  bind ip
		err = CreateContainer2Ares(&thisTask)
		if err != nil {
			//Debug
			if *FlagDebug {
				util.PrintErr("CreateContainer2Ares failed:", err.Error())
			}
			response.Set(util.SERVER_ERROR, err.Error())
			io.WriteString(w, response.String())
			return
		}
		thisTask.CreatedTime = time.Now()
		thisTask.Status = RUNNING
		Tasks[userRequest.TaskName] = thisTask
		LatestTask = userRequest.TaskName
		response.Set(util.OK, thisTask.Cmd.TaskName)
		io.WriteString(w, response.String())

	}
}
示例#2
0
文件: manager.go 项目: qzan9/wharf
/*List the information about one or more tasks.
The information is not in detai */
func ListTaskHandler(w http.ResponseWriter, r *http.Request) {
	//the return value :response
	var response util.HttpResponse
	inputInfo, err := GetInfoFromRequest(&w, r)
	if err != nil {
		return
	}
	var userRequest PsRequest
	userRequest.Init()
	for thisFlag, flagValue := range inputInfo.Data {
		switch thisFlag {
		case "a":
			userRequest.All, _ = strconv.ParseBool(flagValue)
		case "l":
			userRequest.Latest, _ = strconv.ParseBool(flagValue)
		case "n":
			userRequest.Name = flagValue
		case "i":
			userRequest.Image = flagValue
		case "t":
			userRequest.Type = flagValue
		default:
			response.Set(util.INVALID_INPUT, "invalid flag of wharf ps.")
			io.WriteString(w, response.String())
			return
		}
	}
	//flags conflict
	if userRequest.All && userRequest.Latest ||
		userRequest.All && userRequest.Name != "" ||
		userRequest.Latest && userRequest.Name != "" {
		response.Set(util.INVALID_INPUT, "flags are conflicting with each other.")
		io.WriteString(w, response.String())
		return
	}

	var psIterm PsOutput
	//task name is set || latest is set
	if userRequest.Name != "" || userRequest.Latest {
		if userRequest.Latest {
			userRequest.Name = LatestTask
		}
		thisTask, exists := Tasks[userRequest.Name]
		if !exists {
			response.Set(util.INVALID_INPUT, `No such task with name of "`+userRequest.Name+`"`)
			io.WriteString(w, response.String())
			return
		} else {
			fillPSOutpusWithTask(&psIterm, &thisTask)
			data, _ := json.Marshal(psIterm)
			response.Set(util.OK, string(data))
			io.WriteString(w, response.String())
			return
		}
	}
	//traverse all the task in Tasks
	for _, thisTask := range Tasks {
		response.Status = util.OK
		if (userRequest.All == true || thisTask.Status == RUNNING) &&
			(userRequest.Image == "" || thisTask.Cmd.ImageName == userRequest.Image) &&
			(userRequest.Type == "" || thisTask.Cmd.TypeName == userRequest.Type) {
			fillPSOutpusWithTask(&psIterm, &thisTask)
			data, _ := json.Marshal(psIterm)
			response.Append(string(data))
		}
		io.WriteString(w, response.String())
	}
	return
}