-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagwatch.go
168 lines (149 loc) · 4.6 KB
/
tagwatch.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
/*
* Copyright (c) 2022. Wolfgang Popp
*
* This file is part of tagwatch.
*
* tagwatch is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* tagwatch is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tagwatch. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"html"
"html/template"
"log"
"sort"
"strings"
)
const (
Version = "1.0"
AgentStr = "tagwatch/" + Version
EmptyFeed = `
<rss version="2.0">
<channel>
<title>Docker registry tags</title>
<link>https://github.com/woefe/tagwatch</link>
<description>No tags available</description>
<generator>` + AgentStr + `</generator>
</channel>
</rss>
`
tagsItemTemplate = `Available tags of <em>{{ .Repo }}</em> have changed. Current tags are:<br/><ul>{{ range .Tags }}<li><code>{{ . }}</code></li>{{ end }}</ul>`
feedDescriptionTemplate = "New tags for{{ range . }}\n - {{ .Repo }}({{ .Arch }})): {{ join .Tags \", \" }}{{ end }}"
)
type Registries map[*Registry]*RegistryClient
var registryClient = make(Registries)
func (r Registries) For(reg *Registry) *RegistryClient {
if r[reg] == nil {
r[reg] = NewRegistryClientFromConf(reg)
}
return r[reg]
}
func makeGuid(first string, strings ...string) string {
hasher := sha256.New()
hasher.Write([]byte(first))
for _, str := range strings {
hasher.Write([]byte(str))
}
return hex.EncodeToString(hasher.Sum(nil))
}
func makeDescription(tpl *template.Template, watches []*WatchConf) string {
var b strings.Builder
err := tpl.Execute(&b, watches)
if err != nil {
log.Println(err)
return ""
}
return b.String()
}
func makeDigestLink(baseURL string, repo string, taggedDigest TagDigest) string {
if baseURL != "https://registry.hub.docker.com/v2/" {
return baseURL + repo + "/manifests/" + taggedDigest.Tag
}
if strings.HasPrefix(repo, "library/") {
repo = strings.TrimPrefix(repo, "library/") + "/" + repo
}
return fmt.Sprintf(
"https://hub.docker.com/layers/%s/%s/images/%s?context=explore",
repo,
taggedDigest.Tag,
strings.Replace(taggedDigest.Digest, ":", "-", 1),
)
}
func makeTagsLink(baseURL, repo string) string {
if baseURL != "https://registry.hub.docker.com/v2/" {
return baseURL + repo + "/tags/list"
}
if strings.HasPrefix(repo, "library/") {
return "https://hub.docker.com/_/" + strings.TrimPrefix(repo, "library/") + "?tab=tags"
}
return "https://hub.docker.com/r/" + repo + "/tags"
}
func makeTagsDescription(tpl *template.Template, repo string, allTags []string) string {
var b strings.Builder
err := tpl.Execute(&b, struct {
Repo string
Tags []string
}{
Repo: repo,
Tags: allTags,
})
if err != nil {
log.Println(err)
return ""
}
return b.String()
}
func MakeFeed(conf *Conf) *[]byte {
descriptionTpl, err := template.New("description").Funcs(template.FuncMap{"join": strings.Join}).Parse(feedDescriptionTemplate)
tagsTpl, err := template.New("tags").Parse(tagsItemTemplate)
if err != nil {
log.Println(err)
return nil
}
feed := NewFeed("Docker registry tags", "https://github.com/woefe/tagwatch", makeDescription(descriptionTpl, conf.Tagwatch))
for _, watchConf := range conf.Tagwatch {
repo := watchConf.Repo
arch := watchConf.Arch
reg := watchConf.Registry
client := registryClient.For(reg)
allTags := client.ListTags(repo)
sort.Sort(sort.Reverse(ByVersion(allTags)))
if watchConf.WatchNew {
feed.AppendItems(NewItem(
"Available tags of "+html.EscapeString(repo)+" have changed",
makeTagsLink(reg.BaseURL, repo),
makeTagsDescription(tagsTpl, repo, allTags),
makeGuid(repo, allTags...),
))
}
for _, taggedDigest := range client.ListTagDigests(repo, arch, allTags, watchConf.Tags) {
title := html.EscapeString(fmt.Sprintf("%s:%s (%s)", repo, taggedDigest.Tag, arch))
digest := html.EscapeString(taggedDigest.Digest)
feed.AppendItems(NewItem(
title,
makeDigestLink(reg.BaseURL, repo, taggedDigest),
"Digest of <em>"+title+"</em> has changed. New digest is:<br/><code>"+digest+"</code>",
makeGuid(reg.BaseURL, repo, taggedDigest.Tag, arch, taggedDigest.Digest),
))
}
}
xml, err := feed.ToXML()
if err != nil {
log.Println(err)
xml = []byte(EmptyFeed)
}
return &xml
}