-
Notifications
You must be signed in to change notification settings - Fork 633
Expand file tree
/
Copy pathimagecache_local.go
More file actions
367 lines (326 loc) · 9.68 KB
/
Copy pathimagecache_local.go
File metadata and controls
367 lines (326 loc) · 9.68 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// 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 storageman
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
"path"
"sync"
"syscall"
"time"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/httputils"
"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/compute"
imageapi "yunion.io/x/onecloud/pkg/apis/image"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
"yunion.io/x/onecloud/pkg/hostman/storageman/remotefile"
"yunion.io/x/onecloud/pkg/mcclient/auth"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
"yunion.io/x/onecloud/pkg/util/fileutils2"
"yunion.io/x/onecloud/pkg/util/procutils"
"yunion.io/x/onecloud/pkg/util/qemuimg"
)
const (
_TMP_SUFFIX_ = ".tmp"
_INF_SUFFIX_ = ".inf"
CHECK_TIMEOUT = 3600 * time.Second
)
type SLocalImageCache struct {
imageId string
Manager IImageCacheManger
Size int64
Desc *remotefile.SImageDesc
consumerCount int
cond *sync.Cond
lastCheckTime time.Time
remoteFile *remotefile.SRemoteFile
accessDirLock sync.Mutex
}
func NewLocalImageCache(imageId string, imagecacheManager IImageCacheManger) *SLocalImageCache {
imageCache := new(SLocalImageCache)
imageCache.imageId = imageId
imageCache.Manager = imagecacheManager
imageCache.cond = sync.NewCond(new(sync.Mutex))
imageCache.accessDirLock = sync.Mutex{}
return imageCache
}
func (l *SLocalImageCache) GetDesc() *remotefile.SImageDesc {
return l.Desc
}
func (l *SLocalImageCache) GetImageId() string {
return l.imageId
}
func (l *SLocalImageCache) GetName() string {
if l.Desc != nil && len(l.Desc.Name) > 0 {
return l.Desc.Name
}
return l.imageId
}
func (l *SLocalImageCache) Load() error {
var (
imgPath = l.GetPath()
infPath = l.GetInfPath()
desc = &remotefile.SImageDesc{}
)
if fileutils2.Exists(imgPath) {
needReload := false
if fileutils2.Exists(infPath) {
sdesc, err := fileutils2.FileGetContents(infPath)
if err != nil {
return errors.Wrapf(err, "fileutils2.FileGetContents(%s)", infPath)
}
err = json.Unmarshal([]byte(sdesc), desc)
if err != nil {
return errors.Wrapf(err, "jsonutils.Unmarshal(%s)", infPath)
}
fi := l.getFileInfo()
if fi != nil && fi.Size()/1024/1024 != desc.SizeMb {
// fix file size
needReload = true
}
} else {
needReload = true
}
if needReload {
img, err := qemuimg.NewQemuImage(imgPath)
if err != nil {
return errors.Wrapf(err, "NewQemuImage(%s)", imgPath)
}
if !img.IsValid() {
return fmt.Errorf("invalid local image %s", img.String())
}
chksum, err := fileutils2.MD5(imgPath)
if err != nil {
return errors.Wrapf(err, "fileutils2.MD5(%s)", imgPath)
}
desc = &remotefile.SImageDesc{
Format: string(img.Format),
Id: l.imageId,
Chksum: chksum,
Path: imgPath,
}
fi := l.getFileInfo()
if fi != nil {
desc.SizeMb = fi.Size() / 1024 / 1024
if fi.Sys() != nil {
if stat, ok := fi.Sys().(*syscall.Stat_t); ok {
desc.AccessAt = fileutils2.GetAtim(stat)
}
}
}
err = fileutils2.FilePutContents(infPath, jsonutils.Marshal(desc).PrettyString(), false)
if err != nil {
return errors.Wrapf(err, "fileutils2.FilePutContents(%s)", infPath)
}
}
if len(desc.Chksum) > 0 && len(desc.Id) > 0 && desc.Id == l.imageId {
l.Desc = desc
return nil
} else {
log.Errorf("image %s desc not correct, desc.Chksum %s desc.Id %s l.imageId %s", imgPath, desc.Chksum, desc.Id, l.imageId)
}
}
tmpPath := l.GetTmpPath()
if fileutils2.Exists(tmpPath) {
syscall.Unlink(tmpPath)
}
return errors.Wrap(cloudprovider.ErrNotFound, imgPath)
}
func (l *SLocalImageCache) needCheck() bool {
if time.Now().Sub(l.lastCheckTime) > CHECK_TIMEOUT {
return true
}
return false
}
func (l *SLocalImageCache) Release() {
l.cond.L.Lock()
defer l.cond.L.Unlock()
l.consumerCount -= 1
}
func (l *SLocalImageCache) Acquire(ctx context.Context, input api.CacheImageInput, callback func(progress, progressMbps float64, totalSizeMb int64)) error {
isOk, err := l.prepare(ctx, input)
if err != nil {
return errors.Wrapf(err, "prepare")
}
if isOk {
return nil
}
return l.fetch(ctx, input, callback)
}
func (l *SLocalImageCache) prepare(ctx context.Context, input api.CacheImageInput) (bool, error) {
l.cond.L.Lock()
defer l.cond.L.Unlock()
for l.remoteFile != nil {
l.cond.Wait()
}
if l.remoteFile == nil && l.Desc != nil && (l.consumerCount > 0 || !l.needCheck()) {
l.consumerCount++
return true, nil
}
url, err := auth.GetServiceURL(apis.SERVICE_TYPE_IMAGE, "", input.Zone, "")
if err != nil {
return false, errors.Wrapf(err, "GetServiceURL(%s)", apis.SERVICE_TYPE_IMAGE)
}
url += fmt.Sprintf("/images/%s", l.imageId)
if len(input.Format) == 0 {
input.Format = "qcow2"
}
url += fmt.Sprintf("?format=%s&scope=system", input.Format)
l.remoteFile = remotefile.NewRemoteFile(ctx, url,
l.GetPath(), false, input.Checksum, -1, nil, l.GetTmpPath(), input.SrcUrl)
return false, nil
}
func (l *SLocalImageCache) fetch(ctx context.Context, input api.CacheImageInput, callback func(progress, progressMbps float64, totalSizeMb int64)) error {
// Whether successful or not, fetch should reset the condition variable and wakes up other waiters
defer func() {
l.cond.L.Lock()
l.remoteFile = nil
l.cond.Broadcast()
l.cond.L.Unlock()
}()
var _fetch = func() error {
if len(l.Manager.GetId()) > 0 {
_, err := hostutils.RemoteStoragecacheCacheImage(ctx,
l.Manager.GetId(), l.imageId, "active", l.GetPath())
if err != nil {
log.Errorf("Fail to update host cached image: %s", err)
}
}
l.cond.L.Lock()
defer l.cond.L.Unlock()
var err error
l.Desc, err = l.remoteFile.GetInfo()
if err != nil {
return errors.Wrapf(err, "remoteFile.GetInfo")
}
fi := l.getFileInfo()
if fi != nil {
l.Size = fi.Size() / 1024 / 1024
}
l.Desc.Id = l.imageId
l.lastCheckTime = time.Now()
l.consumerCount++
bDesc, err := json.Marshal(l.Desc)
if err != nil {
return errors.Wrapf(err, "json.Marshal(%#v)", l.Desc)
}
err = fileutils2.FilePutContents(l.GetInfPath(), string(bDesc), false)
if err != nil {
return errors.Wrapf(err, "FilePutContents(%s)", string(bDesc))
}
return nil
}
if fileutils2.Exists(l.GetPath()) {
if input.SkipChecksumIfExists {
if err := l.remoteFile.FillAttributes(callback); err != nil {
return errors.Wrap(err, "fetch remote attribute")
}
return _fetch()
} else {
if err := l.remoteFile.VerifyIntegrity(callback); err == nil {
return _fetch()
} else {
log.Warningf("Verify remotefile checksum error: %v, starting fetching it", err)
}
}
}
err := l.remoteFile.Fetch(callback)
if err != nil {
return errors.Wrapf(err, "remoteFile.Fetch")
}
return _fetch()
}
func (l *SLocalImageCache) Remove(ctx context.Context) error {
if fileutils2.Exists(l.GetPath()) {
if err := syscall.Unlink(l.GetPath()); err != nil {
return errors.Wrap(err, l.GetPath())
}
}
if fileutils2.Exists(l.GetInfPath()) {
if err := syscall.Unlink(l.GetInfPath()); err != nil {
return errors.Wrap(err, l.GetInfPath())
}
}
if fileutils2.Exists(l.GetTmpPath()) {
if err := syscall.Unlink(l.GetTmpPath()); err != nil {
return errors.Wrap(err, l.GetTmpPath())
}
}
if fileutils2.Exists(l.getAccessDirPath()) {
if err := procutils.NewCommand("/bin/rm", "-fr", l.getAccessDirPath()).Run(); err != nil {
return errors.Wrapf(err, "remove directory %s", l.getAccessDirPath())
}
}
go func() {
_, err := modules.Storagecachedimages.Detach(hostutils.GetComputeSession(ctx),
l.Manager.GetId(), l.imageId, nil)
if err != nil && httputils.ErrorCode(err) != 404 {
log.Errorf("Fail to delete host cached image %s at %s: %s", l.imageId, l.Manager.GetId(), err)
}
}()
return nil
}
func (l *SLocalImageCache) GetPath() string {
return path.Join(l.Manager.GetPath(), l.imageId)
}
func (l *SLocalImageCache) GetTmpPath() string {
return l.GetPath() + _TMP_SUFFIX_
}
func (l *SLocalImageCache) GetInfPath() string {
return l.GetPath() + _INF_SUFFIX_
}
func (l *SLocalImageCache) getFileInfo() fs.FileInfo {
if fi, err := os.Stat(l.GetPath()); err != nil {
log.Errorln(err)
return nil
} else {
return fi
}
}
func (l *SLocalImageCache) getAccessDirPath() string {
return fmt.Sprintf("%s-dir", l.GetPath())
}
func (l *SLocalImageCache) GetAccessDirectory() (string, error) {
if l.Desc.Format != imageapi.IMAGE_DISK_FORMAT_TGZ {
return "", errors.Wrapf(errors.ErrNotSupported, "format %s", l.Desc.Format)
}
l.accessDirLock.Lock()
defer l.accessDirLock.Unlock()
dir := l.getAccessDirPath()
if fileutils2.Exists(dir) {
return dir, nil
}
tmpDir := fmt.Sprintf("%s-tmp", dir)
// untar cached image
out, err := procutils.NewRemoteCommandAsFarAsPossible("mkdir", "-p", tmpDir).Output()
if err != nil {
return "", errors.Wrapf(err, "mkdir %s: %s", tmpDir, out)
}
out, err = procutils.NewRemoteCommandAsFarAsPossible("tar", "xf", l.GetPath(), "-C", tmpDir).Output()
if err != nil {
return "", errors.Wrapf(err, "untar to %s: %s", tmpDir, out)
}
out, err = procutils.NewRemoteCommandAsFarAsPossible("mv", tmpDir, dir).Output()
if err != nil {
return "", errors.Wrapf(err, "mv %s %s: %s", tmpDir, dir, out)
}
return dir, nil
}