utils.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 apis
  15. import (
  16. "encoding/base64"
  17. "encoding/json"
  18. "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  24. "yunion.io/x/onecloud/pkg/hostman/guestman/desc"
  25. )
  26. func NewDeployInfo(
  27. publicKey *SSHKeys,
  28. deploys []*DeployContent,
  29. password string,
  30. isRandomPassword,
  31. isInit bool,
  32. enableTty bool,
  33. defaultRootUser bool,
  34. windowsDefaultAdminUser bool,
  35. enableCloudInit bool,
  36. loginAccount string,
  37. enableTelegraf bool,
  38. telegrafConf string,
  39. userData string,
  40. ) *DeployInfo {
  41. depInfo := &DeployInfo{
  42. PublicKey: publicKey,
  43. Deploys: deploys,
  44. Password: password,
  45. IsRandomPassword: isRandomPassword,
  46. IsInit: isInit,
  47. EnableTty: enableTty,
  48. DefaultRootUser: defaultRootUser,
  49. WindowsDefaultAdminUser: windowsDefaultAdminUser,
  50. EnableCloudInit: enableCloudInit,
  51. LoginAccount: loginAccount,
  52. }
  53. if enableTelegraf {
  54. depInfo.Telegraf = &Telegraf{
  55. TelegrafConf: telegrafConf,
  56. }
  57. }
  58. userDataDecoded, _ := base64.StdEncoding.DecodeString(userData)
  59. if len(userDataDecoded) > 0 {
  60. userData = string(userDataDecoded)
  61. }
  62. depInfo.UserData = userData
  63. return depInfo
  64. }
  65. func GetKeys(data jsonutils.JSONObject) *SSHKeys {
  66. var ret = new(SSHKeys)
  67. ret.PublicKey, _ = data.GetString("public_key")
  68. ret.DeletePublicKey, _ = data.GetString("delete_public_key")
  69. ret.AdminPublicKey, _ = data.GetString("admin_public_key")
  70. ret.ProjectPublicKey, _ = data.GetString("project_public_key")
  71. return ret
  72. }
  73. func ConvertRoutes(routes string) []types.SRoute {
  74. if len(routes) == 0 {
  75. return nil
  76. }
  77. ret := make([]types.SRoute, 0)
  78. err := json.Unmarshal([]byte(routes), &ret)
  79. if err != nil {
  80. log.Errorf("Can't convert %s to types.SRoute", routes)
  81. return nil
  82. }
  83. return ret
  84. }
  85. func GuestdisksDescToDeployDesc(guestDisks []*desc.SGuestDisk) []*Disk {
  86. if len(guestDisks) == 0 {
  87. return nil
  88. }
  89. disks := make([]*Disk, len(guestDisks))
  90. for i, disk := range guestDisks {
  91. disks[i] = new(Disk)
  92. disks[i].DiskId = disk.DiskId
  93. disks[i].Driver = disk.Driver
  94. disks[i].CacheMode = disk.CacheMode
  95. disks[i].AioMode = disk.AioMode
  96. disks[i].Size = int64(disk.Size)
  97. disks[i].TemplateId = disk.TemplateId
  98. disks[i].ImagePath = disk.ImagePath
  99. disks[i].StorageId = disk.StorageId
  100. disks[i].Migrating = disk.Migrating
  101. disks[i].TargetStorageId = disk.TargetStorageId
  102. disks[i].Path = disk.Path
  103. disks[i].Format = disk.Format
  104. disks[i].Index = int32(disk.Index)
  105. disks[i].MergeSnapshot = disk.MergeSnapshot
  106. disks[i].Fs = disk.Fs
  107. disks[i].Mountpoint = disk.Mountpoint
  108. disks[i].Dev = disk.Dev
  109. }
  110. return disks
  111. }
  112. func GuestnetworksDescToDeployDesc(guestnetworks []*desc.SGuestNetwork) []*Nic {
  113. if len(guestnetworks) == 0 {
  114. return nil
  115. }
  116. nics := make([]*Nic, len(guestnetworks))
  117. for i, nic := range guestnetworks {
  118. domain := nic.Domain
  119. if apis.IsIllegalSearchDomain(domain) {
  120. domain = ""
  121. }
  122. nics[i] = new(Nic)
  123. nics[i].Mac = nic.Mac
  124. nics[i].Ip = nic.Ip
  125. nics[i].Net = nic.Net
  126. nics[i].NetId = nic.NetId
  127. nics[i].Virtual = nic.Virtual
  128. nics[i].Gateway = nic.Gateway
  129. nics[i].Dns = nic.Dns
  130. nics[i].Domain = domain
  131. if nic.Routes != nil {
  132. nics[i].Routes = nic.Routes.String()
  133. }
  134. nics[i].Ifname = nic.Ifname
  135. nics[i].Masklen = int32(nic.Masklen)
  136. nics[i].Driver = nic.Driver
  137. nics[i].Bridge = nic.Bridge
  138. nics[i].WireId = nic.WireId
  139. nics[i].Vlan = int32(nic.Vlan)
  140. nics[i].Interface = nic.Interface
  141. nics[i].Bw = int32(nic.Bw)
  142. nics[i].Index = int32(nic.Index)
  143. nics[i].VirtualIps = nic.VirtualIps
  144. //nics[i].ExternelId = nic.ExternelId
  145. nics[i].TeamWith = nic.TeamWith
  146. if nic.Manual != nil {
  147. nics[i].Manual = *nic.Manual
  148. }
  149. nics[i].NicType = string(nic.NicType)
  150. nics[i].LinkUp = nic.LinkUp
  151. nics[i].Mtu = int64(nic.Mtu)
  152. //nics[i].Name = nic.Name
  153. nics[i].IsDefault = nic.IsDefault
  154. nics[i].Ip6 = nic.Ip6
  155. nics[i].Masklen6 = int32(nic.Masklen6)
  156. nics[i].Gateway6 = nic.Gateway6
  157. }
  158. return nics
  159. }
  160. func GuestJsonDescToDeployDesc(guestDesc *jsonutils.JSONDict) (*GuestDesc, error) {
  161. ret := new(GuestDesc)
  162. if err := guestDesc.Unmarshal(ret); err != nil {
  163. return nil, errors.Wrap(err, "GuestJsonDescToDeployDesc unmarshal")
  164. }
  165. return ret, nil
  166. }
  167. func GuestStructDescToDeployDesc(guestDesc *desc.SGuestDesc) *GuestDesc {
  168. ret := new(GuestDesc)
  169. ret.Name = guestDesc.Name
  170. ret.Domain = guestDesc.Domain
  171. ret.Uuid = guestDesc.Uuid
  172. ret.Hostname = guestDesc.Hostname
  173. ret.Nics = GuestnetworksDescToDeployDesc(guestDesc.Nics)
  174. ret.Disks = GuestdisksDescToDeployDesc(guestDesc.Disks)
  175. return ret
  176. }
  177. func NewReleaseInfo(distro, version, arch string) *ReleaseInfo {
  178. return &ReleaseInfo{
  179. Distro: distro,
  180. Version: version,
  181. Arch: arch,
  182. }
  183. }
  184. func GuestNicsToServerNics(nics []*desc.SGuestNetwork) []*types.SServerNic {
  185. ret := make([]*types.SServerNic, len(nics))
  186. for i := 0; i < len(nics); i++ {
  187. routes := ""
  188. if nics[i].Routes != nil {
  189. routes, _ = nics[i].Routes.GetString()
  190. }
  191. ret[i] = &types.SServerNic{
  192. Name: "",
  193. Index: int(nics[i].Index),
  194. Bridge: nics[i].Bridge,
  195. Domain: nics[i].Domain,
  196. Ip: nics[i].Ip,
  197. Vlan: int(nics[i].Vlan),
  198. Driver: nics[i].Driver,
  199. Masklen: int(nics[i].Masklen),
  200. Virtual: nics[i].Virtual,
  201. Manual: nics[i].Manual != nil && *nics[i].Manual,
  202. WireId: nics[i].WireId,
  203. NetId: nics[i].NetId,
  204. Mac: nics[i].Mac,
  205. BandWidth: int(nics[i].Bw),
  206. Dns: nics[i].Dns,
  207. Net: nics[i].Net,
  208. Interface: nics[i].Interface,
  209. Gateway: nics[i].Gateway,
  210. Ifname: nics[i].Ifname,
  211. Routes: ConvertRoutes(routes),
  212. NicType: compute.TNicType(nics[i].NicType),
  213. LinkUp: nics[i].LinkUp,
  214. Mtu: int16(nics[i].Mtu),
  215. TeamWith: nics[i].TeamWith,
  216. IsDefault: nics[i].IsDefault,
  217. Ip6: nics[i].Ip6,
  218. Masklen6: int(nics[i].Masklen6),
  219. Gateway6: nics[i].Gateway6,
  220. }
  221. }
  222. return ret
  223. }