isle/go/daemon/network/bootstrap.go

95 lines
2.0 KiB
Go
Raw Normal View History

package network
import (
"fmt"
"isle/bootstrap"
"isle/daemon/daecommon"
"isle/garage/garagesrv"
"isle/jsonutil"
"maps"
"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
2024-12-17 15:47:33 +00:00
}
func loadBootstrapFromStateDir(
stateDirPath string,
) (
bootstrap.Bootstrap, error,
) {
var (
currBootstrap bootstrap.Bootstrap
bootstrapFilePath = bootstrap.StateDirPath(stateDirPath)
)
if err := jsonutil.LoadFile(&currBootstrap, bootstrapFilePath); err != nil {
return bootstrap.Bootstrap{}, fmt.Errorf(
"loading bootstrap from %q: %w", bootstrapFilePath, err,
)
}
return currBootstrap, nil
}
func coalesceNetworkConfigAndBootstrap(
networkConfig daecommon.NetworkConfig, hostBootstrap bootstrap.Bootstrap,
) (
bootstrap.Bootstrap, error,
) {
host := bootstrap.Host{
HostAssigned: hostBootstrap.HostAssigned,
HostConfigured: bootstrap.HostConfigured{
Nebula: bootstrap.NebulaHost{
PublicAddr: networkConfig.VPN.PublicAddr,
},
},
}
if allocs := networkConfig.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 = maps.Clone(hostBootstrap.Hosts)
hostBootstrap.Hosts[host.Name] = host
return hostBootstrap, nil
}