2024-06-17 18:51:02 +00:00
|
|
|
// Package daemon implements the isle daemon, which is a long-running service
|
2024-07-06 13:36:48 +00:00
|
|
|
// managing all isle background tasks and sub-processes for a single network.
|
2022-10-26 21:21:31 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2024-06-17 18:51:02 +00:00
|
|
|
"context"
|
2024-09-10 20:51:33 +00:00
|
|
|
"errors"
|
2022-10-26 21:21:31 +00:00
|
|
|
"fmt"
|
2024-06-17 18:51:02 +00:00
|
|
|
"isle/bootstrap"
|
2024-09-07 13:46:59 +00:00
|
|
|
"isle/daemon/children"
|
2024-09-07 13:11:04 +00:00
|
|
|
"isle/daemon/daecommon"
|
2024-09-09 14:34:00 +00:00
|
|
|
"isle/daemon/network"
|
2024-07-07 18:01:10 +00:00
|
|
|
"isle/nebula"
|
2024-09-09 14:34:00 +00:00
|
|
|
"isle/toolkit"
|
2024-09-24 09:03:18 +00:00
|
|
|
"sort"
|
2024-07-06 13:36:48 +00:00
|
|
|
"sync"
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
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
|
|
|
// 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 {
|
2024-09-07 13:46:59 +00:00
|
|
|
ChildrenOpts *children.Opts
|
2024-09-07 13:11:04 +00:00
|
|
|
// Defaults to that returned by daecommon.GetEnvVars.
|
|
|
|
EnvVars daecommon.EnvVars
|
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-09-07 13:11:04 +00:00
|
|
|
if o.EnvVars == (daecommon.EnvVars{}) {
|
|
|
|
o.EnvVars = daecommon.GetEnvVars()
|
2024-06-24 12:45:57 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
return o
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
var _ RPC = (*Daemon)(nil)
|
|
|
|
|
2024-11-09 20:13:33 +00:00
|
|
|
type joinedNetwork struct {
|
|
|
|
network.Network
|
|
|
|
|
|
|
|
userConfig bool
|
|
|
|
}
|
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
// Daemon implements all methods of the Daemon interface, plus others used
|
|
|
|
// to manage this particular implementation.
|
|
|
|
//
|
|
|
|
// Daemon manages all child processes and state required by the isle
|
|
|
|
// service, as well as an HTTP socket over which RPC calls will be served.
|
|
|
|
//
|
|
|
|
// Inner Children instance(s) will be wrapped such that they will be
|
|
|
|
// automatically shutdown and re-created whenever there's changes in the network
|
|
|
|
// which require the configuration to be changed (e.g. a new nebula lighthouse).
|
|
|
|
// During such an inner restart all methods will return ErrRestarting, except
|
|
|
|
// Shutdown which will block until the currently executing restart is finished
|
|
|
|
// and then shutdown cleanly from there.
|
|
|
|
//
|
|
|
|
// While still starting up the Daemon for the first time all methods will return
|
|
|
|
// ErrInitializing, except Shutdown which will block until initialization is
|
|
|
|
// canceled.
|
|
|
|
type Daemon struct {
|
2024-06-24 16:55:36 +00:00
|
|
|
logger *mlog.Logger
|
2024-09-07 13:11:04 +00:00
|
|
|
daemonConfig daecommon.Config
|
2024-07-06 13:36:48 +00:00
|
|
|
envBinDirPath string
|
|
|
|
opts *Opts
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
networksStateDir toolkit.Dir
|
|
|
|
networksRuntimeDir toolkit.Dir
|
2024-07-13 12:34:06 +00:00
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
l sync.RWMutex
|
2024-11-09 20:13:33 +00:00
|
|
|
networks map[string]joinedNetwork
|
2024-06-24 16:55:36 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// New initializes and returns a Daemon.
|
|
|
|
func New(
|
2024-07-20 09:07:11 +00:00
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
2024-09-07 13:11:04 +00:00
|
|
|
daemonConfig daecommon.Config,
|
2024-07-20 09:07:11 +00:00
|
|
|
envBinDirPath string,
|
|
|
|
opts *Opts,
|
2024-07-07 10:44:49 +00:00
|
|
|
) (
|
2024-09-07 11:52:32 +00:00
|
|
|
*Daemon, error,
|
2024-07-07 10:44:49 +00:00
|
|
|
) {
|
2024-09-10 19:02:07 +00:00
|
|
|
opts = opts.withDefaults()
|
|
|
|
|
|
|
|
if err := migrateToMultiNetworkStateDirectory(
|
|
|
|
ctx,
|
|
|
|
logger.WithNamespace("migration-multi-network-state-dir"),
|
|
|
|
opts.EnvVars,
|
|
|
|
); err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"migrating to multi-network state directory: %w", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
d := &Daemon{
|
|
|
|
logger: logger,
|
|
|
|
daemonConfig: daemonConfig,
|
|
|
|
envBinDirPath: envBinDirPath,
|
2024-09-10 19:02:07 +00:00
|
|
|
opts: opts,
|
2024-11-09 20:13:33 +00:00
|
|
|
networks: map[string]joinedNetwork{},
|
2024-07-12 14:34:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
{
|
|
|
|
h := new(toolkit.MkDirHelper)
|
|
|
|
d.networksStateDir, _ = h.Maybe(
|
|
|
|
d.opts.EnvVars.StateDir.MkChildDir("networks", true),
|
2024-07-13 12:34:06 +00:00
|
|
|
)
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
d.networksRuntimeDir, _ = h.Maybe(
|
|
|
|
d.opts.EnvVars.RuntimeDir.MkChildDir("networks", true),
|
2024-07-07 10:44:49 +00:00
|
|
|
)
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
if err := h.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("creating networks sub-directories: %w", err)
|
|
|
|
}
|
2024-07-06 13:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
loadableNetworks, err := loadableNetworks(d.networksStateDir)
|
2024-06-24 16:55:36 +00:00
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return nil, fmt.Errorf("listing loadable networks: %w", err)
|
2024-06-24 16:55:36 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
for _, creationParams := range loadableNetworks {
|
|
|
|
id := creationParams.ID
|
|
|
|
ctx = mctx.WithAnnotator(ctx, creationParams)
|
2024-07-07 18:01:10 +00:00
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
networkStateDir, networkRuntimeDir, err := networkDirs(
|
|
|
|
d.networksStateDir, d.networksRuntimeDir, id, true,
|
2024-07-06 13:36:48 +00:00
|
|
|
)
|
2024-07-20 09:07:11 +00:00
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"creating sub-directories for network %q: %w", id, err,
|
|
|
|
)
|
2024-07-06 13:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
networkConfig, _ := pickNetworkConfig(daemonConfig, creationParams)
|
2024-09-10 20:51:33 +00:00
|
|
|
|
2024-11-09 20:13:33 +00:00
|
|
|
network, err := network.Load(
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx,
|
|
|
|
logger.WithNamespace("network"),
|
2024-09-10 20:51:33 +00:00
|
|
|
networkConfig,
|
2024-09-09 14:34:00 +00:00
|
|
|
d.envBinDirPath,
|
|
|
|
networkStateDir,
|
|
|
|
networkRuntimeDir,
|
|
|
|
&network.Opts{
|
|
|
|
ChildrenOpts: d.opts.ChildrenOpts,
|
|
|
|
},
|
2024-07-20 09:07:11 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return nil, fmt.Errorf("loading network %q: %w", id, err)
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|
2024-11-09 20:13:33 +00:00
|
|
|
|
|
|
|
d.networks[id] = joinedNetwork{
|
|
|
|
Network: network,
|
|
|
|
userConfig: true,
|
|
|
|
}
|
2024-07-20 09:07:11 +00:00
|
|
|
}
|
2024-06-17 18:51:02 +00:00
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
return d, nil
|
2024-07-07 18:01:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
// CreateNetwork will initialize a new network using the given parameters.
|
|
|
|
// - name: Human-readable name of the network.
|
|
|
|
// - domain: Primary domain name that network services are served under.
|
|
|
|
// - ipNet: An IP subnet, in CIDR form, which will be the overall range of
|
|
|
|
// possible IPs in the network. The first IP in this network range will become
|
|
|
|
// this first host's IP.
|
|
|
|
// - hostName: The name of this first host in the network.
|
|
|
|
//
|
|
|
|
// The daemon on which this is called will become the first host in the network,
|
|
|
|
// and will have full administrative privileges.
|
2024-09-09 14:34:00 +00:00
|
|
|
//
|
|
|
|
// Errors:
|
|
|
|
// - network.ErrInvalidConfig
|
2024-09-07 11:52:32 +00:00
|
|
|
func (d *Daemon) CreateNetwork(
|
2024-07-07 18:01:10 +00:00
|
|
|
ctx context.Context,
|
2024-11-09 20:13:33 +00:00
|
|
|
name, domain string,
|
|
|
|
ipNet nebula.IPNet,
|
|
|
|
hostName nebula.HostName,
|
2024-07-14 11:11:18 +00:00
|
|
|
) error {
|
2024-09-09 14:34:00 +00:00
|
|
|
creationParams := bootstrap.NewCreationParams(name, domain)
|
|
|
|
ctx = mctx.WithAnnotator(ctx, creationParams)
|
2024-07-13 12:34:06 +00:00
|
|
|
|
2024-09-10 20:51:33 +00:00
|
|
|
networkConfig, ok := pickNetworkConfig(
|
|
|
|
d.daemonConfig, creationParams,
|
|
|
|
)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("couldn't find network config for network being created")
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
d.l.Lock()
|
|
|
|
defer d.l.Unlock()
|
2024-07-13 12:34:06 +00:00
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
if joined, err := alreadyJoined(ctx, d.networks, creationParams); err != nil {
|
|
|
|
return fmt.Errorf("checking if already joined to network: %w", err)
|
|
|
|
} else if joined {
|
2024-09-09 14:34:00 +00:00
|
|
|
return ErrAlreadyJoined
|
2024-07-14 11:11:18 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
networkStateDir, networkRuntimeDir, err := networkDirs(
|
|
|
|
d.networksStateDir, d.networksRuntimeDir, creationParams.ID, false,
|
2024-09-07 13:11:04 +00:00
|
|
|
)
|
2024-07-14 11:11:18 +00:00
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return fmt.Errorf(
|
|
|
|
"creating sub-directories for network %q: %w",
|
|
|
|
creationParams.ID,
|
|
|
|
err,
|
|
|
|
)
|
2024-07-07 18:01:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
d.logger.Info(ctx, "Creating network")
|
|
|
|
n, err := network.Create(
|
|
|
|
ctx,
|
|
|
|
d.logger.WithNamespace("network"),
|
2024-09-10 20:51:33 +00:00
|
|
|
networkConfig,
|
2024-09-09 14:34:00 +00:00
|
|
|
d.envBinDirPath,
|
|
|
|
networkStateDir,
|
|
|
|
networkRuntimeDir,
|
2024-07-14 11:11:18 +00:00
|
|
|
creationParams,
|
2024-09-09 14:34:00 +00:00
|
|
|
ipNet,
|
2024-07-07 18:01:10 +00:00
|
|
|
hostName,
|
2024-09-09 14:34:00 +00:00
|
|
|
&network.Opts{
|
|
|
|
ChildrenOpts: d.opts.ChildrenOpts,
|
|
|
|
},
|
2024-07-07 18:01:10 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return fmt.Errorf("creating network: %w", err)
|
2024-07-07 18:01:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
d.logger.Info(ctx, "Network created successfully")
|
2024-11-09 20:13:33 +00:00
|
|
|
d.networks[creationParams.ID] = joinedNetwork{
|
|
|
|
Network: n,
|
|
|
|
userConfig: true,
|
|
|
|
}
|
|
|
|
|
2024-07-14 11:11:18 +00:00
|
|
|
return nil
|
2024-07-06 13:36:48 +00:00
|
|
|
}
|
2022-10-26 21:21:31 +00:00
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
// JoinNetwork joins the Daemon to an existing network using the given
|
|
|
|
// Bootstrap.
|
|
|
|
//
|
|
|
|
// Errors:
|
|
|
|
// - ErrAlreadyJoined
|
|
|
|
func (d *Daemon) JoinNetwork(
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx context.Context, newBootstrap network.JoiningBootstrap,
|
2024-07-07 10:44:49 +00:00
|
|
|
) error {
|
2024-09-10 20:51:33 +00:00
|
|
|
var (
|
|
|
|
creationParams = newBootstrap.Bootstrap.NetworkCreationParams
|
|
|
|
networkConfig, _ = pickNetworkConfig(d.daemonConfig, creationParams)
|
|
|
|
networkID = creationParams.ID
|
|
|
|
)
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx = mctx.WithAnnotator(ctx, newBootstrap.Bootstrap.NetworkCreationParams)
|
|
|
|
|
2024-07-07 10:44:49 +00:00
|
|
|
d.l.Lock()
|
2024-07-21 15:20:48 +00:00
|
|
|
defer d.l.Unlock()
|
2024-07-07 10:44:49 +00:00
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
if joined, err := alreadyJoined(ctx, d.networks, creationParams); err != nil {
|
|
|
|
return fmt.Errorf("checking if already joined to network: %w", err)
|
|
|
|
} else if joined {
|
2024-07-07 10:44:49 +00:00
|
|
|
return ErrAlreadyJoined
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
networkStateDir, networkRuntimeDir, err := networkDirs(
|
|
|
|
d.networksStateDir, d.networksRuntimeDir, networkID, false,
|
|
|
|
)
|
2024-07-14 09:58:39 +00:00
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return fmt.Errorf(
|
|
|
|
"creating sub-directories for network %q: %w", networkID, err,
|
|
|
|
)
|
2024-07-14 09:58:39 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
d.logger.Info(ctx, "Joining network")
|
|
|
|
n, err := network.Join(
|
|
|
|
ctx,
|
|
|
|
d.logger.WithNamespace("network"),
|
2024-09-10 20:51:33 +00:00
|
|
|
networkConfig,
|
2024-09-09 14:34:00 +00:00
|
|
|
newBootstrap,
|
|
|
|
d.envBinDirPath,
|
|
|
|
networkStateDir,
|
|
|
|
networkRuntimeDir,
|
|
|
|
&network.Opts{
|
|
|
|
ChildrenOpts: d.opts.ChildrenOpts,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"joining network %q: %w", networkID, err,
|
|
|
|
)
|
2024-07-07 18:01:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
d.logger.Info(ctx, "Network joined successfully")
|
2024-11-09 20:13:33 +00:00
|
|
|
d.networks[networkID] = joinedNetwork{
|
|
|
|
Network: n,
|
|
|
|
userConfig: true,
|
|
|
|
}
|
2024-07-20 09:07:11 +00:00
|
|
|
return nil
|
2024-07-07 10:44:49 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
func withNetwork[Res any](
|
|
|
|
ctx context.Context,
|
|
|
|
d *Daemon,
|
|
|
|
fn func(context.Context, network.Network) (Res, error),
|
|
|
|
) (
|
|
|
|
Res, error,
|
|
|
|
) {
|
|
|
|
d.l.RLock()
|
|
|
|
defer d.l.RUnlock()
|
2024-07-12 14:03:37 +00:00
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
network, err := pickNetwork(ctx, d.networks, d.networksStateDir)
|
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
var zero Res
|
2024-11-09 20:13:33 +00:00
|
|
|
return zero, err
|
2024-09-07 11:52:32 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
return fn(ctx, network)
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
2024-09-07 11:52:32 +00:00
|
|
|
|
2024-09-24 09:03:18 +00:00
|
|
|
// GetNetworks returns all networks which have been joined by the Daemon,
|
|
|
|
// ordered by their name.
|
|
|
|
func (d *Daemon) GetNetworks(
|
|
|
|
ctx context.Context,
|
|
|
|
) (
|
|
|
|
[]bootstrap.CreationParams, error,
|
|
|
|
) {
|
|
|
|
d.l.RLock()
|
|
|
|
defer d.l.RUnlock()
|
|
|
|
|
|
|
|
res := make([]bootstrap.CreationParams, 0, len(d.networks))
|
|
|
|
for id, network := range d.networks {
|
|
|
|
creationParams, err := network.GetNetworkCreationParams(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"getting network creation params of network %q: %w", id, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, creationParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(res, func(i, j int) bool {
|
|
|
|
return res[i].Name < res[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-11-09 20:13:33 +00:00
|
|
|
// SetConfig applies a new NetworkConfig to a joined network. It uses the same
|
|
|
|
// mechanism as the rest of the [network.RPC] methods on the Daemon to choose a
|
|
|
|
// network.
|
|
|
|
//
|
|
|
|
// Besides the errors related to network choosing which are returned by all
|
|
|
|
// other [network.RPC] methods, this may also return ErrUserManagedNetworkConfig
|
|
|
|
// if the network's configuration is not managed by the Daemon, but is instead
|
|
|
|
// provided via the config by the user.
|
|
|
|
func (d *Daemon) SetConfig(
|
|
|
|
ctx context.Context, config daecommon.NetworkConfig,
|
|
|
|
) error {
|
|
|
|
d.l.RLock()
|
|
|
|
defer d.l.RUnlock()
|
|
|
|
|
|
|
|
// TODO needs to check that public addresses aren't being shared
|
|
|
|
// across networks, and whatever else happens in Config.Validate.
|
|
|
|
|
|
|
|
network, err := pickNetwork(ctx, d.networks, d.networksStateDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.userConfig {
|
|
|
|
return ErrUserManagedNetworkConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
return network.SetConfig(ctx, config)
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// GetHost implements the method for the network.RPC interface.
|
|
|
|
func (d *Daemon) GetHosts(ctx context.Context) ([]bootstrap.Host, error) {
|
|
|
|
return withNetwork(
|
|
|
|
ctx,
|
|
|
|
d,
|
|
|
|
func(ctx context.Context, n network.Network) ([]bootstrap.Host, error) {
|
|
|
|
return n.GetHosts(ctx)
|
|
|
|
},
|
|
|
|
)
|
2024-09-07 11:52:32 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// GetGarageClientParams implements the method for the network.RPC interface.
|
2024-09-07 11:52:32 +00:00
|
|
|
func (d *Daemon) GetGarageClientParams(
|
2024-07-13 12:34:06 +00:00
|
|
|
ctx context.Context,
|
|
|
|
) (
|
2024-09-09 14:34:00 +00:00
|
|
|
network.GarageClientParams, error,
|
2024-07-13 12:34:06 +00:00
|
|
|
) {
|
2024-09-09 14:34:00 +00:00
|
|
|
return withNetwork(
|
|
|
|
ctx,
|
|
|
|
d,
|
|
|
|
func(
|
|
|
|
ctx context.Context, n network.Network,
|
|
|
|
) (
|
|
|
|
network.GarageClientParams, error,
|
|
|
|
) {
|
|
|
|
return n.GetGarageClientParams(ctx)
|
|
|
|
},
|
|
|
|
)
|
2024-07-13 12:34:06 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// GetNebulaCAPublicCredentials implements the method for the network.RPC
|
|
|
|
// interface.
|
2024-09-07 11:52:32 +00:00
|
|
|
func (d *Daemon) GetNebulaCAPublicCredentials(
|
|
|
|
ctx context.Context,
|
|
|
|
) (
|
|
|
|
nebula.CAPublicCredentials, error,
|
|
|
|
) {
|
2024-09-09 14:34:00 +00:00
|
|
|
return withNetwork(
|
|
|
|
ctx,
|
|
|
|
d,
|
|
|
|
func(
|
|
|
|
ctx context.Context, n network.Network,
|
|
|
|
) (
|
|
|
|
nebula.CAPublicCredentials, error,
|
|
|
|
) {
|
|
|
|
return n.GetNebulaCAPublicCredentials(ctx)
|
|
|
|
},
|
|
|
|
)
|
2024-09-07 11:52:32 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// RemoveHost implements the method for the network.RPC interface.
|
2024-09-07 11:52:32 +00:00
|
|
|
func (d *Daemon) RemoveHost(ctx context.Context, hostName nebula.HostName) error {
|
2024-09-09 14:34:00 +00:00
|
|
|
_, err := withNetwork(
|
|
|
|
ctx,
|
|
|
|
d,
|
|
|
|
func(
|
|
|
|
ctx context.Context, n network.Network,
|
|
|
|
) (
|
|
|
|
struct{}, error,
|
|
|
|
) {
|
|
|
|
return struct{}{}, n.RemoveHost(ctx, hostName)
|
|
|
|
},
|
2024-07-21 15:03:59 +00:00
|
|
|
)
|
2024-09-09 14:34:00 +00:00
|
|
|
return err
|
2024-09-07 11:52:32 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// CreateHost implements the method for the network.RPC interface.
|
2024-09-07 11:52:32 +00:00
|
|
|
func (d *Daemon) CreateHost(
|
2024-07-13 14:31:52 +00:00
|
|
|
ctx context.Context,
|
|
|
|
hostName nebula.HostName,
|
2024-09-09 14:34:00 +00:00
|
|
|
opts network.CreateHostOpts,
|
2024-07-13 14:31:52 +00:00
|
|
|
) (
|
2024-09-09 14:34:00 +00:00
|
|
|
network.JoiningBootstrap, error,
|
2024-07-13 14:31:52 +00:00
|
|
|
) {
|
2024-09-09 14:34:00 +00:00
|
|
|
return withNetwork(
|
|
|
|
ctx,
|
|
|
|
d,
|
|
|
|
func(
|
|
|
|
ctx context.Context, n network.Network,
|
|
|
|
) (
|
|
|
|
network.JoiningBootstrap, error,
|
|
|
|
) {
|
|
|
|
return n.CreateHost(ctx, hostName, opts)
|
|
|
|
},
|
2024-07-20 10:36:21 +00:00
|
|
|
)
|
2024-07-13 14:31:52 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// CreateNebulaCertificate implements the method for the network.RPC interface.
|
2024-09-07 11:52:32 +00:00
|
|
|
func (d *Daemon) CreateNebulaCertificate(
|
2024-07-13 14:08:13 +00:00
|
|
|
ctx context.Context,
|
|
|
|
hostName nebula.HostName,
|
|
|
|
hostPubKey nebula.EncryptingPublicKey,
|
|
|
|
) (
|
|
|
|
nebula.Certificate, error,
|
|
|
|
) {
|
2024-09-09 14:34:00 +00:00
|
|
|
return withNetwork(
|
|
|
|
ctx,
|
|
|
|
d,
|
|
|
|
func(
|
|
|
|
ctx context.Context, n network.Network,
|
|
|
|
) (
|
|
|
|
nebula.Certificate, error,
|
|
|
|
) {
|
|
|
|
return n.CreateNebulaCertificate(ctx, hostName, hostPubKey)
|
|
|
|
},
|
|
|
|
)
|
2024-07-13 14:08:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-23 18:18:11 +00:00
|
|
|
func (d *Daemon) GetConfig(
|
|
|
|
ctx context.Context,
|
|
|
|
) (
|
|
|
|
daecommon.NetworkConfig, error,
|
|
|
|
) {
|
|
|
|
return withNetwork(
|
|
|
|
ctx,
|
|
|
|
d,
|
|
|
|
func(
|
|
|
|
ctx context.Context, n network.Network,
|
|
|
|
) (
|
|
|
|
daecommon.NetworkConfig, error,
|
|
|
|
) {
|
|
|
|
return n.GetConfig(ctx)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
// Shutdown blocks until all resources held or created by the daemon,
|
|
|
|
// including child processes it has started, have been cleaned up.
|
|
|
|
//
|
|
|
|
// If this returns an error then it's possible that child processes are
|
|
|
|
// still running and are no longer managed.
|
|
|
|
func (d *Daemon) Shutdown() error {
|
2024-07-07 10:44:49 +00:00
|
|
|
d.l.Lock()
|
|
|
|
defer d.l.Unlock()
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
var (
|
|
|
|
errCh = make(chan error, len(d.networks))
|
|
|
|
errs []error
|
|
|
|
)
|
|
|
|
|
|
|
|
for id := range d.networks {
|
|
|
|
var (
|
|
|
|
id = id
|
|
|
|
n = d.networks[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := n.Shutdown(); err != nil {
|
|
|
|
errCh <- fmt.Errorf("shutting down network %q: %w", id, err)
|
|
|
|
}
|
|
|
|
errCh <- nil
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for range cap(errCh) {
|
|
|
|
errs = append(errs, <-errCh)
|
2024-07-07 10:44:49 +00:00
|
|
|
}
|
2024-09-09 14:34:00 +00:00
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
return errors.Join(errs...)
|
2022-10-26 21:21:31 +00:00
|
|
|
}
|