package main import ( "context" "fmt" "isle/bootstrap" "isle/daemon" "isle/garage" "isle/garage/garagesrv" "net" "path/filepath" "strconv" "code.betamike.com/micropelago/pmux/pmuxlib" "github.com/mediocregopher/mediocre-go-lib/v2/mctx" "github.com/mediocregopher/mediocre-go-lib/v2/mlog" ) func garageAdminClientLogger(logger *mlog.Logger) *mlog.Logger { return logger.WithNamespace("garageAdminClient") } // newGarageAdminClient will return an AdminClient for a local garage instance, // or it will _panic_ if there is no local instance configured. func newGarageAdminClient( logger *mlog.Logger, hostBootstrap bootstrap.Bootstrap, daemonConfig daemon.Config, ) *garage.AdminClient { thisHost := hostBootstrap.ThisHost() return garage.NewAdminClient( garageAdminClientLogger(logger), net.JoinHostPort( thisHost.IP().String(), strconv.Itoa(daemonConfig.Storage.Allocations[0].AdminPort), ), hostBootstrap.Garage.AdminToken, ) } func waitForGarageAndNebula( ctx context.Context, logger *mlog.Logger, hostBootstrap bootstrap.Bootstrap, daemonConfig daemon.Config, ) error { if err := waitForNebula(ctx, hostBootstrap); err != nil { return fmt.Errorf("waiting for nebula to start: %w", err) } allocs := daemonConfig.Storage.Allocations // if this host doesn't have any allocations specified then fall back to // waiting for nebula if len(allocs) == 0 { return nil } adminClientLogger := garageAdminClientLogger(logger) for _, alloc := range allocs { adminAddr := net.JoinHostPort( hostBootstrap.ThisHost().IP().String(), strconv.Itoa(alloc.AdminPort), ) adminClient := garage.NewAdminClient( adminClientLogger, adminAddr, hostBootstrap.Garage.AdminToken, ) ctx := mctx.Annotate(ctx, "garageAdminAddr", adminAddr) logger.Debug(ctx, "wating for garage instance to start") 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 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)) } func garageWriteChildConfig( hostBootstrap bootstrap.Bootstrap, alloc daemon.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( envRuntimeDirPath, fmt.Sprintf("garage-%d.toml", alloc.RPCPort), ) err := garagesrv.WriteGarageTomlFile(garageTomlPath, garagesrv.GarageTomlData{ MetaPath: alloc.MetaPath, DataPath: alloc.DataPath, RPCSecret: hostBootstrap.Garage.RPCSecret, AdminToken: hostBootstrap.Garage.AdminToken, 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( hostBootstrap bootstrap.Bootstrap, daemonConfig daemon.Config, ) ( []pmuxlib.ProcessConfig, error, ) { var pmuxProcConfigs []pmuxlib.ProcessConfig for _, alloc := range daemonConfig.Storage.Allocations { childConfigPath, err := garageWriteChildConfig(hostBootstrap, alloc) if err != nil { return nil, fmt.Errorf("writing child config file for alloc %+v: %w", alloc, err) } pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{ Name: fmt.Sprintf("garage-%d", alloc.RPCPort), Cmd: binPath("garage"), Args: []string{"-c", childConfigPath, "server"}, StartAfterFunc: func(ctx context.Context) error { return waitForNebula(ctx, hostBootstrap) }, }) } return pmuxProcConfigs, nil } func garageInitializeGlobalBucket( ctx context.Context, logger *mlog.Logger, hostBootstrap bootstrap.Bootstrap, daemonConfig daemon.Config, ) ( garage.S3APICredentials, error, ) { adminClient := newGarageAdminClient(logger, hostBootstrap, daemonConfig) creds, err := adminClient.CreateS3APICredentials( ctx, garage.GlobalBucketS3APICredentialsName, ) if err != nil { return creds, fmt.Errorf("creating global bucket credentials: %w", err) } bucketID, err := adminClient.CreateBucket(ctx, garage.GlobalBucket) if err != nil { return creds, fmt.Errorf("creating global bucket: %w", err) } if err := adminClient.GrantBucketPermissions( ctx, bucketID, creds.ID, garage.BucketPermissionRead, garage.BucketPermissionWrite, ); err != nil { return creds, fmt.Errorf( "granting permissions to shared global bucket key: %w", err, ) } return creds, nil } func garageApplyLayout( ctx context.Context, logger *mlog.Logger, hostBootstrap bootstrap.Bootstrap, daemonConfig daemon.Config, ) error { var ( adminClient = newGarageAdminClient(logger, hostBootstrap, daemonConfig) thisHost = hostBootstrap.ThisHost() hostName = thisHost.Name allocs = daemonConfig.Storage.Allocations peers = make([]garage.PeerLayout, len(allocs)) ) for i, alloc := range allocs { id := bootstrapGarageHostForAlloc(thisHost, alloc).ID zone := hostName 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) }