common.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/main/config/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. // CreateComConf create config.
  12. func (s *Service) CreateComConf(conf *model.CommonConf, name, env, zone string, skiplint bool) (err error) {
  13. if !skiplint {
  14. if err = lintConfig(conf.Name, conf.Comment); err != nil {
  15. return
  16. }
  17. }
  18. var team *model.Team
  19. if team, err = s.TeamByName(name, env, zone); err != nil {
  20. return
  21. }
  22. conf.TeamID = team.ID
  23. return s.dao.DB.Create(conf).Error
  24. }
  25. // ComConfig get common config by id.
  26. func (s *Service) ComConfig(id int64) (conf *model.CommonConf, err error) {
  27. conf = new(model.CommonConf)
  28. if err = s.dao.DB.First(&conf, "id = ?", id).Error; err != nil {
  29. log.Error("ComConfig() error(%v)", err)
  30. }
  31. return
  32. }
  33. // ComConfigsByTeam common config by team.
  34. func (s *Service) ComConfigsByTeam(name, env, zone string, ps, pn int64) (pager *model.CommonConfPager, err error) {
  35. var (
  36. team *model.Team
  37. confs []*model.CommonConf
  38. temp []*model.CommonTemp
  39. counts model.CommonCounts
  40. array []int64
  41. )
  42. if team, err = s.TeamByName(name, env, zone); err != nil {
  43. return
  44. }
  45. if err = s.dao.DB.Raw("select max(id) as id,count(distinct name) as counts from common_config where team_id =? group by name order by id desc", team.ID).Scan(&temp).Error; err != nil {
  46. log.Error("NamesByTeam(%v) error(%v)", team.ID, err)
  47. if err == sql.ErrNoRows {
  48. err = nil
  49. }
  50. }
  51. if err = s.dao.DB.Raw("select count(distinct name) as counts from common_config where team_id = ?", team.ID).Scan(&counts).Error; err != nil {
  52. if err == sql.ErrNoRows {
  53. err = nil
  54. }
  55. return
  56. }
  57. for _, v := range temp {
  58. array = append(array, v.ID)
  59. }
  60. if err = s.dao.DB.Raw("select id,team_id,name,state,mark,operator,ctime,mtime from common_config where id in (?) limit ?,?", array, (pn-1)*ps, ps).Scan(&confs).Error; err != nil {
  61. log.Error("NamesByTeam(%v) temp(%v) error(%v)", team.ID, temp, err)
  62. return
  63. }
  64. return &model.CommonConfPager{Total: counts.Counts, Pn: pn, Ps: ps, Items: confs}, nil
  65. }
  66. //ComConfigsByName get Config by Config name.
  67. func (s *Service) ComConfigsByName(teamName, env, zone, name string) (confs []*model.CommonConf, err error) {
  68. var team *model.Team
  69. if team, err = s.TeamByName(teamName, env, zone); err != nil {
  70. return
  71. }
  72. if err = s.dao.DB.Select("id,team_id,name,state,mark,operator,ctime,mtime").Where("name = ? and team_id = ?",
  73. name, team.ID).Order("id desc").Limit(10).Find(&confs).Error; err != nil {
  74. return
  75. }
  76. return
  77. }
  78. // UpdateComConfValue update config value.
  79. func (s *Service) UpdateComConfValue(conf *model.CommonConf, skiplint bool) (err error) {
  80. if !skiplint {
  81. if err = lintConfig(conf.Name, conf.Comment); err != nil {
  82. return
  83. }
  84. }
  85. var confDB *model.CommonConf
  86. if confDB, err = s.ComConfig(conf.ID); err != nil {
  87. return
  88. }
  89. if confDB.State == model.ConfigIng { //judge config is configIng.
  90. if conf.Mtime != confDB.Mtime {
  91. err = ecode.TargetBlocked
  92. return
  93. }
  94. conf.Mtime = 0
  95. err = s.dao.DB.Model(&model.CommonConf{ID: confDB.ID}).Updates(conf).Error
  96. return
  97. }
  98. if _, err = s.comConfigIng(confDB.Name, confDB.TeamID); err == nil { //judge have configing.
  99. err = ecode.TargetBlocked
  100. return
  101. }
  102. if err == sql.ErrNoRows || err == ecode.NothingFound {
  103. conf.ID = 0
  104. conf.TeamID = confDB.TeamID
  105. conf.Name = confDB.Name
  106. conf.Mtime = 0
  107. return s.dao.DB.Create(conf).Error
  108. }
  109. return
  110. }
  111. func (s *Service) comConfigIng(name string, teamID int64) (conf *model.CommonConf, err error) {
  112. conf = new(model.CommonConf)
  113. if err = s.dao.DB.Select("id").Where("name = ? and team_id = ? and state=?", name, teamID, model.ConfigIng).First(&conf).Error; err != nil {
  114. log.Error("configIng(%v) error(%v)", name, err)
  115. if err == sql.ErrNoRows {
  116. err = ecode.NothingFound
  117. }
  118. }
  119. return
  120. }
  121. // NamesByTeam get configs by team name.
  122. func (s *Service) NamesByTeam(teamName, env, zone string) (names []*model.CommonName, err error) {
  123. var (
  124. team *model.Team
  125. confs []*model.CommonConf
  126. )
  127. if team, err = s.TeamByName(teamName, env, zone); err != nil {
  128. if err == ecode.NothingFound {
  129. err = s.CreateTeam(teamName, env, zone)
  130. return
  131. }
  132. }
  133. if err = s.dao.DB.Where("team_id = ? and state = 2", team.ID).Order("id desc").Find(&confs).Error; err != nil {
  134. log.Error("NamesByTeam(%v) error(%v)", team.ID, err)
  135. if err == sql.ErrNoRows {
  136. err = nil
  137. }
  138. }
  139. tmp := make(map[string]struct{})
  140. for _, conf := range confs {
  141. if _, ok := tmp[conf.Name]; !ok {
  142. names = append(names, &model.CommonName{Name: conf.Name, ID: conf.ID})
  143. tmp[conf.Name] = struct{}{}
  144. }
  145. }
  146. return
  147. }
  148. //AppByTeam get tagMap
  149. func (s *Service) AppByTeam(commonConfigID int64) (tagMap map[int64]*model.TagMap, err error) {
  150. var commonConfig *model.CommonConf
  151. if commonConfig, err = s.ComConfig(commonConfigID); err != nil {
  152. return
  153. }
  154. team := &model.Team{}
  155. if err = s.dao.DB.Where("id = ?", commonConfig.TeamID).First(team).Error; err != nil {
  156. return
  157. }
  158. commonConf := []*model.CommonConf{}
  159. if err = s.dao.DB.Select("id").Where("name = ? and team_id = ? and state = 2", commonConfig.Name, commonConfig.TeamID).Find(&commonConf).Error; err != nil {
  160. log.Error("AppByTeam() common_config error(%v)", err)
  161. }
  162. var commonConfTmp []int64
  163. for _, val := range commonConf {
  164. commonConfTmp = append(commonConfTmp, val.ID)
  165. }
  166. app := []*model.App{}
  167. if err = s.dao.DB.Where("name like ? and env = ? and zone = ?", team.Name+".%", team.Env, team.Zone).Find(&app).Error; err != nil {
  168. log.Error("AppByTeam() app error(%v)", err)
  169. }
  170. var appTmp []int64
  171. appMap := make(map[int64]*model.App)
  172. for _, val := range app {
  173. appMap[val.ID] = val
  174. appTmp = append(appTmp, val.ID)
  175. }
  176. conf := []*model.Config{}
  177. if err = s.dao.DB.Where("`from` in (?) and app_id in (?) and state = 2 and is_delete = 0", commonConfTmp, appTmp).Find(&conf).Error; err != nil {
  178. log.Error("AppByTeam() config error(%v)", err)
  179. }
  180. confMap := make(map[int64]struct{})
  181. for _, val := range conf {
  182. confMap[val.ID] = struct{}{}
  183. }
  184. build := []*model.Build{}
  185. if err = s.dao.DB.Where("app_id in (?)", appTmp).Find(&build).Error; err != nil {
  186. log.Error("AppByTeam() build error(%v)", err)
  187. }
  188. var buildTmp []int64
  189. buildMap := make(map[int64]string)
  190. for _, val := range build {
  191. buildMap[val.ID] = val.Name
  192. buildTmp = append(buildTmp, val.TagID)
  193. }
  194. tagMap = make(map[int64]*model.TagMap)
  195. tag := []*model.Tag{}
  196. if err = s.dao.DB.Where("id in (?)", buildTmp).Find(&tag).Error; err != nil {
  197. log.Error("AppByTeam() tag error(%v)", err)
  198. }
  199. for _, val := range tag {
  200. tmp := strings.Split(val.ConfigIDs, ",")
  201. for _, vv := range tmp {
  202. vv, _ := strconv.ParseInt(vv, 10, 64)
  203. if _, ok := confMap[vv]; !ok {
  204. continue
  205. }
  206. tagMap[val.ID] = &model.TagMap{Tag: val}
  207. if _, ok := appMap[val.AppID]; ok {
  208. tagMap[val.ID].AppName = appMap[val.AppID].Name
  209. tagMap[val.ID].TreeID = appMap[val.AppID].TreeID
  210. }
  211. if _, ok := buildMap[val.BuildID]; ok {
  212. tagMap[val.ID].BuildName = buildMap[val.BuildID]
  213. }
  214. }
  215. }
  216. return
  217. }
  218. // CommonPush ...
  219. func (s *Service) CommonPush(ctx context.Context, tagID, commonConfigID int64, user string) (err error) {
  220. var tag *model.Tag
  221. tag, err = s.Tag(tagID)
  222. if err != nil {
  223. log.Error("CommonPush() tagid(%v) error(%v)", tagID, err)
  224. return
  225. }
  226. configIDS := strings.Split(tag.ConfigIDs, ",")
  227. app := &model.App{}
  228. if err = s.dao.DB.Where("id = ?", tag.AppID).First(app).Error; err != nil {
  229. log.Error("CommonPush() app error(%v)", err)
  230. return
  231. }
  232. build := &model.Build{}
  233. if err = s.dao.DB.Where("id = ?", tag.BuildID).First(build).Error; err != nil {
  234. log.Error("CommonPush() build error(%v)", err)
  235. return
  236. }
  237. var commonConfig *model.CommonConf
  238. if commonConfig, err = s.ComConfig(commonConfigID); err != nil {
  239. return
  240. }
  241. team := &model.Team{}
  242. if err = s.dao.DB.Where("id = ?", commonConfig.TeamID).First(team).Error; err != nil {
  243. log.Error("CommonPush() team error(%v)", err)
  244. return
  245. }
  246. commonConf := []*model.CommonConf{}
  247. if err = s.dao.DB.Select("id").Where("name = ? and team_id = ? and state = 2", commonConfig.Name, commonConfig.TeamID).Find(&commonConf).Error; err != nil {
  248. log.Error("CommonPush() common_config error(%v)", err)
  249. return
  250. }
  251. var commonConfTmp []int64
  252. for _, val := range commonConf {
  253. commonConfTmp = append(commonConfTmp, val.ID)
  254. }
  255. conf := []*model.Config{}
  256. if err = s.dao.DB.Where("id in (?) and `from` in (?)", configIDS, commonConfTmp).Find(&conf).Error; err != nil {
  257. log.Error("CommonPush() config error(%v)", err)
  258. return
  259. }
  260. if len(conf) != 1 {
  261. log.Error("CommonPush() count config(%v) error(数据有误更新数据非1条)", conf)
  262. return
  263. }
  264. var newConfigIDS string
  265. for _, val := range conf {
  266. newConf := &model.Config{}
  267. newConf.AppID = val.AppID
  268. newConf.Comment = commonConfig.Comment
  269. newConf.Mark = commonConfig.Mark
  270. newConf.Name = val.Name
  271. newConf.State = 2
  272. newConf.From = commonConfigID
  273. newConf.Operator = user
  274. if _, err = s.configIng(newConf.Name, app.ID); err == nil { // judge config is configIng
  275. err = ecode.TargetBlocked
  276. return
  277. }
  278. if err = s.dao.DB.Create(newConf).Error; err != nil {
  279. log.Error("CommonPush() create newConf error(%v)", err)
  280. return
  281. }
  282. newConfigIDS = strconv.FormatInt(newConf.ID, 10)
  283. for _, vv := range configIDS {
  284. if strconv.FormatInt(val.ID, 10) != vv {
  285. if len(newConfigIDS) > 0 {
  286. newConfigIDS += ","
  287. }
  288. newConfigIDS += vv
  289. }
  290. }
  291. //tag发版
  292. newTag := &model.Tag{}
  293. newTag.Operator = user
  294. newTag.Mark = tag.Mark
  295. newTag.ConfigIDs = newConfigIDS
  296. newTag.Force = 1
  297. err = s.UpdateTag(ctx, app.TreeID, app.Env, app.Zone, build.Name, newTag)
  298. }
  299. return
  300. }