Skip to content

Commit

Permalink
fix: templates: Multiple templates import no longer breaks when depen…
Browse files Browse the repository at this point in the history
…ds of each others in sub-task pluggin (#79)

Fixes #74

Signed-off-by: Romain Beuque <[email protected]>
Signed-off-by: Romain Beuque <[email protected]>
  • Loading branch information
rbeuque74 authored and loopfz committed Jan 22, 2020
1 parent dabc1b4 commit e2e4c7a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
15 changes: 14 additions & 1 deletion models/tasktemplate/fromdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import (
"github.com/ghodss/yaml"
"github.com/juju/errors"
"github.com/loopfz/gadgeto/zesty"
"github.com/ovh/utask/pkg/templateimport"
"github.com/sirupsen/logrus"
)

var (
discoveredTemplates []TaskTemplate = []TaskTemplate{}
)

// LoadFromDir reads yaml-formatted task templates
// from a folder and upserts them in database
func LoadFromDir(dbp zesty.DBProvider, dir string) error {
Expand All @@ -31,8 +36,15 @@ func LoadFromDir(dbp zesty.DBProvider, dir string) error {
if err := yaml.Unmarshal(tmpl, &tt); err != nil {
return fmt.Errorf("failed to unmarshal '%s': '%s'", file.Name(), err)
}
verb := "Created"

tt.Normalize()

discoveredTemplates = append(discoveredTemplates, tt)
templateimport.AddTemplate(tt.Name)
}

for _, tt := range discoveredTemplates {
verb := "Created"
existing, err := LoadFromName(dbp, tt.Name)
if err != nil {
if !errors.IsNotFound(err) {
Expand All @@ -50,5 +62,6 @@ func LoadFromDir(dbp zesty.DBProvider, dir string) error {
}
logrus.Infof("%s task template '%s'", verb, tt.Name)
}
templateimport.CleanTemplates()
return nil
}
12 changes: 10 additions & 2 deletions pkg/plugins/builtin/subtask/subtask.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ovh/utask/models/tasktemplate"
"github.com/ovh/utask/pkg/auth"
"github.com/ovh/utask/pkg/plugins/taskplugin"
"github.com/ovh/utask/pkg/templateimport"
"github.com/ovh/utask/pkg/utils"
)

Expand Down Expand Up @@ -54,11 +55,18 @@ func validConfig(config interface{}) error {
}

_, err = tasktemplate.LoadFromName(dbp, cfg.Template)
if err != nil {
if err != nil && !errors.IsNotFound(err) {
return err
}

return nil
templates := templateimport.GetTemplates()
for _, template := range templates {
if template == cfg.Template {
return nil
}
}

return errors.NotFoundf("sub-task template %q", cfg.Template)
}

func exec(stepName string, config interface{}, ctx interface{}) (interface{}, interface{}, error) {
Expand Down
32 changes: 32 additions & 0 deletions pkg/templateimport/templateimport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package templateimport

import (
"sync"
)

var (
mu sync.Mutex
discoveredTemplates []string = []string{}
)

// AddTemplate registers a template name currently being imported
func AddTemplate(templateName string) {
mu.Lock()
defer mu.Unlock()
discoveredTemplates = append(discoveredTemplates, templateName)
}

// GetTemplates returns template names currently being imported.
// Used when template needs to validate dependency with others templates (check existance, ...)
func GetTemplates() []string {
mu.Lock()
defer mu.Unlock()
return discoveredTemplates
}

// CleanTemplates flushes the template names list being imported.
func CleanTemplates() {
mu.Lock()
defer mu.Unlock()
discoveredTemplates = []string{}
}

0 comments on commit e2e4c7a

Please sign in to comment.