feat: add host actions, lifecycle hooks and operational tools
This commit is contained in:
1 parent
8d4915543e
commit
e5bde9c61f
32 files changed
+2083
-64
No files matched your search
@@ -0,0 +1,94 @@
|
||||
// Package activity stores local preferences and optional execution metadata.
|
||||
// No command arguments, environment variables, tokens or command output are logged.
|
||||
package activity
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/storage"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
type Record struct {
|
||||
Time time.Time `json:"time"`
|
||||
Host string `json:"host"`
|
||||
Command string `json:"command"`
|
||||
Code int `json:"exit_code"`
|
||||
Duration time.Duration `json:"duration_ns"`
|
||||
}
|
||||
type State struct {
|
||||
Favorites map[string]bool `json:"favorites,omitempty"`
|
||||
Pinned map[string]bool `json:"pinned,omitempty"`
|
||||
Records []Record `json:"records,omitempty"`
|
||||
}
|
||||
|
||||
func Path() string { return filepath.Join(filepath.Dir(config.Path()), "netssh", "activity.json") }
|
||||
func Read() (State, error) {
|
||||
var s State
|
||||
data, err := os.ReadFile(Path())
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
} else if err == nil {
|
||||
err = json.Unmarshal(data, &s)
|
||||
}
|
||||
if s.Favorites == nil {
|
||||
s.Favorites = map[string]bool{}
|
||||
}
|
||||
if s.Pinned == nil {
|
||||
s.Pinned = map[string]bool{}
|
||||
}
|
||||
return s, err
|
||||
}
|
||||
func Update(change func(*State)) error {
|
||||
p := Path()
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(p+".lock", os.O_CREATE|os.O_RDWR, 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
if err = unix.Flock(int(f.Fd()), unix.LOCK_EX); err != nil {
|
||||
return err
|
||||
}
|
||||
defer unix.Flock(int(f.Fd()), unix.LOCK_UN)
|
||||
s, err := Read()
|
||||
if err != nil {
|
||||
return fmt.Errorf("local state: %w", err)
|
||||
}
|
||||
change(&s)
|
||||
data, err := json.MarshalIndent(s, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return storage.WriteFile(p, data, 0600)
|
||||
}
|
||||
func Prune(s *State, c config.ActivityConfig) {
|
||||
if c.RetentionDays > 0 {
|
||||
cutoff := time.Now().Add(-time.Duration(c.RetentionDays) * 24 * time.Hour)
|
||||
keep := s.Records[:0]
|
||||
for _, r := range s.Records {
|
||||
if !r.Time.Before(cutoff) {
|
||||
keep = append(keep, r)
|
||||
}
|
||||
}
|
||||
s.Records = keep
|
||||
}
|
||||
sort.SliceStable(s.Records, func(i, j int) bool { return s.Records[i].Time.Before(s.Records[j].Time) })
|
||||
if c.MaxEntries > 0 && len(s.Records) > c.MaxEntries {
|
||||
s.Records = s.Records[len(s.Records)-c.MaxEntries:]
|
||||
}
|
||||
}
|
||||
func Append(c config.ActivityConfig, r Record) error {
|
||||
if !c.Enabled {
|
||||
return nil
|
||||
}
|
||||
return Update(func(s *State) { s.Records = append(s.Records, r); Prune(s, c) })
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package activity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
||||
)
|
||||
|
||||
func TestConcurrentActivityAndRetention(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
cfg := config.ActivityConfig{Enabled: true, RetentionDays: 1, MaxEntries: 10}
|
||||
if err := Update(func(s *State) {
|
||||
s.Favorites["prod/web"] = true
|
||||
s.Pinned["prod/web"] = true
|
||||
s.Records = []Record{{Time: time.Now().Add(-48 * time.Hour), Host: "old"}}
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 20; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := Append(cfg, Record{Time: time.Now(), Host: fmt.Sprint(i), Command: "test"}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
s, err := Read()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(s.Records) != 10 || !s.Favorites["prod/web"] || !s.Pinned["prod/web"] {
|
||||
t.Fatalf("%+v", s)
|
||||
}
|
||||
for _, r := range s.Records {
|
||||
if r.Host == "old" {
|
||||
t.Fatal("expired record retained")
|
||||
}
|
||||
}
|
||||
info, _ := os.Stat(Path())
|
||||
if info.Mode().Perm() != 0600 {
|
||||
t.Fatal(info.Mode())
|
||||
}
|
||||
}
|
||||
func TestDisabledHistoryDoesNotWrite(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
if err := Append(config.ActivityConfig{}, Record{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(Path()); !os.IsNotExist(err) {
|
||||
t.Fatal("disabled history wrote data")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user