2022-10-20 19:59:46 +00:00
|
|
|
package main
|
2022-10-16 20:17:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-11 12:54:26 +00:00
|
|
|
"fmt"
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/daemon"
|
|
|
|
"isle/garage"
|
2022-10-16 20:17:26 +00:00
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
2023-07-06 15:51:38 +00:00
|
|
|
"code.betamike.com/micropelago/pmux/pmuxlib"
|
2022-11-16 16:25:55 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
2022-11-13 15:45:42 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
2022-10-16 20:17:26 +00:00
|
|
|
)
|
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
func garageAdminClientLogger(logger *mlog.Logger) *mlog.Logger {
|
2022-11-16 16:27:42 +00:00
|
|
|
return logger.WithNamespace("garageAdminClient")
|
2022-11-13 15:45:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
// newGarageAdminClient will return an AdminClient for a local garage instance,
|
|
|
|
// or it will _panic_ if there is no local instance configured.
|
|
|
|
func newGarageAdminClient(
|
2022-11-13 15:45:42 +00:00
|
|
|
logger *mlog.Logger,
|
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
daemonConfig daemon.Config,
|
2022-10-26 21:21:31 +00:00
|
|
|
) *garage.AdminClient {
|
2022-10-19 14:20:26 +00:00
|
|
|
|
2022-10-26 22:23:39 +00:00
|
|
|
thisHost := hostBootstrap.ThisHost()
|
2022-10-26 21:21:31 +00:00
|
|
|
|
|
|
|
return garage.NewAdminClient(
|
|
|
|
net.JoinHostPort(
|
2022-10-29 19:11:40 +00:00
|
|
|
thisHost.IP().String(),
|
2022-10-26 21:21:31 +00:00
|
|
|
strconv.Itoa(daemonConfig.Storage.Allocations[0].AdminPort),
|
|
|
|
),
|
2022-11-02 13:34:40 +00:00
|
|
|
hostBootstrap.Garage.AdminToken,
|
2022-11-13 15:45:42 +00:00
|
|
|
garageAdminClientLogger(logger),
|
2022-10-26 21:21:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForGarageAndNebula(
|
2022-10-26 22:23:39 +00:00
|
|
|
ctx context.Context,
|
2022-11-13 15:45:42 +00:00
|
|
|
logger *mlog.Logger,
|
2022-10-26 22:23:39 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
daemonConfig daemon.Config,
|
2022-10-26 21:21:31 +00:00
|
|
|
) error {
|
|
|
|
|
2022-11-13 13:55:25 +00:00
|
|
|
if err := waitForNebula(ctx, hostBootstrap); err != nil {
|
|
|
|
return fmt.Errorf("waiting for nebula to start: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
allocs := daemonConfig.Storage.Allocations
|
2022-10-20 19:59:46 +00:00
|
|
|
|
|
|
|
// if this host doesn't have any allocations specified then fall back to
|
|
|
|
// waiting for nebula
|
|
|
|
if len(allocs) == 0 {
|
2022-11-13 13:55:25 +00:00
|
|
|
return nil
|
2022-10-20 19:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 16:25:55 +00:00
|
|
|
adminClientLogger := garageAdminClientLogger(logger)
|
2022-11-13 15:45:42 +00:00
|
|
|
|
2022-10-20 19:59:46 +00:00
|
|
|
for _, alloc := range allocs {
|
2022-10-19 14:20:26 +00:00
|
|
|
|
|
|
|
adminAddr := net.JoinHostPort(
|
2022-10-29 19:11:40 +00:00
|
|
|
hostBootstrap.ThisHost().IP().String(),
|
2022-10-19 14:20:26 +00:00
|
|
|
strconv.Itoa(alloc.AdminPort),
|
|
|
|
)
|
|
|
|
|
|
|
|
adminClient := garage.NewAdminClient(
|
|
|
|
adminAddr,
|
2022-11-02 13:34:40 +00:00
|
|
|
hostBootstrap.Garage.AdminToken,
|
2022-11-16 16:25:55 +00:00
|
|
|
adminClientLogger,
|
2022-10-19 14:20:26 +00:00
|
|
|
)
|
|
|
|
|
2022-11-16 16:27:42 +00:00
|
|
|
ctx := mctx.Annotate(ctx, "garageAdminAddr", adminAddr)
|
2022-11-16 16:25:55 +00:00
|
|
|
logger.Debug(ctx, "wating for garage instance to start")
|
|
|
|
|
2022-10-19 14:20:26 +00:00
|
|
|
if err := adminClient.Wait(ctx); err != nil {
|
2022-11-13 13:55:25 +00:00
|
|
|
return fmt.Errorf("waiting for garage instance %q to start up: %w", adminAddr, err)
|
2022-10-19 14:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-28 22:09:18 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2023-01-17 19:31:22 +00:00
|
|
|
// This assumes that coalesceDaemonConfigAndBootstrap has already been called.
|
2022-10-28 22:09:18 +00:00
|
|
|
func bootstrapGarageHostForAlloc(
|
|
|
|
host bootstrap.Host,
|
|
|
|
alloc daemon.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))
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
func garageWriteChildConfig(
|
2022-10-26 22:23:39 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
2022-10-26 21:21:31 +00:00
|
|
|
alloc daemon.ConfigStorageAllocation,
|
2022-10-16 20:17:26 +00:00
|
|
|
) (
|
|
|
|
string, error,
|
|
|
|
) {
|
|
|
|
|
2022-10-26 22:23:39 +00:00
|
|
|
thisHost := hostBootstrap.ThisHost()
|
2022-10-28 22:09:18 +00:00
|
|
|
id := bootstrapGarageHostForAlloc(thisHost, alloc).ID
|
|
|
|
|
|
|
|
peer := garage.LocalPeer{
|
|
|
|
RemotePeer: garage.RemotePeer{
|
|
|
|
ID: id,
|
2022-10-29 19:11:40 +00:00
|
|
|
IP: thisHost.IP().String(),
|
2022-10-28 22:09:18 +00:00
|
|
|
RPCPort: alloc.RPCPort,
|
|
|
|
S3APIPort: alloc.S3APIPort,
|
|
|
|
},
|
|
|
|
AdminPort: alloc.AdminPort,
|
2022-10-16 20:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
garageTomlPath := filepath.Join(
|
2022-10-26 22:37:03 +00:00
|
|
|
envRuntimeDirPath, fmt.Sprintf("garage-%d.toml", alloc.RPCPort),
|
2022-10-16 20:17:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
err := garage.WriteGarageTomlFile(garageTomlPath, garage.GarageTomlData{
|
|
|
|
MetaPath: alloc.MetaPath,
|
|
|
|
DataPath: alloc.DataPath,
|
|
|
|
|
2022-11-02 13:34:40 +00:00
|
|
|
RPCSecret: hostBootstrap.Garage.RPCSecret,
|
|
|
|
AdminToken: hostBootstrap.Garage.AdminToken,
|
2022-10-16 20:17:26 +00:00
|
|
|
|
2022-10-28 22:09:18 +00:00
|
|
|
RPCAddr: peer.RPCAddr(),
|
|
|
|
S3APIAddr: peer.S3APIAddr(),
|
|
|
|
AdminAddr: peer.AdminAddr(),
|
2022-10-16 20:17:26 +00:00
|
|
|
|
2022-10-26 22:23:39 +00:00
|
|
|
BootstrapPeers: hostBootstrap.GarageRPCPeerAddrs(),
|
2022-10-16 20:17:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("creating garage.toml file at %q: %w", garageTomlPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return garageTomlPath, nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
func garagePmuxProcConfigs(
|
2022-10-26 22:23:39 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
daemonConfig daemon.Config,
|
2022-10-26 21:21:31 +00:00
|
|
|
) (
|
|
|
|
[]pmuxlib.ProcessConfig, error,
|
|
|
|
) {
|
2022-10-16 20:17:26 +00:00
|
|
|
|
|
|
|
var pmuxProcConfigs []pmuxlib.ProcessConfig
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
for _, alloc := range daemonConfig.Storage.Allocations {
|
2022-10-16 20:17:26 +00:00
|
|
|
|
2022-10-26 22:37:03 +00:00
|
|
|
childConfigPath, err := garageWriteChildConfig(hostBootstrap, alloc)
|
2022-10-16 20:17:26 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("writing child config file for alloc %+v: %w", alloc, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
|
2022-10-20 19:59:46 +00:00
|
|
|
Name: fmt.Sprintf("garage-%d", alloc.RPCPort),
|
2023-04-23 14:30:47 +00:00
|
|
|
Cmd: binPath("garage"),
|
2022-10-26 21:21:31 +00:00
|
|
|
Args: []string{"-c", childConfigPath, "server"},
|
2022-10-20 19:59:46 +00:00
|
|
|
StartAfterFunc: func(ctx context.Context) error {
|
2022-10-26 22:23:39 +00:00
|
|
|
return waitForNebula(ctx, hostBootstrap)
|
2022-10-20 19:59:46 +00:00
|
|
|
},
|
2022-10-16 20:17:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return pmuxProcConfigs, nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
func garageInitializeGlobalBucket(
|
2022-10-26 22:23:39 +00:00
|
|
|
ctx context.Context,
|
2022-11-13 15:45:42 +00:00
|
|
|
logger *mlog.Logger,
|
2022-10-26 22:23:39 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
daemonConfig daemon.Config,
|
2024-06-11 12:54:26 +00:00
|
|
|
) (
|
|
|
|
garage.S3APICredentials, error,
|
|
|
|
) {
|
|
|
|
adminClient := newGarageAdminClient(logger, hostBootstrap, daemonConfig)
|
2022-10-19 14:20:26 +00:00
|
|
|
|
2024-06-11 12:54:26 +00:00
|
|
|
var createKeyRes struct {
|
|
|
|
ID string `json:"accessKeyId"`
|
|
|
|
Secret string `json:"secretAccessKey"`
|
|
|
|
}
|
2022-10-19 14:20:26 +00:00
|
|
|
|
|
|
|
// first attempt to import the key
|
2024-06-11 12:54:26 +00:00
|
|
|
err := adminClient.Do(
|
|
|
|
ctx, &createKeyRes, "POST", "/v1/key", map[string]string{
|
|
|
|
"name": "shared-global-bucket-key",
|
|
|
|
},
|
|
|
|
)
|
2022-10-19 14:20:26 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-06-11 12:54:26 +00:00
|
|
|
return garage.S3APICredentials{}, fmt.Errorf(
|
|
|
|
"importing global bucket key into garage: %w", err,
|
|
|
|
)
|
2022-10-19 14:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create global bucket
|
2024-06-11 12:54:26 +00:00
|
|
|
var createBucketRes struct {
|
2022-10-19 14:20:26 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = adminClient.Do(
|
2024-06-11 12:54:26 +00:00
|
|
|
ctx, &createBucketRes, "POST", "/v1/bucket", map[string]string{
|
|
|
|
"globalAlias": garage.GlobalBucket,
|
|
|
|
},
|
2022-10-19 14:20:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-06-11 12:54:26 +00:00
|
|
|
return garage.S3APICredentials{}, fmt.Errorf(
|
|
|
|
"creating global bucket: %w", err,
|
|
|
|
)
|
2022-10-19 14:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allow shared global bucket key to perform all operations
|
2024-06-11 12:54:26 +00:00
|
|
|
err = adminClient.Do(ctx, nil, "POST", "/v1/bucket/allow", map[string]interface{}{
|
|
|
|
"bucketId": createBucketRes.ID,
|
|
|
|
"accessKeyId": createKeyRes.ID,
|
2022-10-19 14:20:26 +00:00
|
|
|
"permissions": map[string]bool{
|
|
|
|
"read": true,
|
|
|
|
"write": true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2024-06-11 12:54:26 +00:00
|
|
|
return garage.S3APICredentials{}, fmt.Errorf(
|
|
|
|
"granting permissions to shared global bucket key: %w", err,
|
|
|
|
)
|
2022-10-19 14:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 12:54:26 +00:00
|
|
|
return garage.S3APICredentials{
|
|
|
|
ID: createKeyRes.ID,
|
|
|
|
Secret: createKeyRes.Secret,
|
|
|
|
}, nil
|
2022-10-19 14:20:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
func garageApplyLayout(
|
2022-10-26 22:23:39 +00:00
|
|
|
ctx context.Context,
|
2022-11-13 15:45:42 +00:00
|
|
|
logger *mlog.Logger,
|
2022-10-26 22:23:39 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
daemonConfig daemon.Config,
|
2022-10-26 21:21:31 +00:00
|
|
|
) error {
|
2022-10-19 14:20:26 +00:00
|
|
|
|
|
|
|
var (
|
2022-11-13 15:45:42 +00:00
|
|
|
adminClient = newGarageAdminClient(logger, hostBootstrap, daemonConfig)
|
2022-10-26 22:23:39 +00:00
|
|
|
thisHost = hostBootstrap.ThisHost()
|
2022-10-19 14:20:26 +00:00
|
|
|
hostName = thisHost.Name
|
2022-10-26 21:21:31 +00:00
|
|
|
allocs = daemonConfig.Storage.Allocations
|
2022-10-19 14:20:26 +00:00
|
|
|
)
|
2022-10-16 20:17:26 +00:00
|
|
|
|
|
|
|
type peerLayout struct {
|
2024-06-11 12:54:26 +00:00
|
|
|
ID string `json:"id"`
|
2022-10-16 20:17:26 +00:00
|
|
|
Capacity int `json:"capacity"`
|
|
|
|
Zone string `json:"zone"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-06-11 12:54:26 +00:00
|
|
|
clusterLayout := make([]peerLayout, len(allocs))
|
2022-10-16 20:17:26 +00:00
|
|
|
|
2024-06-11 12:54:26 +00:00
|
|
|
for i, alloc := range allocs {
|
2022-10-16 20:17:26 +00:00
|
|
|
|
2022-10-28 22:09:18 +00:00
|
|
|
id := bootstrapGarageHostForAlloc(thisHost, alloc).ID
|
2022-10-16 20:17:26 +00:00
|
|
|
|
2022-11-13 19:09:03 +00:00
|
|
|
zone := hostName
|
|
|
|
if alloc.Zone != "" {
|
|
|
|
zone = alloc.Zone
|
|
|
|
}
|
|
|
|
|
2024-06-11 12:54:26 +00:00
|
|
|
clusterLayout[i] = peerLayout{
|
|
|
|
ID: id,
|
|
|
|
Capacity: alloc.Capacity * 1_000_000_000,
|
2022-11-13 19:09:03 +00:00
|
|
|
Zone: zone,
|
2022-10-25 19:15:09 +00:00
|
|
|
Tags: []string{},
|
2022-10-16 20:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 12:54:26 +00:00
|
|
|
err := adminClient.Do(ctx, nil, "POST", "/v1/layout", clusterLayout)
|
2022-10-16 20:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("staging layout changes: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var clusterLayout struct {
|
2024-06-11 12:54:26 +00:00
|
|
|
Version int `json:"version"`
|
|
|
|
StagedRoleChanges []peerLayout `json:"stagedRoleChanges"`
|
2022-10-16 20:17:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 12:54:26 +00:00
|
|
|
if err := adminClient.Do(ctx, &clusterLayout, "GET", "/v1/layout", nil); err != nil {
|
2022-10-16 20:17:26 +00:00
|
|
|
return fmt.Errorf("retrieving staged layout change: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(clusterLayout.StagedRoleChanges) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
applyClusterLayout := struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
}{
|
|
|
|
Version: clusterLayout.Version + 1,
|
|
|
|
}
|
|
|
|
|
2024-06-11 12:54:26 +00:00
|
|
|
err := adminClient.Do(ctx, nil, "POST", "/v1/layout/apply", applyClusterLayout)
|
2022-10-16 20:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("applying new layout (new version:%d): %w", applyClusterLayout.Version, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|