isle/go/daemon/network/bootstrap.go

89 lines
1.9 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 applyNetworkConfigToBootstrap(
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,
},
},
}
2025-01-07 14:40:50 +00:00
for _, alloc := range networkConfig.Storage.Allocations {
id, err := garagesrv.LoadAllocID(alloc.MetaPath)
if err != nil {
return bootstrap.Bootstrap{}, fmt.Errorf(
"getting ID of alloc at %q: %w", alloc.MetaPath, err,
)
}
2025-01-07 14:40:50 +00:00
host.Garage.Instances = append(
host.Garage.Instances,
bootstrap.GarageHostInstance{
ID: id,
RPCPort: alloc.RPCPort,
S3APIPort: alloc.S3APIPort,
},
)
}
hostBootstrap.Hosts = maps.Clone(hostBootstrap.Hosts)
hostBootstrap.Hosts[host.Name] = host
return hostBootstrap, nil
}