/
component.go
366 lines (321 loc) · 8.94 KB
/
component.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"errors"
"io/fs"
"path/filepath"
"sort"
"sync"
)
// definitions and access methods for the generic component types
type Component string
const (
None Component = ""
Unknown Component = "unknown"
)
type Components struct {
// function to call from 'init' and 'add remote' commands to set-up environment
// arg is the name of the remote
Initialise func(*Remotes)
// function to create a new instance of component
New func(string) Instances
ComponentType Component
RelatedTypes []Component
ComponentMatches []string
RealComponent bool
DownloadBase string
PortRange Global
CleanList Global
PurgeList Global
}
func init() {
RegisterComponent(Components{
ComponentType: None,
RelatedTypes: nil,
ComponentMatches: []string{"", "all", "any"},
RealComponent: false,
DownloadBase: "",
})
RegisterDefaultSettings(GlobalSettings{
// Root directory for all operations
"Geneos": "",
// Root URL for all downloads of software archives
"DownloadURL": "https://resources.itrsgroup.com/download/latest/",
// Username to start components if not explicitly defined
// and we are running with elevated privileges
//
// When running as a normal user this is unused and
// we simply test a defined user against the running user
//
// default is owner of Geneos
"DefaultUser": "",
// Path List sperated additions to the reserved names list, over and above
// any words matched by parseComponentName()
"ReservedNames": "",
"PrivateKeys": "id_rsa,id_ecdsa,id_ecdsa_sk,id_ed25519,id_ed25519_sk,id_dsa",
})
}
type ComponentsMap map[Component]Components
// slice of registered component types for indirect calls
// this should actually become an Interface
var components ComponentsMap = make(ComponentsMap)
// The Instances interface is used by all components through
// the InstancesBase struct below
type Instances interface {
Name() string
Home() string
Type() Component
Location() RemoteName
Remote() *Remotes
Base() *InstanceBase
Prefix(string) string
String() string
Load() error
Unload() error
Loaded() bool
Add(string, []string, string) error
Command() ([]string, []string)
// Clean(bool, []string) error
Reload(params []string) (err error)
Rebuild(bool) error
}
// The Common type is the common data shared by all component types
type InstanceBase struct {
Instances `json:"-"`
// A mutex, for ongoing changes
L *sync.RWMutex `json:"-"`
// The Name of an instance. This may be different to the instance
// directory InstanceName during certain operations, e.g. rename
InstanceName string `json:"Name"`
// The remote location name (this is a remote component and not
// a server name). This is NOT written to the config file as it
// may change if the remote name changes
InstanceLocation RemoteName `default:"local" json:"-"`
InstanceRemote *Remotes `json:"-"`
// The Component Type of an instance
InstanceType string `json:"-"`
// The RemoteRoot directory of the Geneos installation. Used in template
// default settings for component types
RemoteRoot string `json:"-"`
// Rebuild options; never / always / initial
// defaults are differemt for gateway and san but go with a safe option
ConfigRebuild string `default:"never"`
// set to true when config successfully loaded
ConfigLoaded bool `json:"-"`
// Env is a slice of environment variables, as "KEY=VALUE", for the instance
Env []string `json:",omitempty"`
}
// currently supported real component types, for looping
// (go doesn't allow const slices, a function is the workaround)
// not including Remote - this is special
func RealComponents() (cts []Component) {
for ct, c := range components {
if c.RealComponent {
cts = append(cts, ct)
}
}
return
}
// register a component type
func RegisterComponent(c Components) {
components[c.ComponentType] = c
}
// register directories that need to be created in the
// root of the install (by init)
func (ct Component) RegisterDirs(dirs []string) {
initDirs[ct] = dirs
}
func (ct Component) String() (name string) {
return string(ct)
}
// return the component type by iterating over all the
// names registered by components. case sensitive.
func parseComponentName(component string) Component {
for ct, v := range components {
for _, m := range v.ComponentMatches {
if m == component {
return ct
}
}
}
return Unknown
}
// create any missing component registered directories
func (ct Component) makeComponentDirs(r *Remotes) (err error) {
if r == rALL {
logError.Fatalln(ErrInvalidArgs)
}
for _, d := range initDirs[ct] {
dir := filepath.Join(r.Geneos, d)
if err = r.MkdirAll(dir, 0775); err != nil {
return
}
}
return
}
// register setting with their defaults
func RegisterDefaultSettings(settings GlobalSettings) {
for k, v := range settings {
GlobalConfig[k] = v
}
}
// return a slice instances for a given component type
func (ct Component) GetInstancesForComponent(r *Remotes) (confs []Instances) {
if ct == None {
for _, c := range RealComponents() {
confs = append(confs, c.GetInstancesForComponent(r)...)
}
return
}
for _, name := range ct.FindNames(r) {
i, err := ct.GetInstance(name)
if err != nil {
continue
}
confs = append(confs, i)
}
return
}
// given a component type and a slice of args, call the function for each arg
//
// rely on New() checking the component type and returning a slice
// of all matching components for a single name in an arg (e.g all instances
// called 'thisserver')
//
// try to use go routines here - mutexes required
func (ct Component) loopCommand(fn func(Instances, []string) error, args []string, params []string) (err error) {
n := 0
logDebug.Println("args, params", args, params)
for _, name := range args {
cs := ct.FindInstances(name)
if len(cs) == 0 {
log.Println("no matches for", ct, name)
continue
// return ErrNotFound
}
n++
for _, c := range cs {
if err = fn(c, params); err != nil && !errors.Is(err, ErrProcNotFound) && !errors.Is(err, ErrNotSupported) {
log.Println(c, err)
}
}
}
if n == 0 {
return ErrNotFound
}
return nil
}
// Return a slice of all instanceNames for a given Component. No
// checking is done to validate that the directory is a populated
// instance.
//
func (ct Component) FindNames(r *Remotes) (names []string) {
var files []fs.DirEntry
if r == rALL {
for _, r := range AllRemotes() {
names = append(names, ct.FindNames(r)...)
}
logDebug.Println("names:", names)
return
}
if ct == None {
for _, t := range RealComponents() {
// ignore errors, we only care about any files found
d, _ := r.ReadDir(t.ComponentDir(r))
files = append(files, d...)
}
} else {
// ignore errors, we only care about any files found
files, _ = r.ReadDir(ct.ComponentDir(r))
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name() < files[j].Name()
})
for i, file := range files {
// skip for values with the same name as previous
if i > 0 && i < len(files) && file.Name() == files[i-1].Name() {
continue
}
if file.IsDir() {
if ct == Remote {
names = append(names, file.Name())
} else {
names = append(names, file.Name()+"@"+r.InstanceName)
}
}
}
return
}
// return an instance of component ct. loads the config.
func (ct Component) GetInstance(name string) (c Instances, err error) {
if ct == None {
return nil, ErrInvalidArgs
}
cm, ok := components[ct]
if !ok || cm.New == nil {
return nil, ErrNotSupported
}
c = cm.New(name)
if c == nil {
return nil, ErrInvalidArgs
}
err = c.Load()
return
}
// construct and return a slice of a/all component types that have
// a matching name
func (ct Component) FindInstances(name string) (c []Instances) {
_, local, r := SplitInstanceName(name, rALL)
if !r.Loaded() {
logDebug.Println("remote", r, "not loaded")
return
}
if ct == None {
for _, t := range RealComponents() {
c = append(c, t.FindInstances(name)...)
}
return
}
for _, name := range ct.FindNames(r) {
// for case insensitive match change to EqualFold here
_, ldir, _ := SplitInstanceName(name, rALL)
if filepath.Base(ldir) == local {
i, err := ct.GetInstance(name)
if err != nil {
continue
}
c = append(c, i)
}
}
return
}
// Looks for exactly one matching instance across types and remotes
// returns Invalid Args if zero of more than 1 match
func (ct Component) FindInstance(name string) (c Instances, err error) {
list := ct.FindInstances(name)
if len(list) == 0 {
err = ErrNotFound
return
}
if len(list) == 1 {
c = list[0]
return
}
err = ErrInvalidArgs
return
}
// Return the base directory for a Component
// ct cannot be None
func (ct Component) ComponentDir(r *Remotes) string {
if ct == None {
logError.Fatalln(ct, ErrNotSupported)
}
switch ct {
case Remote:
return rLOCAL.GeneosPath(ct.String() + "s")
default:
return r.GeneosPath(ct.String(), ct.String()+"s")
}
}
func InstanceFileWithExt(c Instances, extension string) (path string) {
return filepath.Join(c.Home(), c.Type().String()+"."+extension)
}