2024-09-07 13:46:59 +00:00
|
|
|
// Package children manages the creation, lifetime, and shutdown of child
|
|
|
|
// processes created by the daemon.
|
|
|
|
package children
|
2024-07-06 13:36:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-13 12:34:06 +00:00
|
|
|
"errors"
|
2024-07-06 13:36:48 +00:00
|
|
|
"fmt"
|
2024-09-07 13:46:59 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2024-07-06 13:36:48 +00:00
|
|
|
|
|
|
|
"code.betamike.com/micropelago/pmux/pmuxlib"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2024-09-07 13:46:59 +00:00
|
|
|
|
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/daemon/daecommon"
|
|
|
|
"isle/secrets"
|
2024-09-09 14:34:00 +00:00
|
|
|
"isle/toolkit"
|
2024-07-06 13:36:48 +00:00
|
|
|
)
|
|
|
|
|
2024-09-07 13:46:59 +00:00
|
|
|
// Opts are optional parameters which can be passed in when initializing a new
|
|
|
|
// Children instance. A nil Opts is equivalent to a zero value.
|
|
|
|
type Opts struct {
|
|
|
|
// Stdout and Stderr are what the associated outputs from child processes
|
|
|
|
// will be directed to.
|
|
|
|
Stdout, Stderr io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Opts) withDefaults() *Opts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(Opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.Stdout == nil {
|
|
|
|
o.Stdout = os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.Stderr == nil {
|
|
|
|
o.Stderr = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
// Children manages all child processes of a network. Child processes are
|
|
|
|
// comprised of:
|
|
|
|
// - nebula
|
|
|
|
// - dnsmasq
|
|
|
|
// - garage (0 or more, depending on configured storage allocations)
|
|
|
|
type Children struct {
|
2024-09-09 14:34:00 +00:00
|
|
|
logger *mlog.Logger
|
|
|
|
daemonConfig daecommon.Config
|
|
|
|
runtimeDir toolkit.Dir
|
|
|
|
opts Opts
|
2024-07-06 13:36:48 +00:00
|
|
|
|
2024-07-19 14:50:20 +00:00
|
|
|
pmux *pmuxlib.Pmux
|
2024-07-06 13:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 13:46:59 +00:00
|
|
|
// New initializes and returns a Children instance. If initialization fails an
|
|
|
|
// error is returned.
|
|
|
|
func New(
|
2024-07-06 13:36:48 +00:00
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
2024-07-13 12:34:06 +00:00
|
|
|
binDirPath string,
|
|
|
|
secretsStore secrets.Store,
|
2024-09-07 13:11:04 +00:00
|
|
|
daemonConfig daecommon.Config,
|
2024-09-09 14:34:00 +00:00
|
|
|
runtimeDir toolkit.Dir,
|
2024-07-14 10:19:39 +00:00
|
|
|
garageAdminToken string,
|
2024-07-06 13:36:48 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
opts *Opts,
|
|
|
|
) (
|
|
|
|
*Children, error,
|
|
|
|
) {
|
|
|
|
opts = opts.withDefaults()
|
|
|
|
|
2024-07-13 12:34:06 +00:00
|
|
|
logger.Info(ctx, "Loading secrets")
|
2024-09-07 13:11:04 +00:00
|
|
|
garageRPCSecret, err := daecommon.GetGarageRPCSecret(ctx, secretsStore)
|
2024-07-13 12:34:06 +00:00
|
|
|
if err != nil && !errors.Is(err, secrets.ErrNotFound) {
|
|
|
|
return nil, fmt.Errorf("loading garage RPC secret: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
c := &Children{
|
2024-09-09 14:34:00 +00:00
|
|
|
logger: logger,
|
|
|
|
daemonConfig: daemonConfig,
|
|
|
|
runtimeDir: runtimeDir,
|
|
|
|
opts: *opts,
|
2024-07-06 13:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-07-13 12:34:06 +00:00
|
|
|
pmuxConfig, err := c.newPmuxConfig(
|
2024-07-14 10:19:39 +00:00
|
|
|
ctx,
|
|
|
|
garageRPCSecret,
|
|
|
|
binDirPath,
|
|
|
|
daemonConfig,
|
|
|
|
garageAdminToken,
|
|
|
|
hostBootstrap,
|
2024-07-13 12:34:06 +00:00
|
|
|
)
|
2024-07-06 13:36:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("generating pmux config: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-19 14:50:20 +00:00
|
|
|
c.pmux = pmuxlib.NewPmux(pmuxConfig, c.opts.Stdout, c.opts.Stderr)
|
2024-07-06 13:36:48 +00:00
|
|
|
|
2024-07-14 10:19:39 +00:00
|
|
|
initErr := c.postPmuxInit(
|
|
|
|
ctx, daemonConfig, garageAdminToken, hostBootstrap,
|
|
|
|
)
|
2024-07-06 13:36:48 +00:00
|
|
|
if initErr != nil {
|
|
|
|
logger.Warn(ctx, "failed to initialize Children, shutting down child processes", err)
|
2024-07-20 09:07:11 +00:00
|
|
|
c.Shutdown()
|
2024-07-06 13:36:48 +00:00
|
|
|
return nil, initErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2024-07-19 18:49:04 +00:00
|
|
|
// RestartDNSMasq rewrites the dnsmasq config and restarts the process.
|
2024-09-07 13:46:59 +00:00
|
|
|
//
|
|
|
|
// TODO block until process has been confirmed to have come back up
|
|
|
|
// successfully.
|
2024-07-19 18:49:04 +00:00
|
|
|
func (c *Children) RestartDNSMasq(hostBootstrap bootstrap.Bootstrap) error {
|
|
|
|
_, err := dnsmasqWriteConfig(
|
2024-09-09 14:34:00 +00:00
|
|
|
c.runtimeDir.Path, c.daemonConfig, hostBootstrap,
|
2024-07-19 18:49:04 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writing new dnsmasq config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.pmux.Restart("dnsmasq")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartNebula rewrites the nebula config and restarts the process.
|
2024-09-07 13:46:59 +00:00
|
|
|
//
|
|
|
|
// TODO block until process has been confirmed to have come back up
|
|
|
|
// successfully.
|
2024-07-19 18:49:04 +00:00
|
|
|
func (c *Children) RestartNebula(hostBootstrap bootstrap.Bootstrap) error {
|
|
|
|
_, err := nebulaWriteConfig(
|
2024-09-09 14:34:00 +00:00
|
|
|
c.runtimeDir.Path, c.daemonConfig, hostBootstrap,
|
2024-07-19 18:49:04 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writing a new nebula config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.pmux.Restart("nebula")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-07 13:46:59 +00:00
|
|
|
// Reload applies a ReloadDiff to the Children, using the given bootstrap which
|
|
|
|
// must be the same one which was passed to CalculateReloadDiff.
|
|
|
|
func (c *Children) Reload(
|
|
|
|
ctx context.Context, newBootstrap bootstrap.Bootstrap, diff ReloadDiff,
|
|
|
|
) error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if diff.DNSChanged {
|
|
|
|
c.logger.Info(ctx, "Restarting dnsmasq to account for bootstrap changes")
|
|
|
|
if err := c.RestartDNSMasq(newBootstrap); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("restarting dnsmasq: %w", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff.NebulaChanged {
|
|
|
|
c.logger.Info(ctx, "Restarting nebula to account for bootstrap changes")
|
|
|
|
if err := c.RestartNebula(newBootstrap); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("restarting nebula: %w", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Join(errs...)
|
|
|
|
}
|
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
// Shutdown blocks until all child processes have gracefully shut themselves
|
|
|
|
// down.
|
2024-07-20 09:07:11 +00:00
|
|
|
func (c *Children) Shutdown() {
|
2024-07-19 14:50:20 +00:00
|
|
|
c.pmux.Stop()
|
2024-07-06 13:36:48 +00:00
|
|
|
}
|