job.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "go-common/app/admin/ep/melloi/conf"
  9. "go-common/app/admin/ep/melloi/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _rmJob = "/api/rm/job"
  15. _jobNodeURI = "/api/rm/jobs"
  16. statsPort = "8999"
  17. statsPath = "/ep/docker/stats"
  18. _clearableDockers = "/ep/docker/get"
  19. _dockerKeyword = "run"
  20. )
  21. //AddJob add perf job
  22. func (d *Dao) AddJob(c context.Context, token string, jobInfo *model.Job) (cluster *model.PaasJobResponse, err error) {
  23. var (
  24. url = d.c.ServiceCluster.TestHost + _jobNodeURI
  25. req *http.Request
  26. passDetail []byte
  27. )
  28. paasJobDetail := &model.PaasJobDetail{
  29. BusinessUnit: conf.Conf.Paas.BusinessUnit,
  30. Project: conf.Conf.Paas.Project,
  31. App: conf.Conf.Paas.App,
  32. Env: conf.Conf.Paas.Env,
  33. Name: jobInfo.Name,
  34. Image: conf.Conf.Paas.Image,
  35. ImageVersion: conf.Conf.Paas.ImageVersion,
  36. Volumes: conf.Conf.Paas.Volumes,
  37. CPURequest: jobInfo.CPU / 4,
  38. CPULimit: jobInfo.CPU,
  39. MemoryRequest: jobInfo.Memory,
  40. Command: jobInfo.Command,
  41. ResourcePoolID: conf.Conf.Paas.ResourcePoolID,
  42. Parallelism: jobInfo.Parallelism,
  43. Completions: conf.Conf.Paas.Completions,
  44. RetriesLimit: conf.Conf.Paas.RetriesLimit,
  45. NetworkID: conf.Conf.Paas.NetworkID,
  46. ClusterID: conf.Conf.Paas.ClusterID,
  47. TreeID: conf.Conf.Paas.TreeID,
  48. HostInfo: conf.Conf.Paas.HostInfo,
  49. EnvInfo: jobInfo.EnvInfo,
  50. }
  51. log.Info("Command:(%s)", paasJobDetail.Command)
  52. if passDetail, err = json.Marshal(paasJobDetail); err != nil {
  53. return
  54. }
  55. log.Info("passDetail:(%s)", string(passDetail))
  56. if req, err = d.newRequest(http.MethodPost, url, paasJobDetail); err != nil {
  57. return
  58. }
  59. req.Header.Set("Content-Type", "application/json")
  60. req.Header.Set("X-Authorization-Token", token)
  61. if err = d.httpClient.Do(c, req, &cluster); err != nil {
  62. log.Error("d.AddJob url(%s) res($s) error(%v)", url, err)
  63. return
  64. }
  65. return
  66. }
  67. //DeleteJob force stop perf job
  68. func (d *Dao) DeleteJob(c context.Context, token, name string) (cluster *model.PaasJobResponse, err error) {
  69. log.Info("删除job -------- token:(%s), job_name(%s): ", token, name)
  70. var (
  71. url = d.c.ServiceCluster.TestHost + _rmJob
  72. req *http.Request
  73. )
  74. if req, err = d.newRequest(http.MethodDelete, url, nil); err != nil {
  75. return
  76. }
  77. req.Header.Set("X-Authorization-Token", token)
  78. q := req.URL.Query()
  79. q.Add("business_unit", "test")
  80. q.Add("project", "ep")
  81. q.Add("app", "melloi-launch")
  82. q.Add("env", "dev")
  83. q.Add("name", name)
  84. q.Add("cluster_id", "1")
  85. q.Add("tree_id", "39400")
  86. req.URL.RawQuery = q.Encode()
  87. if err = d.httpClient.Do(c, req, &cluster); err != nil {
  88. log.Error("d.Job url(%s) res($s) error(%v)", url, err)
  89. err = ecode.MelloiPaasRequestErr
  90. return
  91. }
  92. // 此处不return
  93. if err = d.DeletePtestJobByJobName(name); err != nil {
  94. log.Error("d.DeletePtestJobByJobName uerror(%v)", name)
  95. }
  96. return
  97. }
  98. // QueryCleanableDocker query clearable docker list
  99. func (d *Dao) QueryCleanableDocker(c context.Context) (dockers []*model.CleanableDocker, err error) {
  100. var (
  101. url = fmt.Sprintf("%s:%d%s", d.c.DockerStatus.Host, d.c.DockerStatus.Port, _clearableDockers)
  102. req *http.Request
  103. bs []byte
  104. result []string
  105. )
  106. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  107. return
  108. }
  109. if bs, err = d.httpClient.Raw(c, req, ""); err != nil {
  110. log.Error("d.job query clearable docker url(%s) error (%v)", url, err)
  111. return
  112. }
  113. result = strings.Split(string(bs), "\n")
  114. for _, item := range result {
  115. var docker = &model.CleanableDocker{}
  116. if strings.Contains(item, _dockerKeyword) {
  117. docker.Name = item
  118. dockers = append(dockers, docker)
  119. }
  120. }
  121. return
  122. }
  123. //QueryJobCPU query job cpu status
  124. func (d *Dao) QueryJobCPU(c context.Context, token, jobName string) (responseBody *model.PaasQueryJobCPUResult, err error) {
  125. Query := strings.Replace(conf.Conf.Paas.Query, "JobNamed", jobName, -1)
  126. var (
  127. url = d.c.ServiceCluster.QueryJobCPUHost
  128. req *http.Request
  129. paasQueryJobPostCPUDetail = &model.PaasQueryJobCPUPostDetail{
  130. Action: conf.Conf.Paas.Action,
  131. PublicKey: conf.Conf.Paas.PublicKey,
  132. Signature: conf.Conf.Paas.Signature,
  133. DataSource: conf.Conf.Paas.DataSource,
  134. Query: Query,
  135. }
  136. )
  137. if req, err = d.newRequest(http.MethodPost, url, paasQueryJobPostCPUDetail); err != nil {
  138. return
  139. }
  140. req.Header.Set("X-Authorization-Token", token)
  141. req.Header.Set("Content-Type", "application/json")
  142. req.Header.Set("X-Authorization-Token", token)
  143. if err = d.httpClient.Do(c, req, &responseBody); err != nil {
  144. log.Error("d.Job url(%s) res($s) error(%v)", url, err)
  145. err = ecode.MelloiPaasRequestErr
  146. return
  147. }
  148. return
  149. }
  150. //QueryJobCPUByEP query job cup by ep
  151. func (d *Dao) QueryJobCPUByEP(c context.Context, id, hostIP string) (dokcerStats *model.DockerStats, err error) {
  152. var (
  153. req *http.Request
  154. )
  155. url := "http://" + hostIP + ":" + statsPort + statsPath
  156. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  157. return
  158. }
  159. q := req.URL.Query()
  160. q.Add("id", id)
  161. req.URL.RawQuery = q.Encode()
  162. if err = d.httpClient.Do(c, req, &dokcerStats); err != nil {
  163. log.Error("d.queryJobCPU By EP err : url(%s) error(%v)", url, err)
  164. return
  165. }
  166. return
  167. }
  168. //Job get job info
  169. func (d *Dao) Job(c context.Context, token, name string) (responseBody *model.PaasJobQueryStatus, err error) {
  170. var (
  171. url = d.c.ServiceCluster.TestHost + _rmJob
  172. req *http.Request
  173. )
  174. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  175. return
  176. }
  177. q := req.URL.Query()
  178. q.Add("business_unit", "test")
  179. q.Add("project", "ep")
  180. q.Add("app", "melloi-launch")
  181. q.Add("env", "dev")
  182. q.Add("name", name)
  183. q.Add("cluster_id", "1")
  184. q.Add("tree_id", "39400")
  185. req.Header.Set("X-Authorization-Token", token)
  186. req.URL.RawQuery = q.Encode()
  187. req.Header.Set("Content-Type", "application/json")
  188. req.Header.Set("X-Authorization-Token", token)
  189. if err = d.httpClient.Do(c, req, &responseBody); err != nil {
  190. log.Error("d.Job url(%s) res($s) error(%v)", url, err)
  191. err = ecode.MelloiPaasRequestErr
  192. return
  193. }
  194. return
  195. }
  196. //ForceDeleteJob Force Delete Job
  197. func (d *Dao) ForceDeleteJob(reportSuId int) (jobList *model.JobInfoList, err error) {
  198. jobList = &model.JobInfoList{}
  199. err = d.DB.Table("ptest_job").Select("host_ip, job_name").Where("report_su_id = ?", reportSuId).Find(&jobList.JobList).Error
  200. return
  201. }
  202. //UpdateJobStatus Update Job Status
  203. func (d *Dao) UpdateJobStatus(reportSuId int) (err error) {
  204. if err = d.DB.Model(&model.PtestJob{}).Where("report_su_id = ?", reportSuId).Update("active", -1).Error; err != nil {
  205. return
  206. }
  207. return d.DB.Table("report_summary").Where("id = ?", reportSuId).Update("test_status", 3).Error
  208. }