Merge remote-tracking branch 'upstream/job-service' into job-service

This commit is contained in:
Tan Jiang 2016-06-01 16:04:24 +08:00
commit 7b79283cad
11 changed files with 55 additions and 91 deletions

View File

@ -31,7 +31,7 @@ type ReplicationReq struct {
TagList []string `json:"tags"`
}
// Post ...
// Post creates replication jobs according to the policy.
func (rj *ReplicationJob) Post() {
var data ReplicationReq
rj.DecodeJSONReq(&data)
@ -102,7 +102,7 @@ type RepActionReq struct {
Action string `json:"action"`
}
// HandleAction stops jobs of policy
// HandleAction supports some operations to all the jobs of one policy
func (rj *ReplicationJob) HandleAction() {
var data RepActionReq
rj.DecodeJSONReq(&data)
@ -125,7 +125,7 @@ func (rj *ReplicationJob) HandleAction() {
job.WorkerPool.StopJobs(jobIDList)
}
// GetLog gets log of a job
// GetLog gets logs of the job
func (rj *ReplicationJob) GetLog() {
idStr := rj.Ctx.Input.Param(":id")
jid, err := strconv.ParseInt(idStr, 10, 64)

View File

@ -40,7 +40,7 @@ func (ra *RepJobAPI) Prepare() {
}
// Get ...
// Get gets all the jobs according to the policy
func (ra *RepJobAPI) Get() {
policyID, err := ra.GetInt64("policy_id")
if err != nil {

View File

@ -49,7 +49,7 @@ func (pa *RepPolicyAPI) Prepare() {
}
}
// Get ...
// Get gets all the policies according to the project
func (pa *RepPolicyAPI) Get() {
projectID, err := pa.GetInt64("project_id")
if err != nil {
@ -67,7 +67,7 @@ func (pa *RepPolicyAPI) Get() {
pa.ServeJSON()
}
// Post ...
// Post creates a policy, and if it is enbled, the replication will be triggered right now.
func (pa *RepPolicyAPI) Post() {
policy := models.RepPolicy{}
pa.DecodeJSONReq(&policy)
@ -95,7 +95,7 @@ type enablementReq struct {
Enabled int `json:"enabled"`
}
// UpdateEnablement changes the enablement of policy
// UpdateEnablement changes the enablement of the policy
func (pa *RepPolicyAPI) UpdateEnablement() {
e := enablementReq{}
pa.DecodeJSONReq(&e)

View File

@ -99,9 +99,9 @@ func (t *TargetAPI) Ping() {
if urlErr, ok := err.(*url.Error); ok {
if netErr, ok := urlErr.Err.(net.Error); ok {
t.CustomAbort(http.StatusBadRequest, netErr.Error())
} else {
t.CustomAbort(http.StatusBadRequest, urlErr.Error())
}
t.CustomAbort(http.StatusBadRequest, urlErr.Error())
}
log.Errorf("failed to create registry client: %#v", err)
@ -109,15 +109,6 @@ func (t *TargetAPI) Ping() {
}
if err = registry.Ping(); err != nil {
// timeout, dns resolve error, connection refused, etc.
if urlErr, ok := err.(*url.Error); ok {
if netErr, ok := urlErr.Err.(net.Error); ok {
t.CustomAbort(http.StatusBadRequest, netErr.Error())
} else {
t.CustomAbort(http.StatusBadRequest, urlErr.Error())
}
}
if regErr, ok := err.(*registry_error.Error); ok {
t.CustomAbort(regErr.StatusCode, regErr.Detail)
}

View File

@ -56,7 +56,7 @@ func hasProjectAdminRole(userID int, projectID int64) bool {
//sysadmin has all privileges to all projects
func listRoles(userID int, projectID int64) ([]models.Role, error) {
roles := make([]models.Role, 1)
roles := make([]models.Role, 0, 1)
isSysAdmin, err := dao.IsAdminRole(userID)
if err != nil {
return roles, err
@ -160,9 +160,9 @@ func TriggerReplicationByRepository(repository string, tags []string, operation
for _, policy := range policies {
if err := TriggerReplication(policy.ID, repository, tags, operation); err != nil {
log.Errorf("failed to trigger replication of %d for %s: %v", policy.ID, repository, err)
log.Errorf("failed to trigger replication of policy %d for %s: %v", policy.ID, repository, err)
} else {
log.Infof("replication of %d for %s triggered", policy.ID, repository)
log.Infof("replication of policy %d for %s triggered", policy.ID, repository)
}
}
}
@ -172,7 +172,7 @@ func buildReplicationURL() string {
url = strings.TrimSpace(url)
url = strings.TrimRight(url, "/")
return fmt.Sprintf("%s/api/replicationJobs", url)
return fmt.Sprintf("%s/api/jobs/replication", url)
}
func buildJobLogURL(jobID string) string {
@ -180,13 +180,13 @@ func buildJobLogURL(jobID string) string {
url = strings.TrimSpace(url)
url = strings.TrimRight(url, "/")
return fmt.Sprintf("%s/api/replicationJobs/%s/log", url, jobID)
return fmt.Sprintf("%s/api/jobs/replication/%s/log", url, jobID)
}
func getJobServiceURL() string {
url := os.Getenv("JOB_SERVICE_URL")
if len(url) == 0 {
url = "http://job_service"
url = "http://jobservice"
}
return url
}

View File

@ -53,7 +53,7 @@ type BaseHandler struct {
dstURL string // url of target registry
dstUsr string // username ...
dstPwd string // username ...
dstPwd string // password ...
srcClient *registry.Repository
dstClient *registry.Repository

View File

@ -71,7 +71,7 @@ func (n *NotificationHandler) Post() {
username = "anonymous"
}
if username == "job-service-user" {
if action == "pull" && username == "job-service-user" {
return
}

View File

@ -52,13 +52,12 @@ func initRouters() {
//API:
beego.Router("/api/search", &api.SearchAPI{})
beego.Router("/api/projects/:pid([0-9]+)/members/?:mid([0-9]+)", &api.ProjectMemberAPI{})
beego.Router("/api/projects/:pid([0-9]+)/members/?:mid", &api.ProjectMemberAPI{})
beego.Router("/api/projects/", &api.ProjectAPI{}, "get:List")
beego.Router("/api/projects/?:id([0-9]+)", &api.ProjectAPI{})
beego.Router("/api/projects/?:id", &api.ProjectAPI{})
beego.Router("/api/statistics", &api.StatisticAPI{})
beego.Router("/api/projects/:id([0-9]+)/logs/filter", &api.ProjectAPI{}, "post:FilterAccessLog")
beego.Router("/api/users", &api.UserAPI{})
beego.Router("/api/users/?:id([0-9]+)", &api.UserAPI{})
beego.Router("/api/users/?:id", &api.UserAPI{})
beego.Router("/api/users/:id([0-9]+)/password", &api.UserAPI{}, "put:ChangePassword")
beego.Router("/api/repositories", &api.RepositoryAPI{})
beego.Router("/api/repositories/tags", &api.RepositoryAPI{}, "get:GetTags")

View File

@ -124,11 +124,7 @@ func (r *Registry) Catalog() ([]string, error) {
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
return repos, regErr
}
return repos, err
return repos, parseError(err)
}
defer resp.Body.Close()
@ -167,17 +163,17 @@ func (r *Registry) Ping() error {
resp, err := r.client.Do(req)
if err != nil {
if urlErr, ok := err.(*url.Error); ok {
if regErr, ok := urlErr.Err.(*registry_error.Error); ok {
return &registry_error.Error{
StatusCode: regErr.StatusCode,
Detail: regErr.Detail,
}
}
return urlErr.Err
}
// if urlErr, ok := err.(*url.Error); ok {
// if regErr, ok := urlErr.Err.(*registry_error.Error); ok {
// return &registry_error.Error{
// StatusCode: regErr.StatusCode,
// Detail: regErr.Detail,
// }
// }
// return urlErr.Err
// }
return err
return parseError(err)
}
if resp.StatusCode == http.StatusOK {

View File

@ -112,6 +112,16 @@ func NewRepositoryWithUsername(name, endpoint, username string) (*Repository, er
return repository, nil
}
func parseError(err error) error {
if urlErr, ok := err.(*url.Error); ok {
if regErr, ok := urlErr.Err.(*registry_error.Error); ok {
return regErr
}
return urlErr.Err
}
return err
}
// ListTag ...
func (r *Repository) ListTag() ([]string, error) {
tags := []string{}
@ -122,11 +132,7 @@ func (r *Repository) ListTag() ([]string, error) {
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
return tags, regErr
}
return tags, err
return tags, parseError(err)
}
defer resp.Body.Close()
@ -168,10 +174,7 @@ func (r *Repository) ManifestExist(reference string) (digest string, exist bool,
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
err = regErr
return
}
err = parseError(err)
return
}
@ -212,10 +215,7 @@ func (r *Repository) PullManifest(reference string, acceptMediaTypes []string) (
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
err = regErr
return
}
err = parseError(err)
return
}
@ -251,10 +251,7 @@ func (r *Repository) PushManifest(reference, mediaType string, payload []byte) (
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
err = regErr
return
}
err = parseError(err)
return
}
@ -287,10 +284,7 @@ func (r *Repository) DeleteManifest(digest string) error {
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
return regErr
}
return err
return parseError(err)
}
if resp.StatusCode == http.StatusAccepted {
@ -335,10 +329,7 @@ func (r *Repository) BlobExist(digest string) (bool, error) {
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
return false, regErr
}
return false, err
return false, parseError(err)
}
if resp.StatusCode == http.StatusOK {
@ -371,10 +362,7 @@ func (r *Repository) PullBlob(digest string) (size int64, data io.ReadCloser, er
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
err = regErr
return
}
err = parseError(err)
return
}
@ -408,10 +396,7 @@ func (r *Repository) initiateBlobUpload(name string) (location, uploadUUID strin
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
err = regErr
return
}
err = parseError(err)
return
}
@ -444,10 +429,7 @@ func (r *Repository) monolithicBlobUpload(location, digest string, size int64, d
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
return regErr
}
return err
return parseError(err)
}
if resp.StatusCode == http.StatusCreated {
@ -486,10 +468,7 @@ func (r *Repository) DeleteBlob(digest string) error {
resp, err := r.client.Do(req)
if err != nil {
if regErr, ok := err.(*registry_error.Error); ok {
return regErr
}
return err
return parseError(err)
}
if resp.StatusCode == http.StatusAccepted {

View File

@ -25,9 +25,8 @@ import (
"testing"
"time"
//"github.com/vmware/harbor/utils/log"
"github.com/vmware/harbor/utils/registry/auth"
"github.com/vmware/harbor/utils/registry/errors"
"github.com/vmware/harbor/utils/registry/error"
)
var (
@ -164,12 +163,12 @@ func TestListTagWithInvalidCredential(t *testing.T) {
t.Error(err)
}
_, err = client.ListTag()
if err != nil {
e, ok := errors.ParseError(err)
if _, err = client.ListTag(); err != nil {
e, ok := err.(*error.Error)
if ok && e.StatusCode == http.StatusUnauthorized {
return
}
t.Error(err)
return
}