diff --git a/internal/dispatcher/agent/dispatcher.go b/internal/dispatcher/agent/dispatcher.go index 2897c8a..e4129a2 100644 --- a/internal/dispatcher/agent/dispatcher.go +++ b/internal/dispatcher/agent/dispatcher.go @@ -6,6 +6,7 @@ import ( "time" configuration "git.g3e.fr/syonad/two/internal/config/agent" + "git.g3e.fr/syonad/two/internal/state" "git.g3e.fr/syonad/two/pkg/worker" "github.com/dgraph-io/badger/v4" ) @@ -13,6 +14,7 @@ import ( type Command interface { Prepare(db *badger.DB, cfg *configuration.Config) error Execute(db *badger.DB, cfg *configuration.Config) error + Key() string } type Dispatcher struct { @@ -43,6 +45,10 @@ func (d *Dispatcher) Dispatch(cmd Command) { } if err != nil { d.logger.Error("command failed", append(attrs, "error", err)...) + if setErr := state.Set(d.db, cmd.Key(), state.Error); setErr != nil { + d.logger.Error("failed to mark resource as errored", + "command", cmdType, "key", cmd.Key(), "error", setErr) + } } else { d.logger.Info("command done", attrs...) } diff --git a/internal/dispatcher/agent/subnet_commands.go b/internal/dispatcher/agent/subnet_commands.go index e488842..a73ab2b 100644 --- a/internal/dispatcher/agent/subnet_commands.go +++ b/internal/dispatcher/agent/subnet_commands.go @@ -22,6 +22,8 @@ type CreateSubnetCommand struct { DefaultRoute bool } +func (c CreateSubnetCommand) Key() string { return "subnet/" + c.Name } + func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) error { if c.Mode == "" { c.Mode = "vxlan" @@ -79,6 +81,8 @@ type DeleteSubnetCommand struct { Name string } +func (c DeleteSubnetCommand) Key() string { return "subnet/" + c.Name } + func (c DeleteSubnetCommand) Prepare(db *badger.DB, _ *configuration.Config) error { if _, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err != nil { return fmt.Errorf("subnet %q not found", c.Name) diff --git a/internal/dispatcher/agent/vm_commands.go b/internal/dispatcher/agent/vm_commands.go index bdfeaec..4190f85 100644 --- a/internal/dispatcher/agent/vm_commands.go +++ b/internal/dispatcher/agent/vm_commands.go @@ -30,6 +30,8 @@ type StartVMCommand struct { SSHKey string } +func (c StartVMCommand) Key() string { return "vm/" + c.Name } + func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error { if _, err := kv.GetFromDB(db, "vm/"+c.Name+"/state"); err == nil { return fmt.Errorf("vm %q already exists", c.Name) @@ -111,6 +113,8 @@ type StopVMCommand struct { Name string } +func (c StopVMCommand) Key() string { return "vm/" + c.Name } + func (c StopVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error { if _, err := kv.GetFromDB(db, "vm/"+c.Name+"/state"); err != nil { return fmt.Errorf("vm %q not found", c.Name) diff --git a/internal/dispatcher/agent/vpc_commands.go b/internal/dispatcher/agent/vpc_commands.go index a129d88..4a0a1dc 100644 --- a/internal/dispatcher/agent/vpc_commands.go +++ b/internal/dispatcher/agent/vpc_commands.go @@ -17,6 +17,8 @@ type CreateVPCCommand struct { CIDR string } +func (c CreateVPCCommand) Key() string { return "vpc/" + c.Name } + func (c CreateVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error { if _, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state"); err == nil { return fmt.Errorf("vpc %q already exists", c.Name) @@ -38,6 +40,8 @@ type DeleteVPCCommand struct { Name string } +func (c DeleteVPCCommand) Key() string { return "vpc/" + c.Name } + func (c DeleteVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error { if _, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state"); err != nil { return fmt.Errorf("vpc %q not found", c.Name)