Files
kairos-agent/internal/bus/bus.go
Ettore Di Giacinto 63cd28d1cb Split off cli into separate binaries (#37)
* 🎨 Split off cli into separate binaries

This commit splits off the cli into 3 binaries:
- agent
- cli
- provider

The provider now is a separate component that can be tested by itself
and have its own lifecycle. This paves the way to a ligher c3os variant,
HA support and other features that can be provided on runtime.

This is working, but still there are low hanging fruit to care about.

Fixes #14

* 🤖 Add provider bin to releases

* ⚙️ Handle signals

* ⚙️ Reduce buildsize footprint

* 🎨 Scan for providers also in /system/providers

* 🤖 Run goreleaser

* 🎨 Refactoring
2022-07-04 22:39:34 +02:00

67 lines
1.3 KiB
Go

package bus
import (
"fmt"
"os"
"os/exec"
"github.com/c3os-io/c3os/pkg/bus"
"github.com/mudler/go-pluggable"
)
// Manager is the bus instance manager, which subscribes plugins to events emitted
var Manager *Bus = &Bus{
Manager: pluggable.NewManager(
[]pluggable.EventType{
bus.EventBootstrap,
bus.EventChallenge,
bus.EventInstall,
},
),
}
type Bus struct {
*pluggable.Manager
}
func (b *Bus) Initialize() {
b.Manager.Autoload("agent-provider", "/system/providers").Register()
for i := range b.Manager.Events {
e := b.Manager.Events[i]
b.Manager.Response(e, func(p *pluggable.Plugin, r *pluggable.EventResponse) {
if os.Getenv("BUS_DEBUG") == "true" {
fmt.Println(
fmt.Sprintf("[provider event: %s]", e),
"received from",
p.Name,
"at",
p.Executable,
r,
)
}
if r.Errored() {
err := fmt.Sprintf("Provider %s at %s had an error: %s", p.Name, p.Executable, r.Error)
fmt.Println(err)
os.Exit(1)
} else {
if r.State != "" {
fmt.Println(fmt.Sprintf("[provider event: %s]", e), r.State)
}
}
})
}
}
func RunHookScript(s string) error {
_, err := os.Stat(s)
if err != nil {
return nil
}
cmd := exec.Command(s)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}