Exemple #1
0
func appInfoHandler(w rest.ResponseWriter, r *rest.Request) {
	var marathonApps marathon.MarathonAppsGlobalInfoResponse
	fasthttp.JsonReqAndResHandler(goCore.MarathonAppsUrl, nil, &marathonApps, "GET")
	appsCnt := len(marathonApps.Apps)

	// should not code like this: appsGlobalInfos := [appsCnt]entity.AppsGlobalInfo{}
	appsGlobalInfos := make([]dto.AppsGlobalInfoResponse, appsCnt)

	for i, v := range marathonApps.Apps {
		var perApp dto.AppsGlobalInfoResponse
		if strings.LastIndex(v.Id, "/") == -1 {
			perApp.Id = v.Id
		} else {
			perApp.Id = v.Id[strings.LastIndex(v.Id, "/")+1:]
		}
		perApp.Cpus = strconv.FormatFloat(v.Cpus, 'f', 1, 64)
		perApp.CurrentInstances = strconv.Itoa(v.TasksRunning)
		fmt.Println(v)
		if strings.LastIndex(v.Id, "/") <= 0 { // exclude like /zk or zk
			perApp.Group = "No Groups"
		} else {
			perApp.Group = v.Id[0:strings.LastIndex(v.Id, "/")]
		}
		perApp.Instances = strconv.Itoa(v.Instances)
		perApp.Mem = strconv.FormatFloat(v.Mem, 'f', 1, 64)
		if v.TasksHealthy == 0 && v.TasksUnhealthy == 0 { // when no and healthy check
			perApp.Healthy = "100"
		} else {
			perApp.Healthy = strconv.FormatFloat(float64(v.TasksHealthy)/float64(v.TasksHealthy+v.TasksUnhealthy), 'f', 1, 64)
		}
		perApp.FormatStatus(v.TasksStaged)
		appsGlobalInfos[i] = perApp
	}
	w.WriteJson(appsGlobalInfos)
}
Exemple #2
0
func deploySimpleHandler(w rest.ResponseWriter, r *rest.Request) {

	var request dto.DeploySimpleRequest
	err := r.DecodeJsonPayload(&request)
	CheckError(err)
	log.Println("the request data is: ", request)

	deployInfo := marathon.NewMarathonAppsRequest()
	deployInfo.Id = request.Id
	deployInfo.Cpus, _ = strconv.ParseFloat(request.Cpus, 64)
	deployInfo.Mem, _ = strconv.ParseFloat(request.Mem, 64)
	deployInfo.Instances, _ = strconv.Atoi(request.Instances)

	container := marathon.NewMarathonDockerContainer()
	container.Image = "10.32.27.82:5000/" + request.Image

	ports := make([]marathon.MarathonDockerPort, len(request.ExportPorts))
	for i, v := range request.ExportPorts {
		port := marathon.NewMarathonDockerPort()
		port.ContainerPort, _ = strconv.Atoi(v.ContainerPort)
		ports[i] = *port
	}

	container.PortMappings = ports
	container.Volumes = make([]interface{}, 0)

	deployInfo.Container = make(map[string]interface{})
	deployInfo.Container["docker"] = container

	// request
	var response map[string]interface{}
	code := fasthttp.JsonReqAndResHandler(goCore.MarathonAppsUrl, deployInfo, &response, "POST")
	fmt.Println("now code is: " + strconv.Itoa(code))
	if code != http.StatusCreated {
		w.WriteJson(response)
	} else {
		w.WriteJson(map[string]string{"status": "ok"})
	}
}