/
manager.go
110 lines (90 loc) · 3.12 KB
/
manager.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package actionexecutor
import (
"github.com/erda-project/erda/modules/pipeline/pipengine/actionexecutor/types"
"github.com/erda-project/erda/modules/pipeline/spec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// Manager is an executor manager, it holds the all executor instances.
type Manager struct {
factory map[types.Kind]types.CreateFn
executors map[types.Name]types.ActionExecutor
}
var mgr Manager
func GetManager() *Manager {
return &mgr
}
func (m *Manager) Initialize(cfgs chan spec.ActionExecutorConfig) error {
m.factory = types.Factory
m.executors = make(map[types.Name]types.ActionExecutor)
logrus.Info("pipengine action executor manager Initialize ...")
for len(cfgs) > 0 {
c := <-cfgs
create, ok := m.factory[types.Kind(c.Kind)]
if !ok {
return errors.Errorf("unregistered action executor kind: %v", c.Kind)
}
name := types.Name(c.Name)
for k, v := range c.Options {
logrus.Infof("=> kind [%s], name [%s], option: %s=%s", c.Kind, c.Name, k, v)
}
actionExecutor, err := create(name, c.Options)
if err != nil {
logrus.Infof("=> kind [%s], name [%s], created failed, err: %v", c.Kind, c.Name, err)
return err
}
m.executors[name] = actionExecutor
logrus.Infof("=> kind [%s], name [%s], created", c.Kind, c.Name)
}
logrus.Info("pipengine action executor manager Initialize Done .")
return nil
}
// Get returns the executor with name.
func (m *Manager) Get(name types.Name) (types.ActionExecutor, error) {
if len(name) == 0 {
return nil, errors.Errorf("executor name is empty")
}
e, ok := m.executors[name]
if !ok {
return nil, errors.Errorf("not found action executor [%s]", name)
}
return e, nil
}
// GetByKind returns the executor instances with specify kind.
func (m *Manager) GetByKind(kind types.Kind) []types.ActionExecutor {
executors := make([]types.ActionExecutor, 0, len(m.executors))
for _, e := range m.executors {
if e.Kind() == kind {
executors = append(executors, e)
}
}
return executors
}
// ListExecutors returns the all executor instances.
func (m *Manager) ListExecutors() []types.ActionExecutor {
executors := make([]types.ActionExecutor, 0, len(m.executors))
for _, e := range m.executors {
executors = append(executors, e)
}
return executors
}
// GetActionExecutorKindByName return executor Kind, e.g. MEMORY
func (m *Manager) GetActionExecutorKindByName(name string) (string, error) {
e, err := m.Get(types.Name(name))
if err != nil {
return "", errors.Wrapf(err, "failed to get action executor by name [%s]", name)
}
return string(e.Kind()), nil
}