mysql_machine.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package dao
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "go-common/app/admin/ep/merlin/model"
  8. pkgerr "github.com/pkg/errors"
  9. )
  10. const (
  11. _wildcards = "%"
  12. _where = "WHERE"
  13. _and = "AND"
  14. _pathAndPodNamesMappingSQL = "SELECT CONCAT(business_unit, '.', project, '.', app) AS \"path\" , GROUP_CONCAT(pod_name) AS \"pod_names\" FROM machines WHERE status >=0 AND status < 100 AND ctime < %s GROUP BY business_unit, project, app;"
  15. _nodeSQL = "SELECT DISTINCT(machine_id) FROM machine_nodes"
  16. )
  17. // FindExpiredMachineByDay find expired machines.
  18. func (d *Dao) FindExpiredMachineByDay(day int) (ms []*model.Machine, err error) {
  19. err = pkgerr.WithStack(d.db.Where("status >= 0 AND DATEDIFF(end_time,NOW())>=0 AND DATEDIFF(end_time,NOW())<= ?", day).Find(&ms).Error)
  20. return
  21. }
  22. // FindExpiredMachine find a day-old machines.
  23. func (d *Dao) FindExpiredMachine() (ms []*model.Machine, err error) {
  24. err = pkgerr.WithStack(d.db.Where("status >= 0 AND DATEDIFF(NOW(),end_time) = ?", -1).Find(&ms).Error)
  25. return
  26. }
  27. // QueryMachine query machine.
  28. func (d *Dao) QueryMachine(id int64) (machine *model.Machine, err error) {
  29. machine = &model.Machine{}
  30. err = pkgerr.WithStack(d.db.Where("id = ?", id).First(machine).Error)
  31. return
  32. }
  33. // QueryMachineByName Query Machine By Name.
  34. func (d *Dao) QueryMachineByName(machineName string) (machine *model.Machine, err error) {
  35. machine = &model.Machine{}
  36. err = pkgerr.WithStack(d.db.Where("name = ?", machineName).First(machine).Error)
  37. return
  38. }
  39. // QueryOnlineMachineByName Query Online Machine By Name.
  40. func (d *Dao) QueryOnlineMachineByName(machineName string) (machine *model.Machine, err error) {
  41. machine = &model.Machine{}
  42. err = pkgerr.WithStack(d.db.Where("status >=100 and name = ?", machineName).First(machine).Error)
  43. return
  44. }
  45. // DelMachine delete machine.
  46. func (d *Dao) DelMachine(id int64, username string) (err error) {
  47. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("id = ?", id).Update("status", model.RemovedMachineInMerlin).Update("update_by", username).Error)
  48. }
  49. // HasMachine verify machine name.
  50. func (d *Dao) HasMachine(name string) (b bool, err error) {
  51. var size int64
  52. if err = pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("name LIKE ?", name+"-"+_wildcards).Where("status <> ?", model.RemovedMachineInMerlin).Count(&size).Error); err != nil {
  53. return
  54. }
  55. b = size > 0
  56. return
  57. }
  58. // UpdateMachineStatus update machine status by giving value.
  59. func (d *Dao) UpdateMachineStatus(id int64, status int) error {
  60. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("id = ?", id).Update("status", status).Error)
  61. }
  62. // QueryMachines query machines and update machine in names status to boot.
  63. func (d *Dao) QueryMachines(names []string, qmr *model.QueryMachineRequest) (total int64, machines []*model.Machine, err error) {
  64. if err = pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("status >= 0 AND status < 100 AND pod_name IN (?)", names).Update("status", model.BootMachineInMerlin).Error); err != nil {
  65. return
  66. }
  67. gDB := d.db.Model(&model.Machine{}).Where("status >= 100 AND status < 300 AND pod_name IN (?) OR (status > -300 AND status <= -200 OR status >= 0 AND status < 100) AND username = ?", names, qmr.Requester)
  68. if qmr.Username != "" {
  69. gDB = gDB.Where("username LIKE ?", qmr.Username+_wildcards)
  70. }
  71. if qmr.MachineName != "" {
  72. gDB = gDB.Where("BINARY name LIKE ?", _wildcards+qmr.MachineName+_wildcards)
  73. }
  74. if qmr.BusinessUnit != "" || qmr.Project != "" || qmr.App != "" {
  75. var (
  76. nSQL = _nodeSQL
  77. logicalWord = _where
  78. )
  79. if qmr.BusinessUnit != "" {
  80. nSQL = fmt.Sprintf("%s %s BINARY business_unit = '%s'", nSQL, logicalWord, qmr.BusinessUnit)
  81. logicalWord = _and
  82. }
  83. if qmr.Project != "" {
  84. nSQL = fmt.Sprintf("%s %s BINARY project = '%s'", nSQL, logicalWord, qmr.Project)
  85. logicalWord = _and
  86. }
  87. if qmr.App != "" {
  88. nSQL = fmt.Sprintf("%s %s BINARY app = '%s'", nSQL, logicalWord, qmr.App)
  89. }
  90. gDB = gDB.Where("ID IN ( " + nSQL + " )")
  91. }
  92. if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
  93. return
  94. }
  95. err = pkgerr.WithStack(gDB.Order("ctime DESC").Offset((qmr.PageNum - 1) * qmr.PageSize).Limit(qmr.PageSize).Find(&machines).Error)
  96. return
  97. }
  98. // UpdateMachineEndTime update machine end time.
  99. func (d *Dao) UpdateMachineEndTime(id int64, delayStatus int, endTime time.Time) (err error) {
  100. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("id = ?", id).Update("delay_status", delayStatus).Update("end_time", endTime).Error)
  101. }
  102. // UpdateMachineDelayStatus update machine delay status.
  103. func (d *Dao) UpdateMachineDelayStatus(id int64, delayStatus int) (err error) {
  104. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("id = ?", id).Update("delay_status", delayStatus).Error)
  105. }
  106. // QueryPathAndPodNamesMapping query path and pod names map.
  107. func (d *Dao) QueryPathAndPodNamesMapping() (pathAndPodNames map[string][]string, err error) {
  108. var (
  109. rows *sql.Rows
  110. path string
  111. podNames string
  112. )
  113. if rows, err = d.db.Raw(fmt.Sprintf(_pathAndPodNamesMappingSQL, d.c.Paas.MachineTimeout)).Rows(); err != nil {
  114. err = pkgerr.WithStack(err)
  115. return
  116. }
  117. defer rows.Close()
  118. pathAndPodNames = make(map[string][]string)
  119. for rows.Next() {
  120. if err = rows.Scan(&path, &podNames); err != nil {
  121. return
  122. }
  123. pathAndPodNames[path] = strings.Split(podNames, ",")
  124. }
  125. return
  126. }
  127. // UpdateMachineStatusByPodNames update machine status by pod names.
  128. func (d *Dao) UpdateMachineStatusByPodNames(podNames []string, target int) (err error) {
  129. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("pod_name IN (?)", podNames).Update("status", target).Error)
  130. }
  131. // UpdateMachineUser Update Machine User By Id.
  132. func (d *Dao) UpdateMachineUser(machineID int64, username string) (err error) {
  133. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("id = ?", machineID).Update("username", username).Error)
  134. }
  135. // QueryMachinesByPodNames query machines by giving pod name slices.
  136. func (d *Dao) QueryMachinesByPodNames(podNames []string) (machines []*model.Machine, err error) {
  137. err = pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("pod_name IN (?)", podNames).Find(&machines).Error)
  138. return
  139. }
  140. // InsertMachines insert machines.
  141. func (d *Dao) InsertMachines(u string, gmr *model.GenMachinesRequest, ins []*model.CreateInstance) (err error) {
  142. var m *model.Machine
  143. tx := d.db.Begin()
  144. if err = tx.Error; err != nil {
  145. return
  146. }
  147. for _, in := range ins {
  148. m = in.ToMachine(u, gmr)
  149. if err = tx.Create(m).Error; err != nil {
  150. tx.Rollback()
  151. return
  152. }
  153. for _, n := range gmr.ToMachineNode(m.ID) {
  154. if err = tx.Create(n).Error; err != nil {
  155. tx.Rollback()
  156. return
  157. }
  158. }
  159. if err = tx.Create(m.ToMachineLog()).Error; err != nil {
  160. tx.Rollback()
  161. return
  162. }
  163. }
  164. if err = tx.Commit().Error; err != nil {
  165. tx.Rollback()
  166. }
  167. return
  168. }
  169. // QueryMachineInRunning Query Machine In Running
  170. func (d *Dao) QueryMachineInRunning() (ms []*model.Machine, err error) {
  171. err = d.db.Where("status >= 100").Find(&ms).Error
  172. return
  173. }
  174. // QueryMachineCount Query Machine Count
  175. func (d *Dao) QueryMachineCount() (total int, err error) {
  176. err = pkgerr.WithStack(d.db.Model(&model.Machine{}).Count(&total).Error)
  177. return
  178. }