task.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package executor
  2. import (
  3. "path/filepath"
  4. "strings"
  5. fw "git.buran.team/main/fairwind"
  6. cbcdocker "git.buran.team/main/cbc/docker"
  7. cbcpreprocessor "git.buran.team/main/cbc/preprocessor"
  8. cbcscheme "git.buran.team/main/cbc/scheme"
  9. cbctemplate "git.buran.team/main/cbc/template"
  10. )
  11. type Task struct {
  12. global *Global
  13. ticket *Ticket
  14. scheme cbcscheme.Task
  15. }
  16. func NewTask(global *Global, ticket *Ticket, taskScheme cbcscheme.Task) (*Task, error) {
  17. return &Task{
  18. global: global,
  19. ticket: ticket,
  20. scheme: taskScheme,
  21. }, nil
  22. }
  23. func (this *Task) Execute() *TaskResult {
  24. result := &TaskResult{
  25. Success: false,
  26. Template: false,
  27. Build: Result{},
  28. Preprocessors: map[string]*Result{},
  29. }
  30. for _, preprocessorScheme := range this.scheme.Preprocessors {
  31. result.Preprocessors[preprocessorScheme.Kind] = &Result{
  32. Success: false,
  33. Stdout: []string{},
  34. Stderr: []string{},
  35. }
  36. }
  37. // Template
  38. files := cbctemplate.Files{}
  39. for _, fileSpecification := range this.scheme.Files {
  40. files[fileSpecification.Path] = fileSpecification.Content
  41. }
  42. values := cbctemplate.Values{}
  43. for _, valueSpecification := range this.scheme.Variables {
  44. values[valueSpecification.Key] = valueSpecification.Value
  45. }
  46. path, err := cbctemplate.Copy(
  47. filepath.Join("template", this.scheme.Template),
  48. files,
  49. values,
  50. )
  51. if err != nil {
  52. return result
  53. } else {
  54. result.Template = true
  55. }
  56. defer cbctemplate.Delete(path)
  57. // Preproces
  58. for _, preprocessorScheme := range this.scheme.Preprocessors {
  59. preprocessor, err := cbcpreprocessor.NewPreprocessor(preprocessorScheme)
  60. if err != nil {
  61. this.global.Log.Error("can't preprocess task", fw.LogError(err))
  62. return result
  63. }
  64. stdout, stderr, err := preprocessor.Process(path)
  65. result.Preprocessors[preprocessorScheme.Kind].Stdout = strings.Split(string(stdout), "\n")
  66. result.Preprocessors[preprocessorScheme.Kind].Stderr = strings.Split(string(stderr), "\n")
  67. if err != nil {
  68. this.global.Log.Error("can't preprocess task", fw.LogError(err))
  69. return result
  70. } else {
  71. result.Preprocessors[preprocessorScheme.Kind].Success = true
  72. }
  73. }
  74. // Image
  75. image := cbcdocker.NewImage(
  76. this.global.Ctx,
  77. this.global.Log,
  78. this.global.Docker,
  79. this.global.Registry,
  80. this.scheme.Image.Tag,
  81. this.scheme.Image.Version,
  82. this.scheme.Image.Build,
  83. this.scheme.Image.Push,
  84. )
  85. // Vendor
  86. // TODO: ...
  87. // Build
  88. stdout, err := image.Build(this.scheme.Timeout.Build, path)
  89. result.Build.Stdout = strings.Split(string(stdout), "\n")
  90. result.Build.Stderr = []string{}
  91. if err != nil {
  92. this.global.Log.Error("can't build image", fw.LogError(err))
  93. return result
  94. } else {
  95. result.Build.Success = true
  96. }
  97. // Push
  98. stdout, err = image.Push(this.scheme.Timeout.Push)
  99. result.Push.Stdout = strings.Split(string(stdout), "\n")
  100. result.Push.Stderr = []string{}
  101. if err != nil {
  102. this.global.Log.Error("can't push image", fw.LogError(err))
  103. return result
  104. } else {
  105. result.Push.Success = true
  106. }
  107. // Done
  108. result.Success = true
  109. return result
  110. }