Brian Picciano
8c3e6a2845
In a world where the daemon can manage more than one network, the Daemon is really responsible only for knowing which networks are currently joined, creating/joining/leaving networks, and routing incoming RPC requests to the correct network handler as needed. The new network package, with its Network interface, inherits most of the logic that Daemon used to have, leaving Daemon only the parts needed for the functionality just described. There's a lot of cleanup done here in order to really nail down the separation of concerns between the two, especially around directory creation.
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package network
|
|
|
|
import (
|
|
"fmt"
|
|
"isle/bootstrap"
|
|
"isle/daemon/daecommon"
|
|
"isle/garage/garagesrv"
|
|
"isle/jsonutil"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func writeBootstrapToStateDir(
|
|
stateDirPath string, hostBootstrap bootstrap.Bootstrap,
|
|
) error {
|
|
var (
|
|
path = bootstrap.StateDirPath(stateDirPath)
|
|
dirPath = filepath.Dir(path)
|
|
)
|
|
|
|
if err := os.MkdirAll(dirPath, 0700); err != nil {
|
|
return fmt.Errorf("creating directory %q: %w", dirPath, err)
|
|
}
|
|
|
|
if err := jsonutil.WriteFile(hostBootstrap, path, 0600); err != nil {
|
|
return fmt.Errorf("writing bootstrap to %q: %w", path, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func coalesceDaemonConfigAndBootstrap(
|
|
daemonConfig daecommon.Config, hostBootstrap bootstrap.Bootstrap,
|
|
) (
|
|
bootstrap.Bootstrap, error,
|
|
) {
|
|
host := bootstrap.Host{
|
|
HostAssigned: hostBootstrap.HostAssigned,
|
|
HostConfigured: bootstrap.HostConfigured{
|
|
Nebula: bootstrap.NebulaHost{
|
|
PublicAddr: daemonConfig.VPN.PublicAddr,
|
|
},
|
|
},
|
|
}
|
|
|
|
if allocs := daemonConfig.Storage.Allocations; len(allocs) > 0 {
|
|
|
|
for i, alloc := range allocs {
|
|
|
|
id, rpcPort, err := garagesrv.InitAlloc(alloc.MetaPath, alloc.RPCPort)
|
|
if err != nil {
|
|
return bootstrap.Bootstrap{}, fmt.Errorf(
|
|
"initializing alloc at %q: %w", alloc.MetaPath, err,
|
|
)
|
|
}
|
|
|
|
host.Garage.Instances = append(host.Garage.Instances, bootstrap.GarageHostInstance{
|
|
ID: id,
|
|
RPCPort: rpcPort,
|
|
S3APIPort: alloc.S3APIPort,
|
|
})
|
|
|
|
allocs[i].RPCPort = rpcPort
|
|
}
|
|
}
|
|
|
|
hostBootstrap.Hosts[host.Name] = host
|
|
|
|
return hostBootstrap, nil
|
|
}
|