-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
docker.go
170 lines (159 loc) · 3.97 KB
/
docker.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
package dockerimagesave
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
docker "github.com/fsouza/go-dockerclient"
)
// PullImage pulls a docker image to local Docker
func PullImage(imageid string) error {
ctx := context.Background()
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
defer func(dockerClient *client.Client) {
err := dockerClient.Close()
if err != nil {
log.Print(err)
}
}(dockerClient)
if err != nil {
return err
}
authConfig := registry.AuthConfig{
Username: os.Getenv("DOCKER_USER"),
Password: os.Getenv("DOCKER_PASSWORD"),
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
panic(err)
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
out, err := dockerClient.ImagePull(ctx, imageid, image.PullOptions{RegistryAuth: authStr})
if err != nil {
return err
}
defer func(out io.ReadCloser) {
err := out.Close()
if err != nil {
log.Print(err)
}
}(out)
_, err = io.Copy(os.Stdout, out)
if err != nil {
return err
}
return nil
}
// SaveImage saves a docker image as tar file on specified folder
func SaveImage(imageid string, folder string) error {
ctx := context.Background()
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
defer func(dockerClient *client.Client) {
err := dockerClient.Close()
if err != nil {
log.Print(err)
}
}(dockerClient)
if err != nil {
return err
}
imageFileName := strings.ReplaceAll(imageid, "/", "_")
imageFileName = strings.Replace(imageFileName, ":", "_", 1)
imageFileName = RemoveDoubleDots(imageFileName)
f, err := os.Create(folder + "/" + imageFileName + ".tar")
if err != nil {
return err
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
log.Print(err)
}
}(f)
w := bufio.NewWriter(f)
data, err := dockerClient.ImageSave(ctx, []string{imageid})
if err != nil {
return err
}
_, err = io.Copy(w, data)
if err != nil {
return err
}
err = w.Flush()
if err != nil {
return err
}
return nil
}
// ImageExists checks if image is downloaded
func ImageExists(imageid string) (bool, error) {
ctx := context.Background()
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
defer func(dockerClient *client.Client) {
err := dockerClient.Close()
if err != nil {
log.Print(err)
}
}(dockerClient)
if err != nil {
return false, err
}
imgs, err := dockerClient.ImageList(ctx, image.ListOptions{
All: false,
Filters: filters.Args{},
})
if err != nil {
return false, err
}
for _, img := range imgs {
for _, repotag := range img.RepoTags {
if repotag == imageid {
return true, nil
}
}
}
return false, nil
}
// ImageExistsInRegistry determines if an image exists in the docker registry
func ImageExistsInRegistry(imageid string) (bool, error) {
if !strings.Contains(imageid, ":") {
return false, errors.New("The use of a Tag is obligatory")
}
imageAndTag := strings.Split(imageid, ":")
if !strings.Contains(imageid, "/") {
imageAndTag[0] = fmt.Sprintf("library/%s", imageAndTag[0])
}
resp, err := http.Get(fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/tags/%s/", imageAndTag[0], imageAndTag[1]))
defer CloseResponse(resp)
if err != nil {
return false, err
}
if resp.StatusCode != http.StatusOK {
return false, nil
}
b, _ := io.ReadAll(resp.Body)
return string(b) != "\"Resource not found\"" && string(b) != "Tag not found", nil
}
// Search does a docker search
func Search(term string) ([]docker.APIImageSearch, error) {
dockerClient, err := docker.NewClientFromEnv()
if err != nil {
return nil, err
}
imageSearch, err := dockerClient.SearchImages(term)
if err != nil {
return nil, err
}
return imageSearch, nil
}