示例#1
0
//GetTagsListV2Handler is
func GetTagsListV2Handler(ctx *macaron.Context) (int, []byte) {
	var err error
	repository := ctx.Params(":repository")
	namespace := ctx.Params(":namespace")

	data := map[string]interface{}{}
	data["name"] = fmt.Sprintf("%s/%s", namespace, repository)

	r := new(models.DockerV2)

	if data["tags"], err = r.GetTags(namespace, repository); err != nil && err == gorm.ErrRecordNotFound {
		log.Info("Not found repository in getting tags list: %s/%s", namespace, repository)

		result, _ := module.EncodingError(module.BLOB_UNKNOWN, fmt.Sprintf("%s/%s", namespace, repository))
		return http.StatusNotFound, result
	} else if err != nil && err != gorm.ErrRecordNotFound {
		log.Info("Failed found repository in getting tags list %s/%s: %s", namespace, repository, err.Error())

		result, _ := module.EncodingError(module.UNKNOWN, err.Error())
		return http.StatusBadRequest, result
	}

	result, _ := json.Marshal(data)
	return http.StatusOK, result
}
示例#2
0
//PostBlobsV2Handler is
//Initiate a resumable blob upload. If successful, an upload location will be provided to complete the upload.
//Optionally, if the digest parameter is present, the request body will be used to complete the upload in a single request.
func PostBlobsV2Handler(ctx *macaron.Context) (int, []byte) {
	//TODO: If standalone == true, Dockyard will check HEADER Authorization; if standalone == false, Dockyard will check HEADER TOEKN.
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")

	r := new(models.DockerV2)

	if err := r.Put(namespace, repository); err != nil {
		log.Errorf("Put or search repository error: %s", err.Error())

		result, _ := module.EncodingError(module.UNKNOWN, map[string]string{"namespace": namespace, "repository": repository})
		return http.StatusBadRequest, result
	}

	uuid := utils.MD5(uuid.NewV4().String())
	state := utils.MD5(fmt.Sprintf("%s/%s/%d", namespace, repository, time.Now().UnixNano()/int64(time.Millisecond)))
	random := fmt.Sprintf("https://%s/v2/%s/%s/blobs/uploads/%s?_state=%s",
		setting.Domains, namespace, repository, uuid, state)

	ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
	ctx.Resp.Header().Set("Docker-Upload-Uuid", uuid)
	ctx.Resp.Header().Set("Location", random)
	ctx.Resp.Header().Set("Range", "0-0")

	result, _ := json.Marshal(map[string]string{})
	return http.StatusAccepted, result
}
示例#3
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
	}
}