forked from mlabouardy/nexus-cli
/
registry.go
191 lines (153 loc) · 4.46 KB
/
registry.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package registry
import (
"encoding/json"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"net/http"
"os"
)
const ACCEPT_HEADER = "application/vnd.docker.distribution.manifest.v2+json"
const CREDENTIALS_FILE = ".credentials"
type Registry struct {
Host string `toml:"nexus_host"`
Username string `toml:"nexus_username"`
Password string `toml:"nexus_password"`
Repository string `toml:"nexus_repository"`
}
type Repositories struct {
Images []string `json:"repositories"`
}
type ImageTags struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
type ImageManifest struct {
SchemaVersion int64 `json:"schemaVersion"`
MediaType string `json:"mediaType"`
Config LayerInfo `json:"config"`
Layers []LayerInfo `json:"layers"`
}
type LayerInfo struct {
MediaType string `json:"mediaType"`
Size int64 `json:"size"`
Digest string `json:"digest"`
}
func NewRegistry() (Registry, error) {
r := Registry{}
if _, err := os.Stat(CREDENTIALS_FILE); os.IsNotExist(err) {
return r, errors.New(fmt.Sprintf("%s file not found\n", CREDENTIALS_FILE))
} else if err != nil {
return r, err
}
if _, err := toml.DecodeFile(CREDENTIALS_FILE, &r); err != nil {
return r, err
}
return r, nil
}
func (r Registry) ListImages() ([]string, error) {
client := &http.Client{}
url := fmt.Sprintf("%s/repository/%s/v2/_catalog", r.Host, r.Repository)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(r.Username, r.Password)
req.Header.Add("Accept", ACCEPT_HEADER)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode))
}
var repositories Repositories
json.NewDecoder(resp.Body).Decode(&repositories)
return repositories.Images, nil
}
func (r Registry) ListTagsByImage(image string) ([]string, error) {
client := &http.Client{}
url := fmt.Sprintf("%s/repository/%s/v2/%s/tags/list", r.Host, r.Repository, image)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(r.Username, r.Password)
req.Header.Add("Accept", ACCEPT_HEADER)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode))
}
var imageTags ImageTags
json.NewDecoder(resp.Body).Decode(&imageTags)
return imageTags.Tags, nil
}
func (r Registry) ImageManifest(image string, tag string) (ImageManifest, error) {
var imageManifest ImageManifest
client := &http.Client{}
url := fmt.Sprintf("%s/repository/%s/v2/%s/manifests/%s", r.Host, r.Repository, image, tag)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return imageManifest, err
}
req.SetBasicAuth(r.Username, r.Password)
req.Header.Add("Accept", ACCEPT_HEADER)
resp, err := client.Do(req)
if err != nil {
return imageManifest, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return imageManifest, errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode))
}
json.NewDecoder(resp.Body).Decode(&imageManifest)
return imageManifest, nil
}
func (r Registry) DeleteImageByTag(image string, tag string) error {
sha, err := r.getImageSHA(image, tag)
if err != nil {
return err
}
client := &http.Client{}
url := fmt.Sprintf("%s/repository/%s/v2/%s/manifests/%s", r.Host, r.Repository, image, sha)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
req.SetBasicAuth(r.Username, r.Password)
req.Header.Add("Accept", ACCEPT_HEADER)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 202 {
return errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode))
}
fmt.Printf("%s:%s has been successful deleted\n", image, tag)
return nil
}
func (r Registry) getImageSHA(image string, tag string) (string, error) {
client := &http.Client{}
url := fmt.Sprintf("%s/repository/%s/v2/%s/manifests/%s", r.Host, r.Repository, image, tag)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.SetBasicAuth(r.Username, r.Password)
req.Header.Add("Accept", ACCEPT_HEADER)
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode))
}
return resp.Header.Get("docker-content-digest"), nil
}