From a7a5237b070cc4f01a0a596540f58dfb30e3d782 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 18 Mar 2025 04:44:12 -0400 Subject: [PATCH] cli, engine, etcd, lib: Pass in init args Improve the API and make it more general. --- cli/deploy.go | 12 +++++++----- engine/world.go | 12 +++++++++++- etcd/world.go | 19 ++++++++++--------- lang/interpret_test.go | 16 ++++++++++------ lib/main.go | 12 +++++++----- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/cli/deploy.go b/cli/deploy.go index 7d7d1a00..d1bebe24 100644 --- a/cli/deploy.go +++ b/cli/deploy.go @@ -194,14 +194,16 @@ func (obj *DeployArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error //MetadataPrefix: lib.MetadataPrefix, //StoragePrefix: lib.StoragePrefix, //StandaloneFs: ???.DeployFs, // used for static deploys - Debug: data.Flags.Debug, - Logf: func(format string, v ...interface{}) { - Logf("world: "+format, v...) - }, //GetURI: func() string { //}, } - if err := world.Init(); err != nil { + worldInit := &engine.WorldInit{ + Debug: data.Flags.Debug, + Logf: func(format string, v ...interface{}) { + Logf("world: etcd: "+format, v...) + }, + } + if err := world.Init(worldInit); err != nil { return false, errwrap.Wrapf(err, "world Init failed") } defer func() { diff --git a/engine/world.go b/engine/world.go index e2a99422..cdba6cdd 100644 --- a/engine/world.go +++ b/engine/world.go @@ -36,12 +36,22 @@ import ( "github.com/purpleidea/mgmt/etcd/scheduler" ) +// WorldInit is some data passed in when starting the World interface. +// TODO: This is a lousy struct name, feel free to change it. +type WorldInit struct { + // Debug represents if we're running in debug mode or not. + Debug bool + + // Logf is a logger which should be used. + Logf func(format string, v ...interface{}) +} + // World is an interface to the rest of the different graph state. It allows the // GAPI to store state and exchange information throughout the cluster. It is // the interface each machine uses to communicate with the rest of the world. type World interface { // TODO: is there a better name for this interface? // Init sets things up and is called once before any other methods. - Init() error + Init(*WorldInit) error // Close does some cleanup and is the last method that is ever called. Close() error diff --git a/etcd/world.go b/etcd/world.go index d0d371a5..60a76982 100644 --- a/etcd/world.go +++ b/etcd/world.go @@ -58,19 +58,20 @@ type World struct { StoragePrefix string // storage prefix for etcdfs storage StandaloneFs engine.Fs // store an fs here for local usage GetURI func() string - Debug bool - Logf func(format string, v ...interface{}) + init *engine.WorldInit simpleDeploy *deployer.SimpleDeploy } // Init runs first. -func (obj *World) Init() error { +func (obj *World) Init(init *engine.WorldInit) error { + obj.init = init + obj.simpleDeploy = &deployer.SimpleDeploy{ Client: obj.Client, - Debug: obj.Debug, + Debug: obj.init.Debug, Logf: func(format string, v ...interface{}) { - obj.Logf("deploy: "+format, v...) + obj.init.Logf("deploy: "+format, v...) }, } if err := obj.simpleDeploy.Init(); err != nil { @@ -239,8 +240,8 @@ func (obj *World) Scheduler(namespace string, opts ...scheduler.Option) (*schedu modifiedOpts = append(modifiedOpts, o) // copy in } - modifiedOpts = append(modifiedOpts, scheduler.Debug(obj.Debug)) - modifiedOpts = append(modifiedOpts, scheduler.Logf(obj.Logf)) + modifiedOpts = append(modifiedOpts, scheduler.Debug(obj.init.Debug)) + modifiedOpts = append(modifiedOpts, scheduler.Logf(obj.init.Logf)) path := fmt.Sprintf(schedulerPathFmt, namespace) return scheduler.Schedule(obj.Client.GetClient(), path, obj.Hostname, modifiedOpts...) @@ -287,9 +288,9 @@ func (obj *World) Fs(uri string) (engine.Fs, error) { Metadata: u.Path, DataPrefix: obj.StoragePrefix, - Debug: obj.Debug, + Debug: obj.init.Debug, Logf: func(format string, v ...interface{}) { - obj.Logf("fs: "+format, v...) + obj.init.Logf("fs: "+format, v...) }, } return etcdFs, nil diff --git a/lang/interpret_test.go b/lang/interpret_test.go index cfc952ce..aead36f4 100644 --- a/lang/interpret_test.go +++ b/lang/interpret_test.go @@ -897,13 +897,15 @@ func TestAstFunc2(t *testing.T) { //MetadataPrefix: /fs, // MetadataPrefix //StoragePrefix: "/storage", // StoragePrefix // TODO: is this correct? (seems to work for testing) - StandaloneFs: fs, // used for static deploys - Debug: testing.Verbose(), // set via the -test.v flag to `go test` + StandaloneFs: fs, // used for static deploys + } + worldInit := &engine.WorldInit{ + Debug: testing.Verbose(), // set via the -test.v flag to `go test` Logf: func(format string, v ...interface{}) { logf("world: etcd: "+format, v...) }, } - if err := world.Init(); err != nil { + if err := world.Init(worldInit); err != nil { t.Errorf("world Init failed: %+v", err) return } @@ -1788,13 +1790,15 @@ func TestAstFunc3(t *testing.T) { //MetadataPrefix: /fs, // MetadataPrefix //StoragePrefix: "/storage", // StoragePrefix // TODO: is this correct? (seems to work for testing) - StandaloneFs: fs, // used for static deploys - Debug: testing.Verbose(), // set via the -test.v flag to `go test` + StandaloneFs: fs, // used for static deploys + } + worldInit := &engine.WorldInit{ + Debug: testing.Verbose(), // set via the -test.v flag to `go test` Logf: func(format string, v ...interface{}) { logf("world: etcd: "+format, v...) }, } - if err := world.Init(); err != nil { + if err := world.Init(worldInit); err != nil { t.Errorf("world Init failed: %+v", err) return } diff --git a/lib/main.go b/lib/main.go index ce2be3e0..b47d99ef 100644 --- a/lib/main.go +++ b/lib/main.go @@ -616,10 +616,6 @@ func (obj *Main) Run() error { MetadataPrefix: MetadataPrefix, StoragePrefix: StoragePrefix, StandaloneFs: obj.DeployFs, // used for static deploys - Debug: obj.Debug, - Logf: func(format string, v ...interface{}) { - obj.Logf("world: etcd: "+format, v...) - }, GetURI: func() string { if gapiInfoResult == nil { return "" @@ -627,7 +623,13 @@ func (obj *Main) Run() error { return gapiInfoResult.URI }, } - if err := world.Init(); err != nil { + worldInit := &engine.WorldInit{ + Debug: obj.Debug, + Logf: func(format string, v ...interface{}) { + obj.Logf("world: etcd: "+format, v...) + }, + } + if err := world.Init(worldInit); err != nil { return errwrap.Wrapf(err, "world Init failed") } defer func() {