guest_create_from_remote.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 guestman
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/cloudmux/pkg/multicloud/esxi"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
  23. "yunion.io/x/onecloud/pkg/hostman/hostdeployer/deployclient"
  24. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  25. "yunion.io/x/onecloud/pkg/hostman/storageman"
  26. )
  27. func (m *SGuestManager) GuestCreateFromEsxi(
  28. ctx context.Context, params interface{},
  29. ) (jsonutils.JSONObject, error) {
  30. createConfig, ok := params.(*SGuestCreateFromEsxi)
  31. if !ok {
  32. return nil, hostutils.ParamsError
  33. }
  34. guest, _ := m.GetKVMServer(createConfig.Sid)
  35. if err := SaveDesc(guest, createConfig.GuestDesc); err != nil {
  36. return nil, err
  37. }
  38. esxiCli, err := esxi.NewESXiClientFromAccessInfo(ctx, &createConfig.EsxiAccessInfo.Datastore)
  39. if err != nil {
  40. return nil, errors.Wrap(err, "new esxi client")
  41. }
  42. host, err := esxiCli.FindHostByIp(createConfig.EsxiAccessInfo.HostIp)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "esxi client find host by ip")
  45. }
  46. ivm, err := host.GetIVMById(createConfig.EsxiAccessInfo.GuestExtId)
  47. if err != nil {
  48. return nil, errors.Wrap(err, "get ivm by id")
  49. }
  50. vm := ivm.(*esxi.SVirtualMachine)
  51. disks, err := vm.GetIDisks()
  52. if err != nil {
  53. return nil, errors.Wrap(err, "vm get idisk")
  54. }
  55. if len(disks) == 0 {
  56. return nil, fmt.Errorf("no such disks for vm %s", vm.GetId())
  57. }
  58. vmref := vm.GetMoid()
  59. var esxiDisks = new(deployapi.ConnectEsxiDisksParams)
  60. esxiDisks.VddkInfo = &deployapi.VDDKConInfo{
  61. Host: createConfig.EsxiAccessInfo.Datastore.Host,
  62. Port: int32(createConfig.EsxiAccessInfo.Datastore.Port),
  63. User: createConfig.EsxiAccessInfo.Datastore.Account,
  64. Passwd: createConfig.EsxiAccessInfo.Datastore.Password,
  65. Vmref: vmref,
  66. }
  67. esxiDisks.AccessInfo = make([]*deployapi.EsxiDiskInfo, len(disks))
  68. for i := 0; i < len(disks); i++ {
  69. esxiDisks.AccessInfo[i] = &deployapi.EsxiDiskInfo{
  70. DiskPath: disks[i].(*esxi.SVirtualDisk).GetFilename(),
  71. }
  72. }
  73. connections, err := deployclient.GetDeployClient().ConnectEsxiDisks(ctx, esxiDisks)
  74. if err != nil {
  75. return nil, errors.Wrap(err, "connect esxi disks")
  76. }
  77. log.Infof("Connection disks %v", connections.String())
  78. var ret = jsonutils.NewDict()
  79. disksDesc := guest.SourceDesc.Disks
  80. for i := 0; i < len(disksDesc); i++ {
  81. storageId := disksDesc[i].StorageId
  82. if storage := storageman.GetManager().GetStorage(storageId); storage == nil {
  83. err = errors.Wrapf(err, "get storage %s", storageId)
  84. break
  85. } else {
  86. var diskInfo jsonutils.JSONObject
  87. diskId := disksDesc[i].DiskId
  88. iDisk := storage.CreateDisk(diskId)
  89. diskInfo, err = iDisk.CreateRaw(ctx, 0, "qcow2", "", nil, nil, "", connections.Disks[i].DiskPath)
  90. if err != nil {
  91. err = errors.Wrapf(err, "create disk %s failed", diskId)
  92. log.Errorf("%s", err.Error())
  93. break
  94. }
  95. diskInfo.(*jsonutils.JSONDict).Set("esxi_flat_filepath",
  96. jsonutils.NewString(connections.Disks[i].DiskPath))
  97. ret.Set(diskId, diskInfo)
  98. }
  99. }
  100. if err != nil {
  101. _, e := deployclient.GetDeployClient().DisconnectEsxiDisks(ctx, connections)
  102. if e != nil {
  103. log.Errorf("disconnect esxi disks failed %s", e)
  104. }
  105. return nil, err
  106. }
  107. return ret, nil
  108. }
  109. func (m *SGuestManager) GuestCreateFromCloudpods(
  110. ctx context.Context, params interface{},
  111. ) (jsonutils.JSONObject, error) {
  112. createConfig, ok := params.(*SGuestCreateFromCloudpods)
  113. if !ok {
  114. return nil, hostutils.ParamsError
  115. }
  116. guest, _ := m.GetKVMServer(createConfig.Sid)
  117. if err := SaveDesc(guest, createConfig.GuestDesc); err != nil {
  118. return nil, err
  119. }
  120. var err error
  121. var ret = jsonutils.NewDict()
  122. disksDesc := guest.SourceDesc.Disks
  123. for i := 0; i < len(disksDesc); i++ {
  124. storageId := disksDesc[i].StorageId
  125. storage := storageman.GetManager().GetStorage(storageId)
  126. if storage == nil {
  127. err = errors.Wrapf(err, "get storage %s", storageId)
  128. break
  129. }
  130. //var diskInfo jsonutils.JSONObject
  131. diskId := disksDesc[i].DiskId
  132. iDisk := storage.CreateDisk(diskId)
  133. diskUrl := fmt.Sprintf("http://%s:48885/disks/%s",
  134. createConfig.CloudpodsAccessInfo.HostIp, createConfig.CloudpodsAccessInfo.OriginDisksId[i])
  135. if err = iDisk.CreateFromRemoteHostImage(ctx, diskUrl, 0, nil); err != nil {
  136. log.Errorf("failed create disk %s from fuse %s", diskUrl, err)
  137. break
  138. }
  139. diskInfo := jsonutils.NewDict()
  140. diskInfo.Set("origin_disk_url", jsonutils.NewString(diskUrl))
  141. diskInfo.Set("access_path", jsonutils.NewString(iDisk.GetPath()))
  142. ret.Set(diskId, diskInfo)
  143. }
  144. if err != nil {
  145. return nil, err
  146. }
  147. return ret, nil
  148. }