/
cloudimages.go
161 lines (141 loc) · 4.33 KB
/
cloudimages.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
// 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 models
import (
"context"
"database/sql"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/mcclient"
)
type SCloudimageManager struct {
db.SStandaloneResourceBaseManager
db.SExternalizedResourceBaseManager
}
var CloudimageManager *SCloudimageManager
func init() {
CloudimageManager = &SCloudimageManager{
SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager(
SCloudimage{},
"cloudimages_tbl",
"cloudimage",
"cloudimages",
),
}
CloudimageManager.SetVirtualObject(CloudimageManager)
}
type SCloudimage struct {
db.SStandaloneResourceBase
db.SExternalizedResourceBase
SCloudregionResourceBase
}
func SyncPublicCloudImages(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
if isStart {
cnt, err := CloudimageManager.Query().CountWithError()
if err != nil {
return
}
if cnt > 0 {
log.Infof("Public cloud image has already synced, skip syncing")
return
}
}
regions := []SCloudregion{}
q := CloudregionManager.Query().In("provider", CloudproviderManager.GetPublicProviderProvidersQuery())
err := db.FetchModelObjects(CloudregionManager, q, ®ions)
if err != nil {
return
}
meta, err := FetchSkuResourcesMeta()
if err != nil {
log.Errorf("SyncServerSkus.FetchSkuResourcesMeta %s", err)
return
}
index, err := meta.getServerSkuIndex()
if err != nil {
log.Errorf("getServerSkuIndex error: %v", err)
return
}
for i := range regions {
region := ®ions[i]
oldMd5, _ := imageIndex[region.ExternalId]
newMd5, ok := index[region.ExternalId]
if ok {
imageIndex[region.ExternalId] = newMd5
}
if newMd5 == EMPTY_MD5 {
log.Infof("%s images is empty skip syncing", region.Name)
continue
}
if len(oldMd5) > 0 && newMd5 == oldMd5 {
log.Infof("%s cloud images not changed skip syncing", region.Name)
continue
}
err = regions[i].SyncCloudImages(ctx, userCred, !isStart)
if err != nil {
log.Errorf("SyncCloudImages for region %s(%s) error: %v", regions[i].Name, regions[i].Id, err)
continue
}
storagecaches, err := regions[i].GetStoragecaches()
if err != nil {
log.Errorf("GetStoragecaches for region %s(%s) error: %v", regions[i].Name, regions[i].Id, err)
}
for j := range storagecaches {
err = storagecaches[j].CheckCloudimages(ctx, userCred, regions[i].Name, regions[i].Id)
if err != nil {
log.Errorf("SyncSystemImages for region %s(%s) storagecache %s error: %v", regions[i].Name, regions[i].Id, storagecaches[j].Name, err)
}
}
}
return
}
func (self *SCloudimage) syncRemove(ctx context.Context, userCred mcclient.TokenCredential) error {
_image, err := db.FetchByExternalId(CachedimageManager, self.ExternalId)
if err != nil {
return errors.Wrapf(err, "db.FetchByExternalId")
}
image := _image.(*SCachedimage)
err = image.ValidateDeleteCondition(ctx, nil)
if err == nil {
image.Delete(ctx, userCred)
}
return self.Delete(ctx, userCred)
}
func (self *SCloudimage) syncWithImage(ctx context.Context, userCred mcclient.TokenCredential, image SCachedimage) error {
_cachedImage, err := db.FetchByExternalId(CachedimageManager, image.GetGlobalId())
if err != nil {
if errors.Cause(err) != sql.ErrNoRows {
return errors.Wrapf(err, "db.FetchByExternalId(%s)", image.GetGlobalId())
}
image := &image
image.Id = ""
image.IsPublic = true
image.ProjectId = "system"
image.SetModelManager(CachedimageManager, image)
err = CachedimageManager.TableSpec().Insert(ctx, image)
if err != nil {
return errors.Wrapf(err, "Insert cachedimage")
}
return nil
}
cachedImage := _cachedImage.(*SCachedimage)
_, err = db.Update(cachedImage, func() error {
cachedImage.Info = image.Info
cachedImage.Size = image.Size
cachedImage.UEFI = image.UEFI
return nil
})
return err
}