yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/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 esxi
    16  
    17  import (
    18  	"context"
    19  	"path"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/vmware/govmomi/object"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  	"yunion.io/x/onecloud/pkg/util/imagetools"
    29  )
    30  
    31  type SImage struct {
    32  	multicloud.SImageBase
    33  	multicloud.STagBase
    34  	cache    *SDatastoreImageCache
    35  	filename string
    36  	size     int64
    37  	createAt time.Time
    38  
    39  	imageInfo *imagetools.ImageInfo
    40  }
    41  
    42  func (self *SImage) GetMinRamSizeMb() int {
    43  	return 0
    44  }
    45  
    46  func (self *SImage) getDatacenter() *object.Datacenter {
    47  	return self.cache.datastore.datacenter.getDcObj()
    48  }
    49  
    50  func (self *SImage) getFullFilename() string {
    51  	return self.cache.datastore.getPathString(self.filename)
    52  }
    53  
    54  func (self *SImage) GetId() string {
    55  	idstr := path.Base(self.filename)
    56  	if strings.HasSuffix(idstr, ".vmdk") {
    57  		idstr = idstr[:len(idstr)-5]
    58  	}
    59  	return strings.ToLower(idstr)
    60  }
    61  
    62  func (self *SImage) GetName() string {
    63  	return self.GetId()
    64  }
    65  
    66  func (self *SImage) GetGlobalId() string {
    67  	return self.GetId()
    68  }
    69  
    70  func (self *SImage) GetStatus() string {
    71  	dm := object.NewVirtualDiskManager(self.cache.datastore.manager.client.Client)
    72  	ctx := context.Background()
    73  	_, err := dm.QueryVirtualDiskInfo(ctx, self.getFullFilename(), self.getDatacenter(), true)
    74  	if err != nil {
    75  		return api.CACHED_IMAGE_STATUS_CACHE_FAILED
    76  	}
    77  	return api.CACHED_IMAGE_STATUS_ACTIVE
    78  }
    79  
    80  func (self *SImage) GetImageStatus() string {
    81  	status := self.GetStatus()
    82  	if status == api.CACHED_IMAGE_STATUS_ACTIVE {
    83  		return cloudprovider.IMAGE_STATUS_ACTIVE
    84  	}
    85  	return cloudprovider.IMAGE_STATUS_DELETED
    86  }
    87  
    88  func (self *SImage) Refresh() error {
    89  	return nil
    90  }
    91  
    92  func (self *SImage) IsEmulated() bool {
    93  	return false
    94  }
    95  
    96  func (self *SImage) Delete(ctx context.Context) error {
    97  	return self.cache.datastore.DeleteVmdk(ctx, self.filename)
    98  }
    99  
   100  func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   101  	return self.cache
   102  }
   103  
   104  func (self *SImage) GetImageType() cloudprovider.TImageType {
   105  	return cloudprovider.ImageTypeCustomized
   106  }
   107  
   108  func (self *SImage) GetSizeByte() int64 {
   109  	return self.size
   110  }
   111  
   112  func (img *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
   113  	if img.imageInfo == nil {
   114  		imgInfo := imagetools.NormalizeImageInfo(img.filename, "", "", "", "")
   115  		img.imageInfo = &imgInfo
   116  	}
   117  	return img.imageInfo
   118  }
   119  
   120  func (img *SImage) GetFullOsName() string {
   121  	return img.filename
   122  }
   123  
   124  func (img *SImage) GetOsType() cloudprovider.TOsType {
   125  	return cloudprovider.TOsType(img.getNormalizedImageInfo().OsType)
   126  }
   127  
   128  func (img *SImage) GetOsArch() string {
   129  	return img.getNormalizedImageInfo().OsArch
   130  }
   131  
   132  func (img *SImage) GetOsDist() string {
   133  	return img.getNormalizedImageInfo().OsDistro
   134  }
   135  
   136  func (img *SImage) GetOsVersion() string {
   137  	return img.getNormalizedImageInfo().OsVersion
   138  }
   139  
   140  func (img *SImage) GetOsLang() string {
   141  	return img.getNormalizedImageInfo().OsLang
   142  }
   143  
   144  func (img *SImage) GetBios() cloudprovider.TBiosType {
   145  	return cloudprovider.ToBiosType(img.getNormalizedImageInfo().OsBios)
   146  }
   147  
   148  func (self *SImage) GetMinOsDiskSizeGb() int {
   149  	return int(self.GetSizeByte() / 1024 / 1024 / 1024)
   150  }
   151  
   152  func (self *SImage) GetImageFormat() string {
   153  	return "vmdk"
   154  }
   155  
   156  func (self *SImage) GetCreatedAt() time.Time {
   157  	return self.createAt
   158  }