package executor import ( "path/filepath" "strings" fw "git.buran.team/main/fairwind" cbcdocker "git.buran.team/main/cbc/docker" cbcpreprocessor "git.buran.team/main/cbc/preprocessor" cbcscheme "git.buran.team/main/cbc/scheme" cbctemplate "git.buran.team/main/cbc/template" ) type Task struct { global *Global ticket *Ticket scheme cbcscheme.Task } func NewTask(global *Global, ticket *Ticket, taskScheme cbcscheme.Task) (*Task, error) { return &Task{ global: global, ticket: ticket, scheme: taskScheme, }, nil } func (this *Task) Execute() *TaskResult { result := &TaskResult{ Success: false, Template: false, Build: Result{}, Preprocessors: map[string]*Result{}, } for _, preprocessorScheme := range this.scheme.Preprocessors { result.Preprocessors[preprocessorScheme.Kind] = &Result{ Success: false, Stdout: []string{}, Stderr: []string{}, } } // Template files := cbctemplate.Files{} for _, fileSpecification := range this.scheme.Files { files[fileSpecification.Path] = fileSpecification.Content } values := cbctemplate.Values{} for _, valueSpecification := range this.scheme.Variables { values[valueSpecification.Key] = valueSpecification.Value } path, err := cbctemplate.Copy( filepath.Join("template", this.scheme.Template), files, values, ) if err != nil { return result } else { result.Template = true } defer cbctemplate.Delete(path) // Preproces for _, preprocessorScheme := range this.scheme.Preprocessors { preprocessor, err := cbcpreprocessor.NewPreprocessor(preprocessorScheme) if err != nil { this.global.Log.Error("can't preprocess task", fw.LogError(err)) return result } stdout, stderr, err := preprocessor.Process(path) result.Preprocessors[preprocessorScheme.Kind].Stdout = strings.Split(string(stdout), "\n") result.Preprocessors[preprocessorScheme.Kind].Stderr = strings.Split(string(stderr), "\n") if err != nil { this.global.Log.Error("can't preprocess task", fw.LogError(err)) return result } else { result.Preprocessors[preprocessorScheme.Kind].Success = true } } // Image image := cbcdocker.NewImage( this.global.Ctx, this.global.Log, this.global.Docker, this.global.Registry, this.scheme.Image.Tag, this.scheme.Image.Version, this.scheme.Image.Build, this.scheme.Image.Push, ) // Vendor // TODO: ... // Build stdout, err := image.Build(this.scheme.Timeout.Build, path) result.Build.Stdout = strings.Split(string(stdout), "\n") result.Build.Stderr = []string{} if err != nil { this.global.Log.Error("can't build image", fw.LogError(err)) return result } else { result.Build.Success = true } // Push stdout, err = image.Push(this.scheme.Timeout.Push) result.Push.Stdout = strings.Split(string(stdout), "\n") result.Push.Stderr = []string{} if err != nil { this.global.Log.Error("can't push image", fw.LogError(err)) return result } else { result.Push.Success = true } // Done result.Success = true return result }