common.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 candidate
  15. import (
  16. "yunion.io/x/pkg/util/sets"
  17. "yunion.io/x/sqlchemy"
  18. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  21. "yunion.io/x/onecloud/pkg/compute/models"
  22. )
  23. var (
  24. VMRunningStatus = sets.NewString(
  25. computeapi.VM_START_START,
  26. computeapi.VM_STARTING,
  27. computeapi.VM_RUNNING,
  28. computeapi.VM_STOP_FAILED,
  29. computeapi.VM_BLOCK_STREAM,
  30. computeapi.VM_UNKNOWN,
  31. computeapi.VM_BACKUP_STARTING,
  32. computeapi.POD_STATUS_CONTAINER_EXITED,
  33. computeapi.POD_STATUS_CRASH_LOOP_BACK_OFF,
  34. computeapi.POD_STATUS_STARTING_CONTAINER,
  35. computeapi.POD_STATUS_STOP_CONTAINER_FAILED,
  36. computeapi.POD_STATUS_STOPPING_CONTAINER,
  37. )
  38. VMCreatingStatus = sets.NewString(
  39. computeapi.VM_SCHEDULE,
  40. computeapi.VM_CREATE_NETWORK,
  41. computeapi.VM_CREATE_DISK,
  42. computeapi.VM_START_DEPLOY,
  43. computeapi.VM_DEPLOYING,
  44. computeapi.VM_BACKUP_CREATING,
  45. computeapi.VM_DEPLOYING_BACKUP,
  46. computeapi.POD_STATUS_CREATING_CONTAINER,
  47. )
  48. VMStoppedStatus = sets.NewString(
  49. computeapi.VM_READY,
  50. computeapi.VM_START_FAILED,
  51. computeapi.VM_SCHEDULE_FAILED,
  52. computeapi.VM_NETWORK_FAILED,
  53. computeapi.VM_CREATE_FAILED,
  54. computeapi.VM_DISK_FAILED,
  55. computeapi.POD_STATUS_START_CONTAINER_FAILED,
  56. computeapi.POD_STATUS_CREATE_CONTAINER_FAILED,
  57. )
  58. )
  59. func GetHostIds(hosts []models.SHost) []string {
  60. ids := make([]string, len(hosts))
  61. for i, host := range hosts {
  62. ids[i] = host.GetId()
  63. }
  64. return ids
  65. }
  66. func FetchGuestByHostIDsQuery(idsQuery sqlchemy.IQuery) ([]models.SGuest, error) {
  67. gs := make([]models.SGuest, 0)
  68. q := models.GuestManager.Query()
  69. hostIdsSubq := idsQuery.SubQuery()
  70. q = q.Join(hostIdsSubq, sqlchemy.Equals(q.Field("host_id"), hostIdsSubq.Field("id")))
  71. err := db.FetchModelObjects(models.GuestManager, q, &gs)
  72. return gs, err
  73. }
  74. func IsGuestRunning(g models.SGuest) bool {
  75. return VMRunningStatus.Has(g.Status)
  76. }
  77. func IsGuestCreating(g models.SGuest) bool {
  78. return VMCreatingStatus.Has(g.Status)
  79. }
  80. func IsGuestPendingDelete(g models.SGuest) bool {
  81. return g.PendingDeleted
  82. }
  83. func IsGuestStoppedStatus(g models.SGuest) bool {
  84. return VMStoppedStatus.Has(g.Status)
  85. }
  86. func ToDict[O lockman.ILockedObject](objs []O) map[string]*O {
  87. ret := make(map[string]*O, 0)
  88. for _, obj := range objs {
  89. tmpObj := obj
  90. objPtr := &tmpObj
  91. ret[obj.GetId()] = objPtr
  92. }
  93. return ret
  94. }