diff --git a/dockyard11 b/dockyard11 new file mode 100755 index 0000000..5582285 Binary files /dev/null and b/dockyard11 differ diff --git a/handler/manifests.go b/handler/manifests.go index 5114d06..9874385 100755 --- a/handler/manifests.go +++ b/handler/manifests.go @@ -10,8 +10,11 @@ import ( "github.com/containerops/dockyard/models" "github.com/containerops/dockyard/module" + "github.com/containerops/wrench/db/redis" "github.com/containerops/wrench/setting" "github.com/containerops/wrench/utils" + "github.com/docker/docker/vendor/src/github.com/docker/engine-api/types/registry" + "strings" ) var ManifestCtx []byte @@ -129,3 +132,87 @@ func GetManifestsV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []by return http.StatusOK, []byte(t.Manifest) } + +func SearchTagsRepoListV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte) { + uri := ctx.Req.RequestURI + + r := new(models.Repository) + var ( + tempname string + namespace string + temprepo string + repository string + tempdate []registry.SearchResult + ) + tempname = strings.Split(uri, "?q=")[1] + + if len(strings.Split(uri, "/v1/")[1]) != 9 { + if strings.Contains(tempname, "%2F") == false { + if strings.Contains(tempname, "%3A") == true { + repository = strings.Split(tempname, "%3A")[0] + namespace = "" + } else { + repository = tempname + namespace = "" + } + tempkeys, _ := redis.Client.HGetAll(models.GLOBAL_REPOSITORY_INDEX).Result() + for _, tempkey := range tempkeys { + if strings.Contains(tempkey, "REPO") == true { + p := strings.Split(tempkey, "-")[1] + q := strings.Split(tempkey, "-")[2] + if (strings.Contains(p, repository) == true) || (strings.Contains(q, repository) == true) { + if _, err := r.Get(p, q); err != nil { + log.Error("[REGISTRY API V2] Failed to get repository %v/%v: %v", namespace, repository, err.Error()) + + result, _ := json.Marshal(map[string]string{"message": "Failed to get repository"}) + return http.StatusBadRequest, result + } + name := fmt.Sprintf("%s/%s", r.Namespace, r.Repository) + datamap := registry.SearchResult{StarCount: 1, IsOfficial: true, Name: name, IsAutomated: true, IsTrusted: true, Description: "nul"} + tempdate = append(tempdate, datamap) + } + } + } + data := registry.SearchResults{Query: "search", NumResults: 1, Results: tempdate} + result, _ := json.Marshal(data) + return http.StatusOK, result + + } else { + if strings.Index(uri, "%2F") != 13 { + namespace = strings.Split(tempname, "%2F")[0] + temprepo = strings.Split(uri, "%2F")[1] + + if strings.Contains(temprepo, "%3A") == false { + repository = temprepo + } else { + repository = strings.Split(temprepo, "%3A")[0] + } + + tempkeys, _ := redis.Client.HGetAll(models.GLOBAL_REPOSITORY_INDEX).Result() + for _, tempkey := range tempkeys { + if strings.Contains(tempkey, "REPO") == true && len(uri) != 9 { + p := strings.Split(tempkey, "-")[1] + q := strings.Split(tempkey, "-")[2] + if (strings.Contains(p, namespace) == true) && (strings.Contains(q, repository) == true) { + if _, err := r.Get(p, q); err != nil { + log.Error("[REGISTRY API V2] Failed to get repository %v/%v: %v", namespace, repository, err.Error()) + + result, _ := json.Marshal(map[string]string{"message": "Failed to get repository"}) + return http.StatusBadRequest, result + } + name := fmt.Sprintf("%s/%s", r.Namespace, r.Repository) + datamap := registry.SearchResult{StarCount: 1, IsOfficial: true, Name: name, IsAutomated: true, IsTrusted: true, Description: "nul"} + tempdate = append(tempdate, datamap) + } + } + } + data := registry.SearchResults{Query: "search", NumResults: 1, Results: tempdate} + result, _ := json.Marshal(data) + return http.StatusOK, result + } + } + } + data := registry.SearchResults{Query: "search", NumResults: 0, Results: tempdate} + result, _ := json.Marshal(data) + return http.StatusOK, result +} diff --git a/models/repository.go b/models/repository.go index cf7d5bd..a984d98 100755 --- a/models/repository.go +++ b/models/repository.go @@ -7,9 +7,15 @@ import ( "time" "github.com/containerops/wrench/db" + "github.com/containerops/wrench/db/redis" "github.com/containerops/wrench/setting" ) +const ( + //Dockyard Data Index + GLOBAL_REPOSITORY_INDEX = "GLOBAL_REPOSITORY_INDEX" +) + type Repository struct { Id int64 `json:"id" orm:"auto"` Namespace string `json:"namespace" orm:"varchar(255)"` @@ -42,7 +48,6 @@ func (r *Repository) Save(namespace, repository string) error { if err != nil { return err } - r.Namespace, r.Repository = namespace, repository if !exists { err = db.Drv.Insert(r) @@ -50,6 +55,10 @@ func (r *Repository) Save(namespace, repository string) error { err = db.Drv.Update(r) } + if _, err = redis.Client.HSet(GLOBAL_REPOSITORY_INDEX, (fmt.Sprintf("%s/%s", r.Namespace, r.Repository)), (fmt.Sprintf("REPO-%s-%s", r.Namespace, r.Repository))).Result(); err != nil { + return err + } + return err } diff --git a/router/router.go b/router/router.go index e55d1a2..a9dcd9f 100755 --- a/router/router.go +++ b/router/router.go @@ -15,6 +15,7 @@ func SetRouters(m *macaron.Macaron) { m.Get("/users", handler.GetUsersV1Handler) m.Post("/users", handler.PostUsersV1Handler) + m.Get("/:search", handler.SearchTagsRepoListV2Handler) m.Group("/repositories", func() { m.Put("/:namespace/:repository/tags/:tag", handler.PutTagV1Handler)