cli, engine, etcd, lib: Pass in init args

Improve the API and make it more general.
This commit is contained in:
James Shubin
2025-03-18 04:44:12 -04:00
parent 7ad54fe3e8
commit a7a5237b07
5 changed files with 45 additions and 26 deletions

View File

@@ -194,14 +194,16 @@ func (obj *DeployArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error
//MetadataPrefix: lib.MetadataPrefix, //MetadataPrefix: lib.MetadataPrefix,
//StoragePrefix: lib.StoragePrefix, //StoragePrefix: lib.StoragePrefix,
//StandaloneFs: ???.DeployFs, // used for static deploys //StandaloneFs: ???.DeployFs, // used for static deploys
Debug: data.Flags.Debug,
Logf: func(format string, v ...interface{}) {
Logf("world: "+format, v...)
},
//GetURI: func() string { //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") return false, errwrap.Wrapf(err, "world Init failed")
} }
defer func() { defer func() {

View File

@@ -36,12 +36,22 @@ import (
"github.com/purpleidea/mgmt/etcd/scheduler" "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 // 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 // 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. // 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? 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 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 does some cleanup and is the last method that is ever called.
Close() error Close() error

View File

@@ -58,19 +58,20 @@ type World struct {
StoragePrefix string // storage prefix for etcdfs storage StoragePrefix string // storage prefix for etcdfs storage
StandaloneFs engine.Fs // store an fs here for local usage StandaloneFs engine.Fs // store an fs here for local usage
GetURI func() string GetURI func() string
Debug bool
Logf func(format string, v ...interface{})
init *engine.WorldInit
simpleDeploy *deployer.SimpleDeploy simpleDeploy *deployer.SimpleDeploy
} }
// Init runs first. // Init runs first.
func (obj *World) Init() error { func (obj *World) Init(init *engine.WorldInit) error {
obj.init = init
obj.simpleDeploy = &deployer.SimpleDeploy{ obj.simpleDeploy = &deployer.SimpleDeploy{
Client: obj.Client, Client: obj.Client,
Debug: obj.Debug, Debug: obj.init.Debug,
Logf: func(format string, v ...interface{}) { Logf: func(format string, v ...interface{}) {
obj.Logf("deploy: "+format, v...) obj.init.Logf("deploy: "+format, v...)
}, },
} }
if err := obj.simpleDeploy.Init(); err != nil { 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, o) // copy in
} }
modifiedOpts = append(modifiedOpts, scheduler.Debug(obj.Debug)) modifiedOpts = append(modifiedOpts, scheduler.Debug(obj.init.Debug))
modifiedOpts = append(modifiedOpts, scheduler.Logf(obj.Logf)) modifiedOpts = append(modifiedOpts, scheduler.Logf(obj.init.Logf))
path := fmt.Sprintf(schedulerPathFmt, namespace) path := fmt.Sprintf(schedulerPathFmt, namespace)
return scheduler.Schedule(obj.Client.GetClient(), path, obj.Hostname, modifiedOpts...) 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, Metadata: u.Path,
DataPrefix: obj.StoragePrefix, DataPrefix: obj.StoragePrefix,
Debug: obj.Debug, Debug: obj.init.Debug,
Logf: func(format string, v ...interface{}) { Logf: func(format string, v ...interface{}) {
obj.Logf("fs: "+format, v...) obj.init.Logf("fs: "+format, v...)
}, },
} }
return etcdFs, nil return etcdFs, nil

View File

@@ -897,13 +897,15 @@ func TestAstFunc2(t *testing.T) {
//MetadataPrefix: /fs, // MetadataPrefix //MetadataPrefix: /fs, // MetadataPrefix
//StoragePrefix: "/storage", // StoragePrefix //StoragePrefix: "/storage", // StoragePrefix
// TODO: is this correct? (seems to work for testing) // TODO: is this correct? (seems to work for testing)
StandaloneFs: fs, // used for static deploys StandaloneFs: fs, // used for static deploys
Debug: testing.Verbose(), // set via the -test.v flag to `go test` }
worldInit := &engine.WorldInit{
Debug: testing.Verbose(), // set via the -test.v flag to `go test`
Logf: func(format string, v ...interface{}) { Logf: func(format string, v ...interface{}) {
logf("world: etcd: "+format, v...) 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) t.Errorf("world Init failed: %+v", err)
return return
} }
@@ -1788,13 +1790,15 @@ func TestAstFunc3(t *testing.T) {
//MetadataPrefix: /fs, // MetadataPrefix //MetadataPrefix: /fs, // MetadataPrefix
//StoragePrefix: "/storage", // StoragePrefix //StoragePrefix: "/storage", // StoragePrefix
// TODO: is this correct? (seems to work for testing) // TODO: is this correct? (seems to work for testing)
StandaloneFs: fs, // used for static deploys StandaloneFs: fs, // used for static deploys
Debug: testing.Verbose(), // set via the -test.v flag to `go test` }
worldInit := &engine.WorldInit{
Debug: testing.Verbose(), // set via the -test.v flag to `go test`
Logf: func(format string, v ...interface{}) { Logf: func(format string, v ...interface{}) {
logf("world: etcd: "+format, v...) 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) t.Errorf("world Init failed: %+v", err)
return return
} }

View File

@@ -616,10 +616,6 @@ func (obj *Main) Run() error {
MetadataPrefix: MetadataPrefix, MetadataPrefix: MetadataPrefix,
StoragePrefix: StoragePrefix, StoragePrefix: StoragePrefix,
StandaloneFs: obj.DeployFs, // used for static deploys 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 { GetURI: func() string {
if gapiInfoResult == nil { if gapiInfoResult == nil {
return "" return ""
@@ -627,7 +623,13 @@ func (obj *Main) Run() error {
return gapiInfoResult.URI 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") return errwrap.Wrapf(err, "world Init failed")
} }
defer func() { defer func() {