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
|
|
|
)
|
|
|
|
|
2024-09-01 10:20:37 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-04-20 21:31:37 +00:00
|
|
|
var subCmdDaemon = subCmd{
|
2024-09-23 18:50:45 +00:00
|
|
|
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 {
|
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-09-23 18:50:45 +00:00
|
|
|
ctx, err := ctx.withParsedFlags()
|
|
|
|
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-11-09 16:08:39 +00:00
|
|
|
logger := ctx.logger()
|
|
|
|
defer logger.Close()
|
2022-11-13 15:45:42 +00:00
|
|
|
|
2024-07-16 15:30:36 +00:00
|
|
|
// 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).
|
|
|
|
|
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,
|
|
|
|
logger.WithNamespace("loader"),
|
|
|
|
envBinDirPath,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
daemonInst, err := daemon.New(ctx, logger, networkLoader, daemonConfig)
|
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() {
|
|
|
|
logger.Info(ctx, "Stopping child processes")
|
|
|
|
if err := daemonInst.Shutdown(); err != nil {
|
|
|
|
logger.Error(ctx, "Shutting down daemon cleanly failed, there may be orphaned child processes", err)
|
|
|
|
}
|
|
|
|
logger.Info(ctx, "Child processes successfully stopped")
|
|
|
|
}()
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-06-24 16:55:36 +00:00
|
|
|
{
|
|
|
|
logger := logger.WithNamespace("http")
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|