apm.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "go-common/app/admin/main/config/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/jinzhu/gorm"
  10. )
  11. //Apm apm.
  12. func (s *Service) Apm(treeID int64, name, apmname, username string) (err error) {
  13. sns := []*model.ServiceName{}
  14. if err = s.DBApm.Where("name=?", apmname).Find(&sns).Error; err != nil {
  15. log.Error("svr.service.Apm name(%d) apmname(%d) error(%v)", name, apmname, err)
  16. return
  17. }
  18. for _, val := range sns {
  19. if err = s.ApmBuild(val, name, username, treeID); err != nil {
  20. log.Error("svr.service.ApmBuild val(%d) apmname(%d) error(%v)", val, apmname, err)
  21. }
  22. }
  23. return
  24. }
  25. //ApmBuild apmBuild.
  26. func (s *Service) ApmBuild(val *model.ServiceName, name, username string, treeID int64) (err error) {
  27. bvs := []*model.BuildVersion{}
  28. mtime := time.Now().Unix() - (3600 * 24 * 60)
  29. if err = s.DBApm.Where("service_id=? and mtime > ?", val.ID, mtime).Find(&bvs).Error; err != nil {
  30. log.Error("svr.service.ApmBuild val(%v) id(%d) error(%v)", val, val.ID, err)
  31. return
  32. }
  33. if len(bvs) <= 0 {
  34. err = ecode.NothingFound
  35. return
  36. }
  37. for _, v := range bvs {
  38. var ver string
  39. scvs := []*model.ServiceConfigValue{}
  40. if err = s.DBApm.Where("config_id=?", v.ConfigID).Find(&scvs).Error; err != nil {
  41. log.Error("svr.service.ServiceConfigValue val(%d) ConfigID(%d) error(%v)", val, v.ConfigID, err)
  42. return
  43. }
  44. if len(scvs) <= 0 {
  45. err = ecode.NothingFound
  46. return
  47. }
  48. var env string
  49. switch val.Environment {
  50. case 10:
  51. env = "dev"
  52. case 11:
  53. env = "fat1"
  54. case 13:
  55. env = "uat"
  56. case 14:
  57. env = "pre"
  58. case 3:
  59. env = "prod"
  60. default:
  61. continue
  62. }
  63. version := strings.Split(v.Version, "-")
  64. if len(version) != 3 {
  65. continue
  66. }
  67. ver = version[1] + "-" + version[2]
  68. zone := version[0]
  69. switch zone {
  70. case "shylf":
  71. zone = "sh001"
  72. case "hzxs":
  73. zone = "sh001"
  74. case "shsb":
  75. zone = "sh001"
  76. default:
  77. continue
  78. }
  79. app := &model.App{
  80. Name: name,
  81. TreeID: treeID,
  82. Env: env,
  83. Zone: zone,
  84. Token: val.Token,
  85. }
  86. var tx *gorm.DB
  87. if tx = s.DB.Begin(); err != nil {
  88. log.Error("begin tran error(%v)", err)
  89. return
  90. }
  91. if err = tx.Where("tree_id=? and env=? and zone=?", treeID, env, zone).Find(&app).Error; err != nil {
  92. if err = tx.Create(&app).Error; err != nil {
  93. log.Error("svr.service.addapp create error(%v)", err)
  94. tx.Rollback()
  95. return
  96. }
  97. }
  98. configIds := ""
  99. for _, vv := range scvs {
  100. config := &model.Config{
  101. AppID: app.ID,
  102. Name: vv.Name,
  103. Comment: vv.Config,
  104. From: 0,
  105. State: 2,
  106. Mark: "一键迁移",
  107. Operator: username,
  108. }
  109. if err = tx.Create(&config).Error; err != nil {
  110. log.Error("svr.service.addconfig create error(%v)", err)
  111. tx.Rollback()
  112. return
  113. }
  114. if len(configIds) > 0 {
  115. configIds += ","
  116. }
  117. configIds += fmt.Sprint(config.ID)
  118. }
  119. tag := &model.Tag{
  120. AppID: app.ID,
  121. BuildID: 0,
  122. ConfigIDs: configIds,
  123. Mark: v.Remark,
  124. Operator: username,
  125. }
  126. if err = tx.Create(&tag).Error; err != nil {
  127. log.Error("svr.service.addtag create error(%v)", err)
  128. tx.Rollback()
  129. return
  130. }
  131. buildNew := &model.Build{
  132. AppID: app.ID,
  133. Name: ver,
  134. TagID: tag.ID,
  135. Mark: v.Remark,
  136. Operator: username,
  137. }
  138. if err = tx.Create(&buildNew).Error; err != nil {
  139. log.Error("svr.service.addbuild create error(%v)", err)
  140. tx.Rollback()
  141. return
  142. }
  143. ups := map[string]interface{}{
  144. "build_id": buildNew.ID,
  145. }
  146. if err = tx.Model(tag).Where("id = ?", tag.ID).Updates(ups).Error; err != nil {
  147. log.Error("svr.service.edittag updates error(%v)", err)
  148. tx.Rollback()
  149. return
  150. }
  151. tx.Commit()
  152. }
  153. return
  154. }