image.go 4.8 KB

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