Skip to content

Commit

Permalink
v3.0.2
Browse files Browse the repository at this point in the history
v3.0.2
  • Loading branch information
snagles authored Mar 24, 2018
2 parents a106660 + 7861443 commit fb39a2c
Show file tree
Hide file tree
Showing 34 changed files with 2,082 additions and 714 deletions.
7 changes: 4 additions & 3 deletions app/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

[[constraint]]
name = "github.com/spf13/viper"
version = "1.0.0"
branch = "master"

[[override]]
name = "github.com/prometheus/client_golang"
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/registry/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (c *ImagesController) GetImages() {
repositoryNameEncode := url.QueryEscape(repositoryName)
c.Data["tagName"] = c.Ctx.Input.Param(":tagName")

manager.AllRegistries.RLock()
registry, _ := manager.AllRegistries.Registries[registryName]
c.Data["registry"] = registry

Expand Down Expand Up @@ -151,4 +152,5 @@ func (c *ImagesController) GetImages() {
"",
}
}
manager.AllRegistries.RUnlock()
}
55 changes: 49 additions & 6 deletions app/controllers/registry/registries.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ type RegistriesController struct {

// Get returns the template for the registries page
func (c *RegistriesController) Get() {
manager.AllRegistries.RLock()
c.Data["registries"] = manager.AllRegistries.Registries

manager.AllRegistries.RUnlock()
// Index template
c.TplName = "registries.tpl"
}

// GetRegistryCount returns the number of currently added registries
func (c *RegistriesController) GetRegistryCount() {
manager.AllRegistries.RLock()
c.Data["registries"] = manager.AllRegistries.Registries

registryCount := struct {
Expand All @@ -34,14 +36,14 @@ func (c *RegistriesController) GetRegistryCount() {
len(manager.AllRegistries.Registries),
}
c.Data["json"] = &registryCount
manager.AllRegistries.RUnlock()
c.ServeJSON()
}

// AddRegistry adds a registry to the active registry list from a form
func (c *RegistriesController) AddRegistry() {
// Registry contains all identifying information for communicating with a registry

scheme, host, port, skipTLS, dockerhubIntegration, err := c.sanitizeForm()
scheme, host, name, port, skipTLS, dockerhubIntegration, err := c.sanitizeForm()
if err != nil {
c.CustomAbort(404, err.Error())
}
Expand All @@ -53,10 +55,38 @@ func (c *RegistriesController) AddRegistry() {

ttl := time.Duration(interval) * time.Second

_, err = manager.AddRegistry(scheme, host, "", "", port, ttl, skipTLS, dockerhubIntegration)
r, err := manager.NewRegistry(scheme, host, name, "", "", port, ttl, skipTLS, dockerhubIntegration)
if err != nil {
c.CustomAbort(404, err.Error())
}
manager.AllRegistries.Add(r)
manager.AllRegistries.WriteConfig()
c.Ctx.Redirect(302, "/registries")
}

func (c *RegistriesController) EditRegistry() {
// Registry contains all identifying information for communicating with a registry
scheme, host, name, port, skipTLS, dockerhubIntegration, err := c.sanitizeForm()
if err != nil {
c.CustomAbort(404, err.Error())
}

interval, err := c.GetInt("interval", 60)
if err != nil {
c.CustomAbort(404, err.Error())
}

new, err := manager.NewRegistry(scheme, host, name, "", "", port, time.Duration(interval)*time.Second, skipTLS, dockerhubIntegration)
if err != nil {
c.CustomAbort(404, err.Error())
}

registryName := c.Ctx.Input.Param(":registryName")
if old, ok := manager.AllRegistries.Registries[registryName]; ok {
manager.AllRegistries.Edit(new, old)
}

manager.AllRegistries.WriteConfig()
c.Ctx.Redirect(302, "/registries")
}

Expand All @@ -75,7 +105,7 @@ func (c *RegistriesController) RegistryStatus() {
c.ServeJSON()
}

scheme, host, port, skipTLS, _, err := c.sanitizeForm()
scheme, host, _, port, skipTLS, _, err := c.sanitizeForm()
if err != nil {
whenErr(err)
return
Expand All @@ -102,12 +132,25 @@ func (c *RegistriesController) RegistryStatus() {
res.IsAvailable = true
c.Data["json"] = &res
c.ServeJSON()
}

// Refresh refreshes the passed registry
func (c *RegistriesController) Refresh() {
registryName := c.Ctx.Input.Param(":registryName")

manager.AllRegistries.RLock()
if r, ok := manager.AllRegistries.Registries[registryName]; ok {
r.Update()
}
manager.AllRegistries.RLock()
// Index template
c.CustomAbort(200, "Refreshed registry")
}

func (c *RegistriesController) sanitizeForm() (scheme, host string, port int, skipTLS bool, dockerhubIntegration bool, err error) {
func (c *RegistriesController) sanitizeForm() (scheme, host string, name string, port int, skipTLS bool, dockerhubIntegration bool, err error) {
host = c.GetString("host")
port, err = c.GetInt("port", 5000)
name = c.GetString("name", fmt.Sprintf("%s:%v", host, port))
scheme = c.GetString("scheme", "https")
skipTLSOn := c.GetString("skip-tls-validation", "off")
if skipTLSOn == "on" {
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/registry/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ func (c *RepositoriesController) GetRepositories() {

registryName := c.Ctx.Input.Param(":registryName")

manager.AllRegistries.RLock()
if r, ok := manager.AllRegistries.Registries[registryName]; ok {
c.Data["registryName"] = registryName
c.Data["repositories"] = r.Repositories
}
manager.AllRegistries.RUnlock()

// Index template
c.TplName = "repositories.tpl"
}

// GetAllRepositoryCount returns the number of currently available repositories
func (c *RepositoriesController) GetAllRepositoryCount() {
manager.AllRegistries.RLock()
c.Data["registries"] = manager.AllRegistries.Registries

var repositoryCount struct {
Expand All @@ -33,6 +37,7 @@ func (c *RepositoriesController) GetAllRepositoryCount() {
for _, reg := range manager.AllRegistries.Registries {
repositoryCount.Count += len(reg.Repositories)
}
manager.AllRegistries.RUnlock()

c.Data["json"] = &repositoryCount
c.ServeJSON()
Expand All @@ -43,13 +48,15 @@ func (c *RepositoriesController) GetAllRepositories() {

repos := make(map[string][]*manager.Repository)

manager.AllRegistries.RLock()
for registryName, registry := range manager.AllRegistries.Registries {
for _, repository := range registry.Repositories {
repos[registryName] = append(repos[registryName], repository)
}
}

c.Data["repositories"] = repos
manager.AllRegistries.RUnlock()

// Index template
c.TplName = "allrepositories.tpl"
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/registry/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (c *TagsController) GetTags() {
repositoryName, _ := url.QueryUnescape(c.Ctx.Input.Param(":splat"))
repositoryNameEncode := url.QueryEscape(repositoryName)

manager.AllRegistries.RLock()
registry, _ := manager.AllRegistries.Registries[registryName]
repository, _ := registry.Repositories[repositoryName]
tags := repository.Tags
Expand All @@ -29,6 +30,7 @@ func (c *TagsController) GetTags() {
c.Data["registryName"] = registryName
c.Data["repositoryNameEncode"] = repositoryNameEncode
c.Data["repositoryName"] = repositoryName
manager.AllRegistries.RUnlock()

// Index template
c.TplName = "tags.tpl"
Expand All @@ -40,17 +42,22 @@ func (c *TagsController) DeleteTags() {
repositoryName, _ := url.QueryUnescape(c.Ctx.Input.Param(":splat"))
tag := c.Ctx.Input.Param(":tagName")

manager.AllRegistries.RLock()
registry, _ := manager.AllRegistries.Registries[registryName]
digest, _ := manager.AllRegistries.Registries[registryName].ManifestDigest(repositoryName, tag)
err := registry.Registry.DeleteManifest(repositoryName, digest)
manager.AllRegistries.RUnlock()
if err != nil {
logrus.WithFields(logrus.Fields{
"Digest": digest.String(),
"Error": err.Error(),
}).Errorf("Failed to delete digest.")
c.CustomAbort(404, fmt.Sprintf("Failure to delete Digest: %v Error: %v", digest.String(), err))
}
registry.Refresh()
manager.AllRegistries.Lock()
ur := registry.Update()
manager.AllRegistries.Registries[registry.Name] = &ur
manager.AllRegistries.Unlock()

c.CustomAbort(200, "Success")

Expand Down
84 changes: 4 additions & 80 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ package main

import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"time"

"github.com/astaxie/beego"
"github.com/sirupsen/logrus"
"github.com/snagles/docker-registry-manager/app/models"
_ "github.com/snagles/docker-registry-manager/app/routers"
"github.com/spf13/viper"
"github.com/urfave/cli"
)

const (
appVersion = "3.0.0"
appVersion = "3.0.2"
)

func main() {
Expand Down Expand Up @@ -73,38 +67,14 @@ func main() {
}

app.Action = func(ctx *cli.Context) {
c, err := parseRegistries(registriesFile)
if err != nil {
logrus.Fatal(err)
}

err = setlevel(logLevel)
manager.AllRegistries.LoadConfig(registriesFile)

err := setlevel(logLevel)
if err != nil {
logrus.Fatal(err)
}

for _, r := range c.Registries {
if r.URL != "" {
url, err := url.Parse(r.URL)
if err != nil {
logrus.Fatalf("Failed to parse registry from the passed url (%s): %s", r.URL, err)
}
duration, err := time.ParseDuration(r.RefreshRate)
if err != nil {
logrus.Fatalf("Failed to add registry (%s), invalid duration: %s", r.URL, err)
}
if r.Password != "" && r.Username != "" {
if _, err := manager.AddRegistry(url.Scheme, url.Hostname(), r.Username, r.Password, r.Port, duration, r.SkipTLS, r.DockerhubIntegration); err != nil {
logrus.Fatalf("Failed to add registry (%s): %s", r.URL, err)
}
} else {
if _, err := manager.AddRegistry(url.Scheme, url.Hostname(), "", "", r.Port, duration, r.SkipTLS, r.DockerhubIntegration); err != nil {
logrus.Fatalf("Failed to add registry (%s): %s", r.URL, err)
}
}
}
}

// Beego configuration
beego.BConfig.AppName = "docker-registry-manager"
beego.BConfig.RunMode = "dev"
Expand Down Expand Up @@ -162,49 +132,3 @@ func setlevel(level string) error {
}
return nil
}

type registries struct {
Registries map[string]struct {
URL string
Port int
Username string
Password string
SkipTLS bool `mapstructure:"skip-tls-validation"`
RefreshRate string `mapstructure:"refresh-rate"`
DockerhubIntegration bool `mapstructure:"dockerhub-integration"`
} `mapstructure:"registries"`
}

func parseRegistries(registriesFile string) (*registries, error) {
v := viper.New()

// If the registries path is not passed use the default project dir
if registriesFile != "" {
v.AddConfigPath(path.Dir(registriesFile))
base := path.Base(registriesFile)
ext := path.Ext(registriesFile)
v.SetConfigName(base[0 : len(base)-len(ext)])
logrus.Infof("Using registries located in %s with file name %s", path.Dir(registriesFile), base[0:len(base)-len(ext)])
} else {
v.SetConfigName("registries")
var root string
_, r, _, ok := runtime.Caller(0)
if ok {
root = filepath.Dir(r)
v.AddConfigPath(root)
} else {
logrus.Fatalf("Failed to get runtime caller for parser")
}
logrus.Infof("Using registries located in %s with file name %s", root, "registries.yml")
}

if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("Failed to read in registries file: %s", err)
}

c := registries{}
if err := v.Unmarshal(&c); err != nil {
return nil, fmt.Errorf("Unable to unmarshal registries file: %s", err)
}
return &c, nil
}
Loading

0 comments on commit fb39a2c

Please sign in to comment.