CI / changes (push) Successful in 9s
CI / commitlint (push) Has been skipped
CI / openapi (push) Successful in 26s
CI / web (push) Successful in 33s
CI / go (push) Successful in 2m11s
CI / bird2 (push) Successful in 16s
CI / release (push) Successful in 3m27s
Added PostgreSQL monitoring and maintenance capabilities to the API, including new endpoints for instance-level metrics, maintenance operations, and job scheduling. Updated the HTTP API to support PostgreSQL monitoring routes and integrated a background scheduler for metrics collection. Enhanced the CLI with database commands for maintenance tasks. Updated documentation to reflect these changes.
107 lines
3.2 KiB
Go
107 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"evobgp/internal/birdfmt"
|
|
"evobgp/internal/config"
|
|
"evobgp/internal/dbcli"
|
|
"evobgp/internal/deploy"
|
|
"evobgp/internal/httpapi"
|
|
"evobgp/internal/ingest"
|
|
"evobgp/internal/observability"
|
|
"evobgp/internal/platform"
|
|
"evobgp/internal/render"
|
|
"evobgp/internal/scheduler"
|
|
"evobgp/internal/version"
|
|
)
|
|
|
|
// microVPS entrypoint: один процесс — HTTP API и фоновые воркеры scheduler, ingest, render, deploy (общий store и jobs.Registry).
|
|
func main() {
|
|
if len(os.Args) > 1 && os.Args[1] == "db" {
|
|
os.Exit(dbcli.Run(os.Args[2:]))
|
|
}
|
|
cfg := config.Load()
|
|
opts := httpapi.Options{
|
|
APIKeys: os.Getenv("EVOBGP_API_KEYS"),
|
|
DatabaseURL: cfg.DatabaseURL,
|
|
InsecureDev: os.Getenv("EVOBGP_DEV_INSECURE") == "1",
|
|
SeedDemo: os.Getenv("EVOBGP_SEED_DEMO") != "0",
|
|
BundleSeedHex: strings.TrimSpace(os.Getenv("EVOBGP_BUNDLE_SEED_HEX")),
|
|
CORSAllowedOrigins: strings.TrimSpace(os.Getenv("EVOBGP_CORS_ORIGINS")),
|
|
}
|
|
srv, err := httpapi.New(opts)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer srv.Close()
|
|
observability.SetBuildInfo(version.Version, version.GitSHA)
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
schedDeps := &scheduler.Deps{Store: srv.Store(), Jobs: srv.Jobs()}
|
|
ingestDeps := &ingest.Deps{Store: srv.Store()}
|
|
renderDeps := &render.Deps{Store: srv.Store()}
|
|
deployDeps := &deploy.Deps{Store: srv.Store()}
|
|
go scheduler.Run(ctx, schedDeps)
|
|
go ingest.Run(ctx, ingestDeps)
|
|
go render.Run(ctx, renderDeps)
|
|
go deploy.Run(ctx, deployDeps)
|
|
|
|
srv.StartBackground(ctx)
|
|
startBirdMetricsPoller(ctx)
|
|
|
|
httpSrv := &http.Server{
|
|
Addr: cfg.HTTPAddr,
|
|
Handler: srv.Handler(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 60 * time.Second,
|
|
WriteTimeout: 120 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
}
|
|
go func() {
|
|
svc := platform.ServiceName("evobgp-all")
|
|
log.Printf("%s (microVPS): HTTP API on %s", svc, cfg.HTTPAddr)
|
|
log.Printf("bundle public key (base64): %s", srv.BundlePublicKeyBase64())
|
|
if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
if err := httpSrv.Shutdown(shutdownCtx); err != nil {
|
|
log.Printf("HTTP shutdown: %v", err)
|
|
}
|
|
log.Printf("%s stopped", platform.ServiceName("evobgp-all"))
|
|
}
|
|
|
|
func startBirdMetricsPoller(ctx context.Context) {
|
|
sock := strings.TrimSpace(os.Getenv("EVOBGP_BIRDC_SOCKET"))
|
|
if sock == "" {
|
|
return
|
|
}
|
|
interval := 30 * time.Second
|
|
if d, err := time.ParseDuration(strings.TrimSpace(os.Getenv("EVOBGP_BIRDC_INTERVAL"))); err == nil && d > 0 {
|
|
interval = d
|
|
}
|
|
bin := strings.TrimSpace(os.Getenv("EVOBGP_BIRDC_BIN"))
|
|
observability.StartBirdProtocolsPoller(ctx, sock, bin, interval,
|
|
func(ctx context.Context, socket, birdcBin string) (string, error) {
|
|
return birdfmt.ShowProtocols(ctx, socket, birdcBin)
|
|
},
|
|
birdfmt.CountEstablishedBGPSessions,
|
|
birdfmt.ParseBGPProtocolStates,
|
|
)
|
|
log.Printf("birdc protocols poller enabled (socket=%s interval=%s)", sock, interval)
|
|
}
|