-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CMD Replace old tasks and steps with new Task Replace slog with logrus. I know we want to move off logrus, but I think we need to do this at a later date. Move handler to bioscfg move publisher to bioscfg, in its own file
- Loading branch information
1 parent
4ecb8a0
commit 324f17a
Showing
21 changed files
with
588 additions
and
836 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/metal-toolbox/bioscfg/internal/bioscfg" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// bioscfgCmd represents the bioscfg command | ||
var bioscfgCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run the BiosCfg Controller", | ||
Run: func(cmd *cobra.Command, _ []string) { | ||
err := bioscfg.Run(cmd.Context(), ConfigFile, LogLevel, EnableProfiling) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(bioscfgCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package bioscfg | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/metal-toolbox/bioscfg/internal/config" | ||
"github.com/metal-toolbox/bioscfg/internal/store/fleetdb" | ||
"github.com/metal-toolbox/ctrl" | ||
rctypes "github.com/metal-toolbox/rivets/condition" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
pkgName = "internal/bioscfg" | ||
) | ||
|
||
// BiosCfg BiosCfg Controller Struct | ||
type BiosCfg struct { | ||
cfg *config.Configuration | ||
logger *logrus.Entry | ||
ctx context.Context | ||
fleetdb *fleetdb.Store | ||
nc *ctrl.NatsController | ||
} | ||
|
||
// New create a new BiosCfg Controller | ||
func New(ctx context.Context, cfg *config.Configuration, logger *logrus.Entry) (*BiosCfg, error) { | ||
bc := &BiosCfg{ | ||
cfg: cfg, | ||
logger: logger, | ||
ctx: ctx, | ||
} | ||
|
||
err := bc.initDependences() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bc, nil | ||
} | ||
|
||
// Listen listen to Nats for tasks | ||
func (bc *BiosCfg) Listen() error { | ||
handleFactory := func() ctrl.TaskHandler { | ||
return &TaskHandler{ | ||
cfg: bc.cfg, | ||
logger: bc.logger, | ||
controllerID: bc.nc.ID(), | ||
fleetdb: bc.fleetdb, | ||
} | ||
} | ||
|
||
err := bc.nc.ListenEvents(bc.ctx, handleFactory) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// initDependences Initialize network dependencies | ||
func (bc *BiosCfg) initDependences() error { | ||
err := bc.initNats() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to initialize connection to nats") | ||
} | ||
|
||
err = bc.initFleetDB() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to initialize connection to fleetdb") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (bc *BiosCfg) initNats() error { | ||
bc.nc = ctrl.NewNatsController( | ||
string(rctypes.BiosControl), | ||
bc.cfg.FacilityCode, | ||
string(rctypes.BiosControl), | ||
bc.cfg.Endpoints.Nats.URL, | ||
bc.cfg.Endpoints.Nats.CredsFile, | ||
rctypes.BiosControl, | ||
ctrl.WithConcurrency(bc.cfg.Concurrency), | ||
ctrl.WithKVReplicas(bc.cfg.Endpoints.Nats.KVReplicationFactor), | ||
ctrl.WithLogger(bc.logger.Logger), | ||
ctrl.WithConnectionTimeout(bc.cfg.Endpoints.Nats.ConnectTimeout), | ||
) | ||
|
||
err := bc.nc.Connect(bc.ctx) | ||
if err != nil { | ||
bc.logger.Error(err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (bc *BiosCfg) initFleetDB() error { | ||
store, err := fleetdb.New( | ||
bc.ctx, | ||
&bc.cfg.Endpoints.FleetDB, | ||
bc.logger.Logger, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bc.fleetdb = store | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package bioscfg | ||
|
||
import "errors" | ||
|
||
var ( | ||
errInvalidConditionParams = errors.New("invalid condition parameters") | ||
errTaskConv = errors.New("error in generic Task conversion") | ||
errUnsupportedAction = errors.New("unsupported action") | ||
) |
Oops, something went wrong.