merlin.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package model
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/library/ecode"
  7. )
  8. // Env Env.
  9. type Env struct {
  10. ClusterID int64 `json:"cluster_id"`
  11. NetworkID int64 `json:"network_id"`
  12. }
  13. // Node Node.
  14. type Node struct {
  15. BusinessUnit string `json:"business_unit"`
  16. Project string `json:"project"`
  17. App string `json:"app"`
  18. TreeID int64 `json:"tree_id"`
  19. }
  20. // GenMachinesRequest Gen Machines Request.
  21. type GenMachinesRequest struct {
  22. Env
  23. PaasMachine
  24. PaasMachineSystem
  25. Nodes []*Node `json:"nodes"`
  26. Comment string `json:"comment"`
  27. Amount int `json:"amount"`
  28. }
  29. // Verify verify GenMachinesRequest.
  30. func (g *GenMachinesRequest) Verify() error {
  31. if g.Amount < 1 {
  32. return ecode.MerlinInvalidMachineAmountErr
  33. }
  34. l := len(g.Nodes)
  35. if l < 1 || l > 10 {
  36. return ecode.MerlinInvalidNodeAmountErr
  37. }
  38. return nil
  39. }
  40. // Mutator Mutator.
  41. func (g *GenMachinesRequest) Mutator(m *Machine) {
  42. m.BusinessUnit = g.Nodes[0].BusinessUnit
  43. m.Project = g.Nodes[0].Project
  44. m.App = g.Nodes[0].App
  45. m.ClusterID = g.ClusterID
  46. m.NetworkID = g.NetworkID
  47. m.Comment = g.Comment
  48. }
  49. // ToMachineNode to machine node.
  50. func (g *GenMachinesRequest) ToMachineNode(mID int64) (treeNodes []*MachineNode) {
  51. for _, node := range g.Nodes {
  52. treeNodes = append(treeNodes, &MachineNode{
  53. MachineID: mID,
  54. BusinessUnit: node.BusinessUnit,
  55. Project: node.Project,
  56. App: node.App,
  57. TreeID: node.TreeID,
  58. })
  59. }
  60. return
  61. }
  62. // PaasMachineSystem Paas Machine System.
  63. type PaasMachineSystem struct {
  64. Command string `json:"command"`
  65. Envs []*EnvVariable `json:"envs"`
  66. HostAlias []*Host `json:"host_alias"`
  67. }
  68. // Host Host.
  69. type Host struct {
  70. IP string `json:"ip"`
  71. Hostnames []string `json:"hostnames"`
  72. }
  73. // EnvVariable EnvVariable.
  74. type EnvVariable struct {
  75. Name string `json:"name"`
  76. Value string `json:"value"`
  77. }
  78. // GenMachine GenMachine.
  79. type GenMachine struct {
  80. Machine
  81. IP string `json:"ip"`
  82. ClusterName string `json:"cluster_name"`
  83. NetworkName string `json:"network_name"`
  84. }
  85. // MachineDetail Machine Detail.
  86. type MachineDetail struct {
  87. Machine
  88. PaasMachineDetail
  89. Nodes []*MachineNode `json:"nodes"`
  90. NetworkName string `json:"network_name"`
  91. Name string `json:"name"`
  92. IsSnapShot bool `json:"is_support_snapshot"`
  93. }
  94. // PaginateMachine Paginate Machine.
  95. type PaginateMachine struct {
  96. Total int64 `json:"total"`
  97. PageNum int `json:"page_num"`
  98. PageSize int `json:"page_size"`
  99. Machines []GenMachine `json:"machines"`
  100. }
  101. // PaginateMachineLog Paginate Machine Log.
  102. type PaginateMachineLog struct {
  103. Total int64 `json:"total"`
  104. PageNum int `json:"page_num"`
  105. PageSize int `json:"page_size"`
  106. MachineLogs []*AboundMachineLog `json:"machine_logs"`
  107. }
  108. // PaginateMobileMachineLog Paginate Mobile Machine Log.
  109. type PaginateMobileMachineLog struct {
  110. Total int64 `json:"total"`
  111. PageNum int `json:"page_num"`
  112. PageSize int `json:"page_size"`
  113. MachineLogs []*AboundMobileMachineLog `json:"machine_logs"`
  114. }
  115. // PaginateMobileMachineLendOutLog Paginate Mobile Machine Lend Out Log.
  116. type PaginateMobileMachineLendOutLog struct {
  117. Total int64 `json:"total"`
  118. PageNum int `json:"page_num"`
  119. PageSize int `json:"page_size"`
  120. MachineLendOutRecords []*MachineLendOutRecord `json:"machine_lendout_records"`
  121. }
  122. // MachineLendOutRecord Machine Lend Out Record.
  123. type MachineLendOutRecord struct {
  124. MachineID int64 `json:"machine_id"`
  125. Lender string `json:"lender"`
  126. Status int `json:"status"`
  127. LendTime time.Time `json:"lend_time"`
  128. EnableReturn bool `json:"enable_return"`
  129. }
  130. // PaginateMobileMachineErrorLog Paginate Mobile Machine Error Log.
  131. type PaginateMobileMachineErrorLog struct {
  132. Total int64 `json:"total"`
  133. PageNum int `json:"page_num"`
  134. PageSize int `json:"page_size"`
  135. MachineLogs []*MobileMachineErrorLog `json:"machine_logs"`
  136. }
  137. // PaginateApplicationRecord Paginate Application Record.
  138. type PaginateApplicationRecord struct {
  139. Total int64 `json:"total"`
  140. PageNum int `json:"page_num"`
  141. PageSize int `json:"page_size"`
  142. ApplicationRecords []*ApplicationRecord `json:"application_records"`
  143. }
  144. // ToPaasGenMachineRequest to Paas GenMachine Request.
  145. func (g GenMachinesRequest) ToPaasGenMachineRequest(machineLimitRatio float32) *PaasGenMachineRequest {
  146. var (
  147. pms = make([]PaasMachine, g.Amount)
  148. treeIDs []int64
  149. fn = g.Nodes[0]
  150. )
  151. g.CPULimit = g.CPURequest * CPURatio
  152. g.MemoryLimit = g.MemoryRequest * MemoryRatio
  153. g.CPURequest = g.CPURequest * CPURatio / machineLimitRatio
  154. g.MemoryRequest = g.MemoryRequest * MemoryRatio / machineLimitRatio
  155. for i := 0; i < g.Amount; i++ {
  156. pms[i] = g.PaasMachine
  157. pms[i].Name = g.Name + "-" + strconv.Itoa(i+1)
  158. pms[i].PaasMachineSystem = g.PaasMachineSystem
  159. }
  160. for _, node := range g.Nodes {
  161. treeIDs = append(treeIDs, node.TreeID)
  162. }
  163. return &PaasGenMachineRequest{
  164. BusinessUnit: fn.BusinessUnit,
  165. Project: fn.Project,
  166. App: fn.App,
  167. TreeIDs: treeIDs,
  168. Env: g.Env,
  169. Machines: pms,
  170. }
  171. }
  172. // Pagination Pagination.
  173. type Pagination struct {
  174. PageSize int `form:"page_size" json:"page_size"`
  175. PageNum int `form:"page_num" json:"page_num"`
  176. }
  177. // Verify verify the value of pageNum and pageSize.
  178. func (p *Pagination) Verify() error {
  179. if p.PageNum < 0 {
  180. return ecode.MerlinIllegalPageNumErr
  181. } else if p.PageNum == 0 {
  182. p.PageNum = DefaultPageNum
  183. }
  184. if p.PageSize < 0 {
  185. return ecode.MerlinIllegalPageSizeErr
  186. } else if p.PageSize == 0 {
  187. p.PageSize = DefaultPageSize
  188. }
  189. return nil
  190. }
  191. // QueryMachineRequest Query Machine Request.
  192. type QueryMachineRequest struct {
  193. Pagination
  194. TreeNode
  195. MachineName string `form:"machine_name"`
  196. Username string `form:"username"`
  197. Requester string
  198. }
  199. // QueryMachineLogRequest Query Machine Log Request.
  200. type QueryMachineLogRequest struct {
  201. Pagination
  202. MachineID int64 `form:"machine_id"`
  203. MachineName string `form:"machine_name"`
  204. OperateUser string `form:"operate_user"`
  205. OperateType string `form:"operate_type"`
  206. }
  207. // QueryMobileMachineLogRequest Query Mobile Machine Log Request.
  208. type QueryMobileMachineLogRequest struct {
  209. Pagination
  210. MachineID int64 `form:"machine_id"`
  211. Serial string `form:"serial"`
  212. OperateUser string `form:"operate_user"`
  213. OperateType string `form:"operate_type"`
  214. }
  215. // QueryMobileMachineErrorLogRequest Query Mobile Machine Error Log Request.
  216. type QueryMobileMachineErrorLogRequest struct {
  217. Pagination
  218. MachineID int64 `form:"machine_id"`
  219. }
  220. // AboundMachineLog Abound Machine Log.
  221. type AboundMachineLog struct {
  222. MachineLog
  223. Name string `json:"machine_name"`
  224. }
  225. // AboundMobileMachineLog Abound mobile Machine Log.
  226. type AboundMobileMachineLog struct {
  227. MobileMachineLog
  228. Serial string `json:"serial"`
  229. }
  230. // ApplyEndTimeRequest Apply End Time Request.
  231. type ApplyEndTimeRequest struct {
  232. MachineID int64 `json:"machine_id"`
  233. ApplyEndTime string `json:"apply_end_time"`
  234. Auditor string `json:"auditor"`
  235. }
  236. // AuditEndTimeRequest Audit End Time Request.
  237. type AuditEndTimeRequest struct {
  238. AuditID int64 `json:"audit_id"`
  239. AuditResult bool `json:"audit_result"`
  240. Comment string `json:"comment"`
  241. }
  242. // BeforeDelMachineFunc Before DelMachine Func.
  243. type BeforeDelMachineFunc func(c context.Context, id int64, username string) error
  244. // MachineStatusResponse Machine Status Response.
  245. type MachineStatusResponse struct {
  246. Initialized string `json:"initialized"`
  247. PodScheduled string `json:"pod_scheduled"`
  248. Ready string `json:"ready"`
  249. SynTree string `json:"syn_tree"`
  250. RetryCount int `json:"retry_count"`
  251. Log string `json:"log"`
  252. MachineEvent []string `json:"events"`
  253. }
  254. // CreatingMachineStatus Creating Machine Status.
  255. func (msr *MachineStatusResponse) CreatingMachineStatus() int {
  256. if msr.Initialized == False {
  257. return CreatingMachineInMerlin
  258. }
  259. if msr.PodScheduled == False {
  260. return InitializeMachineInMerlin
  261. }
  262. if msr.Ready == False {
  263. return ReadyMachineInMerlin
  264. }
  265. if msr.SynTree == False {
  266. return SynTreeMachineInMerlin
  267. }
  268. return BootMachineInMerlin
  269. }
  270. // FailedMachineStatus Failed Machine Status.
  271. func (msr *MachineStatusResponse) FailedMachineStatus() int {
  272. if msr.Initialized == False {
  273. return InitializedFailedMachineInMerlin
  274. }
  275. if msr.PodScheduled == False {
  276. return PodScheduledFailedMachineInMerlin
  277. }
  278. if msr.Ready == False {
  279. return ReadyFailedMachineInMerlin
  280. }
  281. if msr.SynTree == False {
  282. return SynTreeFailedMachineInMerlin
  283. }
  284. return 0
  285. }
  286. // InstanceMachineStatusResponse Instance Machine Status Response.
  287. func InstanceMachineStatusResponse(machineStatus int) *MachineStatusResponse {
  288. switch machineStatus {
  289. case ImmediatelyFailedMachineInMerlin:
  290. return &MachineStatusResponse{}
  291. case InitializedFailedMachineInMerlin:
  292. return &MachineStatusResponse{Initialized: False, PodScheduled: False, Ready: False, SynTree: False, RetryCount: 0}
  293. case PodScheduledFailedMachineInMerlin:
  294. return &MachineStatusResponse{Initialized: True, PodScheduled: False, Ready: False, SynTree: False, RetryCount: 0}
  295. case ReadyFailedMachineInMerlin:
  296. return &MachineStatusResponse{Initialized: True, PodScheduled: True, Ready: False, SynTree: False, RetryCount: 0}
  297. case SynTreeFailedMachineInMerlin:
  298. return &MachineStatusResponse{Initialized: True, PodScheduled: True, Ready: True, SynTree: False, RetryCount: 0}
  299. case BootMachineInMerlin:
  300. return &MachineStatusResponse{Initialized: True, PodScheduled: True, Ready: True, SynTree: True, RetryCount: 0}
  301. case ShutdownMachineInMerlin:
  302. return &MachineStatusResponse{Initialized: True, PodScheduled: True, Ready: True, SynTree: True, RetryCount: 0}
  303. }
  304. return nil
  305. }
  306. // TreeNode tree node struct for merlin.
  307. type TreeNode struct {
  308. BusinessUnit string `form:"business_unit"`
  309. Project string `form:"project"`
  310. App string `form:"app"`
  311. }
  312. // VerifyFieldValue verify that all fields is not empty.
  313. func (t TreeNode) VerifyFieldValue() (err error) {
  314. if t.BusinessUnit == "" || t.Project == "" || t.App == "" {
  315. err = ecode.MerlinShouldTreeFullPath
  316. }
  317. return
  318. }
  319. // TreePath join all fields.
  320. func (t *TreeNode) TreePath() string {
  321. return t.BusinessUnit + "." + t.Project + "." + t.App
  322. }
  323. // TreePathWithoutEmptyField join all fields except empty.
  324. func (t *TreeNode) TreePathWithoutEmptyField() (treePath string) {
  325. if t.BusinessUnit == "" {
  326. return
  327. }
  328. treePath = "bilibili." + t.BusinessUnit
  329. if t.Project == "" {
  330. return
  331. }
  332. treePath = treePath + "." + t.Project
  333. if t.App == "" {
  334. return
  335. }
  336. treePath = treePath + "." + t.App
  337. return
  338. }
  339. // UpdateMachineNodeRequest request struct for updating machine node.
  340. type UpdateMachineNodeRequest struct {
  341. MachineID int64 `json:"machine_id"`
  342. Nodes []*MachineNode `json:"nodes"`
  343. }
  344. // VerifyNodes verify the Nodes field of UpdateMachineNodeRequest
  345. func (u *UpdateMachineNodeRequest) VerifyNodes() error {
  346. l := len(u.Nodes)
  347. if l < 1 || l > 10 {
  348. return ecode.MerlinInvalidNodeAmountErr
  349. }
  350. return nil
  351. }
  352. // ToMachineNodes convert to machine nodes with injecting machine id.
  353. func (u UpdateMachineNodeRequest) ToMachineNodes() []*MachineNode {
  354. for _, n := range u.Nodes {
  355. n.MachineID = u.MachineID
  356. }
  357. return u.Nodes
  358. }
  359. // QueryMobileDeviceRequest Query Device Farm Request.
  360. type QueryMobileDeviceRequest struct {
  361. Pagination
  362. MobileID int64 `json:"mobile_id"`
  363. Serial string `json:"serial"`
  364. Name string `json:"name"`
  365. Username string `json:"username"`
  366. OwnerName string `json:"owner_name"`
  367. CPU string `json:"cpu"`
  368. Version string `json:"version"`
  369. Mode string `json:"mode"`
  370. Type int `json:"type"`
  371. State string `json:"state"`
  372. Usage int `json:"usage"`
  373. Online bool `json:"online"`
  374. }
  375. // PaginateMobileMachines Paginate Device.
  376. type PaginateMobileMachines struct {
  377. Total int64 `json:"total"`
  378. PageNum int `json:"page_num"`
  379. PageSize int `json:"page_size"`
  380. MobileMachines []*MobileMachineResponse `json:"mobile_devices"`
  381. }
  382. // QueryMachine2ImageLogRequest Query Machine to Image Log Request.
  383. type QueryMachine2ImageLogRequest struct {
  384. Pagination
  385. MachineID int64 `form:"machine_id"`
  386. }
  387. // PaginateHubImageLog Paginate Hub Image Log.
  388. type PaginateHubImageLog struct {
  389. Total int64 `json:"total"`
  390. PageNum int `json:"page_num"`
  391. PageSize int `json:"page_size"`
  392. HubImageLogs []*HubImageLog `json:"hub_image_logs"`
  393. }
  394. // ImageConfiguration Image Configuration.
  395. type ImageConfiguration struct {
  396. ImageFullName string `json:"image_full_name"`
  397. PaasMachineSystem
  398. }