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"
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-06-22 15:49:56 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var subCmdDaemon = subCmd{
|
|
|
|
name: "daemon",
|
2023-08-05 21:53:17 +00:00
|
|
|
descr: "Runs the isle daemon (Default if no sub-command given)",
|
2021-04-20 21:31:37 +00:00
|
|
|
do: func(subCmdCtx subCmdCtx) error {
|
|
|
|
|
|
|
|
flags := subCmdCtx.flagSet(false)
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
daemonConfigPath := flags.StringP(
|
2021-04-20 21:31:37 +00:00
|
|
|
"config-path", "c", "",
|
|
|
|
"Optional path to a daemon.yml file to load configuration from.",
|
|
|
|
)
|
|
|
|
|
|
|
|
dumpConfig := flags.Bool(
|
|
|
|
"dump-config", false,
|
|
|
|
"Write the default configuration file to stdout and exit.",
|
|
|
|
)
|
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
logLevelStr := flags.StringP(
|
|
|
|
"log-level", "l", "info",
|
|
|
|
`Maximum log level which should be output. Values can be "debug", "info", "warn", "error", "fatal". Does not apply to sub-processes`,
|
|
|
|
)
|
|
|
|
|
2021-04-20 21:31:37 +00:00
|
|
|
if err := flags.Parse(subCmdCtx.args); err != nil {
|
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
ctx := subCmdCtx.ctx
|
|
|
|
|
2021-04-20 21:31:37 +00:00
|
|
|
if *dumpConfig {
|
2022-10-26 22:37:03 +00:00
|
|
|
return daemon.CopyDefaultConfig(os.Stdout, envAppDirPath)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
logLevel := mlog.LevelFromString(*logLevelStr)
|
|
|
|
if logLevel == nil {
|
|
|
|
return fmt.Errorf("couldn't parse log level %q", *logLevelStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := subCmdCtx.logger.WithMaxLevel(logLevel.Int())
|
|
|
|
|
|
|
|
runtimeDirCleanup, err := setupAndLockRuntimeDir(ctx, logger)
|
2022-10-26 22:45:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up runtime directory: %w", err)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
2022-10-26 22:45:40 +00:00
|
|
|
defer runtimeDirCleanup()
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2022-10-26 22:37:03 +00:00
|
|
|
daemonConfig, err := daemon.LoadConfig(envAppDirPath, *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-07-07 10:44:49 +00:00
|
|
|
daemonInst, err := daemon.NewDaemon(
|
|
|
|
logger, daemonConfig, envBinDirPath, nil,
|
2024-06-24 16:55:36 +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() {
|
|
|
|
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(
|
|
|
|
ctx, logger, daemon.NewRPC(daemonInst),
|
|
|
|
)
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|