/
run.go
100 lines (81 loc) · 2.46 KB
/
run.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
package cli
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/HewlettPackard/galadriel/cmd/common/cli"
"github.com/HewlettPackard/galadriel/pkg/harvester"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func NewRunCmd() *cobra.Command {
return &cobra.Command{
Use: "run",
Short: "Runs the Galadriel Harvester",
Long: "Run this command to start the Galadriel Harvester",
RunE: func(cmd *cobra.Command, args []string) error {
config, err := LoadConfig(cmd)
if err != nil {
return err
}
h := harvester.New(config)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
err = h.Run(ctx)
if err != nil {
config.Logger.WithError(err).Error("Harvester crashed")
return err
}
config.Logger.Info("Harvester stopped gracefully")
return nil
},
}
}
func LoadConfig(cmd *cobra.Command) (*harvester.Config, error) {
socketPath, err := cmd.Flags().GetString(cli.SocketPathFlagName)
if err != nil {
return nil, fmt.Errorf("cannot read flag socketPath: %w", err)
}
configPath, err := cmd.Flags().GetString(cli.ConfigFlagName)
if err != nil {
return nil, fmt.Errorf("cannot get flag config: %w", err)
}
token, err := cmd.Flags().GetString(cli.JoinTokenFlagName)
if err != nil {
return nil, fmt.Errorf("cannot get join token: %v", err)
}
configFile, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("unable to open configuration file: %w", err)
}
defer configFile.Close()
c, err := ParseConfig(configFile)
if err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
// If the socketPath flag is set, override the config file
if socketPath != "" {
c.Harvester.HarvesterSocketPath = socketPath
}
hc, err := NewHarvesterConfig(c)
if err != nil {
return nil, fmt.Errorf("failed to build harvester configuration: %w", err)
}
logLevel, err := logrus.ParseLevel(c.Harvester.LogLevel)
if err != nil {
return nil, err
}
logrus.SetLevel(logLevel)
logrus.SetOutput(os.Stdout)
hc.JoinToken = token
return hc, nil
}
func init() {
runCmd := NewRunCmd()
runCmd.Flags().StringP(cli.SocketPathFlagName, "", "", "Path to bind the Galadriel Harvester API socket to")
runCmd.Flags().StringP(cli.ConfigFlagName, "c", defaultConfigPath, "Path to the Galadriel Harvester config file")
runCmd.Flags().StringP(cli.JoinTokenFlagName, "t", "", "A join token generated by Galadriel Server")
RootCmd.AddCommand(runCmd)
}