Factor out update-global-bucket

This commit is contained in:
Brian Picciano 2022-10-19 16:53:31 +02:00
parent 0d53d0c6d6
commit 8d92b9fe2b
7 changed files with 46 additions and 84 deletions

View File

@ -2,7 +2,6 @@ package bootstrap
import ( import (
"cryptic-net/garage" "cryptic-net/garage"
"fmt"
) )
// Paths within the bootstrap FS related to garage. // Paths within the bootstrap FS related to garage.
@ -73,15 +72,8 @@ func (b Bootstrap) ChooseGaragePeer() garage.Peer {
// GlobalBucketS3APIClient returns an S3 client pre-configured with access to // GlobalBucketS3APIClient returns an S3 client pre-configured with access to
// the global bucket. // the global bucket.
func (b Bootstrap) GlobalBucketS3APIClient() (garage.S3APIClient, error) { func (b Bootstrap) GlobalBucketS3APIClient() garage.S3APIClient {
addr := b.ChooseGaragePeer().S3APIAddr() addr := b.ChooseGaragePeer().S3APIAddr()
creds := b.GarageGlobalBucketS3APICredentials creds := b.GarageGlobalBucketS3APICredentials
return garage.NewS3APIClient(addr, creds)
client, err := garage.NewS3APIClient(addr, creds)
if err != nil {
return nil, fmt.Errorf("connecting to garage S3 API At %q: %w", addr, err)
}
return client, err
} }

View File

@ -17,7 +17,6 @@ package main
import ( import (
"cryptic-net/cmd/entrypoint" "cryptic-net/cmd/entrypoint"
nebula_entrypoint "cryptic-net/cmd/nebula-entrypoint" nebula_entrypoint "cryptic-net/cmd/nebula-entrypoint"
update_global_bucket "cryptic-net/cmd/update-global-bucket"
"fmt" "fmt"
"os" "os"
) )
@ -30,7 +29,6 @@ type mainFn struct {
var mainFns = []mainFn{ var mainFns = []mainFn{
{"entrypoint", entrypoint.Main}, {"entrypoint", entrypoint.Main},
{"nebula-entrypoint", nebula_entrypoint.Main}, {"nebula-entrypoint", nebula_entrypoint.Main},
{"update-global-bucket", update_global_bucket.Main},
} }
var mainFnsMap = func() map[string]mainFn { var mainFnsMap = func() map[string]mainFn {

View File

@ -217,10 +217,7 @@ var subCmdAdminCreateNetwork = subCmd{
return fmt.Errorf("initializing garage shared global bucket: %w", err) return fmt.Errorf("initializing garage shared global bucket: %w", err)
} }
garageS3Client, err := env.Bootstrap.GlobalBucketS3APIClient() garageS3Client := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("initializing garage shared global bucket client: %w", err)
}
fmt.Fprintln(os.Stderr, "writing data for this host into garage") fmt.Fprintln(os.Stderr, "writing data for this host into garage")
err = bootstrap.PutGarageBoostrapHost(ctx, garageS3Client, env.Bootstrap.ThisHost()) err = bootstrap.PutGarageBoostrapHost(ctx, garageS3Client, env.Bootstrap.ThisHost())
@ -279,10 +276,7 @@ var subCmdAdminMakeBootstrap = subCmd{
return fmt.Errorf("reading admin.tgz with --admin-path of %q: %w", *adminPath, err) return fmt.Errorf("reading admin.tgz with --admin-path of %q: %w", *adminPath, err)
} }
client, err := env.Bootstrap.GlobalBucketS3APIClient() client := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
// NOTE this isn't _technically_ required, but if the `hosts add` // NOTE this isn't _technically_ required, but if the `hosts add`
// command for this host has been run recently then it might not have // command for this host has been run recently then it might not have

View File

@ -76,8 +76,9 @@ func reloadBootstrap(env crypticnet.Env, s3Client garage.S3APIClient) (crypticne
// runs a single pmux process ofor daemon, returning only once the env.Context // runs a single pmux process ofor daemon, returning only once the env.Context
// has been canceled or bootstrap info has been changed. This will always block // has been canceled or bootstrap info has been changed. This will always block
// until the spawned pmux has returned. // until the spawned pmux has returned, and returns a copy of Env with updated
func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error { // boostrap info.
func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) (crypticnet.Env, error) {
thisHost := env.Bootstrap.ThisHost() thisHost := env.Bootstrap.ThisHost()
thisDaemon := env.ThisDaemon() thisDaemon := env.ThisDaemon()
@ -96,19 +97,12 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
garageChildrenPmuxProcConfigs, err := garageChildrenPmuxProcConfigs(env) garageChildrenPmuxProcConfigs, err := garageChildrenPmuxProcConfigs(env)
if err != nil { if err != nil {
return fmt.Errorf("generating garage children configs: %w", err) return crypticnet.Env{}, fmt.Errorf("generating garage children configs: %w", err)
} }
pmuxProcConfigs = append(pmuxProcConfigs, garageChildrenPmuxProcConfigs...) pmuxProcConfigs = append(pmuxProcConfigs, garageChildrenPmuxProcConfigs...)
} }
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
Name: "update-global-bucket",
Cmd: "bash",
Args: waitForGarageArgs(env, "update-global-bucket"),
NoRestartOn: []int{0},
})
pmuxConfig := pmuxlib.Config{Processes: pmuxProcConfigs} pmuxConfig := pmuxlib.Config{Processes: pmuxProcConfigs}
doneCh := env.Context.Done() doneCh := env.Context.Done()
@ -125,6 +119,25 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
pmuxlib.Run(ctx, pmuxConfig) pmuxlib.Run(ctx, pmuxConfig)
}() }()
wg.Add(1)
go func() {
defer wg.Done()
// TODO wait for garage or nebula, depending on if allocs are present
client := env.Bootstrap.GlobalBucketS3APIClient()
thisHost := env.Bootstrap.ThisHost()
err := doOnce(ctx, func(ctx context.Context) error {
fmt.Fprintln(os.Stderr, "updating host info in garage")
return bootstrap.PutGarageBoostrapHost(ctx, client, thisHost)
})
if err != nil {
fmt.Fprintf(os.Stderr, "aborted updating host info in garage: %v\n", err)
}
}()
if len(thisDaemon.Storage.Allocations) > 0 { if len(thisDaemon.Storage.Allocations) > 0 {
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -136,6 +149,7 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
} }
err := doOnce(ctx, func(ctx context.Context) error { err := doOnce(ctx, func(ctx context.Context) error {
fmt.Fprintln(os.Stderr, "applying garage layout")
return garageApplyLayout(ctx, env) return garageApplyLayout(ctx, env)
}) })
@ -152,7 +166,7 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
select { select {
case <-doneCh: case <-doneCh:
return env.Context.Err() return crypticnet.Env{}, env.Context.Err()
case <-ticker.C: case <-ticker.C:
@ -164,11 +178,11 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
) )
if env, changed, err = reloadBootstrap(env, s3Client); err != nil { if env, changed, err = reloadBootstrap(env, s3Client); err != nil {
return fmt.Errorf("reloading bootstrap: %w", err) return crypticnet.Env{}, fmt.Errorf("reloading bootstrap: %w", err)
} else if changed { } else if changed {
fmt.Fprintln(os.Stderr, "bootstrap info has changed, restarting all processes") fmt.Fprintln(os.Stderr, "bootstrap info has changed, restarting all processes")
return nil return env, nil
} }
} }
} }
@ -284,12 +298,9 @@ var subCmdDaemon = subCmd{
// create s3Client anew on every loop, in case the topology has // create s3Client anew on every loop, in case the topology has
// changed and we should be connecting to a different garage // changed and we should be connecting to a different garage
// endpoint. // endpoint.
s3Client, err := env.Bootstrap.GlobalBucketS3APIClient() s3Client := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
if err := runDaemonPmuxOnce(env, s3Client); errors.Is(err, context.Canceled) { if env, err = runDaemonPmuxOnce(env, s3Client); errors.Is(err, context.Canceled) {
return nil return nil
} else if err != nil { } else if err != nil {

View File

@ -60,11 +60,7 @@ var subCmdHostsAdd = subCmd{
// TODO validate that the IP is in the correct CIDR // TODO validate that the IP is in the correct CIDR
env := subCmdCtx.env env := subCmdCtx.env
client := env.Bootstrap.GlobalBucketS3APIClient()
client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
host := bootstrap.Host{ host := bootstrap.Host{
Name: *name, Name: *name,
@ -85,10 +81,7 @@ var subCmdHostsList = subCmd{
env := subCmdCtx.env env := subCmdCtx.env
client, err := env.Bootstrap.GlobalBucketS3APIClient() client := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
hostsMap, err := bootstrap.GetGarageBootstrapHosts(env.Context, client) hostsMap, err := bootstrap.GetGarageBootstrapHosts(env.Context, client)
if err != nil { if err != nil {
@ -128,11 +121,7 @@ var subCmdHostsDelete = subCmd{
} }
env := subCmdCtx.env env := subCmdCtx.env
client := env.Bootstrap.GlobalBucketS3APIClient()
client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
return bootstrap.RemoveGarageBootstrapHost(env.Context, client, *name) return bootstrap.RemoveGarageBootstrapHost(env.Context, client, *name)
}, },

View File

@ -1,30 +0,0 @@
package update_global_bucket
import (
crypticnet "cryptic-net"
"cryptic-net/bootstrap"
"log"
)
func Main() {
env, err := crypticnet.ReadEnv()
if err != nil {
log.Fatalf("reading envvars: %v", err)
}
client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
log.Fatalf("creating client for global bucket: %v", err)
}
err = bootstrap.PutGarageBoostrapHost(
env.Context,
client,
env.Bootstrap.ThisHost(),
)
if err != nil {
log.Fatal(err)
}
}

View File

@ -4,6 +4,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
@ -44,9 +45,16 @@ func NewS3APICredentials() S3APICredentials {
// NewS3APIClient returns a minio client configured to use the given garage S3 API // NewS3APIClient returns a minio client configured to use the given garage S3 API
// endpoint. // endpoint.
func NewS3APIClient(addr string, creds S3APICredentials) (S3APIClient, error) { func NewS3APIClient(addr string, creds S3APICredentials) S3APIClient {
return minio.New(addr, &minio.Options{
client, err := minio.New(addr, &minio.Options{
Creds: credentials.NewStaticV4(creds.ID, creds.Secret, ""), Creds: credentials.NewStaticV4(creds.ID, creds.Secret, ""),
Region: Region, Region: Region,
}) })
if err != nil {
panic(fmt.Sprintf("initializing minio client at addr %q and with creds %+v", addr, creds))
}
return client
} }