222 lines
5.4 KiB
Go
222 lines
5.4 KiB
Go
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type DeploymentStatus string
|
|
|
|
const (
|
|
StatusPending DeploymentStatus = "pending"
|
|
StatusRunning DeploymentStatus = "running"
|
|
StatusSucceeded DeploymentStatus = "succeeded"
|
|
StatusFailed DeploymentStatus = "failed"
|
|
)
|
|
|
|
type App struct {
|
|
Name string `json:"name"`
|
|
Repo string `json:"repo"` // owner/name
|
|
Branch string `json:"branch"`
|
|
Namespace string `json:"namespace"`
|
|
Host string `json:"host,omitempty"`
|
|
Image string `json:"image,omitempty"`
|
|
LastDeploymentID string `json:"last_deployment_id,omitempty"`
|
|
LastStatus string `json:"last_status,omitempty"`
|
|
LastRef string `json:"last_ref,omitempty"`
|
|
LastCommit string `json:"last_commit,omitempty"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type Deployment struct {
|
|
ID string `json:"id"`
|
|
AppName string `json:"app_name"`
|
|
Repo string `json:"repo"`
|
|
Ref string `json:"ref"`
|
|
CommitSHA string `json:"commit_sha"`
|
|
ImageTag string `json:"image_tag"`
|
|
Trigger string `json:"trigger"` // webhook:push, webhook:release, manual, rollback
|
|
Actor string `json:"actor"`
|
|
Namespace string `json:"namespace"`
|
|
Status DeploymentStatus `json:"status"`
|
|
AppliedResources []string `json:"applied_resources,omitempty"`
|
|
RenderedYAML string `json:"rendered_yaml,omitempty"`
|
|
Logs []string `json:"logs"`
|
|
Error string `json:"error,omitempty"`
|
|
StartedAt time.Time `json:"started_at"`
|
|
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
|
}
|
|
|
|
type WebhookDelivery struct {
|
|
ID string `json:"id"`
|
|
Event string `json:"event"`
|
|
Repo string `json:"repo"`
|
|
Ref string `json:"ref"`
|
|
CommitSHA string `json:"commit_sha,omitempty"`
|
|
SignatureOK bool `json:"signature_ok"`
|
|
Accepted bool `json:"accepted"`
|
|
Message string `json:"message"`
|
|
DeploymentID string `json:"deployment_id,omitempty"`
|
|
ReceivedAt time.Time `json:"received_at"`
|
|
}
|
|
|
|
type persistedState struct {
|
|
Apps map[string]App `json:"apps"`
|
|
Deployments []Deployment `json:"deployments"`
|
|
Webhooks []WebhookDelivery `json:"webhooks"`
|
|
}
|
|
|
|
type Store struct {
|
|
mu sync.RWMutex
|
|
dataFile string
|
|
apps map[string]App
|
|
deployments []Deployment
|
|
webhooks []WebhookDelivery
|
|
maxHistory int
|
|
}
|
|
|
|
func New(dataFile string) (*Store, error) {
|
|
s := &Store{
|
|
dataFile: dataFile,
|
|
apps: make(map[string]App),
|
|
maxHistory: 200,
|
|
}
|
|
if dataFile != "" {
|
|
if err := s.load(); err != nil && !os.IsNotExist(err) {
|
|
return nil, err
|
|
}
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (s *Store) load() error {
|
|
b, err := os.ReadFile(s.dataFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var st persistedState
|
|
if err := json.Unmarshal(b, &st); err != nil {
|
|
return err
|
|
}
|
|
if st.Apps != nil {
|
|
s.apps = st.Apps
|
|
}
|
|
s.deployments = st.Deployments
|
|
s.webhooks = st.Webhooks
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) saveLocked() {
|
|
if s.dataFile == "" {
|
|
return
|
|
}
|
|
dir := filepath.Dir(s.dataFile)
|
|
_ = os.MkdirAll(dir, 0o755)
|
|
st := persistedState{
|
|
Apps: s.apps,
|
|
Deployments: s.deployments,
|
|
Webhooks: s.webhooks,
|
|
}
|
|
b, err := json.MarshalIndent(st, "", " ")
|
|
if err != nil {
|
|
return
|
|
}
|
|
tmp := s.dataFile + ".tmp"
|
|
if err := os.WriteFile(tmp, b, 0o644); err == nil {
|
|
_ = os.Rename(tmp, s.dataFile)
|
|
}
|
|
}
|
|
|
|
func (s *Store) UpsertApp(app App) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
app.UpdatedAt = time.Now().UTC()
|
|
s.apps[app.Repo] = app
|
|
s.saveLocked()
|
|
}
|
|
|
|
func (s *Store) GetApp(repo string) (App, bool) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
a, ok := s.apps[repo]
|
|
return a, ok
|
|
}
|
|
|
|
func (s *Store) ListApps() []App {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
out := make([]App, 0, len(s.apps))
|
|
for _, a := range s.apps {
|
|
out = append(out, a)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *Store) AddDeployment(d Deployment) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.deployments = append([]Deployment{d}, s.deployments...)
|
|
if len(s.deployments) > s.maxHistory {
|
|
s.deployments = s.deployments[:s.maxHistory]
|
|
}
|
|
s.saveLocked()
|
|
}
|
|
|
|
func (s *Store) UpdateDeployment(d Deployment) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
for i := range s.deployments {
|
|
if s.deployments[i].ID == d.ID {
|
|
s.deployments[i] = d
|
|
break
|
|
}
|
|
}
|
|
s.saveLocked()
|
|
}
|
|
|
|
func (s *Store) GetDeployment(id string) (Deployment, bool) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
for _, d := range s.deployments {
|
|
if d.ID == id {
|
|
return d, true
|
|
}
|
|
}
|
|
return Deployment{}, false
|
|
}
|
|
|
|
func (s *Store) ListDeployments(limit int) []Deployment {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
if limit <= 0 || limit > len(s.deployments) {
|
|
limit = len(s.deployments)
|
|
}
|
|
out := make([]Deployment, limit)
|
|
copy(out, s.deployments[:limit])
|
|
return out
|
|
}
|
|
|
|
func (s *Store) AddWebhook(w WebhookDelivery) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.webhooks = append([]WebhookDelivery{w}, s.webhooks...)
|
|
if len(s.webhooks) > s.maxHistory {
|
|
s.webhooks = s.webhooks[:s.maxHistory]
|
|
}
|
|
s.saveLocked()
|
|
}
|
|
|
|
func (s *Store) ListWebhooks(limit int) []WebhookDelivery {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
if limit <= 0 || limit > len(s.webhooks) {
|
|
limit = len(s.webhooks)
|
|
}
|
|
out := make([]WebhookDelivery, limit)
|
|
copy(out, s.webhooks[:limit])
|
|
return out
|
|
}
|