-
Notifications
You must be signed in to change notification settings - Fork 48
/
ImageHandler.go
175 lines (144 loc) · 5.59 KB
/
ImageHandler.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
// Cloud Driver Interface of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// This is Docker Driver.
//
// by CB-Spider Team, 2020.05.
package resources
import (
"github.com/sirupsen/logrus"
cblog "github.com/cloud-barista/cb-log"
"context"
"github.com/docker/docker/client"
"github.com/docker/docker/api/types"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
"bytes"
"fmt"
"strings"
)
type DockerImageHandler struct {
Region idrv.RegionInfo
Context context.Context
Client *client.Client
}
var cblogger *logrus.Logger
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("CB-SPIDER")
}
// (1) pull from dockerhub
// (2) get repo digests id from pulling return
// (3) get all image summary from local repos
// (4) get image ID and OS Info from inspection
func (imageHandler *DockerImageHandler) CreateImage(imageReqInfo irs.ImageReqInfo) (irs.ImageInfo, error) {
cblogger.Info("Docker Cloud Driver: called CreateImage()!")
// (1) pull from dockerhub
//ref) images, err := cli.ImagePull(context.Background(), "alpine:latest", types.ImagePullOptions{})
out, err := imageHandler.Client.ImagePull(imageHandler.Context, imageReqInfo.IId.NameId, types.ImagePullOptions{})
if err != nil {
cblogger.Error(err)
return irs.ImageInfo{}, err
}
defer out.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(out)
msg := buf.String()
// cblogger.Info(msg)
// (2) get repo digests id from pulling return
repoDigests, err := getRepoDigests(msg)
if err != nil {
cblogger.Error(err)
return irs.ImageInfo{}, err
}
// (3) get all image summary from local repos
images, err := imageHandler.Client.ImageList(imageHandler.Context, types.ImageListOptions{})
if err != nil {
cblogger.Error(err)
return irs.ImageInfo{}, err
}
// (4) get image ID and OS Info from inspection
for _, image := range images {
if strings.Contains(image.RepoDigests[0], repoDigests) {
imageReqInfo.IId.SystemId = image.ID
// (3) inspect image info for OS info
osName, err := getOSInfo(imageHandler, image.ID)
if err != nil {
cblogger.Error(err)
return irs.ImageInfo{}, err
}
return irs.ImageInfo{imageReqInfo.IId, osName, "", nil}, nil
}
}
return irs.ImageInfo{}, fmt.Errorf("[Local Repos:" + imageReqInfo.IId.NameId + "] does not exist!")
}
func getRepoDigests(msg string) (string, error) {
/*---------- msg example
{"status":"Pulling from panubo/sshd","id":"latest"}
{"status":"Digest: sha256:b260ab0136c734d80ef643387af0eeb807deb7e1f0a85cb432c7f310eca3bb83"}
{"status":"Status: Image is up to date for panubo/sshd:latest"}
------------*/
strList := strings.Split(msg, "\n")
for _, str := range strList {
if strings.Contains(str, "sha256") {
tmpList := strings.Split(str, ":")
str1 := strings.Trim(tmpList[2], " ") // sha256
runes := []rune(tmpList[3]) // b260ab0136c734d80ef643387af0eeb807deb7e1f0a85cb432c7f310eca3bb83"}
str2 := string(runes[0:len(tmpList[3])-3]) // b260ab0136c734d80ef643387af0eeb807deb7e1f0a85cb432c7f310eca3bb83
return str1+":"+str2, nil
}
}
return "", fmt.Errorf("failed image pulling-" + msg)
}
func getOSInfo(imageHandler *DockerImageHandler, imageID string) (string, error) {
inspec, _, err := imageHandler.Client.ImageInspectWithRaw(imageHandler.Context, imageID)
if err != nil {
cblogger.Error(err)
return "", err
}
return inspec.Os + ":" + inspec.OsVersion + ":" + inspec.Architecture, nil
}
func (imageHandler *DockerImageHandler) ListImage() ([]*irs.ImageInfo, error) {
cblogger.Info("Docker Cloud Driver: called ListImage()!")
//ref) images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
images, err := imageHandler.Client.ImageList(imageHandler.Context, types.ImageListOptions{})
if err != nil {
cblogger.Error(err)
return nil, err
}
listImages := make([]*irs.ImageInfo, len(images))
for i, image := range images {
osName, err := getOSInfo(imageHandler, image.ID)
if err != nil {
cblogger.Error(err)
return []*irs.ImageInfo{}, err
}
//listImages[i] = &irs.ImageInfo{irs.IID{"", image.ID}, osName, "", nil }
// To avoid empty validator, Using CSPID for NameID by powerkim, 2022.01.25.
listImages[i] = &irs.ImageInfo{irs.IID{image.ID, image.ID}, osName, "", nil }
}
return listImages, nil
}
func (imageHandler *DockerImageHandler) GetImage(imageIID irs.IID) (irs.ImageInfo, error) {
cblogger.Info("Docker Cloud Driver: called GetImage()!")
// inspect image info for OS info
osName, err := getOSInfo(imageHandler, imageIID.SystemId)
if err != nil {
cblogger.Error(err)
return irs.ImageInfo{}, err
}
return irs.ImageInfo{imageIID, osName, "", nil}, nil
}
func (imageHandler *DockerImageHandler) DeleteImage(imageIID irs.IID) (bool, error) {
cblogger.Info("Docker Cloud Driver: called DeleteImage()!")
response, err := imageHandler.Client.ImageRemove(imageHandler.Context, imageIID.SystemId, types.ImageRemoveOptions{})
if err != nil {
cblogger.Error(err)
return false, err
}
fmt.Printf("\n\n=================\n %#v", response)
return true, nil
}