-
Notifications
You must be signed in to change notification settings - Fork 6
/
core.go
161 lines (136 loc) · 3.15 KB
/
core.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package object
import (
"context"
"fmt"
"os"
"sync"
"github.com/google/uuid"
"github.com/opensvc/om3/core/env"
"github.com/opensvc/om3/core/instance"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/core/xconfig"
"github.com/opensvc/om3/util/compliance"
"github.com/opensvc/om3/util/funcopt"
"github.com/opensvc/om3/util/plog"
"github.com/opensvc/om3/util/progress"
)
type (
// core is the base struct embedded in all kinded objects.
core struct {
sync.Mutex
path naming.Path
// private
volatile bool
log *plog.Logger
// caches
id uuid.UUID
configFile string
configData any
config *xconfig.T
node *Node
paths struct {
varDir string
logDir string
tmpDir string
}
// method plugs
postCommit func() error
}
compliancer interface {
NewCompliance() (*compliance.T, error)
}
volatiler interface {
IsVolatile() bool
SetVolatile(v bool)
}
// Core is implemented by all object kinds.
Core interface {
Configurer
compliancer
volatiler
Nodes() ([]string, error)
Path() naming.Path
FQDN() string
FreshStatus(context.Context) (instance.Status, error)
MonitorStatus(context.Context) (instance.Status, error)
Status(context.Context) (instance.Status, error)
}
)
func (t *core) PostCommit() error {
if t.postCommit == nil {
return nil
}
return t.postCommit()
}
func (t *core) SetPostCommit(fn func() error) {
t.postCommit = fn
}
// List returns the stringified path as data
func (t *core) List() (string, error) {
return t.path.String(), nil
}
func (t *core) init(referrer xconfig.Referrer, id any, opts ...funcopt.O) error {
if parsed, err := toPathType(id); err != nil {
return err
} else {
t.path = parsed
}
if err := funcopt.Apply(t, opts...); err != nil {
return err
}
t.log = naming.LogWithPath(plog.NewDefaultLogger(), t.path).WithPrefix(fmt.Sprintf("instance: %s: ", t.path))
if v := os.Getenv(env.ActionOrchestrationIDVar); v != "" {
t.log = t.log.Attr("ORCHESTRATION_ID", v)
}
if err := t.loadConfig(referrer); err != nil {
return err
}
return nil
}
func (t *core) String() string {
return t.path.String()
}
func (t *core) SetVolatile(v bool) {
t.volatile = v
}
func (t *core) IsVolatile() bool {
return t.volatile
}
func (t *core) Path() naming.Path {
return t.path
}
// ConfigFile returns the absolute path of an opensvc object configuration
// file.
func (t *core) ConfigFile() string {
if t.configFile == "" {
t.configFile = t.path.ConfigFile()
}
return t.configFile
}
// Node returns a cache Node struct pointer. If none is already cached,
// allocate a new Node{} and cache it.
func (t *core) Node() (*Node, error) {
if t.node != nil {
return t.node, nil
}
if n, err := NewNode(); err != nil {
return nil, err
} else {
t.node = n
return t.node, nil
}
}
func (t *core) Log() *plog.Logger {
return t.log
}
func (t *core) ProgressKey() []string {
p := rawconfig.Colorize.Bold(t.path.String())
return []string{p}
}
func (t *core) Progress(ctx context.Context, cols ...any) {
if view := progress.ViewFromContext(ctx); view != nil {
key := t.ProgressKey()
view.Info(key, cols)
}
}