82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package deploy
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"evobgp/internal/broker"
|
|
"evobgp/internal/config"
|
|
"evobgp/internal/store"
|
|
)
|
|
|
|
// Deps enables deploy-side drift logging between published and last-applied revision per speaker.
|
|
type Deps struct {
|
|
Store store.Backend
|
|
}
|
|
|
|
// Run blocks until ctx is cancelled.
|
|
func Run(ctx context.Context, deps *Deps) {
|
|
cfg := config.Load()
|
|
broker.LogConnect(ctx, cfg.BrokerURL)
|
|
if d := strings.TrimSpace(os.Getenv("EVOBGP_BIRD_ACTIVE_DIR")); d != "" {
|
|
log.Printf("evobgp-deploy: EVOBGP_BIRD_ACTIVE_DIR=%q (apply via API jobs when API has same env)", d)
|
|
}
|
|
if deps == nil || deps.Store == nil {
|
|
runStub(ctx)
|
|
return
|
|
}
|
|
t := time.NewTicker(90 * time.Second)
|
|
defer t.Stop()
|
|
log.Printf("evobgp-deploy: active (speaker published vs applied drift log)")
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Printf("evobgp-deploy: stopped")
|
|
return
|
|
case <-t.C:
|
|
logDrift(context.Background(), deps.Store)
|
|
}
|
|
}
|
|
}
|
|
|
|
func logDrift(ctx context.Context, st store.Backend) {
|
|
_ = ctx
|
|
tenants, err := st.ListTenantIDs()
|
|
if err != nil {
|
|
log.Printf("evobgp-deploy: list tenants: %v", err)
|
|
return
|
|
}
|
|
for _, tid := range tenants {
|
|
for _, sp := range st.ListSpeakersForTenant(tid) {
|
|
pub, _, err := st.LatestPublishedRevision(sp.ID)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
applied := ""
|
|
if sp.LastAppliedRevisionID != nil {
|
|
applied = *sp.LastAppliedRevisionID
|
|
}
|
|
if applied != "" && applied != pub {
|
|
log.Printf("evobgp-deploy: drift speaker=%s applied=%s published=%s", sp.ID, applied, pub)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func runStub(ctx context.Context) {
|
|
t := time.NewTicker(60 * time.Second)
|
|
defer t.Stop()
|
|
log.Printf("evobgp-deploy: idle stub (no store in Deps)")
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Printf("evobgp-deploy: stopped")
|
|
return
|
|
case <-t.C:
|
|
}
|
|
}
|
|
}
|