/
make_thin.go
118 lines (110 loc) · 3.51 KB
/
make_thin.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
package cmd
import (
"archive/tar"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/cvmfs/docker-graphdriver/docker2cvmfs/lib"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
"log"
"strings"
)
var MakeThin = &cobra.Command{
Use: "make-thin creates a thin image out of a regular docker images storing the files inside the provided repository.",
Short: "Directly creates a thin image from a regular docker image.",
Run: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags()
registry := flags.Lookup("registry").Value.String()
inputReference := flags.Lookup("input-reference").Value.String()
outputReference := flags.Lookup("output-reference").Value.String()
repository := flags.Lookup("repository").Value.String()
subdirectory := flags.Lookup("subdirectory").Value.String()
err := lib.PullLayers(registry, inputReference, repository, subdirectory)
if err != nil {
log.Fatal(err)
}
manifest, err := lib.GetManifest(registry, inputReference)
if err != nil {
log.Fatal(err)
}
changes := []string{"ENV CVMFS_IMAGE true"}
configString, err := lib.GetConfig(registry, inputReference)
if err != nil {
log.Println("Unable to get the configuration for the image")
} else {
// setting the option for the docker image
var config map[string]interface{}
json.Unmarshal([]byte(configString), &config)
configConfigInterface, ok := config["config"]
if ok {
configConfig := configConfigInterface.(map[string]interface{})
envInterface, okEnv := configConfig["Env"]
if okEnv {
env := envInterface.([]interface{})
for e := range env {
envStr := interface{}(env[e]).(string)
changes = append(changes,
fmt.Sprintf("ENV %s", envStr))
}
}
cmdInterface, okCmd := configConfig["Cmd"]
if okCmd {
cmd := cmdInterface.([]interface{})
for c := range cmd {
cmdStr := interface{}(cmd[c].(string))
changes = append(changes,
fmt.Sprintf("CMD %s", cmdStr))
}
}
}
}
origin := inputReference + "@" + registry
thinImage := lib.MakeThinImage(manifest, repository+"/"+strings.TrimSuffix(subdirectory, "/"), origin)
thinImageJson, err := json.MarshalIndent(thinImage, "", " ")
if err != nil {
log.Fatal(err)
}
var imageTarFileStorange bytes.Buffer
tarFile := tar.NewWriter(&imageTarFileStorange)
header := &tar.Header{Name: "thin.json",
Mode: 0600,
Size: int64(len(thinImageJson)),
}
err = tarFile.WriteHeader(header)
if err != nil {
log.Fatal("Error in creating the tarfile for the thin image. [WriteHeader]", err)
}
_, err = tarFile.Write(thinImageJson)
if err != nil {
log.Fatal("Error in creating the tarfile for the thin image. [Write] ", err)
}
err = tarFile.Close()
if err != nil {
log.Fatal("Error in creating the tarfile for the thin image. [Close] ", err)
}
dockerClient, err := client.NewEnvClient()
if err != nil {
log.Fatal("Impossible to get a docker client using your env variables: ", err)
}
image := types.ImageImportSource{
Source: bytes.NewBuffer(imageTarFileStorange.Bytes()),
SourceName: "-",
}
options := types.ImageImportOptions{
Tag: "",
Message: "",
Changes: changes,
}
importResult, err := dockerClient.ImageImport(context.Background(), image, outputReference, options)
if err != nil {
log.Fatal("Error in importing the images: ", err)
} else {
defer importResult.Close()
}
importResultBuffer := new(bytes.Buffer)
importResultBuffer.ReadFrom(importResult)
},
}