/
registries.go
177 lines (153 loc) · 4.45 KB
/
registries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package registry
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/astaxie/beego"
client "github.com/snagles/docker-registry-client/registry"
"github.com/snagles/docker-registry-manager/app/models"
)
// RegistriesController extends the beego.Controller type
type RegistriesController struct {
beego.Controller
}
// 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 {
Count int
}{
len(manager.AllRegistries.Registries),
}
c.Data["json"] = ®istryCount
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, 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())
}
ttl := time.Duration(interval) * time.Second
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")
}
// RegistryStatus responds with JSON containing the status of the registry
func (c *RegistriesController) RegistryStatus() {
// Define the response
var res struct {
Error string `json:"error, omitempty"`
IsAvailable bool `json:"is_available"`
}
var err error
whenErr := func(err error) {
res.Error = err.Error()
res.IsAvailable = false
c.Data["json"] = &res
c.ServeJSON()
}
scheme, host, _, port, skipTLS, _, err := c.sanitizeForm()
if err != nil {
whenErr(err)
return
}
url := fmt.Sprintf("%s://%s:%v", scheme, host, port)
var temp *client.Registry
if skipTLS {
temp, err = client.NewInsecure(url, "", "")
} else {
temp, err = client.New(url, "", "")
}
if err != nil {
whenErr(err)
return
}
err = temp.Ping()
if err != nil {
whenErr(err)
return
}
res.Error = ""
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, 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" {
skipTLS = true
}
dockerhubIntegrationOn := c.GetString("dockerhub-integration", "off")
if dockerhubIntegrationOn == "on" {
dockerhubIntegration = true
}
switch {
case scheme == "":
err = errors.New("Invalid scheme: " + scheme)
return
case host == "":
err = errors.New("Invalid host: " + host)
return
case port == 0:
err = errors.New("Invalid port: " + strconv.Itoa(port))
return
}
return
}