2024-06-24 16:55:36 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2024-07-14 09:58:39 +00:00
|
|
|
"encoding/json"
|
2024-06-24 16:55:36 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"isle/bootstrap"
|
2024-07-07 10:44:49 +00:00
|
|
|
"isle/garage/garagesrv"
|
2024-07-14 09:58:39 +00:00
|
|
|
"isle/jsonutil"
|
|
|
|
"isle/secrets"
|
2024-06-24 16:55:36 +00:00
|
|
|
)
|
|
|
|
|
2024-07-14 09:58:39 +00:00
|
|
|
// JoiningBootstrap wraps a normal Bootstrap to include extra data which a host
|
|
|
|
// might need while joining a network.
|
|
|
|
type JoiningBootstrap struct {
|
|
|
|
Bootstrap bootstrap.Bootstrap
|
|
|
|
Secrets map[secrets.ID]json.RawMessage
|
|
|
|
}
|
|
|
|
|
2024-06-24 16:55:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-14 09:58:39 +00:00
|
|
|
if err := jsonutil.WriteFile(hostBootstrap, path, 0700); err != nil {
|
|
|
|
return fmt.Errorf("writing bootstrap to %q: %w", path, err)
|
2024-06-24 16:55:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-14 09:58:39 +00:00
|
|
|
return nil
|
2024-06-24 16:55:36 +00:00
|
|
|
}
|
2024-07-07 10:44:49 +00:00
|
|
|
|
|
|
|
func coalesceDaemonConfigAndBootstrap(
|
|
|
|
daemonConfig 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
|
|
|
|
}
|