isle/go/cmd/entrypoint/daemon.go

96 lines
2.4 KiB
Go
Raw Normal View History

package main
import (
"context"
"fmt"
"os"
"isle/daemon"
"isle/daemon/daecommon"
"isle/daemon/network"
)
// TODO it would be good to have an `isle daemon config-check` kind of command,
// which could be run prior to a systemd service restart to make sure we don't
// restart the service into a configuration that will definitely fail.
var subCmdDaemon = subCmd{
name: "daemon",
descr: "Runs the isle daemon (Default if no sub-command given)",
noNetwork: true,
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
daemonConfigPath := ctx.flags.StringP(
"config-path", "c", "",
"Optional path to a daemon.yml file to load configuration from.",
)
dumpConfig := ctx.flags.Bool(
"dump-config", false,
"Write the default configuration file to stdout and exit.",
)
ctx, err := ctx.withParsedFlags()
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
if *dumpConfig {
return daecommon.CopyDefaultConfig(os.Stdout)
}
// TODO check that daemon is either running as root, or that the
// required linux capabilities are set.
// TODO check that the tun module is loaded (for nebula).
daemonConfig, err := daecommon.LoadConfig(*daemonConfigPath)
if err != nil {
return fmt.Errorf("loading daemon config: %w", err)
}
networkLoader, err := network.NewLoader(
ctx,
2024-11-14 20:49:35 +00:00
ctx.logger.WithNamespace("loader"),
envBinDirPath,
nil,
)
if err != nil {
return fmt.Errorf("instantiating network loader: %w", err)
}
2024-11-14 20:49:35 +00:00
daemonInst, err := daemon.New(
ctx, ctx.logger, networkLoader, daemonConfig,
)
if err != nil {
return fmt.Errorf("starting daemon: %w", err)
}
defer func() {
2024-11-14 20:49:35 +00:00
ctx.logger.Info(ctx, "Stopping child processes")
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-11-14 20:49:35 +00:00
ctx.logger.Info(ctx, "Child processes successfully stopped")
}()
{
2024-11-14 20:49:35 +00:00
logger := ctx.logger.WithNamespace("http")
httpSrv, err := newHTTPServer(
ctx, logger, daemonInst,
)
if err != nil {
return fmt.Errorf("starting HTTP server: %w", err)
}
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)
}
}()
}
<-ctx.Done()
return nil
},
}