|
| 1 | +/* |
| 2 | + * === This file is part of ALICE O² === |
| 3 | + * |
| 4 | + * Copyright 2021 CERN and copyright holders of ALICE O². |
| 5 | + * Author: Teo Mrnjavac <teo.mrnjavac@cern.ch> |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + * In applying this license CERN does not waive the privileges and |
| 21 | + * immunities granted to it by virtue of its status as an |
| 22 | + * Intergovernmental Organization or submit itself to any jurisdiction. |
| 23 | + */ |
| 24 | + |
| 25 | +package workflow |
| 26 | + |
| 27 | +import ( |
| 28 | + "errors" |
| 29 | + "strings" |
| 30 | + texttemplate "text/template" |
| 31 | + |
| 32 | + "github.com/AliceO2Group/Control/configuration/template" |
| 33 | + "github.com/AliceO2Group/Control/core/repos" |
| 34 | + "github.com/AliceO2Group/Control/core/task" |
| 35 | + "github.com/AliceO2Group/Control/core/the" |
| 36 | +) |
| 37 | + |
| 38 | +// An includeRole is a delayed aggregatorRole |
| 39 | +// It takes an `include` entry, which is then expanded into a full aggregatorRole |
| 40 | +// during the ProcessTemplates function. |
| 41 | +type includeRole struct { |
| 42 | + aggregatorRole |
| 43 | + |
| 44 | + Include string `yaml:"include,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +func (r *includeRole) UnmarshalYAML(unmarshal func(interface{}) error) (err error) { |
| 48 | + // NOTE: see NOTE in roleBase.UnmarshalYAML |
| 49 | + |
| 50 | + innerRoleBase := roleBase{} |
| 51 | + err = unmarshal(&innerRoleBase) |
| 52 | + if err != nil { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + type auxInclude struct{ |
| 57 | + Include string `yaml:"include,omitempty"` |
| 58 | + } |
| 59 | + _auxInclude := auxInclude{} |
| 60 | + err = unmarshal(&_auxInclude) |
| 61 | + if err != nil { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + role := includeRole{ |
| 66 | + aggregatorRole: aggregatorRole{ |
| 67 | + roleBase: innerRoleBase, |
| 68 | + aggregator: aggregator{}, |
| 69 | + }, |
| 70 | + Include: _auxInclude.Include, |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + *r = role |
| 75 | + return |
| 76 | +} |
| 77 | + |
| 78 | +func (r *includeRole) MarshalYAML() (interface{}, error) { |
| 79 | + return nil, nil |
| 80 | +} |
| 81 | + |
| 82 | +func (r *includeRole) ProcessTemplates(workflowRepo *repos.Repo, loadSubworkflow LoadSubworkflowFunc) (err error) { |
| 83 | + if r == nil { |
| 84 | + return errors.New("role tree error when processing templates") |
| 85 | + } |
| 86 | + |
| 87 | + templSequence := template.Sequence{ |
| 88 | + template.STAGE0: template.WrapMapItems(r.Defaults.Raw()), |
| 89 | + template.STAGE1: template.WrapMapItems(r.Vars.Raw()), |
| 90 | + template.STAGE2: template.WrapMapItems(r.UserVars.Raw()), |
| 91 | + template.STAGE3: template.Fields{ |
| 92 | + template.WrapPointer(&r.Name), |
| 93 | + template.WrapPointer(&r.Include), |
| 94 | + }, |
| 95 | + template.STAGE4: append(append( |
| 96 | + WrapConstraints(r.Constraints), |
| 97 | + r.wrapConnectFields()...), |
| 98 | + template.WrapPointer(&r.Enabled)), |
| 99 | + } |
| 100 | + |
| 101 | + // TODO: push cached templates here |
| 102 | + err = templSequence.Execute(the.ConfSvc(), r.GetPath(), template.VarStack{ |
| 103 | + Locals: r.Locals, |
| 104 | + Defaults: r.Defaults, |
| 105 | + Vars: r.Vars, |
| 106 | + UserVars: r.UserVars, |
| 107 | + }, r.makeBuildObjectStackFunc(), make(map[string]texttemplate.Template)) |
| 108 | + if err != nil { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + // After template processing we write the Locals to Vars in order to make them available to children |
| 113 | + for k, v := range r.Locals { |
| 114 | + r.Vars.Set(k, v) |
| 115 | + } |
| 116 | + |
| 117 | + r.Enabled = strings.TrimSpace(r.Enabled) |
| 118 | + |
| 119 | + // Common part done, include resolution starts here. |
| 120 | + // An includeRole is essentially a baseRole + `include:` expression. We first need to resolve |
| 121 | + // the expression to obtain a full subworkflow template identifier, i.e. a full repo/wft/branch |
| 122 | + // combo. |
| 123 | + // Once that's done we can load the subworkflow and obtain the root `aggregatorRole` plus a new |
| 124 | + // repos.Repo definition. If a repo or branch is already provided in the subworkflow expression |
| 125 | + // then the returned newWfRepo will reflect this, and any additionally nested includes will |
| 126 | + // default to the repo of their direct parent. |
| 127 | + var subWfRoot *aggregatorRole |
| 128 | + var newWfRepo *repos.Repo |
| 129 | + include := workflowRepo.ResolveSubworkflowTemplateIdentifier(r.Include) |
| 130 | + subWfRoot, newWfRepo, err = loadSubworkflow(include, r) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + // By now the subworkflow is loaded and reparented to this includeRole. This reparenting is |
| 136 | + // needed to ensure the correct gera.StringMap hierarchies, but now that we replace the |
| 137 | + // composed aggregatorRole with the newly loaded one, we must also fix the reparenting and |
| 138 | + // ensure the loaded name doesn't overwrite the original name of the includeRole. |
| 139 | + parent := r.parent |
| 140 | + name := r.Name |
| 141 | + r.aggregatorRole = *subWfRoot // The previously composed aggregatorRole+roleBase are overwritten here |
| 142 | + r.parent = parent |
| 143 | + r.Name = name |
| 144 | + |
| 145 | + // Process templates for child roles |
| 146 | + for _, role := range r.Roles { |
| 147 | + role.setParent(r) |
| 148 | + err = role.ProcessTemplates(newWfRepo, loadSubworkflow) |
| 149 | + if err != nil { |
| 150 | + return |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // If any child is not Enabled after template resolution, |
| 155 | + // we filter it out of existence |
| 156 | + enabledRoles := make([]Role, 0) |
| 157 | + for _, role := range r.Roles { |
| 158 | + if role.IsEnabled() { |
| 159 | + enabledRoles = append(enabledRoles, role) |
| 160 | + } |
| 161 | + } |
| 162 | + r.Roles = enabledRoles |
| 163 | + |
| 164 | + return |
| 165 | +} |
| 166 | + |
| 167 | +func (r* includeRole) UpdateStatus(s task.Status) { |
| 168 | + r.updateStatus(s) |
| 169 | +} |
| 170 | + |
| 171 | +func (r* includeRole) UpdateState(s task.State) { |
| 172 | + r.updateState(s) |
| 173 | +} |
| 174 | + |
| 175 | +func (r *includeRole) copy() copyable { |
| 176 | + rCopy := includeRole{ |
| 177 | + aggregatorRole: *r.aggregatorRole.copy().(*aggregatorRole), |
| 178 | + Include: r.Include, |
| 179 | + } |
| 180 | + rCopy.status = SafeStatus{status:rCopy.GetStatus()} |
| 181 | + rCopy.state = SafeState{state:rCopy.GetState()} |
| 182 | + return &rCopy |
| 183 | +} |
0 commit comments