예제 #1
0
func postImageInformationCreate(request *restful.Request, response *restful.Response) {
	imageInformation := new(image.ImageInformation)
	err := request.ReadEntity(&imageInformation)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	oldImageInformation, _ := image.GetStorage().LoadImageInformation(imageInformation.Name)
	if oldImageInformation != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Duplicated repository error"
		jsonMap["ErrorMessage"] = "Already exists"
		jsonMap["imageInformation"] = imageInformation
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(401, string(errorMessageByteSlice))
		return
	}

	// Initialize the output file for websocket
	image.TouchOutMessageFile(imageInformation.Name)

	go func() {
		image.BuildCreate(imageInformation)
	}()
	/*
		outputMessage, err := image.BuildCreate(imageInformation)

		resultJsonMap := make(map[string]interface{})
		resultJsonMap["OutputMessage"] = outputMessage
		statusCode := 200
		if err != nil {
			statusCode = 422
			resultJsonMap["Error"] = "Create build failure"
			resultJsonMap["ErrorMessage"] = err.Error()
			resultJsonMap["imageInformation"] = imageInformation
			log.Error(resultJsonMap)
		}
		result, err := json.Marshal(resultJsonMap)
		if err != nil {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "Marshal output message failure"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["resultJsonMap"] = resultJsonMap
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(422, string(errorMessageByteSlice))
			return
		}

		response.WriteErrorString(statusCode, string(result))
		return
	*/
}
예제 #2
0
func putImageInformationUpgrade(request *restful.Request, response *restful.Response) {
	kubeApiServerEndPoint, kubeApiServerToken, err := configuration.GetAvailablekubeApiServerEndPoint()
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get kube apiserver endpoint and token failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	imageInformationUpgradeInput := new(ImageInformationUpgradeInput)
	err = request.ReadEntity(&imageInformationUpgradeInput)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	if lock.LockAvailable(image.LockKind, imageInformationUpgradeInput.ImageInformationName) == false {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Duplicated command failure"
		jsonMap["ErrorMessage"] = "Image is under construction"
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(403, string(errorMessageByteSlice))
		return
	}

	// Initialize the output file for websocket
	image.TouchOutMessageFile(imageInformationUpgradeInput.ImageInformationName)

	go func() {
		_, err := image.BuildUpgrade(
			imageInformationUpgradeInput.ImageInformationName,
			imageInformationUpgradeInput.Description)
		if err == nil {
			// Build sccessfully
			// Auto rolling update the deployment if configured
			imageInformation, err := image.GetStorage().LoadImageInformation(imageInformationUpgradeInput.ImageInformationName)
			if err != nil {
				log.Error(err)
			} else {
				deployInformationSlice, err := deploy.GetDeployInformationWithAutoUpdateForNewBuild(imageInformationUpgradeInput.ImageInformationName)
				if err != nil {
					log.Error(err)
				} else {
					for _, deployInformation := range deployInformationSlice {
						description := "Trigged by version " + imageInformation.CurrentVersion
						err := deploy.DeployUpdate(
							kubeApiServerEndPoint,
							kubeApiServerToken,
							deployInformation.Namespace,
							imageInformation.Name,
							imageInformation.CurrentVersion,
							description,
							deployInformation.EnvironmentSlice)
						if err != nil {
							log.Error(err)
						}
					}
				}
			}
		}
	}()
	/*
		outputMessage, err := image.BuildUpgrade(
			imageInformationUpgradeInput.ImageInformationName,
			imageInformationUpgradeInput.Description)

		resultJsonMap := make(map[string]interface{})
		resultJsonMap["OutputMessage"] = outputMessage
		statusCode := 200
		if err != nil {
			statusCode = 422
			resultJsonMap["Error"] = "Upgrade build failure"
			resultJsonMap["ErrorMessage"] = err.Error()
			resultJsonMap["imageInformationUpgradeInput"] = imageInformationUpgradeInput
			log.Error(resultJsonMap)
		}
		result, err := json.Marshal(resultJsonMap)
		if err != nil {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "Marshal output message failure"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["resultJsonMap"] = resultJsonMap
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(422, string(errorMessageByteSlice))
			return
		}

		response.WriteErrorString(statusCode, string(result))
		return
	*/
}