| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- // 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 zstack
- import (
- "context"
- "fmt"
- "io"
- "net/http"
- "net/url"
- "sort"
- "time"
- "yunion.io/x/jsonutils"
- "yunion.io/x/log"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/util/httputils"
- "yunion.io/x/pkg/util/imagetools"
- "yunion.io/x/pkg/util/multipart"
- api "yunion.io/x/cloudmux/pkg/apis/compute"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- )
- type SBackupStorageRef struct {
- BackupStorageUUID string `json:"backupStorageUuid"`
- CreateDate time.Time `json:"createDate"`
- ImageUUID string `json:"ImageUuid"`
- InstallPath string `json:"installPath"`
- LastOpDate time.Time `json:"lastOpDate"`
- Status string `json:"status"`
- }
- type SImage struct {
- multicloud.SImageBase
- ZStackTags
- storageCache *SStoragecache
- // normalized image info
- imgInfo *imagetools.ImageInfo
- BackupStorageRefs []SBackupStorageRef `json:"backupStorageRefs"`
- ActualSize int `json:"actualSize"`
- CreateDate time.Time `json:"createDate"`
- Description string `json:"description"`
- Format string `json:"format"`
- LastOpDate time.Time `json:"lastOpDate"`
- MD5Sum string `json:"md5sum"`
- MediaType string `json:"mediaType"`
- Name string `json:"name"`
- Platform string `json:"platform"`
- Size int `json:"size"`
- State string `json:"state"`
- Status string `json:"Ready"`
- System bool `json:"system"`
- Type string `json:"type"`
- URL string `json:"url"`
- UUID string `json:"uuid"`
- }
- func (image *SImage) GetMinRamSizeMb() int {
- return 0
- }
- func (image *SImage) GetId() string {
- return image.UUID
- }
- func (image *SImage) GetName() string {
- return image.Name
- }
- func (image *SImage) IsEmulated() bool {
- return false
- }
- func (image *SImage) Delete(ctx context.Context) error {
- return image.storageCache.region.DeleteImage(image.UUID)
- }
- func (image *SImage) GetGlobalId() string {
- return image.UUID
- }
- func (region *SRegion) DeleteImage(imageId string) error {
- err := region.client.delete("images", imageId, "")
- if err != nil {
- return err
- }
- params := map[string]interface{}{
- "expungeImage": jsonutils.NewDict(),
- }
- _, err = region.client.put("images", imageId, jsonutils.Marshal(params))
- return err
- }
- func (image *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
- return image.storageCache
- }
- func (image *SImage) GetStatus() string {
- if image.State != "Enabled" {
- return api.CACHED_IMAGE_STATUS_CACHING
- }
- switch image.Status {
- case "Ready":
- return api.CACHED_IMAGE_STATUS_ACTIVE
- case "Downloading":
- return api.CACHED_IMAGE_STATUS_CACHING
- case "Deleted":
- return api.CACHED_IMAGE_STATUS_DELETING
- default:
- log.Errorf("Unknown image status: %s", image.Status)
- return api.CACHED_IMAGE_STATUS_CACHE_FAILED
- }
- }
- func (image *SImage) GetImageStatus() string {
- switch image.Status {
- case "Ready":
- return cloudprovider.IMAGE_STATUS_ACTIVE
- case "Deleted":
- return cloudprovider.IMAGE_STATUS_DELETED
- default:
- return cloudprovider.IMAGE_STATUS_KILLED
- }
- }
- func (image *SImage) Refresh() error {
- new, err := image.storageCache.region.GetImage(image.UUID)
- if err != nil {
- return err
- }
- return jsonutils.Update(image, new)
- }
- func (image *SImage) GetImageType() cloudprovider.TImageType {
- return cloudprovider.ImageTypeSystem
- }
- func (image *SImage) GetSizeByte() int64 {
- return int64(image.Size)
- }
- func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
- if self.imgInfo == nil {
- imgInfo := imagetools.NormalizeImageInfo(self.URL, "", self.Platform, self.Platform, "")
- self.imgInfo = &imgInfo
- }
- return self.imgInfo
- }
- func (image *SImage) GetOsType() cloudprovider.TOsType {
- return cloudprovider.TOsType(image.getNormalizedImageInfo().OsType)
- }
- func (image *SImage) GetOsDist() string {
- return image.getNormalizedImageInfo().OsDistro
- }
- func (image *SImage) GetOsVersion() string {
- return image.getNormalizedImageInfo().OsVersion
- }
- func (image *SImage) GetOsArch() string {
- return image.getNormalizedImageInfo().OsArch
- }
- func (image *SImage) GetBios() cloudprovider.TBiosType {
- return cloudprovider.TBiosType(image.getNormalizedImageInfo().OsBios)
- }
- func (image *SImage) GetOsLang() string {
- return image.getNormalizedImageInfo().OsLang
- }
- func (image *SImage) GetFullOsName() string {
- return image.URL
- }
- func (image *SImage) GetMinOsDiskSizeGb() int {
- return image.Size / 1024 / 1024 / 1024
- }
- func (image *SImage) GetImageFormat() string {
- return image.Format
- }
- func (image *SImage) GetCreatedAt() time.Time {
- return image.CreateDate
- }
- func (region *SRegion) GetImage(imageId string) (*SImage, error) {
- image := &SImage{}
- err := region.client.getResource("images", imageId, image)
- if err != nil {
- return nil, errors.Wrapf(err, "region.GetImage")
- }
- return image, nil
- }
- func (region *SRegion) GetImages(zoneId string, imageId string) ([]SImage, error) {
- images := []SImage{}
- params := url.Values{}
- params.Add("q", "system=false")
- if len(zoneId) > 0 {
- params.Add("q", "backupStorage.zone.uuid="+zoneId)
- }
- if len(imageId) > 0 {
- params.Add("q", "uuid="+imageId)
- }
- if SkipEsxi {
- params.Add("q", "type!=vmware")
- }
- return images, region.client.listAll("images", params, &images)
- }
- func (region *SRegion) GetBackupStorageUUID(zondId string) ([]string, error) {
- imageServers, err := region.GetImageServers(zondId, "")
- if err != nil {
- return nil, err
- }
- if len(imageServers) == 0 {
- return nil, fmt.Errorf("failed to found any image servers")
- }
- servers := ImageServers(imageServers)
- sort.Sort(servers)
- return []string{servers[0].UUID}, nil
- }
- func (region *SRegion) CreateImage(zoneId string, imageName, format, osType, desc string, reader io.Reader, size int64, callback func(progress float32)) (*SImage, error) {
- backupStorageUUIDs, err := region.GetBackupStorageUUID(zoneId)
- if err != nil {
- return nil, err
- }
- platform := ""
- switch osType {
- case "linux":
- platform = "Linux"
- case "windows":
- platform = "Windows"
- default:
- platform = "Other"
- }
- parmas := map[string]interface{}{
- "params": map[string]interface{}{
- "name": imageName,
- "url": fmt.Sprintf("upload://%s", imageName),
- "description": desc,
- "mediaType": "RootVolumeTemplate",
- "system": false,
- "format": format,
- "platform": platform,
- "backupStorageUuids": backupStorageUUIDs,
- "systemTags": []string{"qemuga", "bootMode::Legacy"},
- },
- }
- if reader == nil {
- return nil, fmt.Errorf("invalid reader")
- }
- if size == 0 {
- return nil, fmt.Errorf("invalid image size")
- }
- body := multipart.NewReader(reader, "", imageName)
- image := &SImage{}
- err = region.client.create("images", jsonutils.Marshal(parmas), image)
- if err != nil {
- return nil, err
- }
- if len(image.BackupStorageRefs) < 0 {
- return nil, fmt.Errorf("no InstallPath reture")
- }
- header := http.Header{}
- header.Add("X-IMAGE-UUID", image.UUID)
- header.Add("X-IMAGE-SIZE", fmt.Sprintf("%d", size))
- header.Add("Content-Type", body.FormDataContentType())
- r := multicloud.NewProgress(size, 99, body, callback)
- resp, err := httputils.Request(httputils.GetTimeoutClient(0), context.Background(), "POST", image.BackupStorageRefs[0].InstallPath, header, r, false)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- return image, nil
- }
|