Ejemplo n.º 1
0
Archivo: net.go Proyecto: qzan9/wharf
/*the first time, we will update all the bits of freeBit.
After that, we call this function because some container or host shut down without their ips collected.
So we just test these ips not available.
*/
func Update() error {
	fmt.Println("Ip pool begin to update...")

	var data networkConfig
	filename := `/etc/wharf/network.conf`
	reader, err := os.Open(filename)
	if err != nil {
		return err
	}

	errjson := util.UnmarshalReader(reader, &data)
	if errjson != nil {
		return errjson
	}
	busynum := len(data.Busy)
	for i := 0; i < busynum; i++ {
		thisip := data.Busy[i]
		SetStateByIp(thisip, 0)
	}
	Updated = true
	fmt.Println("OK:	Ip pool update!")
	return nil
}
Ejemplo n.º 2
0
/*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())

	}
}