guest_migrate_network.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 models
  15. import (
  16. "context"
  17. "database/sql"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/netutils"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. /*
  28. * Migrate a server from one network to another network, without change IP address
  29. * Scenerio: the server used to be in a VPC, migrate it to a underlay network without network interruption
  30. */
  31. func (guest *SGuest) PerformMigrateNetwork(ctx context.Context, userCred mcclient.TokenCredential,
  32. query jsonutils.JSONObject,
  33. input api.ServerMigrateNetworkInput,
  34. ) (jsonutils.JSONObject, error) {
  35. if guest.Hypervisor != api.HYPERVISOR_KVM {
  36. return nil, errors.Wrap(httperrors.ErrNotSupported, "operation not supported for this hypervisor")
  37. }
  38. // first validate it against the source network, ensure the following:
  39. // 1. the network is attach to this guest
  40. srcModel, err := NetworkManager.FetchByIdOrName(ctx, userCred, input.Src)
  41. if err != nil {
  42. if errors.Cause(err) == sql.ErrNoRows {
  43. return nil, errors.Wrapf(httperrors.ErrResourceNotFound, "source network %s not found", input.Src)
  44. } else {
  45. return nil, errors.Wrap(err, "NetworkManager.FetchByIdOrName")
  46. }
  47. }
  48. srcNics, err := guest.GetNetworks(srcModel.GetId())
  49. if err != nil {
  50. if errors.Cause(err) == sql.ErrNoRows {
  51. return nil, errors.Wrapf(httperrors.ErrBadRequest, "server not attach to network %s", input.Src)
  52. } else {
  53. return nil, errors.Wrap(err, "GetNetworks")
  54. }
  55. }
  56. if len(srcNics) == 0 {
  57. return nil, errors.Wrapf(httperrors.ErrBadRequest, "server not attach to network %s", input.Src)
  58. } else if len(srcNics) > 1 {
  59. return nil, errors.Wrapf(httperrors.ErrNotSupported, "not support to migrate multiple interfaces")
  60. }
  61. srcNic := srcNics[0]
  62. ipAddr, err := netutils.NewIPV4Addr(srcNic.IpAddr)
  63. if err != nil {
  64. return nil, errors.Wrapf(err, "NewIPV4Addr")
  65. }
  66. // next validate against the destination network, ensure the following:
  67. // 1. the network is reachable to this server
  68. // 1. the IP address is availalble in the new network
  69. destModel, err := NetworkManager.FetchByIdOrName(ctx, userCred, input.Dest)
  70. if err != nil {
  71. if errors.Cause(err) == sql.ErrNoRows {
  72. return nil, errors.Wrapf(httperrors.ErrResourceNotFound, "destination network %s not found", input.Src)
  73. } else {
  74. return nil, errors.Wrap(err, "NetworkManager.FetchByIdOrName")
  75. }
  76. }
  77. destNet := destModel.(*SNetwork)
  78. host, _ := guest.GetHost()
  79. if host == nil {
  80. return nil, errors.Wrap(httperrors.ErrInvalidStatus, "guest is not allocated!")
  81. }
  82. if destNet.isOneCloudVpcNetwork() {
  83. // vpc network should be in the same Zone
  84. destZone, _ := destNet.GetZone()
  85. if destZone == nil || destZone.Id != host.ZoneId {
  86. return nil, errors.Wrap(httperrors.ErrBadRequest, "destination overlay network not in same zone as server")
  87. }
  88. } else {
  89. // underlay network should be reachable in wire
  90. var destWire *SWire
  91. wires := host.getAttachedWires()
  92. for i := range wires {
  93. if wires[i].Id == destNet.WireId {
  94. // reachable
  95. destWire = &wires[i]
  96. break
  97. }
  98. }
  99. if destWire == nil {
  100. return nil, errors.Wrap(httperrors.ErrBadRequest, "destination underlay network not reachable")
  101. }
  102. }
  103. lockman.LockObject(ctx, destNet)
  104. defer lockman.ReleaseObject(ctx, destNet)
  105. if !destNet.IsAddressInRange(ipAddr) {
  106. return nil, errors.Wrapf(httperrors.ErrBadRequest, "ip %s not in range of destination network", ipAddr.String())
  107. }
  108. used, err := destNet.isAddressUsed(ctx, ipAddr.String())
  109. if err != nil {
  110. return nil, errors.Wrap(err, "isAddressUsed")
  111. }
  112. if used {
  113. return nil, errors.Wrapf(httperrors.ErrBadRequest, "ip %s has been allocated in destination network", ipAddr.String())
  114. }
  115. // perform the database change
  116. _, err = db.Update(&srcNic, func() error {
  117. srcNic.NetworkId = destNet.Id
  118. srcNic.MappedIpAddr = "" // reset MappedIpAddr anyway
  119. srcNic.MappedIp6Addr = "" // reset MappedIp6Addr anyway
  120. return nil
  121. })
  122. if err != nil {
  123. return nil, errors.Wrap(err, "fail to update nic network_id")
  124. }
  125. // synchronize the change to host, and wait it to be effective
  126. err = guest.StartSyncTask(ctx, userCred, false, "")
  127. if err != nil {
  128. return nil, errors.Wrap(err, "fail to SyncTask")
  129. }
  130. return nil, nil
  131. }