yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/image.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 cloudpods
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  	"yunion.io/x/pkg/utils"
    24  
    25  	api "yunion.io/x/onecloud/pkg/apis/image"
    26  	"yunion.io/x/onecloud/pkg/esxi/options"
    27  	"yunion.io/x/onecloud/pkg/mcclient"
    28  	"yunion.io/x/onecloud/pkg/mcclient/auth"
    29  	modules "yunion.io/x/onecloud/pkg/mcclient/modules/image"
    30  	"yunion.io/x/onecloud/pkg/util/imagetools"
    31  	"yunion.io/x/onecloud/pkg/util/qemuimg"
    32  
    33  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    34  	"yunion.io/x/cloudmux/pkg/multicloud"
    35  )
    36  
    37  type SImage struct {
    38  	multicloud.SImageBase
    39  	CloudpodsTags
    40  	cache *SStoragecache
    41  
    42  	api.ImageDetails
    43  }
    44  
    45  func (self *SImage) GetProjectId() string {
    46  	return self.TenantId
    47  }
    48  
    49  func (self *SImage) GetName() string {
    50  	return self.Name
    51  }
    52  
    53  func (self *SImage) GetId() string {
    54  	return self.Id
    55  }
    56  
    57  func (self *SImage) GetGlobalId() string {
    58  	return self.Id
    59  }
    60  
    61  func (self *SImage) GetStatus() string {
    62  	return self.Status
    63  }
    64  
    65  func (self *SImage) Delete(ctx context.Context) error {
    66  	return self.cache.region.cli.delete(&modules.Images, self.Id)
    67  }
    68  
    69  func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    70  	return self.cache
    71  }
    72  
    73  func (self *SImage) GetSizeByte() int64 {
    74  	return self.Size
    75  }
    76  
    77  func (self *SImage) GetImageType() cloudprovider.TImageType {
    78  	if self.IsStandard != nil && *self.IsStandard {
    79  		return cloudprovider.ImageTypeSystem
    80  	}
    81  	return cloudprovider.ImageTypeCustomized
    82  }
    83  
    84  func (self *SImage) GetImageStatus() string {
    85  	return self.Status
    86  }
    87  
    88  func (self *SImage) GetOsType() cloudprovider.TOsType {
    89  	osType, ok := self.Properties["os_type"]
    90  	if ok {
    91  		return cloudprovider.TOsType(osType)
    92  	}
    93  	return cloudprovider.OsTypeLinux
    94  }
    95  
    96  func (self *SImage) GetOsDist() string {
    97  	osDist, ok := self.Properties["os_distribution"]
    98  	if ok {
    99  		return osDist
   100  	}
   101  	return ""
   102  }
   103  
   104  func (self *SImage) GetOsVersion() string {
   105  	osVer, ok := self.Properties["os_version"]
   106  	if ok {
   107  		return osVer
   108  	}
   109  	return ""
   110  }
   111  
   112  func (self *SImage) GetOsArch() string {
   113  	osArch, ok := self.Properties["os_arch"]
   114  	if ok {
   115  		return osArch
   116  	}
   117  	return ""
   118  }
   119  
   120  func (self *SImage) GetOsLang() string {
   121  	osLang, ok := self.Properties["os_language"]
   122  	if ok {
   123  		return osLang
   124  	}
   125  	return ""
   126  }
   127  
   128  func (self *SImage) GetBios() cloudprovider.TBiosType {
   129  	uefi, ok := self.Properties["uefi_support"]
   130  	if ok && utils.ToBool(uefi) {
   131  		return cloudprovider.UEFI
   132  	}
   133  	return cloudprovider.BIOS
   134  }
   135  
   136  func (img *SImage) GetFullOsName() string {
   137  	imgInfo := imagetools.NormalizeImageInfo("", img.GetOsArch(), string(img.GetOsType()), img.GetOsDist(), img.GetOsVersion())
   138  	return imgInfo.GetFullOsName()
   139  }
   140  
   141  func (self *SImage) GetMinOsDiskSizeGb() int {
   142  	return int(self.MinDiskMB / 1024)
   143  }
   144  
   145  func (self *SImage) GetMinRamSizeMb() int {
   146  	return int(self.MinRamMB)
   147  }
   148  
   149  func (self *SImage) GetImageFormat() string {
   150  	return self.DiskFormat
   151  }
   152  
   153  func (self *SImage) GetCreatedAt() time.Time {
   154  	return self.CreatedAt
   155  }
   156  
   157  func (self *SImage) Refresh() error {
   158  	image, err := self.cache.region.GetImage(self.Id)
   159  	if err != nil {
   160  		return err
   161  	}
   162  	return jsonutils.Update(self, image)
   163  }
   164  
   165  func (self *SStoragecache) GetICloudImages() ([]cloudprovider.ICloudImage, error) {
   166  	images, err := self.region.GetImages()
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	ret := []cloudprovider.ICloudImage{}
   171  	for i := range images {
   172  		images[i].cache = self
   173  		ret = append(ret, &images[i])
   174  	}
   175  	return ret, nil
   176  }
   177  
   178  func (self *SRegion) GetImages() ([]SImage, error) {
   179  	params := map[string]interface{}{
   180  		"is_guest_image": false,
   181  	}
   182  	images := []SImage{}
   183  	return images, self.list(&modules.Images, params, &images)
   184  }
   185  
   186  func (self *SRegion) GetImage(id string) (*SImage, error) {
   187  	image := &SImage{}
   188  	resp, err := modules.Images.GetById(self.cli.s, id, nil)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  	return image, resp.Unmarshal(image)
   193  }
   194  
   195  func (self *SRegion) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, opts *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) {
   196  	s := auth.GetAdminSession(ctx, options.Options.Region)
   197  
   198  	meta, reader, sizeByte, err := modules.Images.Download(s, opts.ImageId, string(qemuimg.QCOW2), false)
   199  	if err != nil {
   200  		return "", err
   201  	}
   202  	log.Infof("meta data %s", meta)
   203  
   204  	params := map[string]interface{}{
   205  		"name": opts.ImageName,
   206  		"properties": map[string]string{
   207  			"os_type":         opts.OsType,
   208  			"os_distribution": opts.OsDistribution,
   209  			"os_arch":         opts.OsArch,
   210  			"os_version":      opts.OsVersion,
   211  		},
   212  	}
   213  
   214  	body := multicloud.NewProgress(sizeByte, 90, reader, callback)
   215  	resp, err := modules.Images.Upload(self.cli.s, jsonutils.Marshal(params), body, sizeByte)
   216  	if err != nil {
   217  		return "", err
   218  	}
   219  	return resp.GetString("id")
   220  }