forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapd.go
792 lines (730 loc) · 21.7 KB
/
snapd.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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"golang.org/x/crypto/ssh/terminal"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/vrischmann/jsonutil"
"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/serror"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/tribe"
"github.com/intelsdi-x/snap/mgmt/tribe/agreement"
"github.com/intelsdi-x/snap/pkg/cfgfile"
"github.com/intelsdi-x/snap/scheduler"
)
var (
flAPIDisabled = cli.BoolFlag{
Name: "disable-api, d",
Usage: "Disable the agent REST API",
}
flAPIPort = cli.IntFlag{
Name: "api-port, p",
Usage: "API port (Default: 8181)",
}
flMaxProcs = cli.IntFlag{
Name: "max-procs, c",
Usage: "Set max cores to use for snap Agent. Default is 1 core.",
EnvVar: "GOMAXPROCS",
}
flNumberOfPLs = cli.IntFlag{
Name: "max-running-plugins, m",
Usage: "The maximum number of instances of a loaded plugin to run",
EnvVar: "SNAP_MAX_PLUGINS",
}
// plugin
flLogPath = cli.StringFlag{
Name: "log-path, o",
Usage: "Path for logs. Empty path logs to stdout.",
EnvVar: "SNAP_LOG_PATH",
}
flLogLevel = cli.IntFlag{
Name: "log-level, l",
Usage: "1-5 (Debug, Info, Warning, Error, Fatal)",
EnvVar: "SNAP_LOG_LEVEL",
}
flAutoDiscover = cli.StringFlag{
Name: "auto-discover, a",
Usage: "Auto discover paths separated by colons.",
EnvVar: "SNAP_AUTODISCOVER_PATH",
}
flPluginTrust = cli.IntFlag{
Name: "plugin-trust, t",
Usage: "0-2 (Disabled, Enabled, Warning)",
EnvVar: "SNAP_TRUST_LEVEL",
}
flKeyringPaths = cli.StringFlag{
Name: "keyring-paths, k",
Usage: "Keyring paths for signing verification separated by colons",
EnvVar: "SNAP_KEYRING_PATHS",
}
flCache = cli.DurationFlag{
Name: "cache-expiration",
Usage: "The time limit for which a metric cache entry is valid",
EnvVar: "SNAP_CACHE_EXPIRATION",
}
flConfig = cli.StringFlag{
Name: "config",
Usage: "A path to a config file",
EnvVar: "SNAP_CONFIG_PATH",
}
flRestHTTPS = cli.BoolFlag{
Name: "rest-https",
Usage: "start snap's API as https",
}
flRestCert = cli.StringFlag{
Name: "rest-cert",
Usage: "A path to a certificate to use for HTTPS deployment of snap's REST API",
}
flRestKey = cli.StringFlag{
Name: "rest-key",
Usage: "A path to a key file to use for HTTPS deployment of snap's REST API",
}
flRestAuth = cli.BoolFlag{
Name: "rest-auth",
Usage: "Enables snap's REST API authentication",
}
gitversion string
coreModules []coreModule
// log levels
l = map[int]string{
1: "debug",
2: "info",
3: "warning",
4: "error",
5: "fatal",
}
// plugin trust levels
t = map[int]string{
0: "disabled",
1: "enabled",
2: "warning",
}
)
// default configuration values
const (
defaultLogLevel int = 3
defaultGoMaxProcs int = 1
defaultLogPath string = ""
defaultConfigPath string = "/etc/snap/snapd.conf"
)
// holds the configuration passed in through the SNAP config file
type Config struct {
LogLevel int `json:"log_level,omitempty"yaml:"log_level,omitempty"`
GoMaxProcs int `json:"gomaxprocs,omitempty"yaml:"gomaxprocs,omitempty"`
LogPath string `json:"log_path,omitempty"yaml:"log_path,omitempty"`
Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"`
Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"`
RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
Tribe *tribe.Config `json:"tribe,omitempty"yaml:"tribe,omitempty"`
}
type coreModule interface {
Start() error
Stop()
Name() string
}
type managesTribe interface {
GetAgreement(name string) (*agreement.Agreement, serror.SnapError)
GetAgreements() map[string]*agreement.Agreement
AddAgreement(name string) serror.SnapError
RemoveAgreement(name string) serror.SnapError
JoinAgreement(agreementName, memberName string) serror.SnapError
LeaveAgreement(agreementName, memberName string) serror.SnapError
GetMembers() []string
GetMember(name string) *agreement.Member
}
func main() {
// Add a check to see if gitversion is blank from the build process
if gitversion == "" {
gitversion = "unknown"
}
app := cli.NewApp()
app.Name = "snapd"
app.Version = gitversion
app.Usage = "A powerful telemetry framework"
app.Flags = []cli.Flag{
flAPIDisabled,
flAPIPort,
flLogLevel,
flLogPath,
flMaxProcs,
flAutoDiscover,
flNumberOfPLs,
flCache,
flPluginTrust,
flKeyringPaths,
flRestCert,
flConfig,
flRestHTTPS,
flRestKey,
flRestAuth,
}
app.Flags = append(app.Flags, scheduler.Flags...)
app.Flags = append(app.Flags, tribe.Flags...)
app.Action = action
app.Run(os.Args)
}
func action(ctx *cli.Context) {
// get default configuration
cfg := getDefaultConfig()
// read config file
readConfig(cfg, ctx.String("config"))
// apply values that may have been passed from the command line
// to the configuration that we have built so far, overriding the
// values that may have already been set (if any) for the
// same variables in that configuration
applyCmdLineFlags(cfg, ctx)
// If logPath is set, we verify the logPath and set it so that all logging
// goes to the log file instead of stdout.
logPath := cfg.LogPath
if logPath != "" {
f, err := os.Stat(logPath)
if err != nil {
log.Fatal(err)
}
if !f.IsDir() {
log.Fatal("log path provided must be a directory")
}
file, err := os.OpenFile(fmt.Sprintf("%s/snapd.log", logPath), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.SetOutput(file)
}
log.Info("Starting snapd (version: ", gitversion, ")")
// Set Max Processors for snapd.
setMaxProcs(cfg.GoMaxProcs)
// Validate log level and trust level settings for snapd
validateLevelSettings(cfg.LogLevel, cfg.Control.PluginTrust)
c := control.New(cfg.Control)
coreModules = []coreModule{}
coreModules = append(coreModules, c)
s := scheduler.New(cfg.Scheduler)
s.SetMetricManager(c)
coreModules = append(coreModules, s)
// Auth requested and not provided as part of config
if cfg.RestAPI.Enable && cfg.RestAPI.RestAuth && cfg.RestAPI.RestAuthPassword == "" {
fmt.Println("What password do you want to use for authentication?")
fmt.Print("Password:")
password, err := terminal.ReadPassword(0)
fmt.Println()
if err != nil {
log.Fatal("Failed to get credentials")
}
cfg.RestAPI.RestAuthPassword = string(password)
}
var tr managesTribe
if cfg.Tribe.Enable {
cfg.Tribe.RestAPIPort = cfg.RestAPI.Port
if cfg.RestAPI.RestAuth {
cfg.Tribe.RestAPIPassword = cfg.RestAPI.RestAuthPassword
}
log.Info("Tribe is enabled")
t, err := tribe.New(cfg.Tribe)
if err != nil {
printErrorAndExit(t.Name(), err)
}
c.RegisterEventHandler("tribe", t)
t.SetPluginCatalog(c)
s.RegisterEventHandler("tribe", t)
t.SetTaskManager(s)
coreModules = append(coreModules, t)
tr = t
}
// Set interrupt handling so we can die gracefully.
startInterruptHandling(coreModules...)
// Start our modules
var started []coreModule
for _, m := range coreModules {
if err := startModule(m); err != nil {
for _, m := range started {
m.Stop()
}
printErrorAndExit(m.Name(), err)
}
started = append(started, m)
}
// Plugin Trust
c.SetPluginTrustLevel(cfg.Control.PluginTrust)
log.Info("setting plugin trust level to: ", t[cfg.Control.PluginTrust])
// Keyring checking for trust levels 1 and 2
if cfg.Control.PluginTrust > 0 {
keyrings := filepath.SplitList(cfg.Control.KeyringPaths)
if len(keyrings) == 0 {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
}).Fatal("need keyring file when trust is on (--keyring-file or -k)")
}
for _, k := range keyrings {
keyringPath, err := filepath.Abs(k)
if err != nil {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"error": err.Error(),
"keyringPath": keyringPath,
}).Fatal("Unable to determine absolute path to keyring file")
}
f, err := os.Stat(keyringPath)
if err != nil {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"error": err.Error(),
"keyringPath": keyringPath,
}).Fatal("bad keyring file")
}
if f.IsDir() {
log.Info("Adding keyrings from: ", keyringPath)
files, err := ioutil.ReadDir(keyringPath)
if err != nil {
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"error": err.Error(),
"keyringPath": keyringPath,
}).Fatal(err)
}
if len(files) == 0 {
log.Fatal(fmt.Sprintf("given keyring path [%s] is an empty directory!", keyringPath))
}
for _, keyringFile := range files {
if keyringFile.IsDir() {
continue
}
if strings.HasSuffix(keyringFile.Name(), ".gpg") || (strings.HasSuffix(keyringFile.Name(), ".pub")) || (strings.HasSuffix(keyringFile.Name(), ".pubring")) {
f, err := os.Open(keyringPath)
if err != nil {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"error": err.Error(),
"keyringPath": keyringPath,
}).Warning("unable to open keyring file. not adding to keyring path")
continue
}
f.Close()
log.Info("adding keyring file: ", keyringPath+"/"+keyringFile.Name())
c.SetKeyringFile(keyringPath + "/" + keyringFile.Name())
}
}
} else {
f, err := os.Open(keyringPath)
if err != nil {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"error": err.Error(),
"keyringPath": keyringPath,
}).Fatal("unable to open keyring file.")
}
f.Close()
log.Info("adding keyring file ", keyringPath)
c.SetKeyringFile(keyringPath)
}
}
}
//Autodiscover
if cfg.Control.AutoDiscoverPath != "" {
log.Info("auto discover path is enabled")
paths := filepath.SplitList(cfg.Control.AutoDiscoverPath)
c.SetAutodiscoverPaths(paths)
for _, p := range paths {
fullPath, err := filepath.Abs(p)
if err != nil {
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": p,
}).Fatal(err)
}
log.Info("autoloading plugins from: ", fullPath)
files, err := ioutil.ReadDir(fullPath)
if err != nil {
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
}).Fatal(err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasSuffix(file.Name(), ".aci") || !(strings.HasSuffix(file.Name(), ".asc")) {
rp, err := core.NewRequestedPlugin(path.Join(fullPath, file.Name()))
if err != nil {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin": file,
}).Error(err)
}
signatureFile := file.Name() + ".asc"
if _, err := os.Stat(path.Join(fullPath, signatureFile)); err == nil {
err = rp.ReadSignatureFile(path.Join(fullPath, signatureFile))
if err != nil {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin": file.Name() + ".asc",
}).Error(err)
}
}
pl, err := c.Load(rp)
if err != nil {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin": file,
}).Error(err)
} else {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin": file,
"plugin-name": pl.Name(),
"plugin-version": pl.Version(),
"plugin-type": pl.TypeName(),
}).Info("Loading plugin")
}
}
}
}
} else {
log.Info("auto discover path is disabled")
}
//Setup RESTful API if it was enbled in th configuration
if cfg.RestAPI.Enable {
r, err := rest.New(cfg.RestAPI)
if err != nil {
log.Fatal(err)
}
r.BindMetricManager(c)
r.BindConfigManager(c.Config)
r.BindTaskManager(s)
//Rest Authentication
if cfg.RestAPI.RestAuth {
log.Info("REST API authentication is enabled")
r.SetAPIAuth(cfg.RestAPI.RestAuth)
log.Info("REST API authentication password is set")
r.SetAPIAuthPwd(cfg.RestAPI.RestAuthPassword)
if !cfg.RestAPI.HTTPS {
log.Warning("Using REST API authentication without HTTPS enabled.")
}
}
if tr != nil {
r.BindTribeManager(tr)
}
go monitorErrors(r.Err())
r.Start(fmt.Sprintf(":%d", cfg.RestAPI.Port))
log.Info("REST API is enabled")
} else {
log.Info("REST API is disabled")
}
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
}).Info("snapd started")
// Switch log level to user defined
log.Info("setting log level to: ", l[cfg.LogLevel])
log.SetLevel(getLevel(cfg.LogLevel))
select {} //run forever and ever
}
// get the default snapd configuration
func getDefaultConfig() *Config {
return &Config{
LogLevel: defaultLogLevel,
GoMaxProcs: defaultGoMaxProcs,
LogPath: defaultLogPath,
Control: control.GetDefaultConfig(),
Scheduler: scheduler.GetDefaultConfig(),
RestAPI: rest.GetDefaultConfig(),
Tribe: tribe.GetDefaultConfig(),
}
}
// Read the snapd configuration from a configuration file
func readConfig(cfg *Config, fpath string) {
var path string
if !defaultConfigFile() && fpath == "" {
return
}
if defaultConfigFile() && fpath == "" {
path = defaultConfigPath
}
if fpath != "" {
f, err := os.Stat(fpath)
if err != nil {
log.Fatal(err)
}
if f.IsDir() {
log.Fatal("configuration path provided must be a file")
}
path = fpath
}
err := cfgfile.Read(path, &cfg)
if err != nil {
log.Fatal(err)
}
}
func defaultConfigFile() bool {
_, err := os.Stat(defaultConfigPath)
if err != nil {
return false
}
return true
}
// used to set fields in the configuration to values from the
// command line context if the corresponding flagName is set
// in that context
func setBoolVal(field bool, ctx *cli.Context, flagName string, inverse ...bool) bool {
if ctx.IsSet(flagName) {
field = ctx.Bool(flagName)
if len(inverse) > 0 {
field = !field
}
}
return field
}
func setStringVal(field string, ctx *cli.Context, flagName string) string {
if ctx.IsSet(flagName) {
field = ctx.String(flagName)
}
return field
}
func setIntVal(field int, ctx *cli.Context, flagName string) int {
if ctx.IsSet(flagName) {
field = ctx.Int(flagName)
}
return field
}
func setUIntVal(field uint, ctx *cli.Context, flagName string) uint {
if ctx.IsSet(flagName) {
field = uint(ctx.Int(flagName))
}
return field
}
func setDurationVal(field time.Duration, ctx *cli.Context, flagName string) time.Duration {
if ctx.IsSet(flagName) {
field = ctx.Duration(flagName)
}
return field
}
// Apply the command line flags set (if any) to override the values
// in the input configuration
func applyCmdLineFlags(cfg *Config, ctx *cli.Context) {
invertBoolean := true
// apply any command line flags that might have been set, first for the
// snapd-related flags
cfg.GoMaxProcs = setIntVal(cfg.GoMaxProcs, ctx, "max-procs")
cfg.LogLevel = setIntVal(cfg.LogLevel, ctx, "log-level")
cfg.LogPath = setStringVal(cfg.LogPath, ctx, "log-path")
// next for the flags related to the control package
cfg.Control.MaxRunningPlugins = setIntVal(cfg.Control.MaxRunningPlugins, ctx, "max-running-plugins")
cfg.Control.PluginTrust = setIntVal(cfg.Control.PluginTrust, ctx, "plugin-trust")
cfg.Control.AutoDiscoverPath = setStringVal(cfg.Control.AutoDiscoverPath, ctx, "auto-discover")
cfg.Control.KeyringPaths = setStringVal(cfg.Control.KeyringPaths, ctx, "keyring-paths")
cfg.Control.CacheExpiration = jsonutil.Duration{setDurationVal(cfg.Control.CacheExpiration.Duration, ctx, "cache-expiration")}
// next for the RESTful server related flags
cfg.RestAPI.Enable = setBoolVal(cfg.RestAPI.Enable, ctx, "disable-api", invertBoolean)
cfg.RestAPI.Port = setIntVal(cfg.RestAPI.Port, ctx, "api-port")
cfg.RestAPI.HTTPS = setBoolVal(cfg.RestAPI.HTTPS, ctx, "rest-https")
cfg.RestAPI.RestCertificate = setStringVal(cfg.RestAPI.RestCertificate, ctx, "rest-cert")
cfg.RestAPI.RestKey = setStringVal(cfg.RestAPI.RestKey, ctx, "rest-key")
cfg.RestAPI.RestAuth = setBoolVal(cfg.RestAPI.RestAuth, ctx, "rest-auth")
cfg.RestAPI.RestAuthPassword = setStringVal(cfg.RestAPI.RestAuthPassword, ctx, "rest-auth-pwd")
// next for the scheduler related flags
cfg.Scheduler.WorkManagerQueueSize = setUIntVal(cfg.Scheduler.WorkManagerQueueSize, ctx, "work-manager-queue-size")
cfg.Scheduler.WorkManagerPoolSize = setUIntVal(cfg.Scheduler.WorkManagerPoolSize, ctx, "work-manager-pool-size")
// and finally for the tribe-related flags
cfg.Tribe.Name = setStringVal(cfg.Tribe.Name, ctx, "tribe-node-name")
cfg.Tribe.Enable = setBoolVal(cfg.Tribe.Enable, ctx, "tribe")
cfg.Tribe.BindAddr = setStringVal(cfg.Tribe.BindAddr, ctx, "tribe-addr")
cfg.Tribe.BindPort = setIntVal(cfg.Tribe.BindPort, ctx, "tribe-port")
cfg.Tribe.Seed = setStringVal(cfg.Tribe.Seed, ctx, "tribe-seed")
}
func monitorErrors(ch <-chan error) {
// Block on the error channel. Will return exit status 1 for an error or just return if the channel closes.
err, ok := <-ch
if !ok {
return
}
log.Fatal(err)
}
// setMaxProcs configures runtime.GOMAXPROCS for snapd. GOMAXPROCS can be set by using
// the env variable GOMAXPROCS and snapd will honor this setting. A user can override the env
// variable by setting max-procs flag on the command line. Snapd will be limited to the max CPUs
// on the system even if the env variable or the command line setting is set above the max CPUs.
// The default value if the env variable or the command line option is not set is 1.
func setMaxProcs(maxProcs int) {
var _maxProcs int
numProcs := runtime.NumCPU()
if maxProcs <= 0 {
// We prefer sane values for GOMAXPROCS
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"maxprocs": maxProcs,
}).Error("Trying to set GOMAXPROCS to an invalid value")
_maxProcs = 1
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"maxprocs": _maxProcs,
}).Warning("Setting GOMAXPROCS to 1")
_maxProcs = 1
} else if maxProcs <= numProcs {
_maxProcs = maxProcs
} else {
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"maxprocs": maxProcs,
}).Error("Trying to set GOMAXPROCS larger than number of CPUs available on system")
_maxProcs = numProcs
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"maxprocs": _maxProcs,
}).Warning("Setting GOMAXPROCS to number of CPUs on host")
}
log.Info("setting GOMAXPROCS to: ", _maxProcs, " core(s)")
runtime.GOMAXPROCS(_maxProcs)
//Verify setting worked
actualNumProcs := runtime.GOMAXPROCS(0)
if actualNumProcs != _maxProcs {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"given maxprocs": _maxProcs,
"real maxprocs": actualNumProcs,
}).Warning("not using given maxprocs")
}
}
func startModule(m coreModule) error {
err := m.Start()
if err == nil {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"snap-module": m.Name(),
}).Info("module started")
}
return err
}
func printErrorAndExit(name string, err error) {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"error": err.Error(),
"snap-module": name,
}).Fatal("error starting module")
}
func startInterruptHandling(modules ...coreModule) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM)
//Let's block until someone tells us to quit
go func() {
sig := <-c
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
}).Info("shutting down modules")
for _, m := range modules {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"snap-module": m.Name(),
}).Info("stopping module")
m.Stop()
}
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"signal": sig.String(),
}).Info("exiting on signal")
os.Exit(0)
}()
}
func getLevel(i int) log.Level {
switch i {
case 1:
return log.DebugLevel
case 2:
return log.InfoLevel
case 3:
return log.WarnLevel
case 4:
return log.ErrorLevel
case 5:
return log.FatalLevel
default:
panic("bad level")
}
}
func validateLevelSettings(logLevel, pluginTrust int) {
if logLevel < 1 || logLevel > 5 {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"level": logLevel,
}).Fatal("log level was invalid (needs: 1-5)")
}
if pluginTrust < 0 || pluginTrust > 2 {
log.WithFields(
log.Fields{
"block": "main",
"_module": "snapd",
"level": pluginTrust,
}).Fatal("Plugin trust was invalid (needs: 0-2)")
}
}