-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhyperdrive-daemon.go
171 lines (154 loc) · 4.97 KB
/
hyperdrive-daemon.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
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"github.com/nodeset-org/hyperdrive-daemon/common"
"github.com/nodeset-org/hyperdrive-daemon/server"
"github.com/nodeset-org/hyperdrive-daemon/shared"
"github.com/nodeset-org/hyperdrive-daemon/shared/auth"
"github.com/nodeset-org/hyperdrive-daemon/shared/config"
"github.com/nodeset-org/hyperdrive-daemon/tasks"
"github.com/urfave/cli/v2"
)
// Run
func main() {
// Add logo and attribution to application help template
attribution := "ATTRIBUTION:\n Adapted from the Rocket Pool Smart Node (https://github.com/rocketpool/smartnode) with love."
cli.AppHelpTemplate = fmt.Sprintf("\n%s\n\n%s\n%s\n", shared.Logo, cli.AppHelpTemplate, attribution)
cli.CommandHelpTemplate = fmt.Sprintf("%s\n%s\n", cli.CommandHelpTemplate, attribution)
cli.SubcommandHelpTemplate = fmt.Sprintf("%s\n%s\n", cli.SubcommandHelpTemplate, attribution)
// Initialise application
app := cli.NewApp()
// Set application info
app.Name = "hyperdrive-daemon"
app.Usage = "Hyperdrive Daemon for NodeSet Node Operator Management"
app.Version = shared.HyperdriveVersion
app.Authors = []*cli.Author{
{
Name: "Nodeset",
Email: "[email protected]",
},
}
app.Copyright = "(C) 2024 NodeSet LLC"
userDirFlag := &cli.StringFlag{
Name: "user-dir",
Aliases: []string{"u"},
Usage: "The path of the user data directory, which contains the configuration file to load and all of the user's runtime data",
Required: true,
}
settingsFolderFlag := &cli.StringFlag{
Name: "settings-folder",
Aliases: []string{"s"},
Usage: "The path to the folder containing the network settings files",
Required: true,
}
ipFlag := &cli.StringFlag{
Name: "ip",
Aliases: []string{"i"},
Usage: "The IP address to bind the API server to",
Value: "127.0.0.1",
}
portFlag := &cli.UintFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "The port to bind the API server to",
Value: uint(config.DefaultApiPort),
}
apiKeyFlag := &cli.StringFlag{
Name: "api-key",
Aliases: []string{"k"},
Usage: "Path of the key to use for authenticating incoming API requests",
Required: true,
}
app.Flags = []cli.Flag{
userDirFlag,
settingsFolderFlag,
ipFlag,
portFlag,
apiKeyFlag,
}
app.Action = func(c *cli.Context) error {
// Get the config file path
userDir := c.String(userDirFlag.Name)
cfgPath := filepath.Join(userDir, config.ConfigFilename)
_, err := os.Stat(cfgPath)
if errors.Is(err, fs.ErrNotExist) {
fmt.Printf("Configuration file not found at [%s].", cfgPath)
os.Exit(1)
}
// Get the settings file path
settingsFolder := c.String(settingsFolderFlag.Name)
if settingsFolder == "" {
fmt.Println("No settings folder provided.")
os.Exit(1)
}
_, err = os.Stat(settingsFolder)
if errors.Is(err, fs.ErrNotExist) {
fmt.Printf("Settings folder not found at [%s].", settingsFolder)
os.Exit(1)
}
// Make an API auth manager
apiKeyPath := c.String(apiKeyFlag.Name)
authMgr := auth.NewAuthorizationManager(apiKeyPath, "hd-daemon", auth.DefaultRequestLifespan)
err = authMgr.LoadAuthKey()
if err != nil {
return fmt.Errorf("error loading API key: %w", err)
}
// Wait group to handle graceful stopping
stopWg := new(sync.WaitGroup)
// Create the service provider
sp, err := common.NewHyperdriveServiceProvider(userDir, settingsFolder)
if err != nil {
return fmt.Errorf("error creating service provider: %w", err)
}
// Create the data dir
dataDir := sp.GetConfig().UserDataPath.Value
err = os.MkdirAll(dataDir, 0755)
if err != nil {
return fmt.Errorf("error creating user data directory [%s]: %w", dataDir, err)
}
// Start the task loop
fmt.Println("Starting task loop...")
taskLoop := tasks.NewTaskLoop(sp, stopWg)
err = taskLoop.Run()
if err != nil {
return fmt.Errorf("error starting task loop: %w", err)
}
// Start the server after the task loop so it can log into NodeSet before this starts serving registration status checks
ip := c.String(ipFlag.Name)
port := c.Uint64(portFlag.Name)
serverMgr, err := server.NewServerManager(sp, ip, uint16(port), stopWg, authMgr)
if err != nil {
return fmt.Errorf("error creating server manager: %w", err)
}
// Handle process closures
termListener := make(chan os.Signal, 1)
signal.Notify(termListener, os.Interrupt, syscall.SIGTERM)
go func() {
<-termListener
fmt.Println("Shutting down daemon...")
sp.CancelContextOnShutdown()
serverMgr.Stop()
}()
// Run the daemon until closed
fmt.Println("Daemon online.")
fmt.Printf("API calls are being logged to: %s\n", sp.GetApiLogger().GetFilePath())
fmt.Printf("Tasks are being logged to: %s\n", sp.GetTasksLogger().GetFilePath())
fmt.Println("To view them, use `hyperdrive service daemon-logs [api | tasks].")
stopWg.Wait()
sp.Close()
fmt.Println("Daemon stopped.")
return nil
}
// Run application
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}