/
service.go
825 lines (724 loc) · 23.7 KB
/
service.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
* === This file is part of ALICE O² ===
*
* Copyright 2019-2021 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package local
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/AliceO2Group/Control/common/logger"
"github.com/AliceO2Group/Control/common/logger/infologger"
"github.com/AliceO2Group/Control/configuration/cfgbackend"
"github.com/AliceO2Group/Control/configuration/componentcfg"
"github.com/AliceO2Group/Control/configuration/template"
"github.com/flosch/pongo2/v6"
"github.com/hashicorp/go-multierror"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var log = logger.New(logrus.StandardLogger(), "confsys")
const inventoryKeyPrefix = "o2/hardware/"
type Service struct {
src cfgbackend.Source
templateSets map[string]*pongo2.TemplateSet
templateSetsMu sync.Mutex
}
func NewService(uri string) (svc *Service, err error) {
var src cfgbackend.Source
src, err = cfgbackend.NewSource(uri)
return &Service{
src: src,
}, err
}
func (s *Service) NewRunNumber() (runNumber uint32, err error) {
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
return cSrc.GetNextUInt32(filepath.Join(getConsulRuntimePrefix(), "run_number"))
} else {
// Unsafe check-and-set, only for file backend
var rnf string
rnf = filepath.Join(viper.GetString("coreWorkingDir"), "runcounter.txt")
if _, err = os.Stat(rnf); os.IsNotExist(err) {
err = ioutil.WriteFile(rnf, []byte("0"), 0644)
if err != nil {
return
}
}
var raw []byte
raw, err = ioutil.ReadFile(rnf)
if err != nil {
return
}
var rn64 uint64
rn64, err = strconv.ParseUint(string(raw[:]), 10, 32)
if err != nil {
return
}
runNumber = uint32(rn64)
runNumber++
raw = []byte(strconv.FormatUint(uint64(runNumber), 10))
err = ioutil.WriteFile(rnf, raw, 0)
return
}
}
// maybe this one shouldn't exist at all, because vars should get inserted
// response: but not all of them! some vars will likely only get parsed at deployment time i.e. right
// before pushing TaskInfos
func (s *Service) GetDefaults() map[string]string {
s.logMethod()
smap := s.getStringMap(filepath.Join(getAliECSRuntimePrefix(), "defaults"))
// Fill in some global constants we want to make available everywhere
var configUri string
if viper.IsSet("config_endpoint") { //coconut
configUri = viper.GetString("config_endpoint")
} else if viper.IsSet("globalConfigurationUri") { //core
configUri = viper.GetString("globalConfigurationUri")
} else { //apricot
configUri = viper.GetString("backendUri")
}
smap["consul_base_uri"] = configUri
consulUrl, err := url.ParseRequestURI(configUri)
if err == nil {
smap["consul_hostname"] = consulUrl.Hostname()
smap["consul_port"] = consulUrl.Port()
smap["consul_endpoint"] = consulUrl.Host
} else {
log.WithField("globalConfigurationUri", configUri).
Warn("cannot parse global configuration endpoint")
}
smap["framework_id"], _ = s.GetRuntimeEntry("aliecs", "mesos_fid")
smap["core_hostname"], _ = os.Hostname()
return smap
}
func (s *Service) ListDetectors(getAll bool) (detectors []string, err error) {
s.logMethod()
keyPrefix := inventoryKeyPrefix + "detectors/"
var keys []string
keys, err = s.src.GetKeysByPrefix(keyPrefix)
if err != nil {
log.WithError(err).Error("could not retrieve detectors")
return []string{}, err
}
detectorSet := make(map[string]bool, 0)
detectors = make([]string, 0)
for _, key := range keys {
detTrimmed := strings.TrimPrefix(key, keyPrefix)
detname := strings.Split(detTrimmed, "/")
if !getAll && detname[0] == "TRG" {
continue
}
if _, ok := detectorSet[detname[0]]; !ok { // the detector name we found in the path isn't already accounted for
detectorSet[detname[0]] = true
detectors = append(detectors, detname[0])
}
}
return detectors, err
}
func (s *Service) GetHostInventory(detector string) (hosts []string, err error) {
s.logMethod()
var keyPrefix string
if detector != "" {
keyPrefix = inventoryKeyPrefix + "detectors/" + detector + "/flps/"
} else {
keyPrefix = inventoryKeyPrefix + "flps/"
}
var keys []string
keys, err = s.src.GetKeysByPrefix(keyPrefix)
if err != nil {
log.WithError(err).Error("could not retrieve host list")
return []string{}, err
}
hostSet := make(map[string]bool, 0)
hosts = make([]string, 0)
for _, key := range keys {
hostTrimmed := strings.TrimPrefix(key, keyPrefix)
hostname := strings.Split(hostTrimmed, "/")
if _, ok := hostSet[hostname[0]]; !ok {
hostSet[hostname[0]] = true
hosts = append(hosts, hostname[0])
}
}
return hosts, err
}
func (s *Service) GetDetectorsInventory() (inventory map[string][]string, err error) {
s.logMethod()
inventory = map[string][]string{}
detectors, err := s.ListDetectors(true)
if err != nil {
log.WithError(err).Error("could not retrieve detectors list")
return nil, err
}
for _, detector := range detectors {
hosts, err := s.GetHostInventory(detector)
if err != nil {
log.WithError(err).Error("could not retrieve hosts list")
return nil, err
}
inventory[detector] = hosts
}
return inventory, err
}
func (s *Service) GetVars() map[string]string {
s.logMethod()
return s.getStringMap(filepath.Join(getAliECSRuntimePrefix(), "vars"))
}
// Returns a YAML file OR even a structure made of Roles or Nodes with:
// import() functions already computed and resolved
// vars inserted
func (s *Service) GenerateWorkflowDescriptor(wfPath string, vars map[string]string /*vars from cli/gui*/) string {
panic("not implemented yet")
}
// TODO: remove these, replaced by 2 calls to Get/SetRuntimeEntry in task/manager.go
//
//// Persist Mesos Framework ID by saving to Consul, or to a local file.
//func (s *Service) SetMesosFID(fidValue string) error {
// if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
// return cSrc.Put(filepath.Join(getConsulRuntimePrefix(),"mesos_fid"), fidValue)
// } else {
// data := []byte(fidValue)
// return ioutil.WriteFile(filepath.Join(viper.GetString("workingDir"), "mesos_fid.txt"), data, 0644)
// }
//}
//
//// Retrieve Mesos Framework ID from Consul, or local file.
//func (s *Service) GetMesosFID() (fidValue string, err error) {
// if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
// return cSrc.Get(filepath.Join(getConsulRuntimePrefix(),"mesos_fid"))
// } else {
// var byteFidValue []byte
// byteFidValue, err = ioutil.ReadFile(filepath.Join(viper.GetString("workingDir"), "mesos_fid.txt"))
// if err != nil {
// return
// }
// fidValue = strings.TrimSuffix(string(byteFidValue), "/n")
// return
// }
//}
func (s *Service) GetComponentConfiguration(query *componentcfg.Query) (payload string, err error) {
s.logMethod()
var absolutePath string
absolutePath, err = s.queryToAbsPath(query)
if err != nil {
return
}
payload, err = s.src.Get(absolutePath)
return
}
func (s *Service) GetComponentConfigurationWithLastIndex(query *componentcfg.Query) (payload string, lastIndex uint64, err error) {
s.logMethod()
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
var absolutePath string
absolutePath, err = s.queryToAbsPath(query)
if err != nil {
return
}
payload, lastIndex, err = cSrc.GetWithLastIndex(absolutePath)
} else {
err = errors.New("component with last index not supported with file backend")
}
return
}
func (s *Service) GetAndProcessComponentConfiguration(query *componentcfg.Query, varStack map[string]string) (payload string, err error) {
s.logMethod()
path := query.Path()
// We need to decompose the requested GetConfig path into prefix and suffix,
// with the last / as separator (any timestamp if present stays part of the
// suffix).
// This is done to allow internal references between template snippets
// within the same Consul directory.
indexOfLastSeparator := strings.LastIndex(path, "/")
var basePath string
shortPath := path
if indexOfLastSeparator != -1 {
basePath = path[:indexOfLastSeparator]
shortPath = path[indexOfLastSeparator+1:]
}
// We get a TemplateSet, with a custom TemplateLoader. Depending on past events, a template set for this base path
// might already exist in the service's template set cache map. We will then use the cache of this template set to
// speed up future requests.
// In order for resolution of short paths to work (i.e. entry name within a component/runtype/rolename directory),
// we need to build one templateSet+templateLoader per base path.
// Our ConsulTemplateLoader takes control of the FromFile code path in pongo2, effectively adding support for
// Consul as file-like backend.
tplSet := s.templateSetForBasePath(basePath)
var tpl *pongo2.Template
tpl, err = tplSet.FromCache(shortPath)
if err != nil {
return fmt.Sprintf("{\"error\":\"%s\"}", err.Error()), err
}
bindings := make(map[string]interface{})
for k, v := range varStack {
bindings[strings.TrimSpace(k)] = v
}
// Add custom functions to bindings:
funcMap := template.MakeUtilFuncMap(varStack)
for k, v := range funcMap {
bindings[k] = v
}
payload, err = tpl.Execute(bindings)
return
}
func (s *Service) ResolveComponentQuery(query *componentcfg.Query) (resolved *componentcfg.Query, err error) {
s.logMethod()
resolved = &componentcfg.Query{}
if query == nil {
*resolved = *query
return
}
if _, ok := s.src.(*cfgbackend.ConsulSource); ok {
return s.resolveComponentQuery(query)
}
return
}
func (s *Service) RawGetRecursive(path string) (string, error) {
s.logMethod()
cfgDump, err := s.src.GetRecursive(path)
if err != nil {
log.WithError(err).Error("cannot retrieve configuration")
return "", err
}
cfgBytes, err := json.MarshalIndent(cfgDump, "", "\t")
if err != nil {
log.WithError(err).Error("cannot marshal configuration dump")
return "", err
}
return string(cfgBytes[:]), nil
}
func (s *Service) GetDetectorForHost(hostname string) (string, error) {
s.logMethod()
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
keys, err := cSrc.GetKeysByPrefix(filepath.Join("o2/hardware", "detectors"))
if err != nil {
return "", err
}
for _, key := range keys {
// key example: o2/hardware/detectors/TST/flps/some-hostname/
splitKey := strings.Split(key, "/")
if len(splitKey) == 7 {
if splitKey[5] == hostname {
return splitKey[3], nil
}
}
}
return "", fmt.Errorf("detector not found for host %s", hostname)
} else {
return "", errors.New("runtime KV not supported with file backend")
}
}
func (s *Service) GetDetectorsForHosts(hosts []string) ([]string, error) {
s.logMethod()
detectorMap := make(map[string]struct{})
for _, host := range hosts {
det, err := s.GetDetectorForHost(host)
if err != nil {
return []string{}, err
}
detectorMap[det] = struct{}{}
}
detectorSlice := make([]string, len(detectorMap))
i := 0
for k, _ := range detectorMap {
detectorSlice[i] = k
i++
}
sort.Strings(detectorSlice)
return detectorSlice, nil
}
func (s *Service) GetCRUCardsForHost(hostname string) (string, error) {
s.logMethod()
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
var cards map[string]Card
var serials []string
cfgCards, err := cSrc.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
if err != nil {
return "", err
}
json.Unmarshal([]byte(cfgCards), &cards)
unique := make(map[string]bool)
for _, card := range cards {
if _, value := unique[card.Serial]; !value {
unique[card.Serial] = true
serials = append(serials, card.Serial)
}
}
bytes, err := json.Marshal(serials)
if err != nil {
return "", err
}
return string(bytes), nil
} else {
return "", errors.New("runtime KV not supported with file backend")
}
}
func (s *Service) GetEndpointsForCRUCard(hostname, cardSerial string) (string, error) {
s.logMethod()
log.WithPrefix("rpcserver").
WithField("method", "GetEndpointsForCRUCard").
WithField("level", infologger.IL_Devel).
WithField("hostname", hostname).
WithField("cardSerial", cardSerial).
Debug("getting endpoints")
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
var cards map[string]Card
var endpoints string
cfgCards, err := cSrc.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
if err != nil {
return "", err
}
err = json.Unmarshal([]byte(cfgCards), &cards)
if err != nil {
return "", err
}
for _, card := range cards {
if card.Serial == cardSerial {
endpoints = endpoints + card.Endpoint + " "
}
}
return endpoints, nil
} else {
return "", errors.New("runtime KV not supported with file backend")
}
}
func (s *Service) GetRuntimeEntry(component string, key string) (string, error) {
s.logMethod()
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
return cSrc.Get(filepath.Join(getConsulRuntimePrefix(), component, key))
} else {
return "", errors.New("runtime KV not supported with file backend")
}
}
func (s *Service) SetRuntimeEntry(component string, key string, value string) error {
s.logMethod()
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
return cSrc.Put(filepath.Join(getConsulRuntimePrefix(), component, key), value)
} else {
return errors.New("runtime KV not supported with file backend")
}
}
func (s *Service) GetRuntimeEntries(component string) (map[string]string, error) {
s.logMethod()
if keys, err := s.ListRuntimeEntries(component); err == nil {
var keysErrors *multierror.Error
entries := make(map[string]string)
for _, key := range keys {
if entry, err := s.GetRuntimeEntry(component, key); err == nil {
entries[key] = entry
} else {
keysErrors = multierror.Append(keysErrors, err)
}
}
return entries, keysErrors.ErrorOrNil()
} else {
return nil, err
}
}
func (s *Service) ListRuntimeEntries(component string) ([]string, error) {
s.logMethod()
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
path := filepath.Join(getConsulRuntimePrefix(), component)
keys, err := cSrc.GetKeysByPrefix(path)
if err != nil {
return nil, err
}
payload := make([]string, 0)
for _, k := range keys {
keySuffix := strings.TrimPrefix(k, path+"/")
if keySuffix == "" {
continue
}
split := strings.Split(keySuffix, componentcfg.SEPARATOR)
var last string
last = split[len(split)-1]
if last == "" {
continue
} else {
payload = append(payload, keySuffix)
}
}
return payload, nil
} else {
return nil, errors.New("runtime KV not supported with file backend")
}
}
func (s *Service) ListComponents() (components []string, err error) {
s.logMethod()
keyPrefix := componentcfg.ConfigComponentsPath
var keys []string
keys, err = s.src.GetKeysByPrefix(keyPrefix)
if err != nil {
return
}
componentSet := make(map[string]struct{})
for _, key := range keys {
componentsFullName := strings.TrimPrefix(key, keyPrefix)
componentParts := strings.Split(componentsFullName, componentcfg.SEPARATOR)
// Criterion for being a component:
// length of parts == 2 because of trailing slash in Consul output for folders
// part[1] must be len=0, otherwise it's an actual entry within the component and not a trailing slash
if len(componentParts) != 2 || len(componentParts[1]) != 0 {
continue
}
component := componentParts[0]
componentSet[component] = struct{}{}
}
components = make([]string, len(componentSet))
i := 0
for component, _ := range componentSet {
components[i] = component
i++
}
return
}
func formatComponentEntriesList(keys []string, keyPrefix string, showTimestamp bool) ([]string, error) {
if len(keys) == 0 {
return []string{}, errors.New("no keys found")
}
var components sort.StringSlice
// map of key to timestamp
componentsSet := make(map[string]string)
for _, key := range keys {
// The input is assumed to be absolute paths, so we must trim the prefix.
// The prefix includes the component name, e.g. o2/components/readout
componentsFullName := strings.TrimPrefix(key, keyPrefix)
componentParts := strings.Split(componentsFullName, "/")
var componentTimestamp string
// The component name is already stripped as part of the keyPrefix.
// len(ANY/any/entry[/timestamp]) is 4, therefore ↓
if len(componentParts) == 3 {
// 1st acceptable case: single untimestamped entry
if len(componentParts[len(componentParts)-1]) == 0 { // means this is a folder key with trailing slash "ANY/any/"
continue
}
componentTimestamp = "" // we're sure this path cannot contain a timestamp
componentsSet[componentsFullName] = ""
} else if len(componentParts) == 4 {
// A 5-len componentParts could be a timestamped entry, or a folder
// in the latter case, the final component is an empty string, because
// the full path has a trailing slash.
// For this reason, we have 2 cases: showTimestamp=true or false
// If false, we only need to pick 5-len folders (in addition to 4-len
// entries).
// If true, we must pick all true 5-len entries in order to compare them
// & pick the newest (in addition to, as usual, 4-len ones).
componentTimestamp = componentParts[len(componentParts)-1]
componentsFullName = strings.TrimSuffix(componentsFullName, componentcfg.SEPARATOR+componentTimestamp)
if !showTimestamp {
componentsSet[componentsFullName] = ""
} else {
// if we *do* need to compare timestamps to find the latest
if len(componentTimestamp) == 0 { // means this is a folder key with trailing slash "component/ANY/any/entry/"
continue
}
if strings.Compare(componentsSet[componentsFullName], componentTimestamp) < 0 {
componentsSet[componentsFullName] = componentTimestamp
}
}
} else {
continue
}
}
for entryKey, entryTimestamp := range componentsSet {
if showTimestamp {
if len(entryTimestamp) == 0 {
components = append(components, entryKey)
} else {
components = append(components, entryKey+"@"+entryTimestamp)
}
} else {
components = append(components, entryKey)
}
}
sort.Sort(components)
return components, nil
}
func (s *Service) ListComponentEntries(query *componentcfg.EntriesQuery, showLatestTimestamp bool) (entries []string, err error) {
s.logMethod()
keyPrefix := componentcfg.ConfigComponentsPath
if query == nil {
err = errors.New("bad query for ListComponentEntries")
return
}
keyPrefix += query.Component + "/"
var keys []string
keys, err = s.src.GetKeysByPrefix(keyPrefix)
if err != nil {
return
}
entries, err = formatComponentEntriesList(keys, keyPrefix, showLatestTimestamp)
if err != nil {
return
}
return
}
func (s *Service) ListComponentEntryHistory(query *componentcfg.Query) (entries []string, err error) {
s.logMethod()
if query == nil {
return
}
fullKeyToQuery := query.AbsoluteWithoutTimestamp()
var keys sort.StringSlice
keys, err = s.src.GetKeysByPrefix(fullKeyToQuery)
if err != nil {
return
}
if len(keys) == 0 {
err = errors.New("empty data returned from configuration backend")
return
}
if len(query.EntryKey) == 0 {
err = errors.New("history requested for empty entry name")
return
}
// We trim the prefix + component
keyPrefix := componentcfg.ConfigComponentsPath + query.Component + componentcfg.SEPARATOR
for i := 0; i < len(keys); i++ {
trimmed := strings.TrimPrefix(keys[i], keyPrefix)
componentParts := strings.Split(trimmed, componentcfg.SEPARATOR)
if len(componentParts) != 4 {
// bad key!
continue
}
keys[i] = componentParts[0] + componentcfg.SEPARATOR +
componentParts[1] + componentcfg.SEPARATOR +
componentParts[2] + "@" +
componentParts[3]
}
sort.Sort(sort.Reverse(keys))
entries = keys
return
}
func (s *Service) ImportComponentConfiguration(query *componentcfg.Query, payload string, newComponent bool, useVersioning bool) (existingComponentUpdated bool, existingEntryUpdated bool, newTimestamp int64, err error) {
s.logMethod()
if query == nil {
return
}
var keys []string
keys, err = s.src.GetKeysByPrefix("")
if err != nil {
return
}
components := componentcfg.GetComponentsMapFromKeysList(keys)
componentExist := components[query.Component]
if !componentExist && !newComponent {
componentMsg := ""
for key := range components {
componentMsg += "\n- " + key
}
err = errors.New("component " + query.Component + " does not exist. " +
"Available components in configuration database:" + componentMsg +
"\nTo create a new component, use the new component parameter")
return
}
if componentExist && newComponent {
err = errors.New("invalid use of new component parameter: component " + query.Component + " already exists")
return
}
entryExists := false
if !newComponent {
entriesMap := componentcfg.GetEntriesMapOfComponentFromKeysList(query.Component, query.RunType, query.RoleName, keys)
entryExists = entriesMap[query.EntryKey]
}
// Temporary workaround to allow no-versioning
var latestTimestamp string
latestTimestamp, err = componentcfg.GetLatestTimestamp(keys, query)
if err != nil {
return
}
if entryExists {
if (latestTimestamp != "0" && latestTimestamp != "") && !useVersioning {
// If a timestamp already exists in the entry specified by the user, than it cannot be used
err = errors.New("Specified entry: '" + query.EntryKey + "' already contains versioned items. Please " +
"specify a different entry name")
return
}
if (latestTimestamp == "0" || latestTimestamp == "") && useVersioning {
// If a timestamp does not exist for specified entry but user wants versioning than an error is thrown
err = errors.New("Specified entry: '" + query.EntryKey + "' already contains un-versioned items. Please " +
"specify a different entry name")
return
}
}
timestamp := time.Now().Unix()
fullKey := query.AbsoluteWithoutTimestamp()
if useVersioning {
fullKey += componentcfg.SEPARATOR + strconv.FormatInt(timestamp, 10)
}
err = s.src.Put(fullKey, payload)
if err != nil {
return
}
existingComponentUpdated = componentExist
existingEntryUpdated = entryExists
newTimestamp = timestamp
return
}
func getConsulRuntimePrefix() string {
// FIXME: this should not be hardcoded
return "o2/runtime"
}
func getAliECSRuntimePrefix() string {
return getConsulRuntimePrefix() + "/aliecs"
}
func (s *Service) logMethod() {
if !viper.GetBool("verbose") {
return
}
pc, _, _, ok := runtime.Caller(1)
if !ok {
return
}
fun := runtime.FuncForPC(pc)
if fun == nil {
return
}
log.WithPrefix("rpcserver").
WithField("method", fun.Name()).
WithField("level", infologger.IL_Devel).
Debug("handling RPC request")
}
func (s *Service) templateSetForBasePath(basePath string) *pongo2.TemplateSet {
s.templateSetsMu.Lock()
defer s.templateSetsMu.Unlock()
if s.templateSets == nil {
s.templateSets = make(map[string]*pongo2.TemplateSet)
}
if _, ok := s.templateSets[basePath]; !ok {
s.templateSets[basePath] = pongo2.NewSet(basePath, template.NewConsulTemplateLoader(s, basePath))
}
return s.templateSets[basePath]
}
func (s *Service) InvalidateComponentTemplateCache() {
s.templateSetsMu.Lock()
defer s.templateSetsMu.Unlock()
// In principle we could also foreach templateSet call ClearCache(), but this is quicker and has the same effect
s.templateSets = make(map[string]*pongo2.TemplateSet)
}