yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/storagecache.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package openstack 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/log" 24 "yunion.io/x/pkg/errors" 25 26 api "yunion.io/x/cloudmux/pkg/apis/compute" 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 "yunion.io/x/onecloud/pkg/compute/options" 29 "yunion.io/x/onecloud/pkg/mcclient" 30 "yunion.io/x/onecloud/pkg/mcclient/auth" 31 modules "yunion.io/x/onecloud/pkg/mcclient/modules/image" 32 "yunion.io/x/cloudmux/pkg/multicloud" 33 "yunion.io/x/onecloud/pkg/util/qemuimg" 34 ) 35 36 type SStoragecache struct { 37 multicloud.SResourceBase 38 OpenStackTags 39 region *SRegion 40 } 41 42 func (cache *SStoragecache) GetId() string { 43 return fmt.Sprintf("%s-%s", cache.region.client.cpcfg.Id, cache.region.GetId()) 44 } 45 46 func (cache *SStoragecache) GetName() string { 47 return fmt.Sprintf("%s-%s", cache.region.client.cpcfg.Name, cache.region.GetId()) 48 } 49 50 func (cache *SStoragecache) GetStatus() string { 51 return "available" 52 } 53 54 func (cache *SStoragecache) Refresh() error { 55 return nil 56 } 57 58 func (cache *SStoragecache) GetGlobalId() string { 59 return fmt.Sprintf("%s-%s", cache.region.client.cpcfg.Id, cache.region.GetGlobalId()) 60 } 61 62 func (cache *SStoragecache) IsEmulated() bool { 63 return false 64 } 65 66 func (cache *SStoragecache) GetICloudImages() ([]cloudprovider.ICloudImage, error) { 67 images, err := cache.region.GetImages("", ACTIVE, "") 68 if err != nil { 69 return nil, err 70 } 71 ret := []cloudprovider.ICloudImage{} 72 for i := 0; i < len(images); i++ { 73 images[i].storageCache = cache 74 ret = append(ret, &images[i]) 75 } 76 return ret, nil 77 } 78 79 func (cache *SStoragecache) GetICustomizedCloudImages() ([]cloudprovider.ICloudImage, error) { 80 return nil, cloudprovider.ErrNotImplemented 81 } 82 83 func (cache *SStoragecache) GetIImageById(extId string) (cloudprovider.ICloudImage, error) { 84 image, err := cache.region.GetImage(extId) 85 if err != nil { 86 return nil, err 87 } 88 image.storageCache = cache 89 return image, nil 90 } 91 92 func (cache *SStoragecache) GetPath() string { 93 return "" 94 } 95 96 func (cache *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { 97 return cache.uploadImage(ctx, userCred, image, callback) 98 } 99 100 func (cache *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { 101 s := auth.GetAdminSession(ctx, options.Options.Region) 102 103 meta, reader, size, err := modules.Images.Download(s, image.ImageId, string(qemuimg.QCOW2), false) 104 if err != nil { 105 return "", err 106 } 107 log.Infof("meta data %s", meta) 108 109 imageBaseName := image.ImageName 110 imageName := imageBaseName 111 nameIdx := 1 112 113 for { 114 _, err = cache.region.GetImageByName(imageName) 115 if err != nil { 116 if errors.Cause(err) == cloudprovider.ErrNotFound { 117 break 118 } else { 119 return "", err 120 } 121 } 122 imageName = fmt.Sprintf("%s-%d", imageBaseName, nameIdx) 123 nameIdx++ 124 } 125 126 minDiskSizeMb, _ := meta.Int("min_disk") 127 minRamMb, _ := meta.Int("min_ram") 128 osType, _ := meta.GetString("properties", "os_type") 129 osDist, _ := meta.GetString("properties", "os_distribution") 130 minDiskSizeGB := minDiskSizeMb / 1024 131 if minDiskSizeMb%1024 > 0 { 132 minDiskSizeGB += 1 133 } 134 135 img, err := cache.region.CreateImage(imageName, osType, osDist, int(minDiskSizeGB), int(minRamMb), size, reader, callback) 136 if err != nil { 137 return "", err 138 } 139 140 img.storageCache = cache 141 142 err = cloudprovider.WaitStatus(img, api.CACHED_IMAGE_STATUS_ACTIVE, 15*time.Second, 3600*time.Second) 143 if err != nil { 144 return "", errors.Wrapf(err, "WaitStatus") 145 } 146 147 if callback != nil { 148 callback(100) 149 } 150 151 return img.Id, nil 152 } 153 154 func (cache *SStoragecache) CreateIImage(snapshoutId, imageName, osType, imageDesc string) (cloudprovider.ICloudImage, error) { 155 return nil, cloudprovider.ErrNotImplemented 156 } 157 158 func (cache *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { 159 return nil, cloudprovider.ErrNotImplemented 160 }