/
build.go
112 lines (87 loc) · 2.56 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the URIs of this project regarding your
// rights to use or distribute this software.
package syplugin
import (
"fmt"
"sync"
"github.com/sylabs/singularity/internal/pkg/build/types"
"github.com/sylabs/singularity/internal/pkg/sylog"
)
var registeredBuildPlugins BuildPluginRegistry
func init() {
registeredBuildPlugins = BuildPluginRegistry{
Plugins: make(map[string]BuildPlugin),
}
}
// BasePluginRegistry ...
type BasePluginRegistry struct {
sync.Mutex
}
// BuildPluginRegistry ...
type BuildPluginRegistry struct {
BasePluginRegistry
Plugins map[string]BuildPlugin
}
// RegisterBuildPlugin adds the plugin to the known plugins
func RegisterBuildPlugin(_pl interface{}) error {
pl, ok := _pl.(BuildPlugin)
if !ok {
return nil
}
registeredBuildPlugins.Lock()
defer registeredBuildPlugins.Unlock()
if _, ok := registeredBuildPlugins.Plugins[pl.Name()]; ok {
return fmt.Errorf("plugin name already registered: %s", pl.Name())
}
registeredBuildPlugins.Plugins[pl.Name()] = pl
return nil
}
// GetBuildPlugins returns the list of known plugins
func GetBuildPlugins() map[string]BuildPlugin {
registeredBuildPlugins.Lock()
defer registeredBuildPlugins.Unlock()
return registeredBuildPlugins.Plugins
}
// BuildHandleSections runs the HandleSection() hook on every plugin
func BuildHandleSections(i, s string) {
var plwait sync.WaitGroup
for name, pl := range GetBuildPlugins() {
plwait.Add(1)
go func(name string, pl BuildPlugin) {
defer plwait.Done()
sylog.Debugf("Running %s plugin: HandleSection() hook", name)
pl.HandleSection(i, s)
}(name, pl)
}
plwait.Wait()
}
// BuildHandleBundles runs the HandleBundle() hook on every plugin
func BuildHandleBundles(b *types.Bundle) {
var plwait sync.WaitGroup
for name, pl := range GetBuildPlugins() {
plwait.Add(1)
go func(name string, pl BuildPlugin) {
defer plwait.Done()
sylog.Debugf("Running %s plugin: HandleBundle() hook", name)
pl.HandleBundle(b)
}(name, pl)
}
plwait.Wait()
}
// BuildHandlePosts runs the HandleBundle() hook on every plugin
func BuildHandlePosts() (ret string) {
for name, pl := range GetBuildPlugins() {
sylog.Debugf("Running %s plugin: HandlePost() hook", name)
ret += pl.HandlePost()
}
return
}
// BuildPlugin is the interface for plugins on the build system
type BuildPlugin interface {
Name() string
HandleSection(string, string)
HandleBundle(*types.Bundle)
HandlePost() string
}