-
Notifications
You must be signed in to change notification settings - Fork 697
/
images.go
184 lines (156 loc) · 4.01 KB
/
images.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
package dependencies
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)
// ImagesConfig is the configuration for the images updater.
type ImagesConfig struct {
Names []string
TargetFilePath string
}
type imageUpdater struct {
fs afero.Fs
targetFilePath string
dockerUsername string
dockerPassword string
images []string
}
func newImageUpdater(images []string, targetFilePath string) *imageUpdater {
return &imageUpdater{
fs: afero.NewOsFs(),
targetFilePath: targetFilePath,
dockerUsername: os.Getenv("DOCKERHUB_USERNAME"),
dockerPassword: os.Getenv("DOCKERHUB_PASSWORD"),
images: images,
}
}
func (iu *imageUpdater) update() error {
for _, image := range iu.images {
if err := iu.updateImage(image); err != nil {
return err
}
}
return nil
}
func (iu *imageUpdater) updateImage(imageName string) error {
currentDigest, err := iu.readCurrentDigest(imageName)
if err != nil {
return err
}
remoteDigest, err := iu.readRemoteDigest(imageName)
if err != nil {
return err
}
if currentDigest != remoteDigest {
if err := iu.updateDigest(imageName, currentDigest, remoteDigest); err != nil {
return err
}
}
return nil
}
func (iu *imageUpdater) readCurrentDigest(imageName string) (string, error) {
data, err := afero.ReadFile(iu.fs, getTargetPath(iu.targetFilePath))
if err != nil {
return "", err
}
content := struct {
Version string
Services map[string]struct {
Image string
}
}{}
err = yaml.Unmarshal(data, &content)
if err != nil {
return "", err
}
for _, c := range content.Services {
if c.Image == "" {
continue
}
items := strings.Split(c.Image, "@")
const requiredItems = 2
if len(items) < requiredItems {
continue
}
if strings.HasPrefix(items[0], imageName) {
log.Infof("Current digest of %q is %q", imageName, items[1])
return items[1], nil
}
}
return "", fmt.Errorf("image %q not found in %q", imageName, iu.targetFilePath)
}
func (iu *imageUpdater) readRemoteDigest(imageName string) (string, error) {
err := iu.dockerLogin()
if err != nil {
return "", err
}
return iu.readLatestTag(imageName)
}
func (iu *imageUpdater) updateDigest(imageName, currentDigest, remoteDigest string) error {
targetFilePath := getTargetPath(iu.targetFilePath)
log.Infof("Updating %q...", targetFilePath)
oldContent, err := afero.ReadFile(iu.fs, targetFilePath)
if err != nil {
return err
}
oldImageField := fmt.Sprintf("%s@%s", imageName, currentDigest)
newImageField := fmt.Sprintf("%s@%s", imageName, remoteDigest)
newContent := strings.ReplaceAll(string(oldContent), oldImageField, newImageField)
return afero.WriteFile(iu.fs, targetFilePath, []byte(newContent), 0664) //nolint:gomnd
}
func (iu *imageUpdater) dockerLogin() error {
c := exec.Command("docker", "login", "--username", iu.dockerUsername, "--password-stdin") // #nosec G204
stdin, err := c.StdinPipe()
if err != nil {
return err
}
passReader := strings.NewReader(iu.dockerPassword)
_, err = io.Copy(stdin, passReader)
if err != nil {
return err
}
err = stdin.Close()
if err != nil {
return err
}
return c.Run()
}
type result struct {
RepoDigests []string
}
func (iu *imageUpdater) readLatestTag(imageName string) (string, error) {
c := exec.Command("docker", "pull", imageName) // #nosec G204
err := c.Run()
if err != nil {
return "", err
}
c = exec.Command("docker", "inspect", imageName) // #nosec G204
output, err := c.CombinedOutput()
if err != nil {
return "", err
}
reader := bytes.NewReader(output)
decoder := json.NewDecoder(reader)
r := struct {
Results []result
}{}
err = decoder.Decode(&r.Results)
if err != nil {
return "", err
}
items := strings.Split(r.Results[0].RepoDigests[0], "@")
const requiredItems = 2
if len(items) < requiredItems {
return "", fmt.Errorf("Returned image does not include digest %q", r.Results[0].RepoDigests[0])
}
log.Infof("Remote digest of %q is %q", imageName, items[1])
return items[1], nil
}