From 50bd6f581173b6068da636bd47236c9350f703b8 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 10 Mar 2025 14:56:42 -0400 Subject: [PATCH] lib, gapi, cli: Add a wait flag to empty and a new default Change the default "wait" state for if you run the empty frontend when there's already an available deploy waiting. You almost certainly want to start running it right away. Example: mgmt etcd mgmt run --hostname h1 --no-server --tmp-prefix --seeds=http://127.0.0.1:2379 empty mgmt run --hostname h2 --no-server --tmp-prefix --seeds=http://127.0.0.1:2379 empty mgmt deploy --no-git --seeds=http://127.0.0.1:2379 lang examples/lang/hello0.mcl mgmt run --hostname h3 --no-server --tmp-prefix --seeds=http://127.0.0.1:2379 empty In fact, you don't even need to start up etcd first for this to all work. --- cli/util/args.go | 4 +++- gapi/empty/empty.go | 24 ++++++++++++++++++++++-- lib/main.go | 6 ++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/cli/util/args.go b/cli/util/args.go index b636de37..1fcc1951 100644 --- a/cli/util/args.go +++ b/cli/util/args.go @@ -70,7 +70,9 @@ func LookupSubcommand(obj interface{}, st interface{}) string { } // EmptyArgs is the empty CLI parsing structure and type of the parsed result. -type EmptyArgs struct{} +type EmptyArgs struct { + Wait bool `arg:"--wait" help:"don't use any existing (stale) deploys"` +} // LangArgs is the lang CLI parsing structure and type of the parsed result. type LangArgs struct { diff --git a/gapi/empty/empty.go b/gapi/empty/empty.go index c12d99e6..e25e4256 100644 --- a/gapi/empty/empty.go +++ b/gapi/empty/empty.go @@ -33,6 +33,7 @@ import ( "fmt" "sync" + cliUtil "github.com/purpleidea/mgmt/cli/util" "github.com/purpleidea/mgmt/gapi" "github.com/purpleidea/mgmt/pgraph" ) @@ -48,6 +49,17 @@ func init() { // GAPI implements the main lang GAPI interface. type GAPI struct { + // Wait should be true if we don't use any existing (stale) deploys. + // This means that if you start an empty GAPI, then it will immediately + // try to look for and run any existing deploys that have been stored in + // the cluster that it has connected to. If this is true, then it will + // only start on the next deploy. To be honest, we should probably never + // wait, but this was accidentally how it was initially implemented, so + // we'll change the default and add this in as a flag for now. We may + // remove this in the future unless someone has a good reason for + // needing it. + Wait bool + data *gapi.Data initialized bool closeChan chan struct{} @@ -57,11 +69,19 @@ type GAPI struct { // Cli takes an *Info struct, and returns our deploy if activated, and if there // are any validation problems, you should return an error. If there is no // deploy, then you should return a nil deploy and a nil error. -func (obj *GAPI) Cli(*gapi.Info) (*gapi.Deploy, error) { +func (obj *GAPI) Cli(info *gapi.Info) (*gapi.Deploy, error) { + args, ok := info.Args.(*cliUtil.EmptyArgs) + if !ok { + // programming error + return nil, fmt.Errorf("could not convert to our struct") + } + return &gapi.Deploy{ Name: Name, //Noop: false, - GAPI: &GAPI{}, + GAPI: &GAPI{ + Wait: args.Wait, + }, }, nil } diff --git a/lib/main.go b/lib/main.go index 86780f84..2ec3235b 100644 --- a/lib/main.go +++ b/lib/main.go @@ -1024,6 +1024,12 @@ func (obj *Main) Run() error { defer wg.Done() defer close(deployChan) // no more are coming ever! + // if "empty" and we don't want to wait for a fresh deploy... + if obj.Deploy != nil && max != 0 { + if emptyGAPI, ok := obj.Deploy.GAPI.(*empty.GAPI); ok && !emptyGAPI.Wait { + obj.Deploy = nil // erase the empty deploy + } + } // we've been asked to deploy, so do that first... if obj.Deploy != nil { deploy := obj.Deploy