internal/dis: Add 'watcher' component for infos

This commit is contained in:
Tom Wiesing 2022-11-14 11:39:23 +01:00
parent 162b1d3d65
commit 8cfb23bed0
No known key found for this signature in database
8 changed files with 132 additions and 33 deletions

View file

@ -24,7 +24,7 @@ func (info) Description() wisski_distillery.Description {
NeedsDistillery: true, NeedsDistillery: true,
}, },
Command: "info", Command: "info",
Description: "Provide information about a single repository", Description: "Provide information about a single instance",
} }
} }

48
cmd/status.go Normal file
View file

@ -0,0 +1,48 @@
package cmd
import (
"encoding/json"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/cli"
)
// Info is then 'info' command
var Status wisski_distillery.Command = cStatus{}
type cStatus struct {
JSON bool `short:"j" long:"json" description:"Print status as JSON instead of as string"`
}
func (cStatus) Description() wisski_distillery.Description {
return wisski_distillery.Description{
Requirements: cli.Requirements{
NeedsDistillery: true,
},
Command: "status",
Description: "Provide information about the distillery as a whole",
}
}
func (s cStatus) Run(context wisski_distillery.Context) error {
status, _, err := context.Environment.Info().Status(true)
if err != nil {
return err
}
if s.JSON {
json.NewEncoder(context.Stdout).Encode(status)
return nil
}
context.Printf("Total Instances: %v\n", status.TotalCount)
context.Printf(" (running): %v\n", status.RunningCount)
context.Printf(" (stopped): %v\n", status.StoppedCount)
context.Printf("Backups: (count %d)\n", len(status.Backups))
for _, s := range status.Backups {
context.Printf("- %s (slug %q, taken %s, packed %v)\n", s.Path, s.Slug, s.Created.String(), s.Packed)
}
return nil
}

View file

@ -57,6 +57,9 @@ func init() {
// servers // servers
wdcli.Register(cmd.Server) wdcli.Register(cmd.Server)
wdcli.Register(cmd.SSH) wdcli.Register(cmd.SSH)
// status
wdcli.Register(cmd.Status)
} }
// an error when no arguments are provided. // an error when no arguments are provided.

View file

@ -6,9 +6,8 @@ import (
_ "embed" _ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/config" "github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient" "github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
@ -21,31 +20,29 @@ var indexTemplate = static.AssetsControlIndex.MustParseShared(
) )
type indexContext struct { type indexContext struct {
Time time.Time component.Observation
Config *config.Config
Instances []ingredient.Information Instances []ingredient.Information
TotalCount int
RunningCount int
StoppedCount int
Backups []models.Export
} }
func (nfo *Info) index(r *http.Request) (idx indexContext, err error) { func (info *Info) index(r *http.Request) (idx indexContext, err error) {
idx.Observation, idx.Instances, err = info.Status(true)
return
}
// Status produces a new observation of the distillery, and a new information of all instances
// The information on all instances is passed the given quick flag.
func (info *Info) Status(QuickInformation bool) (observation component.Observation, information []ingredient.Information, err error) {
var group errgroup.Group var group errgroup.Group
group.Go(func() error { group.Go(func() error {
// list all the instances // list all the instances
all, err := nfo.Instances.All() all, err := info.Instances.All()
if err != nil { if err != nil {
return err return err
} }
// get all of their info! // get all of their info!
idx.Instances = make([]ingredient.Information, len(all)) information = make([]ingredient.Information, len(all))
for i, instance := range all { for i, instance := range all {
{ {
i := i i := i
@ -53,36 +50,43 @@ func (nfo *Info) index(r *http.Request) (idx indexContext, err error) {
// store the info for this group! // store the info for this group!
group.Go(func() (err error) { group.Go(func() (err error) {
idx.Instances[i], err = instance.Info().Information(true) information[i], err = instance.Info().Information(true)
return err return err
}) })
} }
} }
return nil return nil
}) })
// get the log entries // gather all the observations
group.Go(func() (err error) { var flags component.ObservationFlags
idx.Backups, err = nfo.SnapshotsLog.For("") for _, o := range info.Obervers {
return o := o
}) group.Go(func() error {
return o.Observe(flags, &observation)
})
}
// get the static properties // wait for all the observes to finish
idx.Config = nfo.Config if err := group.Wait(); err != nil {
idx.Time = time.Now().UTC() return component.Observation{}, nil, err
}
group.Wait() // count overall instances
for _, i := range information {
// count how many are running and how many are stopped
for _, i := range idx.Instances {
if i.Running { if i.Running {
idx.RunningCount++ observation.RunningCount++
} else { } else {
idx.StoppedCount++ observation.StoppedCount++
} }
} }
idx.TotalCount = len(idx.Instances) observation.TotalCount = len(information)
return return
} }
func (nfo *Info) Observe(flags component.ObservationFlags, observation *component.Observation) error {
observation.Time = time.Now().UTC()
observation.Config = nfo.Config
return nil
}

View file

@ -18,6 +18,7 @@ type Info struct {
component.Base component.Base
Analytics *lazy.PoolAnalytics Analytics *lazy.PoolAnalytics
Obervers []component.Observer
Exporter *exporter.Exporter Exporter *exporter.Exporter
Instances *instances.Instances Instances *instances.Instances

View file

@ -76,3 +76,9 @@ func (log *Logger) Add(export models.Export) error {
} }
return nil return nil
} }
// Observe writes the SnapshotLog into the given observation
func (logger *Logger) Observe(flags component.ObservationFlags, observation *component.Observation) (err error) {
observation.Backups, err = logger.For("")
return
}

View file

@ -0,0 +1,34 @@
package component
import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/models"
)
// Observer is a component with an Observe method
type Observer interface {
Component
// Observe observes this distillery component and writes the result into observation
// Distinct Observers must write into distinct fields.
Observe(flags ObservationFlags, observation *Observation) error
}
type ObservationFlags struct{}
// Observation represents fetched information about the distillery
type Observation struct {
Time time.Time // Time this obervation was built
// Configuration of the distillery
Config *config.Config
// number of instances
TotalCount int
RunningCount int
StoppedCount int
Backups []models.Export // list of backups
}

View file

@ -96,6 +96,9 @@ func (dis *Distillery) Updatable() []component.Updatable {
func (dis *Distillery) Provisionable() []component.Provisionable { func (dis *Distillery) Provisionable() []component.Provisionable {
return exportAll[component.Provisionable](dis) return exportAll[component.Provisionable](dis)
} }
func (dis *Distillery) Info() *info.Info {
return export[*info.Info](dis)
}
// //
// All components // All components