2024-06-17 18:51:02 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/garage"
|
|
|
|
"isle/garage/garagesrv"
|
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"code.betamike.com/micropelago/pmux/pmuxlib"
|
2024-06-22 15:49:56 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2024-06-17 18:51:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func garageAdminClientLogger(logger *mlog.Logger) *mlog.Logger {
|
|
|
|
return logger.WithNamespace("garageAdminClient")
|
|
|
|
}
|
|
|
|
|
2024-07-14 10:19:39 +00:00
|
|
|
// newGarageAdminClient will return an AdminClient for a local garage instance,
|
2024-06-17 18:51:02 +00:00
|
|
|
// or it will _panic_ if there is no local instance configured.
|
2024-07-14 10:19:39 +00:00
|
|
|
func newGarageAdminClient(
|
2024-06-17 18:51:02 +00:00
|
|
|
logger *mlog.Logger,
|
2024-07-07 18:01:10 +00:00
|
|
|
daemonConfig Config,
|
2024-07-14 10:19:39 +00:00
|
|
|
adminToken string,
|
2024-06-17 18:51:02 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) *garage.AdminClient {
|
|
|
|
|
|
|
|
thisHost := hostBootstrap.ThisHost()
|
|
|
|
|
|
|
|
return garage.NewAdminClient(
|
|
|
|
garageAdminClientLogger(logger),
|
|
|
|
net.JoinHostPort(
|
|
|
|
thisHost.IP().String(),
|
2024-07-07 18:01:10 +00:00
|
|
|
strconv.Itoa(daemonConfig.Storage.Allocations[0].AdminPort),
|
2024-06-17 18:51:02 +00:00
|
|
|
),
|
2024-07-14 10:19:39 +00:00
|
|
|
adminToken,
|
2024-06-17 18:51:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
func waitForGarage(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
|
|
|
daemonConfig Config,
|
2024-07-14 10:19:39 +00:00
|
|
|
adminToken string,
|
2024-07-06 13:36:48 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) error {
|
2024-06-17 18:51:02 +00:00
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
allocs := daemonConfig.Storage.Allocations
|
2024-06-17 18:51:02 +00:00
|
|
|
|
|
|
|
// if this host doesn't have any allocations specified then fall back to
|
|
|
|
// waiting for nebula
|
|
|
|
if len(allocs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
adminClientLogger := garageAdminClientLogger(logger)
|
2024-06-17 18:51:02 +00:00
|
|
|
|
|
|
|
for _, alloc := range allocs {
|
|
|
|
|
|
|
|
adminAddr := net.JoinHostPort(
|
2024-07-06 13:36:48 +00:00
|
|
|
hostBootstrap.ThisHost().IP().String(),
|
2024-06-17 18:51:02 +00:00
|
|
|
strconv.Itoa(alloc.AdminPort),
|
|
|
|
)
|
|
|
|
|
|
|
|
adminClient := garage.NewAdminClient(
|
2024-07-14 10:19:39 +00:00
|
|
|
adminClientLogger, adminAddr, adminToken,
|
2024-06-17 18:51:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ctx := mctx.Annotate(ctx, "garageAdminAddr", adminAddr)
|
2024-07-06 13:36:48 +00:00
|
|
|
logger.Debug(ctx, "waiting for garage instance to start")
|
2024-06-17 18:51:02 +00:00
|
|
|
|
|
|
|
if err := adminClient.Wait(ctx); err != nil {
|
|
|
|
return fmt.Errorf("waiting for garage instance %q to start up: %w", adminAddr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// bootstrapGarageHostForAlloc returns the bootstrap.GarageHostInstance which
|
|
|
|
// corresponds with the given alloc from the daemon config. This will panic if
|
|
|
|
// no associated instance can be found.
|
|
|
|
//
|
|
|
|
// This assumes that coalesceDaemonConfigAndBootstrap has already been called.
|
|
|
|
func bootstrapGarageHostForAlloc(
|
|
|
|
host bootstrap.Host,
|
|
|
|
alloc ConfigStorageAllocation,
|
|
|
|
) bootstrap.GarageHostInstance {
|
|
|
|
|
|
|
|
for _, inst := range host.Garage.Instances {
|
|
|
|
if inst.RPCPort == alloc.RPCPort {
|
|
|
|
return inst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Sprintf("could not find alloc %+v in the bootstrap data", alloc))
|
|
|
|
}
|
|
|
|
|
|
|
|
func garageWriteChildConfig(
|
2024-07-14 10:19:39 +00:00
|
|
|
rpcSecret, runtimeDirPath, adminToken string,
|
2024-06-17 18:51:02 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
alloc ConfigStorageAllocation,
|
|
|
|
) (
|
|
|
|
string, error,
|
|
|
|
) {
|
|
|
|
|
|
|
|
thisHost := hostBootstrap.ThisHost()
|
|
|
|
id := bootstrapGarageHostForAlloc(thisHost, alloc).ID
|
|
|
|
|
|
|
|
peer := garage.LocalPeer{
|
|
|
|
RemotePeer: garage.RemotePeer{
|
|
|
|
ID: id,
|
|
|
|
IP: thisHost.IP().String(),
|
|
|
|
RPCPort: alloc.RPCPort,
|
|
|
|
S3APIPort: alloc.S3APIPort,
|
|
|
|
},
|
|
|
|
AdminPort: alloc.AdminPort,
|
|
|
|
}
|
|
|
|
|
|
|
|
garageTomlPath := filepath.Join(
|
|
|
|
runtimeDirPath, fmt.Sprintf("garage-%d.toml", alloc.RPCPort),
|
|
|
|
)
|
|
|
|
|
|
|
|
err := garagesrv.WriteGarageTomlFile(garageTomlPath, garagesrv.GarageTomlData{
|
|
|
|
MetaPath: alloc.MetaPath,
|
|
|
|
DataPath: alloc.DataPath,
|
|
|
|
|
2024-07-13 12:34:06 +00:00
|
|
|
RPCSecret: rpcSecret,
|
2024-07-14 10:19:39 +00:00
|
|
|
AdminToken: adminToken,
|
2024-06-17 18:51:02 +00:00
|
|
|
|
|
|
|
LocalPeer: peer,
|
|
|
|
BootstrapPeers: hostBootstrap.GaragePeers(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("creating garage.toml file at %q: %w", garageTomlPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return garageTomlPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func garagePmuxProcConfigs(
|
2024-07-13 12:34:06 +00:00
|
|
|
ctx context.Context,
|
2024-07-06 13:36:48 +00:00
|
|
|
logger *mlog.Logger,
|
2024-07-13 12:34:06 +00:00
|
|
|
rpcSecret, runtimeDirPath, binDirPath string,
|
2024-06-17 18:51:02 +00:00
|
|
|
daemonConfig Config,
|
2024-07-14 10:19:39 +00:00
|
|
|
adminToken string,
|
2024-07-06 13:36:48 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
2024-06-17 18:51:02 +00:00
|
|
|
) (
|
2024-07-19 14:50:20 +00:00
|
|
|
map[string]pmuxlib.ProcessConfig, error,
|
2024-06-17 18:51:02 +00:00
|
|
|
) {
|
2024-07-13 12:34:06 +00:00
|
|
|
var (
|
2024-07-19 14:50:20 +00:00
|
|
|
pmuxProcConfigs = map[string]pmuxlib.ProcessConfig{}
|
2024-07-13 12:34:06 +00:00
|
|
|
allocs = daemonConfig.Storage.Allocations
|
|
|
|
)
|
2024-06-17 18:51:02 +00:00
|
|
|
|
2024-07-13 12:34:06 +00:00
|
|
|
if len(allocs) > 0 && rpcSecret == "" {
|
|
|
|
logger.WarnString(ctx, "Not starting garage instances for storage allocations, missing garage RPC secret")
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-06-17 18:51:02 +00:00
|
|
|
|
2024-07-13 12:34:06 +00:00
|
|
|
for _, alloc := range allocs {
|
2024-06-17 18:51:02 +00:00
|
|
|
|
|
|
|
childConfigPath, err := garageWriteChildConfig(
|
2024-07-14 10:19:39 +00:00
|
|
|
rpcSecret, runtimeDirPath, adminToken, hostBootstrap, alloc,
|
2024-06-17 18:51:02 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("writing child config file for alloc %+v: %w", alloc, err)
|
|
|
|
}
|
|
|
|
|
2024-07-19 14:50:20 +00:00
|
|
|
procName := fmt.Sprintf("garage-%d", alloc.RPCPort)
|
|
|
|
pmuxProcConfigs[procName] = pmuxlib.ProcessConfig{
|
2024-06-17 18:51:02 +00:00
|
|
|
Cmd: filepath.Join(binDirPath, "garage"),
|
|
|
|
Args: []string{"-c", childConfigPath, "server"},
|
|
|
|
StartAfterFunc: func(ctx context.Context) error {
|
2024-07-06 13:36:48 +00:00
|
|
|
return waitForNebula(ctx, logger, hostBootstrap)
|
2024-06-17 18:51:02 +00:00
|
|
|
},
|
2024-07-19 14:50:20 +00:00
|
|
|
}
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pmuxProcConfigs, nil
|
|
|
|
}
|
|
|
|
|
2024-07-13 12:34:06 +00:00
|
|
|
func garageApplyLayout(
|
2024-06-17 18:51:02 +00:00
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
2024-07-07 18:01:10 +00:00
|
|
|
daemonConfig Config,
|
2024-07-14 10:19:39 +00:00
|
|
|
adminToken string,
|
2024-06-17 18:51:02 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) error {
|
|
|
|
|
|
|
|
var (
|
2024-07-14 10:19:39 +00:00
|
|
|
adminClient = newGarageAdminClient(
|
|
|
|
logger, daemonConfig, adminToken, hostBootstrap,
|
|
|
|
)
|
|
|
|
thisHost = hostBootstrap.ThisHost()
|
|
|
|
hostName = thisHost.Name
|
|
|
|
allocs = daemonConfig.Storage.Allocations
|
|
|
|
peers = make([]garage.PeerLayout, len(allocs))
|
2024-06-17 18:51:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for i, alloc := range allocs {
|
|
|
|
|
|
|
|
id := bootstrapGarageHostForAlloc(thisHost, alloc).ID
|
|
|
|
|
2024-07-12 13:30:21 +00:00
|
|
|
zone := string(hostName)
|
2024-06-17 18:51:02 +00:00
|
|
|
if alloc.Zone != "" {
|
|
|
|
zone = alloc.Zone
|
|
|
|
}
|
|
|
|
|
|
|
|
peers[i] = garage.PeerLayout{
|
|
|
|
ID: id,
|
|
|
|
Capacity: alloc.Capacity * 1_000_000_000,
|
|
|
|
Zone: zone,
|
|
|
|
Tags: []string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return adminClient.ApplyLayout(ctx, peers)
|
|
|
|
}
|