2024-06-17 18:51:02 +00:00
|
|
|
// Package daemon implements the isle daemon, which is a long-running service
|
|
|
|
// managing all isle background tasks and sub-processes for a single cluster.
|
2022-10-26 21:21:31 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2024-06-17 18:51:02 +00:00
|
|
|
"context"
|
2022-10-26 21:21:31 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-06-17 18:51:02 +00:00
|
|
|
"isle/bootstrap"
|
2022-10-26 21:21:31 +00:00
|
|
|
"os"
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
"code.betamike.com/micropelago/pmux/pmuxlib"
|
2024-06-22 15:49:56 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2022-10-26 21:21:31 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
// Daemon presents all functionality required for client frontends to interact
|
|
|
|
// with isle, typically via the unix socket.
|
|
|
|
type Daemon interface {
|
|
|
|
|
2024-06-17 20:15:28 +00:00
|
|
|
// GetGarageBootstrapHosts loads (and verifies) the <hostname>.json.signed
|
|
|
|
// file for all hosts stored in garage.
|
|
|
|
GetGarageBootstrapHosts(
|
|
|
|
ctx context.Context,
|
|
|
|
) (
|
|
|
|
map[string]bootstrap.Host, error,
|
|
|
|
)
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
// Shutdown blocks until all resources held or created by the daemon,
|
2024-06-24 16:55:36 +00:00
|
|
|
// including child processes it has started, have been cleaned up.
|
2024-06-17 18:51:02 +00:00
|
|
|
//
|
|
|
|
// If this returns an error then it's possible that child processes are
|
|
|
|
// still running and are no longer managed.
|
2024-06-24 16:55:36 +00:00
|
|
|
Shutdown() error
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
// Opts are optional parameters which can be passed in when initializing a new
|
|
|
|
// Daemon instance. A nil Opts is equivalent to a zero value.
|
|
|
|
type Opts struct {
|
|
|
|
// SkipHostBootstrapPush, if set, will cause the Daemon to not push the
|
|
|
|
// bootstrap to garage upon a successful initialization.
|
|
|
|
SkipHostBootstrapPush bool
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
// Stdout and Stderr are what the associated outputs from child processes
|
|
|
|
// will be directed to.
|
|
|
|
Stdout, Stderr io.Writer
|
2024-06-24 12:45:57 +00:00
|
|
|
|
|
|
|
EnvVars EnvVars // Defaults to that returned by GetEnvVars.
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Opts) withDefaults() *Opts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(Opts)
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
if o.Stdout == nil {
|
|
|
|
o.Stdout = os.Stdout
|
|
|
|
}
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
if o.Stderr == nil {
|
|
|
|
o.Stderr = os.Stderr
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 12:45:57 +00:00
|
|
|
if o.EnvVars == (EnvVars{}) {
|
|
|
|
o.EnvVars = GetEnvVars()
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
return o
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 16:55:36 +00:00
|
|
|
type daemon struct {
|
|
|
|
logger *mlog.Logger
|
|
|
|
config Config
|
|
|
|
hostBootstrap bootstrap.Bootstrap
|
|
|
|
binDirPath string
|
|
|
|
opts Opts
|
|
|
|
|
|
|
|
pmuxCancelFn context.CancelFunc
|
|
|
|
pmuxStoppedCh chan struct{}
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
// New initialized and returns a Daemon. If initialization fails an error is
|
|
|
|
// returned.
|
|
|
|
func New(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
|
|
|
config Config,
|
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
2024-06-24 12:45:57 +00:00
|
|
|
binDirPath string,
|
2024-06-17 18:51:02 +00:00
|
|
|
opts *Opts,
|
2022-10-26 21:21:31 +00:00
|
|
|
) (
|
2024-06-17 18:51:02 +00:00
|
|
|
Daemon, error,
|
2022-10-26 21:21:31 +00:00
|
|
|
) {
|
2024-06-17 18:51:02 +00:00
|
|
|
opts = opts.withDefaults()
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
pmuxCtx, pmuxCancelFn := context.WithCancel(context.Background())
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
d := &daemon{
|
|
|
|
logger: logger,
|
|
|
|
config: config,
|
|
|
|
hostBootstrap: hostBootstrap,
|
2024-06-24 12:45:57 +00:00
|
|
|
binDirPath: binDirPath,
|
2024-06-17 18:51:02 +00:00
|
|
|
opts: *opts,
|
|
|
|
pmuxCancelFn: pmuxCancelFn,
|
|
|
|
pmuxStoppedCh: make(chan struct{}),
|
|
|
|
}
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-06-24 16:55:36 +00:00
|
|
|
pmuxConfig, err := d.newPmuxConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("generating pmux config: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
go func() {
|
|
|
|
defer close(d.pmuxStoppedCh)
|
|
|
|
pmuxlib.Run(pmuxCtx, d.opts.Stdout, d.opts.Stderr, pmuxConfig)
|
2024-06-24 16:55:36 +00:00
|
|
|
d.logger.Debug(pmuxCtx, "pmux stopped")
|
2024-06-17 18:51:02 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if initErr := d.postPmuxInit(ctx); initErr != nil {
|
|
|
|
logger.Warn(ctx, "failed to initialize daemon, shutting down child processes", err)
|
2024-06-24 16:55:36 +00:00
|
|
|
if err := d.Shutdown(); err != nil {
|
2024-06-17 18:51:02 +00:00
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"failed to shut down child processes after initialization"+
|
|
|
|
" error, there may be zombie children leftover."+
|
|
|
|
" Original error: %v",
|
|
|
|
initErr,
|
|
|
|
))
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|
2024-06-17 18:51:02 +00:00
|
|
|
|
|
|
|
return nil, initErr
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
return d, nil
|
|
|
|
}
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-06-24 16:55:36 +00:00
|
|
|
func (d *daemon) Shutdown() error {
|
2024-06-17 18:51:02 +00:00
|
|
|
d.pmuxCancelFn()
|
2024-06-24 16:55:36 +00:00
|
|
|
<-d.pmuxStoppedCh
|
|
|
|
return nil
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|