Ejemplo n.º 1
0
func Notify(username string, imageInformationName string, signature string, payload string, kubeApiServerEndPoint string, kubeApiServerToken string) error {
	if len(username) == 0 {
		log.Error("User couldn't be empty. Signature %s", signature)
		log.Debug(payload)
		return errors.New("User couldn't be empty")
	}

	if len(signature) == 0 {
		return errors.New("The secret is required")
	}

	user, err := authorization.GetStorage().LoadUser(username)
	etcdError, _ := err.(client.Error)
	if etcdError.Code == client.ErrorCodeKeyNotFound {
		return errors.New("The user " + username + " doesn't exist")
	}
	if err != nil {
		log.Error("Get user error %s. User name %s, signature %s", err, username, signature)
		log.Debug(payload)
		return err
	}

	// If secret is used
	githubWebhookSecret := user.MetaDataMap["githubWebhookSecret"]
	generatedSignature := getGitHashSignature(githubWebhookSecret, payload)
	if generatedSignature != signature {
		log.Error("The signature is invalid. User name %s, signature %s, generated signature %s", username, signature, generatedSignature)
		log.Debug(payload)
		return errors.New("The signature is invalid")
	}

	jsonMap := make(map[string]interface{})
	err = json.Unmarshal([]byte(payload), &jsonMap)
	if err != nil {
		log.Error("Unmarshal payload error %s. User name %s, signature %s", err, username, signature)
		log.Debug(payload)
		return err
	}

	pusherJsonMap, _ := jsonMap["pusher"].(map[string]interface{})
	pusherName, _ := pusherJsonMap["name"].(string)

	repositoryJsonMap, _ := jsonMap["repository"].(map[string]interface{})
	cloneUrl, _ := repositoryJsonMap["clone_url"].(string)

	if len(cloneUrl) == 0 {
		log.Error("Can't find clone_url in github payload. User name %s, signature %s", username, signature)
		log.Debug(payload)
		return errors.New("Can't find clone_url in github payload")
	}

	imageInformation, err := image.GetStorage().LoadImageInformation(imageInformationName)
	etcdError, _ = err.(client.Error)
	if etcdError.Code == client.ErrorCodeKeyNotFound {
		return errors.New("The repository " + imageInformationName + " doesn't exist")
	}
	if err != nil {
		log.Error(err)
		return err
	}

	sourceCodeURL := imageInformation.BuildParameter["sourceCodeURL"]
	if sourceCodeURL != cloneUrl {
		// Not the target, ignore.
		return nil
	}

	if len(imageInformationName) == 0 {
		log.Error("Can't find image information using the github url %s. User name %s, signature %s", cloneUrl, username, signature)
		log.Debug(payload)
		return errors.New("Can't find image information using the github url")
	}

	// Asyncronized build
	go func() {
		outputMessage, err := image.BuildUpgrade(imageInformationName, "Github webhook. Pusher: "+pusherName)
		if err != nil {
			log.Error(err)
			log.Debug(outputMessage)
		} else {
			// Build sccessfully
			// Auto rolling update the deployment if configured
			imageInformation, err := image.GetStorage().LoadImageInformation(imageInformationName)
			if err != nil {
				log.Error(err)
			} else {
				deployInformationSlice, err := deploy.GetDeployInformationWithAutoUpdateForNewBuild(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)
						}
					}
				}
			}
		}
	}()

	return nil
}
Ejemplo n.º 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
	*/
}