mirror of
https://github.com/kairos-io/kairos-sdk.git
synced 2025-09-25 14:17:18 +00:00
feat: add event management and provider loading to bus (#606)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -83,14 +83,16 @@ var AllEvents = []pluggable.EventType{
|
|||||||
EventRecoveryStop,
|
EventRecoveryStop,
|
||||||
EventAvailableReleases,
|
EventAvailableReleases,
|
||||||
EventVersionImage,
|
EventVersionImage,
|
||||||
|
InitProviderInstall,
|
||||||
|
InitProviderConfigure,
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEventDefined checks wether an event is defined in the bus.
|
// IsEventDefined checks wether an event is defined in the bus.
|
||||||
// It accepts strings or EventType, returns a boolean indicating that
|
// It accepts strings or EventType, returns a boolean indicating that
|
||||||
// the event was defined among the events emitted by the bus.
|
// the event was defined among the events emitted by the bus.
|
||||||
func IsEventDefined(i interface{}) bool {
|
func IsEventDefined(i interface{}, events ...pluggable.EventType) bool {
|
||||||
checkEvent := func(e pluggable.EventType) bool {
|
checkEvent := func(e pluggable.EventType) bool {
|
||||||
for _, ee := range AllEvents {
|
for _, ee := range append(AllEvents, events...) {
|
||||||
if ee == e {
|
if ee == e {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
116
bus/manager.go
Normal file
116
bus/manager.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package bus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/kairos-io/kairos-sdk/types"
|
||||||
|
"github.com/mudler/go-pluggable"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultProviderPrefix = "agent-provider"
|
||||||
|
DefaultLogName = "kairos-bus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var DefaultProviderPaths = []string{"/system/providers", "/usr/local/system/providers"}
|
||||||
|
|
||||||
|
func NewBus(withEvents ...pluggable.EventType) *Bus {
|
||||||
|
if len(withEvents) == 0 {
|
||||||
|
withEvents = AllEvents
|
||||||
|
}
|
||||||
|
return &Bus{
|
||||||
|
Manager: pluggable.NewManager(withEvents),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Bus struct {
|
||||||
|
*pluggable.Manager
|
||||||
|
registered bool
|
||||||
|
logger *types.KairosLogger // Fully override the logger
|
||||||
|
logLevel string // Log level for the logger, defaults to "info" unless BUS_DEBUG is set to "true". This only valid if logger is not set.
|
||||||
|
logName string // Name of the logger, defaults to "bus". This only valid if logger is not set.
|
||||||
|
providerPrefix string // Prefix for provider plugins, defaults to "agent-provider". This is used to autoload providers.
|
||||||
|
providerPaths []string // Paths to search for provider plugins, defaults to system and current working directory.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bus) LoadProviders() *pluggable.Manager {
|
||||||
|
return b.Autoload(b.providerPrefix, b.providerPaths...).Register()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bus) Initialize(o ...Options) {
|
||||||
|
if b.registered {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range o {
|
||||||
|
opt(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no provider prefix is set, use the default "agent-provider"
|
||||||
|
if b.providerPrefix == "" {
|
||||||
|
b.providerPrefix = DefaultProviderPrefix
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no provider paths are set, use the default system paths and current working directory
|
||||||
|
if b.providerPaths == nil {
|
||||||
|
wd, _ := os.Getwd()
|
||||||
|
b.providerPaths = append(DefaultProviderPaths, wd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no logger is set, create a new one with the default log level and name
|
||||||
|
if b.logger == nil {
|
||||||
|
if b.logLevel == "" {
|
||||||
|
b.logLevel = "info"
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getenv("BUS_DEBUG") == "true" {
|
||||||
|
b.logLevel = "debug"
|
||||||
|
}
|
||||||
|
if b.logName == "" {
|
||||||
|
b.logName = DefaultLogName
|
||||||
|
}
|
||||||
|
l := types.NewKairosLogger(b.logName, b.logLevel, false)
|
||||||
|
b.logger = &l
|
||||||
|
}
|
||||||
|
|
||||||
|
b.LoadProviders()
|
||||||
|
b.registered = true
|
||||||
|
b.logger.Logger.Debug().Interface("bus", b).Msg("Bus initialized with options")
|
||||||
|
}
|
||||||
|
|
||||||
|
type Options func(d *Bus)
|
||||||
|
|
||||||
|
// WithLogger allows to set a custom logger for the bus. If set, it will override the default logger.
|
||||||
|
func WithLogger(logger *types.KairosLogger) Options {
|
||||||
|
return func(d *Bus) {
|
||||||
|
d.logger = logger
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithLoggerLevel allows to set the log level for the bus logger. If set, it will override the default log level.
|
||||||
|
func WithLoggerLevel(level string) Options {
|
||||||
|
return func(d *Bus) {
|
||||||
|
d.logLevel = level
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithLoggerName allows to set the name of the logger for the bus. If set, it will override the default logger name.
|
||||||
|
func WithLoggerName(name string) Options {
|
||||||
|
return func(d *Bus) {
|
||||||
|
d.logName = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithProviderPrefix allows to set the prefix for provider plugins. If set, it will override the default prefix.
|
||||||
|
func WithProviderPrefix(prefix string) Options {
|
||||||
|
return func(d *Bus) {
|
||||||
|
d.providerPrefix = prefix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithProviderPaths allows to set the paths to search for provider plugins. If set, it will override the default paths.
|
||||||
|
func WithProviderPaths(paths ...string) Options {
|
||||||
|
return func(d *Bus) {
|
||||||
|
d.providerPaths = paths
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user