Skip to content

Commit

Permalink
Huge Refactor
Browse files Browse the repository at this point in the history
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
jakeschuurmans committed Sep 24, 2024
1 parent 4ecb8a0 commit db83bfc
Show file tree
Hide file tree
Showing 19 changed files with 584 additions and 832 deletions.
2 changes: 1 addition & 1 deletion chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: bioscfg
description: A chart to control BMCs
version: v0.1.0
version: v0.1.1
keywords:
- bmc
- bios
Expand Down
26 changes: 26 additions & 0 deletions cmd/bioscfg.go
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)
}
29 changes: 8 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,21 @@ package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/metal-toolbox/bioscfg/internal/model"
"github.com/spf13/cobra"
)

var (
args = &model.Args{}
LogLevel string
ConfigFile string
EnableProfiling bool
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bioscfg",
Short: "bioscfg remotely manages BIOS settings",
Run: func(cmd *cobra.Command, _ []string) {
if err := runWorker(cmd.Context(), args); err != nil {
os.Exit(1)
}
},
Use: "controller",
Short: "Fleet Services Controllers",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -51,19 +46,11 @@ func Execute() {

func init() {
rootCmd.PersistentFlags().
StringVar(&args.ConfigFile, "config", "", "configuration file (default is $HOME/.bioscfg.yml)")
StringVar(&ConfigFile, "config", "", "configuration file (default is $HOME/.bioscfg.yml)")

rootCmd.PersistentFlags().
StringVar(&args.LogLevel, "log-level", "info", "set logging level - debug, trace")
StringVar(&LogLevel, "log-level", "info", "set logging level - debug, trace")

rootCmd.PersistentFlags().
BoolVarP(&args.EnableProfiling, "enable-pprof", "", false, "Enable profiling endpoint at: http://localhost:9091")

rootCmd.PersistentFlags().
StringVarP(&args.FacilityCode, "facility-code", "f", "", "The facility code this bioscfg instance is associated with")

if err := rootCmd.MarkPersistentFlagRequired("facility-code"); err != nil {
slog.Error("failed to mark required flag", "error", err)
os.Exit(1)
}
BoolVarP(&EnableProfiling, "enable-pprof", "", false, "Enable profiling endpoint at: http://localhost:9091")
}
93 changes: 0 additions & 93 deletions cmd/run.go

This file was deleted.

113 changes: 113 additions & 0 deletions internal/bioscfg/bioscfg.go
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
}
9 changes: 9 additions & 0 deletions internal/bioscfg/errors.go
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")
)
Loading

0 comments on commit db83bfc

Please sign in to comment.