forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
270 lines (232 loc) · 6.38 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
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google
import (
"context"
"fmt"
"strings"
"time"
"github.com/nyl1001/pkg/util/imagetools"
"github.com/nyl1001/pkg/jsonutils"
"yunion.io/x/log"
api "github.com/nyl1001/cloudmux/pkg/apis/compute"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
"github.com/nyl1001/cloudmux/pkg/multicloud"
)
type GuestOsFeature struct {
Type string
}
type SDeprecated struct {
State string
Replacement string
Deprecated bool
}
type SImage struct {
multicloud.SImageBase
GoogleTags
storagecache *SStoragecache
SResourceBase
Name string
SelfLink string
Id string
// normalized image info
imgInfo *imagetools.ImageInfo
CreationTimestamp time.Time
Description string
SourceType string
RawDisk map[string]string
Deprecated SDeprecated
Status string
ArchiveSizeBytes int64
DiskSizeGb int
Licenses []string
Family string
LabelFingerprint string
GuestOsFeatures []GuestOsFeature
LicenseCodes []string
StorageLocations []string
Kind string
}
func (self *SImage) GetId() string {
return self.SelfLink
}
func (self *SImage) GetGlobalId() string {
return strings.TrimPrefix(self.SelfLink, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
}
func (self *SImage) GetName() string {
return self.Name
}
func (region *SRegion) SetProjectId(id string) {
region.client.projectId = id
}
func (region *SRegion) GetAllAvailableImages() ([]SImage, error) {
images := []SImage{}
projectId := region.client.projectId
for _, project := range []string{
"centos-cloud",
"ubuntu-os-cloud",
"windows-cloud",
"windows-sql-cloud",
"suse-cloud",
"suse-sap-cloud",
"rhel-cloud",
"rhel-sap-cloud",
"cos-cloud",
"coreos-cloud",
"debian-cloud",
projectId,
} {
_images, err := region.GetImages(project, 0, "")
if err != nil {
return nil, err
}
for _, image := range _images {
if image.Deprecated.State == "" {
images = append(images, image)
}
}
}
return images, nil
}
func (region *SRegion) GetImages(project string, maxResults int, pageToken string) ([]SImage, error) {
images := []SImage{}
resource := "global/images"
params := map[string]string{}
if len(project) > 0 {
region.SetProjectId(project)
}
return images, region.List(resource, params, maxResults, pageToken, &images)
}
func (region *SRegion) GetImage(id string) (*SImage, error) {
image := &SImage{}
return image, region.GetBySelfId(id, image)
}
func (image *SImage) GetMinRamSizeMb() int {
return 0
}
func (image *SImage) GetStatus() string {
switch image.Status {
case "READY":
return api.CACHED_IMAGE_STATUS_ACTIVE
case "FAILED":
return api.CACHED_IMAGE_STATUS_CACHE_FAILED
case "PENDING":
return api.CACHED_IMAGE_STATUS_SAVING
default:
log.Errorf("Unknown image status: %s", image.Status)
return api.CACHED_IMAGE_STATUS_CACHE_FAILED
}
}
func (image *SImage) GetImageStatus() string {
switch image.Status {
case "READY":
return cloudprovider.IMAGE_STATUS_ACTIVE
case "FAILED":
return cloudprovider.IMAGE_STATUS_KILLED
case "PENDING":
return cloudprovider.IMAGE_STATUS_QUEUED
default:
return cloudprovider.IMAGE_STATUS_KILLED
}
}
func (image *SImage) Refresh() error {
_image, err := image.storagecache.region.GetImage(image.SelfLink)
if err != nil {
return err
}
return jsonutils.Update(image, _image)
}
func (image *SImage) GetImageType() cloudprovider.TImageType {
if strings.Index(image.SelfLink, image.storagecache.region.GetProjectId()) >= 0 {
return cloudprovider.ImageTypeCustomized
}
return cloudprovider.ImageTypeSystem
}
func (image *SImage) GetSizeByte() int64 {
return image.ArchiveSizeBytes
}
func (image *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
if image.imgInfo == nil {
imgInfo := imagetools.NormalizeImageInfo(image.Name, "", "", "", "")
image.imgInfo = &imgInfo
}
return image.imgInfo
}
func (image *SImage) GetOsType() cloudprovider.TOsType {
return cloudprovider.TOsType(image.getNormalizedImageInfo().OsType)
}
func (image *SImage) GetOsDist() string {
return image.getNormalizedImageInfo().OsDistro
}
func (image *SImage) GetOsVersion() string {
return image.getNormalizedImageInfo().OsVersion
}
func (image *SImage) GetOsLang() string {
return image.getNormalizedImageInfo().OsLang
}
func (image *SImage) GetOsArch() string {
return image.getNormalizedImageInfo().OsArch
}
func (image *SImage) GetFullOsName() string {
return image.Name
}
func (image *SImage) GetBios() cloudprovider.TBiosType {
return cloudprovider.ToBiosType(image.getNormalizedImageInfo().OsBios)
}
func (image *SImage) GetMinOsDiskSizeGb() int {
return image.DiskSizeGb
}
func (image *SImage) GetCreatedAt() time.Time {
return image.CreationTimestamp
}
func (image *SImage) GetImageFormat() string {
return "raw"
}
func (image *SImage) IsEmulated() bool {
return false
}
func (image *SImage) Delete(ctx context.Context) error {
return cloudprovider.ErrNotImplemented
}
func (image *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
return image.storagecache
}
func (region *SRegion) CreateImage(name string, desc string, bucketName string, sourceFile string) (*SImage, error) {
body := map[string]interface{}{
"timeout": "7200s",
"steps": []struct {
Args []string
Name string
}{
{
Args: []string{
fmt.Sprintf("-source_file=gs://%s/%s", bucketName, sourceFile),
"-data_disk",
"-timeout=7056s",
"-image_name=" + name,
"-no_guest_environment",
"-client_id=onecloud",
"-description=" + desc,
},
Name: "gcr.io/compute-image-tools/gce_vm_image_import:release",
},
},
"tags": []string{"gce-daisy", "gce-daisy-image-import"},
}
err := region.CloudbuildInsert(jsonutils.Marshal(body))
if err != nil {
return nil, err
}
return region.GetImage(fmt.Sprintf("projects/%s/global/images/%s", region.GetProjectId(), name))
}