forked from projecteru2/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcium.go
80 lines (70 loc) · 2.07 KB
/
calcium.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
package calcium
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/projecteru2/core/cluster"
"github.com/projecteru2/core/discovery"
"github.com/projecteru2/core/discovery/helium"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/scheduler"
complexscheduler "github.com/projecteru2/core/scheduler/complex"
"github.com/projecteru2/core/source"
"github.com/projecteru2/core/source/github"
"github.com/projecteru2/core/source/gitlab"
"github.com/projecteru2/core/store"
"github.com/projecteru2/core/store/etcdv3"
"github.com/projecteru2/core/types"
)
// Calcium implement the cluster
type Calcium struct {
config types.Config
store store.Store
scheduler scheduler.Scheduler
source source.Source
watcher discovery.Service
wal *WAL
}
// New returns a new cluster config
func New(config types.Config, embeddedStorage bool) (*Calcium, error) {
logger := log.WithField("Calcium", "New").WithField("config", config)
// set store
store, err := etcdv3.New(config, embeddedStorage)
if err != nil {
return nil, logger.Err(errors.WithStack(err))
}
// set scheduler
potassium, err := complexscheduler.New(config)
if err != nil {
return nil, logger.Err(errors.WithStack(err))
}
scheduler.InitSchedulerV1(potassium)
// set scm
var scm source.Source
scmtype := strings.ToLower(config.Git.SCMType)
switch scmtype {
case cluster.Gitlab:
scm, err = gitlab.New(config)
case cluster.Github:
scm, err = github.New(config)
default:
log.Warn("[Calcium] SCM not set, build API disabled")
}
if err != nil {
logger.Errorf("[Calcium] SCAM failed: %+v", err)
return nil, errors.WithStack(err)
}
// set watcher
watcher := helium.New(config.GRPCConfig, store)
cal := &Calcium{store: store, config: config, scheduler: potassium, source: scm, watcher: watcher}
cal.wal, err = newCalciumWAL(cal)
return cal, logger.Err(errors.WithStack(err))
}
// DisasterRecover .
func (c *Calcium) DisasterRecover(ctx context.Context) {
c.wal.Recover(ctx)
}
// Finalizer use for defer
func (c *Calcium) Finalizer() {
c.store.TerminateEmbededStorage()
}