forked from shuveb/containers-the-hard-way
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
315 lines (286 loc) · 8.44 KB
/
image.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"encoding/json"
"fmt"
"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"io/ioutil"
"log"
"os"
"strings"
)
type manifest []struct {
Config string
RepoTags []string
Layers []string
}
type imageConfigDetails struct {
Env []string `json:"Env"`
Cmd []string `json:"Cmd"`
}
type imageConfig struct {
Config imageConfigDetails `json:"config"`
}
/*
This is the format of our imageDB file where we store the
list of images we have on the system.
{
"ubuntu" : {
"18.04": "[image-hash]",
"18.10": "[image-hash]",
"19.04": "[image-hash]",
"19.10": "[image-hash]",
},
"centos" : {
"6.0": "[image-hash]",
"6.1": "[image-hash]",
"6.2": "[image-hash]",
"7.0": "[image-hash]",
}
}
*/
type imageEntries map[string]string
type imagesDB map[string]imageEntries
func getBasePathForImage(imageShaHex string) string {
return getGockerImagesPath() + "/" + imageShaHex
}
func getManifestPathForImage(imageShaHex string) string {
return getBasePathForImage(imageShaHex) + "/manifest.json"
}
func getConfigPathForImage(imageShaHex string) string {
return getBasePathForImage(imageShaHex) + "/" + imageShaHex + ".json"
}
func deleteTempImageFiles(imageShaHash string) {
tmpPath := getGockerTempPath() + "/" + imageShaHash
doOrDieWithMsg(os.RemoveAll(tmpPath),
"Unable to remove temporary image files")
}
func getImageAndTagForHash(imageShaHash string) (string, string) {
idb := imagesDB{}
parseImagesMetadata(&idb)
for image,versions := range idb {
for version, hash := range versions {
if hash == imageShaHash {
return image, version
}
}
}
return "", ""
}
func imageExistsByHash(imageShaHex string) (string, string) {
idb := imagesDB{}
parseImagesMetadata(&idb)
for imgName, avlImages := range idb {
for imgTag, imgHash := range avlImages {
if imgHash == imageShaHex {
return imgName, imgTag
}
}
}
return "", ""
}
func imageExistByTag(imgName string, tagName string) (bool, string) {
idb := imagesDB{}
parseImagesMetadata(&idb)
for k, v := range idb {
if k == imgName {
for k, v := range v {
if k == tagName {
return true, v
}
}
}
}
return false, ""
}
func downloadImage(img v1.Image, imageShaHex string, src string) {
path := getGockerTempPath() + "/" + imageShaHex
os.Mkdir(path, 0755)
path +="/package.tar"
/* Save the image as a tar file */
if err := crane.SaveLegacy(img, src, path); err != nil {
log.Fatalf("saving tarball %s: %v", path, err)
}
log.Printf("Successfully downloaded %s\n", src)
}
func untarFile(imageShaHex string) {
pathDir := getGockerTempPath() + "/" + imageShaHex
pathTar := pathDir + "/package.tar"
if err := untar(pathTar, pathDir); err != nil {
log.Fatalf("Error untaring file: %v\n", err)
}
}
func processLayerTarballs(imageShaHex string, fullImageHex string) {
tmpPathDir := getGockerTempPath() + "/" + imageShaHex
pathManifest := tmpPathDir + "/manifest.json"
pathConfig := tmpPathDir + "/" + fullImageHex + ".json"
mani := manifest{}
parseManifest(pathManifest, &mani)
if len(mani) == 0 || len(mani[0].Layers) == 0 {
log.Fatal("Could not find any layers.")
}
if len(mani) > 1 {
log.Fatal("I don't know how to handle more than one manifest.")
}
imagesDir := getGockerImagesPath() + "/" + imageShaHex
_ = os.Mkdir(imagesDir, 0755)
/* untar the layer files. These become the basis of our container root fs */
for _, layer := range mani[0].Layers {
imageLayerDir := imagesDir + "/" + layer[:12] + "/fs"
log.Printf("Uncompressing layer to: %s \n", imageLayerDir)
_ = os.MkdirAll(imageLayerDir, 0755)
srcLayer := tmpPathDir + "/" + layer
if err:= untar(srcLayer, imageLayerDir); err != nil {
log.Fatalf("Unable to untar layer file: %s: %v\n", srcLayer, err)
}
}
/* Copy the manifest file for reference later */
copyFile(pathManifest, getManifestPathForImage(imageShaHex))
copyFile(pathConfig, getConfigPathForImage(imageShaHex))
}
func parseContainerConfig(imageShaHex string) imageConfig {
imagesConfigPath := getConfigPathForImage(imageShaHex)
data, err := ioutil.ReadFile(imagesConfigPath)
if err != nil {
log.Fatalf("Could not read image config file")
}
imgConfig := imageConfig{}
if err := json.Unmarshal(data, &imgConfig); err != nil {
log.Fatalf("Unable to parse image config data!")
}
return imgConfig
}
func parseImagesMetadata(idb *imagesDB) {
imagesDBPath := getGockerImagesPath() + "/" + "images.json"
if _, err := os.Stat(imagesDBPath); os.IsNotExist(err) {
/* If it doesn't exist create an empty DB */
ioutil.WriteFile(imagesDBPath, []byte("{}"), 0644)
}
data, err := ioutil.ReadFile(imagesDBPath)
if err != nil {
log.Fatalf("Could not read images DB: %v\n", err)
}
if err := json.Unmarshal(data, idb); err != nil {
log.Fatalf("Unable to parse images DB: %v\n", err)
}
}
func marshalImageMetadata(idb imagesDB) {
fileBytes, err := json.Marshal(idb)
if err != nil {
log.Fatalf("Unable to marshall images data: %v\n", err)
}
imagesDBPath := getGockerImagesPath() + "/" + "images.json"
if err := ioutil.WriteFile(imagesDBPath, fileBytes, 0644); err != nil {
log.Fatalf("Unable to save images DB: %v\n", err)
}
}
func storeImageMetadata(image string, tag string, imageShaHex string) {
idb := imagesDB{}
ientry := imageEntries{}
parseImagesMetadata(&idb)
if idb[image] != nil {
ientry = idb[image]
}
ientry[tag] = imageShaHex
idb[image] = ientry
marshalImageMetadata(idb)
}
func removeImageMetadata(imageShaHex string) {
idb := imagesDB{}
ientries := imageEntries{}
parseImagesMetadata(&idb)
imgName, _ := imageExistsByHash(imageShaHex)
if len(imgName) == 0 {
log.Fatalf("Could not get image details")
}
ientries = idb[imgName]
for tag, hash := range ientries {
if hash == imageShaHex {
delete(ientries, tag)
}
}
if len(ientries) == 0 {
delete(idb, imgName)
} else {
idb[imgName] = ientries
}
marshalImageMetadata(idb)
}
func deleteImageByHash(imageShaHex string) {
// Ensure that no running container is using the image we're setting
// out to delete. There is a race condition possible here, but we use
// the ostrich algorithm
imgName, imgTag := getImageAndTagForHash(imageShaHex)
if len(imgName) == 0 {
log.Fatalf("No such image")
}
containers, err := getRunningContainers()
if err != nil {
log.Fatalf("Unable to get running containers list: %v\n", err)
}
for _, container := range containers {
if container.image == imgName + ":" + imgTag {
log.Fatalf("Cannot delete image becuase it is in use by: %s",
container.containerId)
}
}
doOrDieWithMsg(os.RemoveAll(getGockerImagesPath() + "/" + imageShaHex),
"Unable to remove image directory")
removeImageMetadata(imageShaHex)
}
func printAvailableImages() {
idb := imagesDB{}
parseImagesMetadata(&idb)
fmt.Printf("IMAGE\t TAG\t ID\n")
for image, details := range idb {
fmt.Println(image)
for tag, hash := range details {
fmt.Printf("\t%16s %s\n", tag, hash)
}
}
}
func getImageNameAndTag(src string) (string, string) {
s := strings.Split(src, ":")
var img, tag string
if len(s) > 1 {
img, tag = s[0], s[1]
} else {
img = s[0]
tag = "latest"
}
return img, tag
}
func downloadImageIfRequired(src string) string {
imgName, tagName := getImageNameAndTag(src)
if downloadRequired, imageShaHex := imageExistByTag(imgName, tagName); !downloadRequired {
/* Setup the image we want to pull */
log.Printf("Downloading metadata for %s:%s, please wait...", imgName, tagName)
img, err := crane.Pull(strings.Join([]string{imgName, tagName}, ":"))
if err != nil {
log.Fatal(err)
}
manifest, _ := img.Manifest()
imageShaHex = manifest.Config.Digest.Hex[:12]
log.Printf("imageHash: %v\n", imageShaHex)
log.Println("Checking if image exists under another name...")
/* Identify cases where ubuntu:latest could be the same as ubuntu:20.04*/
altImgName, altImgTag := imageExistsByHash(imageShaHex)
if len(altImgName) > 0 && len(altImgTag) > 0 {
log.Printf("The image you requested %s:%s is the same as %s:%s\n",
imgName, tagName, altImgName, altImgTag)
storeImageMetadata(imgName, tagName, imageShaHex)
return imageShaHex
} else {
log.Println("Image doesn't exist. Downloading...")
downloadImage(img, imageShaHex, src)
untarFile(imageShaHex)
processLayerTarballs(imageShaHex, manifest.Config.Digest.Hex)
storeImageMetadata(imgName, tagName, imageShaHex)
deleteTempImageFiles(imageShaHex)
return imageShaHex
}
} else {
log.Println("Image already exists. Not downloading.")
return imageShaHex
}
}