utils.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 guestdrivers
  15. import (
  16. "time"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/utils"
  21. billing_api "yunion.io/x/onecloud/pkg/apis/billing"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/util/seclib2"
  24. )
  25. type SDiskInfo struct {
  26. DiskType string
  27. Size int
  28. Uuid string
  29. BillingType billing_api.TBillingType
  30. FsFromat string
  31. AutoDelete bool
  32. TemplateId string
  33. DiskFormat string
  34. Path string
  35. Driver string
  36. CacheMode string
  37. ExpiredAt time.Time
  38. StorageExternalId string
  39. Device string
  40. Metadata map[string]string
  41. }
  42. func fetchIVMinfo(desc cloudprovider.SManagedVMCreateConfig, iVM cloudprovider.ICloudVM, guestId string, account, passwd string, publicKey string, action string) *jsonutils.JSONDict {
  43. data := jsonutils.NewDict()
  44. data.Add(jsonutils.NewString(string(iVM.GetOsType())), "os")
  45. //避免在rebuild_root时绑定秘钥,没有account信息
  46. data.Add(jsonutils.NewString(account), "account")
  47. if len(passwd) > 0 || len(publicKey) > 0 {
  48. var encpasswd string
  49. var err error
  50. if len(publicKey) > 0 {
  51. encpasswd, err = seclib2.EncryptBase64(publicKey, passwd)
  52. } else {
  53. encpasswd, err = utils.EncryptAESBase64(guestId, passwd)
  54. }
  55. if err != nil {
  56. log.Errorf("encrypt password failed %s", err)
  57. } else {
  58. data.Add(jsonutils.NewString(encpasswd), "key")
  59. }
  60. }
  61. if len(desc.OsDistribution) > 0 {
  62. data.Add(jsonutils.NewString(desc.OsDistribution), "distro")
  63. }
  64. if len(desc.OsVersion) > 0 {
  65. data.Add(jsonutils.NewString(desc.OsVersion), "version")
  66. }
  67. idisks, err := iVM.GetIDisks()
  68. if err != nil {
  69. log.Errorf("GetiDisks error %s", err)
  70. } else {
  71. diskInfo := make([]SDiskInfo, len(idisks))
  72. for i := 0; i < len(idisks); i += 1 {
  73. dinfo := SDiskInfo{}
  74. dinfo.Uuid = idisks[i].GetGlobalId()
  75. dinfo.Size = idisks[i].GetDiskSizeMB()
  76. dinfo.DiskType = idisks[i].GetDiskType()
  77. dinfo.BillingType = billing_api.TBillingType(idisks[i].GetBillingType())
  78. dinfo.DiskFormat = idisks[i].GetDiskFormat()
  79. dinfo.AutoDelete = idisks[i].GetIsAutoDelete()
  80. if action == "create" {
  81. dinfo.AutoDelete = true
  82. }
  83. dinfo.Path = idisks[i].GetAccessPath()
  84. dinfo.Driver = idisks[i].GetDriver()
  85. dinfo.CacheMode = idisks[i].GetCacheMode()
  86. dinfo.TemplateId = idisks[i].GetTemplateId()
  87. dinfo.FsFromat = idisks[i].GetFsFormat()
  88. dinfo.ExpiredAt = idisks[i].GetExpiredAt()
  89. dinfo.Device = idisks[i].GetDeviceName()
  90. dinfo.StorageExternalId = idisks[i].GetIStorageId()
  91. diskSysTags := idisks[i].GetSysTags()
  92. diskTags, _ := idisks[i].GetTags()
  93. if diskSysTags != nil || diskTags != nil {
  94. dinfo.Metadata = make(map[string]string, 0)
  95. for k, v := range diskSysTags {
  96. dinfo.Metadata[db.SYS_CLOUD_TAG_PREFIX+k] = v
  97. }
  98. for k, v := range diskTags {
  99. dinfo.Metadata[db.CLOUD_TAG_PREFIX+k] = v
  100. }
  101. }
  102. diskInfo[i] = dinfo
  103. }
  104. data.Add(jsonutils.Marshal(&diskInfo), "disks")
  105. }
  106. data.Add(jsonutils.NewString(iVM.GetGlobalId()), "uuid")
  107. sysTags := iVM.GetSysTags()
  108. tags, _ := iVM.GetTags()
  109. metadataDict := jsonutils.NewDict()
  110. for k, v := range sysTags {
  111. metadataDict.Add(jsonutils.NewString(v), db.SYS_CLOUD_TAG_PREFIX+k)
  112. }
  113. for k, v := range tags {
  114. metadataDict.Add(jsonutils.NewString(v), db.CLOUD_TAG_PREFIX+k)
  115. }
  116. data.Add(metadataDict, "metadata")
  117. if iVM.GetBillingType() == string(billing_api.BILLING_TYPE_PREPAID) {
  118. data.Add(jsonutils.NewTimeString(iVM.GetExpiredAt()), "expired_at")
  119. data.Add(jsonutils.NewBool(iVM.IsAutoRenew()), "auto_renew")
  120. }
  121. return data
  122. }