示例#1
0
文件: repoapiv1.go 项目: rechen/wharf
func (this *RepoAPIV1Controller) PutRepository() {
	username, _, _ := utils.DecodeBasicAuth(this.Ctx.Input.Header("Authorization"))

	namespace := string(this.Ctx.Input.Param(":namespace"))
	repository := string(this.Ctx.Input.Param(":repo_name"))

	repo := new(models.Repository)

	if err := repo.Put(namespace, repository, string(this.Ctx.Input.CopyBody()), this.Ctx.Input.Header("User-Agent"), models.APIVERSION_V1); err != nil {
		this.JSONOut(http.StatusForbidden, err.Error(), nil)
		return
	}

	if this.Ctx.Input.Header("X-Docker-Token") == "true" {
		token := string(utils.GeneralKey(username))
		this.SetSession("token", token)
		this.Ctx.Output.Context.ResponseWriter.Header().Set("X-Docker-Token", token)
		this.Ctx.Output.Context.ResponseWriter.Header().Set("WWW-Authenticate", token)
	}

	user := new(models.User)
	if _, _, err := user.Has(username); err != nil {
		this.JSONOut(http.StatusForbidden, err.Error(), nil)
		return
	}

	memo, _ := json.Marshal(this.Ctx.Input.Header)
	user.Log(models.ACTION_UPDATE_REPO, models.LEVELINFORMATIONAL, models.TYPE_APIV1, repo.Id, memo)
	repo.Log(models.ACTION_UPDATE_REPO, models.LEVELINFORMATIONAL, models.TYPE_APIV1, repo.Id, memo)

	this.Ctx.Output.Context.ResponseWriter.Header().Set("X-Docker-Endpoints", beego.AppConfig.String("docker::Endpoints"))
	this.Ctx.Output.Context.Output.SetStatus(http.StatusOK)
	this.Ctx.Output.Context.Output.Body([]byte(""))
	return
}
示例#2
0
文件: teamwebv1.go 项目: rechen/wharf
func (this *TeamWebV1Controller) PostTeam() {
	user, exist := this.Ctx.Input.CruSession.Get("user").(models.User)

	if exist != true {
		this.JSONOut(http.StatusBadRequest, "", map[string]string{"message": "Session load failure", "url": "/auth"})
		return
	}

	var team models.Team

	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &team); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	}

	org := new(models.Organization)

	if exist, _, err := org.Has(team.Organization); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	} else if exist == false {
		this.JSONOut(http.StatusBadRequest, "Organization don't exist", nil)
		return
	}

	team.Id = string(utils.GeneralKey(team.Name))
	team.Username = user.Username
	team.Users = append(team.Users, user.Username)

	if err := team.Save(); err != nil {
		this.JSONOut(http.StatusBadRequest, "Team save error", nil)
		return
	}

	user.Teams = append(user.Teams, team.Name)
	user.JoinTeams = append(user.JoinTeams, team.Name)

	if err := user.Save(); err != nil {
		this.JSONOut(http.StatusBadRequest, "User save error", nil)
		return
	}

	org.Teams = append(org.Teams, team.Name)

	if err := org.Save(); err != nil {
		this.JSONOut(http.StatusBadRequest, "Org save error", nil)
		return
	}

	memo, _ := json.Marshal(this.Ctx.Input.Header)
	team.Log(models.ACTION_ADD_TEAM, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo)
	user.Log(models.ACTION_ADD_TEAM, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, team.Id, memo)

	//Reload User Data In Session
	user.Get(user.Username, user.Password)
	this.Ctx.Input.CruSession.Set("user", user)

	this.JSONOut(http.StatusOK, "Team Create Successfully!", nil)
	return
}
示例#3
0
文件: orgwebv1.go 项目: rechen/wharf
func (this *OrganizationWebV1Controller) PostOrg() {
	user := new(models.User)
	org := new(models.Organization)

	if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	} else if exist == false {
		this.JSONOut(http.StatusBadRequest, "User not exist", nil)
		return
	}

	if exist, _, err := user.Has(this.Ctx.Input.Param(":org")); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	} else if exist == true {
		this.JSONOut(http.StatusBadRequest, "Namespace is occupation already by another user", nil)
		return
	}

	if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	} else if exist == true {
		this.JSONOut(http.StatusBadRequest, "Namespace is occupation already by another organization", nil)
		return
	}

	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &org); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	}

	org.Id = string(utils.GeneralKey(org.Name))
	org.Username = user.Username
	org.Created = time.Now().UnixNano() / int64(time.Millisecond)
	org.Updated = time.Now().UnixNano() / int64(time.Millisecond)

	if err := org.Save(); err != nil {
		this.JSONOut(http.StatusBadRequest, "Organization save error", nil)
		return
	}

	user.Organizations = append(user.Organizations, org.Name)
	user.Updated = time.Now().UnixNano() / int64(time.Millisecond)

	if err := user.Save(); err != nil {
		this.JSONOut(http.StatusBadRequest, "User save error", nil)
		return
	}

	memo, _ := json.Marshal(this.Ctx.Input.Header)
	user.Log(models.ACTION_ADD_ORG, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, org.Id, memo)
	org.Log(models.ACTION_ADD_ORG, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo)

	this.JSONOut(http.StatusOK, "Create organization successfully.", nil)
	return
}
示例#4
0
文件: blobapiv2.go 项目: rechen/wharf
func (this *BlobAPIV2Controller) PostBlobs() {
	uuid := utils.GeneralKey(fmt.Sprintf("%s/%s", this.Ctx.Input.Param(":namespace"), this.Ctx.Input.Param(":repo_name")))
	random := fmt.Sprintf("https://%s/v2/%s/%s/blobs/uploads/%s", beego.AppConfig.String("docker::Endpoints"), this.Ctx.Input.Param(":namespace"), this.Ctx.Input.Param(":repo_name"), uuid)

	this.Ctx.Output.Context.ResponseWriter.Header().Set("Location", random)
	this.Ctx.Output.Context.ResponseWriter.Header().Set("Range", "bytes=0-0")
	this.Ctx.Output.Context.Output.SetStatus(http.StatusAccepted)
	this.Ctx.Output.Context.Output.Body([]byte(""))
	return
}
示例#5
0
文件: repo.go 项目: rechen/wharf
func (r *Repository) PutJSONFromManifests(image map[string]string, namespace, repository string) error {
	if has, _, err := r.Has(namespace, repository); err != nil {
		return err
	} else if has == false {
		r.Id = string(utils.GeneralKey(fmt.Sprintf("%s:%s", namespace, repository)))
		r.Created = time.Now().UnixNano() / int64(time.Millisecond)
		r.JSON = ""
	}

	r.Namespace, r.Repository, r.Version = namespace, repository, APIVERSION_V2

	r.Updated = time.Now().UnixNano() / int64(time.Millisecond)
	r.Checksumed, r.Uploaded, r.Cleared, r.Encrypted = true, true, true, false
	r.Size, r.Download = 0, 0

	if len(r.JSON) == 0 {
		if data, err := json.Marshal([]map[string]string{image}); err != nil {
			return err
		} else {
			r.JSON = string(data)
		}

	} else {
		var ids []map[string]string

		if err := json.Unmarshal([]byte(r.JSON), &ids); err != nil {
			return err
		}

		has := false
		for _, v := range ids {
			if v["id"] == image["id"] {
				has = true
			}
		}

		if has == false {
			ids = append(ids, image)
		}

		if data, err := json.Marshal(ids); err != nil {
			return err
		} else {
			r.JSON = string(data)
		}
	}

	if err := r.Save(); err != nil {
		return err
	}

	return nil
}
示例#6
0
文件: compose.go 项目: rechen/wharf
func (compose *Compose) Log(action, level, t int64, actionId string, content []byte) error {
	log := Log{Action: action, ActionId: actionId, Level: level, Type: t, Content: string(content), Created: time.Now().UnixNano() / int64(time.Millisecond)}
	log.Id = string(utils.GeneralKey(actionId))

	if err := log.Save(); err != nil {
		return err
	}

	compose.Memo = append(compose.Memo, log.Id)

	if err := compose.Save(); err != nil {
		return err
	}

	return nil
}
示例#7
0
文件: image.go 项目: rechen/wharf
func (i *Image) PutJSON(imageId, json string, version int64) error {
	if has, _, err := i.Has(imageId); err != nil {
		return err
	} else if has == false {
		i.Id, i.ImageId, i.JSON, i.Created, i.Version = string(utils.GeneralKey(uuid.NewV4().String())), imageId, json, time.Now().UnixNano()/int64(time.Millisecond), version

		if err = i.Save(); err != nil {
			return err
		}
	} else {
		i.ImageId, i.JSON = imageId, json
		i.Uploaded, i.Checksumed, i.Encrypted, i.Size, i.Updated, i.Version = false, false, false, 0, time.Now().UnixNano()/int64(time.Millisecond), version

		if err := i.Save(); err != nil {
			return err
		}
	}

	return nil
}
示例#8
0
文件: repo.go 项目: rechen/wharf
func (r *Repository) Put(namespace, repository, json, agent string, version int64) error {
	if has, _, err := r.Has(namespace, repository); err != nil {
		return err
	} else if has == false {
		r.Id = string(utils.GeneralKey(fmt.Sprintf("%s:%s", namespace, repository)))
		r.Created = time.Now().UnixNano() / int64(time.Millisecond)
		r.Collaborators, r.Permissions = []string{}, []string{}
	}

	r.Namespace, r.Repository, r.JSON, r.Agent, r.Version = namespace, repository, json, agent, version

	r.Updated = time.Now().UnixNano() / int64(time.Millisecond)
	r.Checksumed, r.Uploaded, r.Cleared, r.Encrypted = false, false, false, false
	r.Size, r.Download = 0, 0

	if err := r.Save(); err != nil {
		return err
	}

	return nil
}
示例#9
0
func (this *UserWebAPIV1Controller) Signup() {
	user := new(models.User)
	org := new(models.Organization)

	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &user); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	} else {
		if exist, _, err := org.Has(user.Username); err != nil {
			this.JSONOut(http.StatusBadRequest, err.Error(), nil)
			return
		} else if exist == true {
			this.JSONOut(http.StatusBadRequest, "Namespace is occupation already by organization.", nil)
			return
		}

		if exist, _, err := user.Has(user.Username); err != nil {
			this.JSONOut(http.StatusBadRequest, err.Error(), nil)
			return
		} else if exist == true {
			this.JSONOut(http.StatusBadRequest, "User already exist.", nil)
			return
		} else {
			user.Id = string(utils.GeneralKey(user.Username))
			user.Created = time.Now().UnixNano() / int64(time.Millisecond)
			user.Gravatar = "/static/images/default-user-icon-profile.png"

			if err := user.Save(); err != nil {
				this.JSONOut(http.StatusBadRequest, err.Error(), nil)
				return
			}

			memo, _ := json.Marshal(this.Ctx.Input.Header)
			user.Log(models.ACTION_SIGNUP, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo)

			this.JSONOut(http.StatusOK, "User singup successfully!", nil)
			return
		}
	}
}