machineV2.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/merlin/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. )
  8. // GenMachinesV2 create multiple machines for v2.
  9. func (s *Service) GenMachinesV2(c context.Context, gmr *model.GenMachinesRequest, u string) (err error) {
  10. var (
  11. ins []*model.CreateInstance
  12. cluster *model.Cluster
  13. hasMachine bool
  14. ms []*model.Machine
  15. )
  16. if hasMachine, err = s.dao.HasMachine(gmr.Name); err != nil {
  17. return
  18. }
  19. if hasMachine {
  20. err = ecode.MerlinDuplicateMachineNameErr
  21. return
  22. }
  23. if cluster, err = s.dao.QueryCluster(c, gmr.NetworkID); err != nil {
  24. return
  25. }
  26. if cluster == nil {
  27. err = ecode.MerlinInvalidClusterErr
  28. return
  29. }
  30. if err = cluster.Verify(); err != nil {
  31. return
  32. }
  33. pgmr := gmr.ToPaasGenMachineRequest(s.c.Paas.MachineLimitRatio)
  34. if ms, err = s.dao.InsertMachinesV2(u, gmr, pgmr); err != nil {
  35. return
  36. }
  37. go func() {
  38. var (
  39. mIDs []int64
  40. mim map[int64]string
  41. nmm = make(map[string]*model.Machine)
  42. cTODO = context.TODO()
  43. )
  44. for _, m := range ms {
  45. mIDs = append(mIDs, m.ID)
  46. nmm[m.Name] = m
  47. }
  48. if mim, err = s.AddTagToMachine(cTODO, u, gmr.Image, mIDs); err != nil {
  49. if err = s.dao.UpdateStatusForMachines(model.CreateTagFailedMachineInMerlin, mIDs); err != nil {
  50. log.Error("Update the status of machines(%v) err(%v)", mIDs, err)
  51. }
  52. return
  53. }
  54. for i, pm := range pgmr.Machines {
  55. pgmr.Machines[i].Image = mim[nmm[pm.Name].ID]
  56. pgmr.Machines[i].Snapshot = true
  57. pgmr.Machines[i].ForcePullImage = true
  58. }
  59. if ins, err = s.dao.GenPaasMachines(cTODO, pgmr); err != nil {
  60. return
  61. }
  62. for _, in := range ins {
  63. if in.InstanceCreateStatus == model.CreateFailedMachineInPaas {
  64. s.dao.UpdateMachineStatusByName(model.ImmediatelyFailedMachineInMerlin, in.InstanceName)
  65. }
  66. }
  67. }()
  68. return
  69. }