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 (
"cryptic-net/garage"
"fmt"
)
// 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
// the global bucket.
func (b Bootstrap) GlobalBucketS3APIClient() (garage.S3APIClient, error) {
func (b Bootstrap) GlobalBucketS3APIClient() garage.S3APIClient {
addr := b.ChooseGaragePeer().S3APIAddr()
creds := b.GarageGlobalBucketS3APICredentials
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
return garage.NewS3APIClient(addr, creds)
}

View File

@ -17,7 +17,6 @@ package main
import (
"cryptic-net/cmd/entrypoint"
nebula_entrypoint "cryptic-net/cmd/nebula-entrypoint"
update_global_bucket "cryptic-net/cmd/update-global-bucket"
"fmt"
"os"
)
@ -30,7 +29,6 @@ type mainFn struct {
var mainFns = []mainFn{
{"entrypoint", entrypoint.Main},
{"nebula-entrypoint", nebula_entrypoint.Main},
{"update-global-bucket", update_global_bucket.Main},
}
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)
}
garageS3Client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("initializing garage shared global bucket client: %w", err)
}
garageS3Client := env.Bootstrap.GlobalBucketS3APIClient()
fmt.Fprintln(os.Stderr, "writing data for this host into garage")
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)
}
client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
client := env.Bootstrap.GlobalBucketS3APIClient()
// NOTE this isn't _technically_ required, but if the `hosts add`
// 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
// has been canceled or bootstrap info has been changed. This will always block
// until the spawned pmux has returned.
func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
// until the spawned pmux has returned, and returns a copy of Env with updated
// boostrap info.
func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) (crypticnet.Env, error) {
thisHost := env.Bootstrap.ThisHost()
thisDaemon := env.ThisDaemon()
@ -96,19 +97,12 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
garageChildrenPmuxProcConfigs, err := garageChildrenPmuxProcConfigs(env)
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, pmuxlib.ProcessConfig{
Name: "update-global-bucket",
Cmd: "bash",
Args: waitForGarageArgs(env, "update-global-bucket"),
NoRestartOn: []int{0},
})
pmuxConfig := pmuxlib.Config{Processes: pmuxProcConfigs}
doneCh := env.Context.Done()
@ -125,6 +119,25 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
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 {
wg.Add(1)
go func() {
@ -136,6 +149,7 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
}
err := doOnce(ctx, func(ctx context.Context) error {
fmt.Fprintln(os.Stderr, "applying garage layout")
return garageApplyLayout(ctx, env)
})
@ -152,7 +166,7 @@ func runDaemonPmuxOnce(env crypticnet.Env, s3Client garage.S3APIClient) error {
select {
case <-doneCh:
return env.Context.Err()
return crypticnet.Env{}, env.Context.Err()
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 {
return fmt.Errorf("reloading bootstrap: %w", err)
return crypticnet.Env{}, fmt.Errorf("reloading bootstrap: %w", err)
} else if changed {
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
// changed and we should be connecting to a different garage
// endpoint.
s3Client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
s3Client := env.Bootstrap.GlobalBucketS3APIClient()
if err := runDaemonPmuxOnce(env, s3Client); errors.Is(err, context.Canceled) {
if env, err = runDaemonPmuxOnce(env, s3Client); errors.Is(err, context.Canceled) {
return nil
} else if err != nil {

View File

@ -60,11 +60,7 @@ var subCmdHostsAdd = subCmd{
// TODO validate that the IP is in the correct CIDR
env := subCmdCtx.env
client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
client := env.Bootstrap.GlobalBucketS3APIClient()
host := bootstrap.Host{
Name: *name,
@ -85,10 +81,7 @@ var subCmdHostsList = subCmd{
env := subCmdCtx.env
client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
client := env.Bootstrap.GlobalBucketS3APIClient()
hostsMap, err := bootstrap.GetGarageBootstrapHosts(env.Context, client)
if err != nil {
@ -128,11 +121,7 @@ var subCmdHostsDelete = subCmd{
}
env := subCmdCtx.env
client, err := env.Bootstrap.GlobalBucketS3APIClient()
if err != nil {
return fmt.Errorf("creating client for global bucket: %w", err)
}
client := env.Bootstrap.GlobalBucketS3APIClient()
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"
"encoding/hex"
"errors"
"fmt"
"github.com/minio/minio-go/v7"
"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
// endpoint.
func NewS3APIClient(addr string, creds S3APICredentials) (S3APIClient, error) {
return minio.New(addr, &minio.Options{
func NewS3APIClient(addr string, creds S3APICredentials) S3APIClient {
client, err := minio.New(addr, &minio.Options{
Creds: credentials.NewStaticV4(creds.ID, creds.Secret, ""),
Region: Region,
})
if err != nil {
panic(fmt.Sprintf("initializing minio client at addr %q and with creds %+v", addr, creds))
}
return client
}