Example #1
0
//PutManifestsV2Handler is
func PutManifestsV2Handler(ctx *macaron.Context) (int, []byte) {
	repository := ctx.Params(":repository")
	namespace := ctx.Params(":namespace")
	agent := ctx.Req.Header.Get("User-Agent")
	tag := ctx.Params(":tag")

	if data, err := ctx.Req.Body().String(); err != nil {
		log.Errorf("Get the manifest data error: %s", err.Error())

		result, _ := json.Marshal(map[string]string{})
		return http.StatusBadRequest, result
	} else {
		_, imageID, version, _ := module.GetTarsumlist([]byte(data))
		digest, _ := signature.DigestManifest([]byte(data))

		r := new(models.DockerV2)
		if err := r.PutAgent(namespace, repository, agent, strconv.FormatInt(version, 10)); err != nil {
			log.Errorf("Put the manifest data error: %s", err.Error())

			result, _ := json.Marshal(map[string]string{})
			return http.StatusBadRequest, result
		}

		t := new(models.DockerTagV2)
		if err := t.Put(namespace, repository, tag, imageID, data, strconv.FormatInt(version, 10)); err != nil {
			log.Errorf("Put the manifest data error: %s", err.Error())

			result, _ := json.Marshal(map[string]string{})
			return http.StatusBadRequest, result
		}

		random := fmt.Sprintf("https://%s/v2/%s/%s/manifests/%s",
			setting.Domains, namespace, repository, digest)

		ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
		ctx.Resp.Header().Set("Docker-Content-Digest", digest)
		ctx.Resp.Header().Set("Location", random)

		status := []int{http.StatusBadRequest, http.StatusAccepted, http.StatusCreated}

		result, _ := json.Marshal(map[string]string{})
		return status[version], result
	}
}
Example #2
0
func PutManifestsV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte) {
	//TODO: to consider parallel situation
	manifest := ManifestCtx
	defer func() {
		ManifestCtx = []byte{}
	}()

	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	agent := ctx.Req.Header.Get("User-Agent")
	tag := ctx.Params(":tag")

	if len(manifest) == 0 {
		manifest, _ = ctx.Req.Body().Bytes()
	}

	tarsumlist, err := module.GetTarsumlist(manifest)
	if err != nil {
		log.Error("[REGISTRY API V2] Failed to get tarsum in manifest")

		result, _ := json.Marshal(map[string]string{"message": "Failed to get manifest tarsum"})
		return http.StatusBadRequest, result
	}

	digest, err := signature.DigestManifest(manifest)
	if err != nil {
		log.Error("[REGISTRY API V2] Failed to get manifest digest: %v", err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Failed to get manifest digest"})
		return http.StatusBadRequest, result
	}

	r := new(models.Repository)
	if err := r.Put(namespace, repository, "", agent, setting.APIVERSION_V2); err != nil {
		log.Error("[REGISTRY API V2] Failed to save repository %v/%v: %v", namespace, repository, err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Failed to save repository"})
		return http.StatusInternalServerError, result
	}

	err, schema := module.ParseManifest(manifest, namespace, repository, tag)
	if err != nil {
		log.Error("[REGISTRY API V2] Failed to decode manifest: %v", err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Failed to decode manifest"})
		return http.StatusBadRequest, result
	}

	random := fmt.Sprintf("%s://%s/v2/%s/%s/manifests/%s",
		setting.ListenMode,
		setting.Domains,
		namespace,
		repository,
		digest)

	ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
	ctx.Resp.Header().Set("Docker-Content-Digest", digest)
	ctx.Resp.Header().Set("Location", random)

	if err := module.UploadLayer(tarsumlist); err != nil {
		log.Error("[REGISTRY API V2] Failed to upload layer: %v", err)

		result, _ := json.Marshal(map[string]string{"message": "Failed to upload layer"})
		return http.StatusBadRequest, result
	}

	var status = []int{http.StatusBadRequest, http.StatusAccepted, http.StatusCreated}
	result, _ := json.Marshal(map[string]string{})
	return status[schema], result
}