yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	"yunion.io/x/jsonutils"
    24  	"yunion.io/x/log"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  	"yunion.io/x/onecloud/pkg/util/imagetools"
    30  )
    31  
    32  type GuestOsFeature struct {
    33  	Type string
    34  }
    35  
    36  type SDeprecated struct {
    37  	State       string
    38  	Replacement string
    39  	Deprecated  bool
    40  }
    41  
    42  type SImage struct {
    43  	multicloud.SImageBase
    44  	GoogleTags
    45  	storagecache *SStoragecache
    46  	SResourceBase
    47  	Name     string
    48  	SelfLink string
    49  	Id       string
    50  
    51  	// normalized image info
    52  	imgInfo *imagetools.ImageInfo
    53  
    54  	CreationTimestamp time.Time
    55  	Description       string
    56  	SourceType        string
    57  	RawDisk           map[string]string
    58  	Deprecated        SDeprecated
    59  	Status            string
    60  	ArchiveSizeBytes  int64
    61  	DiskSizeGb        int
    62  	Licenses          []string
    63  	Family            string
    64  	LabelFingerprint  string
    65  	GuestOsFeatures   []GuestOsFeature
    66  	LicenseCodes      []string
    67  	StorageLocations  []string
    68  	Kind              string
    69  }
    70  
    71  func (self *SImage) GetId() string {
    72  	return self.SelfLink
    73  }
    74  
    75  func (self *SImage) GetGlobalId() string {
    76  	return strings.TrimPrefix(self.SelfLink, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
    77  }
    78  
    79  func (self *SImage) GetName() string {
    80  	return self.Name
    81  }
    82  
    83  func (region *SRegion) SetProjectId(id string) {
    84  	region.client.projectId = id
    85  }
    86  
    87  func (region *SRegion) GetAllAvailableImages() ([]SImage, error) {
    88  	images := []SImage{}
    89  	projectId := region.client.projectId
    90  	for _, project := range []string{
    91  		"centos-cloud",
    92  		"ubuntu-os-cloud",
    93  		"windows-cloud",
    94  		"windows-sql-cloud",
    95  		"suse-cloud",
    96  		"suse-sap-cloud",
    97  		"rhel-cloud",
    98  		"rhel-sap-cloud",
    99  		"cos-cloud",
   100  		"coreos-cloud",
   101  		"debian-cloud",
   102  		projectId,
   103  	} {
   104  		_images, err := region.GetImages(project, 0, "")
   105  		if err != nil {
   106  			return nil, err
   107  		}
   108  		for _, image := range _images {
   109  			if image.Deprecated.State == "" {
   110  				images = append(images, image)
   111  			}
   112  		}
   113  	}
   114  	return images, nil
   115  }
   116  
   117  func (region *SRegion) GetImages(project string, maxResults int, pageToken string) ([]SImage, error) {
   118  	images := []SImage{}
   119  	resource := "global/images"
   120  	params := map[string]string{}
   121  	if len(project) > 0 {
   122  		region.SetProjectId(project)
   123  	}
   124  	return images, region.List(resource, params, maxResults, pageToken, &images)
   125  }
   126  
   127  func (region *SRegion) GetImage(id string) (*SImage, error) {
   128  	image := &SImage{}
   129  	return image, region.GetBySelfId(id, image)
   130  }
   131  
   132  func (image *SImage) GetMinRamSizeMb() int {
   133  	return 0
   134  }
   135  
   136  func (image *SImage) GetStatus() string {
   137  	switch image.Status {
   138  	case "READY":
   139  		return api.CACHED_IMAGE_STATUS_ACTIVE
   140  	case "FAILED":
   141  		return api.CACHED_IMAGE_STATUS_CACHE_FAILED
   142  	case "PENDING":
   143  		return api.CACHED_IMAGE_STATUS_SAVING
   144  	default:
   145  		log.Errorf("Unknown image status: %s", image.Status)
   146  		return api.CACHED_IMAGE_STATUS_CACHE_FAILED
   147  	}
   148  }
   149  
   150  func (image *SImage) GetImageStatus() string {
   151  	switch image.Status {
   152  	case "READY":
   153  		return cloudprovider.IMAGE_STATUS_ACTIVE
   154  	case "FAILED":
   155  		return cloudprovider.IMAGE_STATUS_KILLED
   156  	case "PENDING":
   157  		return cloudprovider.IMAGE_STATUS_QUEUED
   158  	default:
   159  		return cloudprovider.IMAGE_STATUS_KILLED
   160  	}
   161  }
   162  
   163  func (image *SImage) Refresh() error {
   164  	_image, err := image.storagecache.region.GetImage(image.SelfLink)
   165  	if err != nil {
   166  		return err
   167  	}
   168  	return jsonutils.Update(image, _image)
   169  }
   170  
   171  func (image *SImage) GetImageType() cloudprovider.TImageType {
   172  	if strings.Index(image.SelfLink, image.storagecache.region.GetProjectId()) >= 0 {
   173  		return cloudprovider.ImageTypeCustomized
   174  	}
   175  	return cloudprovider.ImageTypeSystem
   176  }
   177  
   178  func (image *SImage) GetSizeByte() int64 {
   179  	return image.ArchiveSizeBytes
   180  }
   181  
   182  func (image *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
   183  	if image.imgInfo == nil {
   184  		imgInfo := imagetools.NormalizeImageInfo(image.Name, "", "", "", "")
   185  		image.imgInfo = &imgInfo
   186  	}
   187  
   188  	return image.imgInfo
   189  }
   190  
   191  func (image *SImage) GetOsType() cloudprovider.TOsType {
   192  	return cloudprovider.TOsType(image.getNormalizedImageInfo().OsType)
   193  }
   194  
   195  func (image *SImage) GetOsDist() string {
   196  	return image.getNormalizedImageInfo().OsDistro
   197  }
   198  
   199  func (image *SImage) GetOsVersion() string {
   200  	return image.getNormalizedImageInfo().OsVersion
   201  }
   202  
   203  func (image *SImage) GetOsLang() string {
   204  	return image.getNormalizedImageInfo().OsLang
   205  }
   206  
   207  func (image *SImage) GetOsArch() string {
   208  	return image.getNormalizedImageInfo().OsArch
   209  }
   210  
   211  func (image *SImage) GetFullOsName() string {
   212  	return image.Name
   213  }
   214  
   215  func (image *SImage) GetBios() cloudprovider.TBiosType {
   216  	return cloudprovider.ToBiosType(image.getNormalizedImageInfo().OsBios)
   217  }
   218  
   219  func (image *SImage) GetMinOsDiskSizeGb() int {
   220  	return image.DiskSizeGb
   221  }
   222  
   223  func (image *SImage) GetCreatedAt() time.Time {
   224  	return image.CreationTimestamp
   225  }
   226  
   227  func (image *SImage) GetImageFormat() string {
   228  	return "raw"
   229  }
   230  
   231  func (image *SImage) IsEmulated() bool {
   232  	return false
   233  }
   234  
   235  func (image *SImage) Delete(ctx context.Context) error {
   236  	return cloudprovider.ErrNotImplemented
   237  }
   238  
   239  func (image *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   240  	return image.storagecache
   241  }
   242  
   243  func (region *SRegion) CreateImage(name string, desc string, bucketName string, sourceFile string) (*SImage, error) {
   244  	body := map[string]interface{}{
   245  		"timeout": "7200s",
   246  		"steps": []struct {
   247  			Args []string
   248  			Name string
   249  		}{
   250  			{
   251  				Args: []string{
   252  					fmt.Sprintf("-source_file=gs://%s/%s", bucketName, sourceFile),
   253  					"-data_disk",
   254  					"-timeout=7056s",
   255  					"-image_name=" + name,
   256  					"-no_guest_environment",
   257  					"-client_id=onecloud",
   258  					"-description=" + desc,
   259  				},
   260  				Name: "gcr.io/compute-image-tools/gce_vm_image_import:release",
   261  			},
   262  		},
   263  		"tags": []string{"gce-daisy", "gce-daisy-image-import"},
   264  	}
   265  	err := region.CloudbuildInsert(jsonutils.Marshal(body))
   266  	if err != nil {
   267  		return nil, err
   268  	}
   269  	return region.GetImage(fmt.Sprintf("projects/%s/global/images/%s", region.GetProjectId(), name))
   270  }