build.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/admin/main/config/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. // CreateBuild create build.
  10. func (s *Service) CreateBuild(build *model.Build, treeID int64, env, zone string) (err error) {
  11. var app *model.App
  12. if app, err = s.AppByTree(treeID, env, zone); err != nil {
  13. return
  14. }
  15. build.AppID = app.ID
  16. return s.dao.DB.Create(build).Error
  17. }
  18. //UpdateTag update tag.
  19. func (s *Service) UpdateTag(ctx context.Context, treeID int64, env, zone, name string, tag *model.Tag) (err error) {
  20. var (
  21. app *model.App
  22. build *model.Build
  23. )
  24. if app, err = s.AppByTree(treeID, env, zone); err != nil {
  25. return
  26. }
  27. if build, err = s.BuildByName(app.ID, name); err != nil {
  28. if err != ecode.NothingFound {
  29. return
  30. }
  31. build = &model.Build{AppID: app.ID, Name: name, Mark: tag.Mark, Operator: tag.Operator}
  32. if err = s.dao.DB.Create(build).Error; err != nil {
  33. log.Error("CreateBuild(%s) error(%v)", build.Name, err)
  34. return
  35. }
  36. }
  37. tag.AppID = app.ID
  38. tag.BuildID = build.ID
  39. if err = s.dao.DB.Create(&tag).Error; err != nil {
  40. log.Error("CreateTag(%s) error(%v)", tag.Mark, err)
  41. return
  42. }
  43. if tag.Force == 1 {
  44. //Clear stand-alone force
  45. forces := []*model.Force{}
  46. if err = s.dao.DB.Where("app_id = ?", app.ID).Find(&forces).Error; err != nil {
  47. log.Error("select forces(%s) error(%v)", app.ID, err)
  48. return
  49. }
  50. mHosts := model.MapHosts{}
  51. for _, val := range forces {
  52. mHosts[val.Hostname] = val.IP
  53. }
  54. if len(mHosts) > 0 {
  55. if err = s.ClearForce(ctx, treeID, env, zone, name, mHosts); err != nil {
  56. log.Error("clear forces(%s) error(%v)", app.ID, err)
  57. return
  58. }
  59. }
  60. }
  61. tx := s.dao.DB.Begin()
  62. if err = tx.Model(&model.Build{ID: build.ID}).Update(map[string]interface{}{
  63. "tag_id": tag.ID,
  64. "operator": tag.Operator,
  65. }).Error; err != nil {
  66. tx.Rollback()
  67. log.Error("updateTagID(%d) error(%v)", tag.ID, err)
  68. return
  69. }
  70. //push
  71. if err = s.Push(ctx, treeID, env, zone, build.Name, tag.ID); err != nil {
  72. tx.Rollback()
  73. return
  74. }
  75. tx.Commit()
  76. return
  77. }
  78. //UpdateTagID update tag.
  79. func (s *Service) UpdateTagID(ctx context.Context, env, zone, bName string, tag, TreeID int64) (err error) {
  80. build := new(model.Build)
  81. build.Name = bName
  82. build.TagID = tag
  83. var app *model.App
  84. if app, err = s.AppByTree(TreeID, env, zone); err != nil {
  85. return
  86. }
  87. tx := s.dao.DB.Begin()
  88. if err = tx.Model(&model.Build{}).Where("app_id = ? and name = ?", app.ID, build.Name).Update(map[string]interface{}{
  89. "tag_id": build.TagID,
  90. "operator": build.Operator,
  91. }).Error; err != nil {
  92. tx.Rollback()
  93. log.Error("updateTagID(%d) error(%v)", build.TagID, err)
  94. return
  95. }
  96. if err = s.Push(ctx, TreeID, env, zone, build.Name, build.TagID); err != nil {
  97. tx.Rollback()
  98. return
  99. }
  100. tx.Commit()
  101. return
  102. }
  103. //Builds get builds by app id.
  104. func (s *Service) Builds(treeID int64, appName, env, zone string) (builds []*model.Build, err error) {
  105. var (
  106. app *model.App
  107. tag *model.Tag
  108. )
  109. if app, err = s.AppByTree(treeID, env, zone); err != nil {
  110. if err == ecode.NothingFound {
  111. if err = s.CreateApp(appName, env, zone, treeID); err == nil {
  112. builds = make([]*model.Build, 0)
  113. }
  114. return
  115. }
  116. }
  117. if builds, err = s.BuildsByApp(app.ID); err != nil {
  118. return
  119. }
  120. for _, build := range builds {
  121. if tag, err = s.Tag(build.TagID); err != nil {
  122. if err == ecode.NothingFound {
  123. err = nil
  124. }
  125. }
  126. build.Mark = tag.Mark
  127. build.Operator = tag.Operator
  128. build.Mtime = tag.Mtime
  129. }
  130. return
  131. }
  132. //BuildsByApp buildsByApp.
  133. func (s *Service) BuildsByApp(appID int64) (builds []*model.Build, err error) {
  134. if err = s.dao.DB.Find(&builds, "app_id = ? ", appID).Error; err != nil {
  135. log.Error("BuildsByApp(%s) error(%v)", appID, err)
  136. if err == sql.ErrNoRows {
  137. err = nil
  138. }
  139. }
  140. return
  141. }
  142. //Build get Build by build ID.
  143. func (s *Service) Build(ID int64) (build *model.Build, err error) {
  144. build = new(model.Build)
  145. if err = s.dao.DB.First(&build, ID).Error; err != nil {
  146. log.Error("Build(%v) error(%v)", ID, err)
  147. if err == sql.ErrNoRows {
  148. err = ecode.NothingFound
  149. }
  150. }
  151. return
  152. }
  153. //Delete delete Build by build ID.
  154. func (s *Service) Delete(ID int64) (err error) {
  155. if err = s.dao.DB.Delete(&model.Build{}, ID).Error; err != nil {
  156. log.Error("Delete(%v) error(%v)", ID, err)
  157. }
  158. return
  159. }
  160. //BuildByName get Build by build ID.
  161. func (s *Service) BuildByName(appID int64, name string) (build *model.Build, err error) {
  162. build = new(model.Build)
  163. if err = s.dao.DB.First(&build, "app_id = ? and name = ?", appID, name).Error; err != nil {
  164. log.Error("BuildByName(%s) error(%v)", name, err)
  165. if err == sql.ErrNoRows {
  166. err = ecode.NothingFound
  167. }
  168. }
  169. return
  170. }
  171. //GetDelInfos get delete info.
  172. func (s *Service) GetDelInfos(c context.Context, BuildID int64) (err error) {
  173. build := &model.Build{}
  174. if err = s.dao.DB.Where("id = ?", BuildID).First(build).Error; err != nil {
  175. log.Error("GetDelInfos BuildID(%v) error(%v)", BuildID, err)
  176. return
  177. }
  178. app := &model.App{}
  179. if err = s.dao.DB.Where("id = ?", build.AppID).First(app).Error; err != nil {
  180. log.Error("GetDelInfos AppID(%v) error(%v)", build.AppID, err)
  181. return
  182. }
  183. hosts, err := s.Hosts(c, app.TreeID, app.Name, app.Env, app.Zone)
  184. if err != nil {
  185. log.Error("GetDelInfos hosts(%v) error(%v)", hosts, err)
  186. return
  187. }
  188. for _, v := range hosts {
  189. if v.BuildVersion == build.Name {
  190. err = ecode.NothingFound
  191. return
  192. }
  193. }
  194. return
  195. }
  196. // AllBuilds ...
  197. func (s *Service) AllBuilds(appIDS []int64) (builds []*model.Build, err error) {
  198. if err = s.dao.DB.Where("app_id in (?)", appIDS).Find(&builds).Error; err != nil {
  199. log.Error("AllBuild error(%v)", err)
  200. }
  201. return
  202. }