image.go 6.5 KB

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