2022-10-20 19:59:46 +00:00
|
|
|
package main
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/daemon"
|
2024-09-07 13:11:04 +00:00
|
|
|
"isle/daemon/daecommon"
|
2024-11-10 21:13:06 +00:00
|
|
|
"isle/daemon/network"
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var subCmdDaemon = subCmd{
|
2024-12-10 21:07:25 +00:00
|
|
|
name: "daemon",
|
|
|
|
descr: "Runs the isle daemon (Default if no sub-command given)",
|
2024-09-04 20:35:29 +00:00
|
|
|
do: func(ctx subCmdCtx) error {
|
2024-09-23 18:50:45 +00:00
|
|
|
daemonConfigPath := ctx.flags.StringP(
|
2021-04-20 21:31:37 +00:00
|
|
|
"config-path", "c", "",
|
|
|
|
"Optional path to a daemon.yml file to load configuration from.",
|
|
|
|
)
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
dumpConfig := ctx.flags.Bool(
|
2021-04-20 21:31:37 +00:00
|
|
|
"dump-config", false,
|
|
|
|
"Write the default configuration file to stdout and exit.",
|
|
|
|
)
|
|
|
|
|
2024-12-10 21:07:25 +00:00
|
|
|
ctx, err := ctx.withParsedFlags(&withParsedFlagsOpts{
|
|
|
|
noNetwork: true,
|
|
|
|
})
|
2024-09-23 18:50:45 +00:00
|
|
|
if err != nil {
|
2021-04-20 21:31:37 +00:00
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *dumpConfig {
|
2024-09-24 09:22:00 +00:00
|
|
|
return daecommon.CopyDefaultConfig(os.Stdout)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 20:51:33 +00:00
|
|
|
daemonConfig, err := daecommon.LoadConfig(*daemonConfigPath)
|
2022-10-26 21:21:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading daemon config: %w", err)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 21:13:06 +00:00
|
|
|
networkLoader, err := network.NewLoader(
|
|
|
|
ctx,
|
2024-12-13 12:52:39 +00:00
|
|
|
ctx.logger.WithNamespace("network-loader"),
|
2024-12-19 19:57:09 +00:00
|
|
|
getBinDirPath(),
|
2024-11-10 21:13:06 +00:00
|
|
|
nil,
|
2024-06-24 16:55:36 +00:00
|
|
|
)
|
2024-11-10 21:13:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("instantiating network loader: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-12-13 12:52:39 +00:00
|
|
|
daemonLogger := ctx.logger.WithNamespace("daemon")
|
2024-11-14 20:49:35 +00:00
|
|
|
daemonInst, err := daemon.New(
|
2024-12-13 12:52:39 +00:00
|
|
|
ctx,
|
|
|
|
daemonLogger,
|
|
|
|
networkLoader,
|
|
|
|
daemonConfig,
|
2024-11-14 20:49:35 +00:00
|
|
|
)
|
2024-07-07 10:44:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("starting daemon: %w", err)
|
|
|
|
}
|
2024-06-24 16:55:36 +00:00
|
|
|
defer func() {
|
2024-12-13 12:52:39 +00:00
|
|
|
daemonLogger.Info(ctx, "Stopping daemon")
|
2024-06-24 16:55:36 +00:00
|
|
|
if err := daemonInst.Shutdown(); err != nil {
|
2024-11-14 20:49:35 +00:00
|
|
|
ctx.logger.Error(ctx, "Shutting down daemon cleanly failed, there may be orphaned child processes", err)
|
2024-06-24 16:55:36 +00:00
|
|
|
}
|
2024-12-13 12:52:39 +00:00
|
|
|
daemonLogger.Info(ctx, "Daemon successfully stopped")
|
2024-06-24 16:55:36 +00:00
|
|
|
}()
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-06-24 16:55:36 +00:00
|
|
|
{
|
2024-11-14 20:49:35 +00:00
|
|
|
logger := ctx.logger.WithNamespace("http")
|
2024-06-24 16:55:36 +00:00
|
|
|
httpSrv, err := newHTTPServer(
|
2024-09-07 11:52:32 +00:00
|
|
|
ctx, logger, daemonInst,
|
2024-06-24 16:55:36 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("starting HTTP server: %w", err)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
2024-06-24 16:55:36 +00:00
|
|
|
defer func() {
|
|
|
|
// see comment in daemonInst shutdown logic regarding background
|
|
|
|
// context.
|
|
|
|
logger.Info(ctx, "Shutting down HTTP socket")
|
|
|
|
if err := httpSrv.Shutdown(context.Background()); err != nil {
|
|
|
|
logger.Error(ctx, "Failed to cleanly shutdown http server", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
2024-06-24 16:55:36 +00:00
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
return nil
|
2021-04-20 21:31:37 +00:00
|
|
|
},
|
|
|
|
}
|