Skip to content

Commit

Permalink
status web
Browse files Browse the repository at this point in the history
  • Loading branch information
XANi committed Dec 25, 2024
1 parent 10afa92 commit aaf128e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
13 changes: 8 additions & 5 deletions dpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,17 @@ func MainLoop(c *cli.Context) {
if err != nil {
log.Errorf("error initializing database: %s", err)
}
cfg.Logger = log
r, err := overlord.New(&cfg)
if err != nil {
log.Panicf("Error while starting overlord: %s", err)
}
if cfg.Web != nil {
cfg.Web.Version = version
cfg.Web.DB = db
cfg.Web.Logger = log
cfg.Web.UnixSocketDir = cfg.UnixSocketDir
cfg.Web.LastRunStatusFunc = r.LastRunSummary
w, err := web.New(*cfg.Web, embeddedWebContent)
if err != nil {
log.Errorf("error setting up web server: %s", err)
Expand All @@ -187,11 +194,7 @@ func MainLoop(c *cli.Context) {
log.Errorf("error listening on web socket %s: %s", cfg.Web.ListenAddr, w.Run())
}()
}
cfg.Logger = log
r, err := overlord.New(&cfg)
if err != nil {
log.Panicf("Error while starting overlord: %s", err)
}

go func() {
for {
log.Infof("updating repos")
Expand Down
5 changes: 5 additions & 0 deletions overlord/overlord.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,8 @@ func (o *Overlord) Update() error {
o.Unlock()
return nil
}

func (o *Overlord) LastRunSummary() (success bool, stats puppet.LastRunSummary, ts time.Time) {
return o.puppet.LastRunStats()

}
42 changes: 34 additions & 8 deletions web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web
import (
"fmt"
"github.com/XANi/go-dpp/messdb"
"github.com/XANi/go-dpp/puppet"
"github.com/efigence/go-mon"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
Expand All @@ -24,11 +25,13 @@ type WebBackend struct {
}

type Config struct {
Logger *zap.SugaredLogger `yaml:"-"`
AccessLogger *zap.SugaredLogger `yaml:"-"`
ListenAddr string `yaml:"listen_addr"`
UnixSocketDir string `yaml:"-"`
DB *messdb.MessDB `yaml:"-"`
Logger *zap.SugaredLogger `yaml:"-"`
AccessLogger *zap.SugaredLogger `yaml:"-"`
ListenAddr string `yaml:"listen_addr"`
UnixSocketDir string `yaml:"-"`
Version string `yaml:"-"`
DB *messdb.MessDB `yaml:"-"`
LastRunStatusFunc func() (success bool, stats puppet.LastRunSummary, ts time.Time) `yaml:"-"`
}

func New(cfg Config, webFS fs.FS) (backend *WebBackend, err error) {
Expand Down Expand Up @@ -59,7 +62,11 @@ func New(cfg Config, webFS fs.FS) (backend *WebBackend, err error) {
r.Use(ginzap.GinzapWithConfig(w.al.Desugar(), &ginzap.Config{
TimeFormat: time.RFC3339,
UTC: false,
SkipPaths: []string{"/_status/health", "/_status/metrics"},
SkipPaths: []string{
"/_status/health",
"/_status/metrics",
"/last_run",
},
}))
//r.Use(ginzap.RecoveryWithZap(w.al.Desugar(), true))
// basic logging to stdout
Expand Down Expand Up @@ -87,9 +94,11 @@ func New(cfg Config, webFS fs.FS) (backend *WebBackend, err error) {
"title": c.Request.RemoteAddr,
})
})
r.GET("/source", func(c *gin.Context) {
c.String(http.StatusOK, fmt.Sprintf("%+v", c.Request.RemoteAddr))
r.GET("/version", func(c *gin.Context) {
c.String(http.StatusOK, cfg.Version)
})
r.GET("/last_run", w.Status)
r.HEAD("/last_run", w.Status)
r.NoRoute(func(c *gin.Context) {
c.HTML(http.StatusNotFound, "404.tmpl", gin.H{
"notfound": c.Request.URL.Path,
Expand Down Expand Up @@ -126,3 +135,20 @@ func (b *WebBackend) Run() error {
}
return b.r.Run(b.cfg.ListenAddr)
}

func (b *WebBackend) Status(c *gin.Context) {
ok, stats, ts := b.cfg.LastRunStatusFunc()
status := http.StatusInternalServerError
if ok {
status = http.StatusOK
}
if !ts.IsZero() {
c.String(status, fmt.Sprintf("last: %s, changes: %v, dpp version %s",
time.Now().Sub(ts).Round(time.Second),
stats.Events,
b.cfg.Version,
))
} else {
c.String(http.StatusOK, "waiting for first run [%s]", b.cfg.Version)
}
}

0 comments on commit aaf128e

Please sign in to comment.