isle/go/cmd/entrypoint/daemon.go

93 lines
2.1 KiB
Go

package main
import (
"context"
"fmt"
"os"
"isle/daemon"
"isle/daemon/daecommon"
"isle/daemon/network"
)
var subCmdDaemon = subCmd{
name: "daemon",
descr: "Runs the isle daemon (Default if no sub-command given)",
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(&withParsedFlagsOpts{
noNetwork: true,
})
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
if *dumpConfig {
return daecommon.CopyDefaultConfig(os.Stdout)
}
daemonConfig, err := daecommon.LoadConfig(*daemonConfigPath)
if err != nil {
return fmt.Errorf("loading daemon config: %w", err)
}
networkLoader, err := network.NewLoader(
ctx,
ctx.logger.WithNamespace("network-loader"),
envBinDirPath,
nil,
)
if err != nil {
return fmt.Errorf("instantiating network loader: %w", err)
}
daemonLogger := ctx.logger.WithNamespace("daemon")
daemonInst, err := daemon.New(
ctx,
daemonLogger,
networkLoader,
daemonConfig,
)
if err != nil {
return fmt.Errorf("starting daemon: %w", err)
}
defer func() {
daemonLogger.Info(ctx, "Stopping daemon")
if err := daemonInst.Shutdown(); err != nil {
ctx.logger.Error(ctx, "Shutting down daemon cleanly failed, there may be orphaned child processes", err)
}
daemonLogger.Info(ctx, "Daemon successfully stopped")
}()
{
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
},
}