d8ab22876f
Build / build (push) Successful in 10m41s
End-to-end extraction of the Instance concept. After this commit:
* internal/store/instances.go — DELETED
* internal/store/models.go — Instance struct gone, ProxyRoute moved here
* containers table is the single source of truth for project/stack/site
container state. instances table is dropped via DROP TABLE migration
(idempotent; re-runnable on every boot).
* Legacy tinyforge.project / tinyforge.stage / tinyforge.instance-id
Docker labels are no longer emitted; only tinyforge.workload.{id,kind},
tinyforge.role, and tinyforge.managed are stamped on new containers.
Backend rewrites:
- internal/deployer: executeDeploy + blueGreenDeploy + rollback +
promote use store.Container natively. New
removeContainer() replaces removeInstance().
enforceMaxInstances reads via
ListContainersByStageID.
- internal/reconciler: legacy tinyforge.instance-id dispatch removed;
upsertByWorkloadLabel now finds existing rows
by docker container ID first and falls back to
the deterministic workloadID:role key.
- internal/stale/scanner: Scan + new FindStaleContainers walk the
containers table; emit StaleContainer JSON.
- internal/stats/collector: ListContainers replaces ListAllInstances.
- internal/webhook/handler: workload-secret lookup tried first; falls back
to project / static_site secret column.
- internal/api: instances.go, stale.go, stats.go, stats_history.go,
projects.go, settings.go, docker.go, dns.go all read /
write through Container.
Docker layer:
- ManagedContainer exposes WorkloadID/Kind/Role from the canonical labels.
- ListContainers filters by tinyforge.managed=true.
- Network creation uses LabelManaged instead of LabelProject.
Frontend:
- Instance type is now a Container alias; .status → .state,
.last_alive_at → .last_seen_at.
- InstanceCard takes stageId as a prop (no longer derived from Instance).
- StaleContainer JSON shape rewritten: { container, workload_name, role,
days_stale }. StaleContainerCard + /containers/stale page updated.
- ProjectCard / homepage / SystemHealthCard filter by .state.
The migration loop now tolerates "no such table" alongside "duplicate
column" / "already exists" so obsolete ALTER TABLE entries targeting the
dropped instances table no-op cleanly on first boot.
Tests: store + deployer + reconciler + webhook + staticsite + notify all
still pass. Frontend svelte-check: zero errors.
226 lines
5.9 KiB
Go
226 lines
5.9 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/alexei/tinyforge/internal/events"
|
|
"github.com/alexei/tinyforge/internal/store"
|
|
)
|
|
|
|
// projectRequest is the expected JSON body for creating/updating a project.
|
|
type projectRequest struct {
|
|
Name string `json:"name"`
|
|
Registry string `json:"registry"`
|
|
Image string `json:"image"`
|
|
Port int `json:"port"`
|
|
Healthcheck string `json:"healthcheck"`
|
|
Env string `json:"env"`
|
|
Volumes string `json:"volumes"`
|
|
NpmAccessListID *int `json:"npm_access_list_id,omitempty"`
|
|
NotificationURL *string `json:"notification_url,omitempty"`
|
|
}
|
|
|
|
// listProjects handles GET /api/projects.
|
|
func (s *Server) listProjects(w http.ResponseWriter, r *http.Request) {
|
|
projects, err := s.store.GetAllProjects()
|
|
if err != nil {
|
|
slog.Error("failed to list projects", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, projects)
|
|
}
|
|
|
|
// createProject handles POST /api/projects.
|
|
func (s *Server) createProject(w http.ResponseWriter, r *http.Request) {
|
|
var req projectRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
|
|
if req.Name == "" {
|
|
respondError(w, http.StatusBadRequest, "name is required")
|
|
return
|
|
}
|
|
if req.Image == "" {
|
|
respondError(w, http.StatusBadRequest, "image is required")
|
|
return
|
|
}
|
|
if req.Env == "" {
|
|
req.Env = "{}"
|
|
}
|
|
if req.Volumes == "" {
|
|
req.Volumes = "{}"
|
|
}
|
|
|
|
npmAccessListID := 0
|
|
if req.NpmAccessListID != nil {
|
|
npmAccessListID = *req.NpmAccessListID
|
|
}
|
|
|
|
project, err := s.store.CreateProject(store.Project{
|
|
Name: req.Name,
|
|
Registry: req.Registry,
|
|
Image: req.Image,
|
|
Port: req.Port,
|
|
Healthcheck: req.Healthcheck,
|
|
Env: req.Env,
|
|
Volumes: req.Volumes,
|
|
NpmAccessListID: npmAccessListID,
|
|
})
|
|
if err != nil {
|
|
slog.Error("failed to create project", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
|
|
s.eventBus.Publish(events.Event{
|
|
Type: events.EventLog,
|
|
Payload: events.EventLogPayload{
|
|
Source: "admin",
|
|
Severity: "info",
|
|
Message: "project created: " + project.Name,
|
|
},
|
|
})
|
|
|
|
respondJSON(w, http.StatusCreated, project)
|
|
}
|
|
|
|
// getProject handles GET /api/projects/{id}.
|
|
func (s *Server) getProject(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
project, err := s.store.GetProjectByID(id)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
respondNotFound(w, "project")
|
|
return
|
|
}
|
|
slog.Error("failed to get project", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
|
|
// Also fetch stages for this project.
|
|
stages, err := s.store.GetStagesByProjectID(id)
|
|
if err != nil {
|
|
slog.Error("failed to get stages", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
|
|
respondJSON(w, http.StatusOK, map[string]any{
|
|
"project": project,
|
|
"stages": stages,
|
|
})
|
|
}
|
|
|
|
// updateProject handles PUT /api/projects/{id}.
|
|
func (s *Server) updateProject(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
|
|
existing, err := s.store.GetProjectByID(id)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
respondNotFound(w, "project")
|
|
return
|
|
}
|
|
slog.Error("failed to get project", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
|
|
var req projectRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
|
|
// Apply updates to existing project, preserving fields not provided.
|
|
updated := existing
|
|
if req.Name != "" {
|
|
updated.Name = req.Name
|
|
}
|
|
if req.Image != "" {
|
|
updated.Image = req.Image
|
|
}
|
|
updated.Registry = req.Registry
|
|
updated.Port = req.Port
|
|
updated.Healthcheck = req.Healthcheck
|
|
if req.Env != "" {
|
|
updated.Env = req.Env
|
|
}
|
|
if req.Volumes != "" {
|
|
updated.Volumes = req.Volumes
|
|
}
|
|
if req.NpmAccessListID != nil {
|
|
updated.NpmAccessListID = *req.NpmAccessListID
|
|
}
|
|
if req.NotificationURL != nil {
|
|
updated.NotificationURL = *req.NotificationURL
|
|
}
|
|
|
|
if err := s.store.UpdateProject(updated); err != nil {
|
|
slog.Error("failed to update project", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
|
|
s.eventBus.Publish(events.Event{
|
|
Type: events.EventLog,
|
|
Payload: events.EventLogPayload{
|
|
Source: "admin",
|
|
Severity: "info",
|
|
Message: "project updated: " + updated.Name,
|
|
},
|
|
})
|
|
|
|
respondJSON(w, http.StatusOK, updated)
|
|
}
|
|
|
|
// deleteProject handles DELETE /api/projects/{id}.
|
|
func (s *Server) deleteProject(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
|
|
// Clean up Docker containers and proxy routes before deleting the project.
|
|
ctx := r.Context()
|
|
stages, _ := s.store.GetStagesByProjectID(id)
|
|
for _, stage := range stages {
|
|
rows, _ := s.store.ListContainersByStageID(stage.ID)
|
|
for _, c := range rows {
|
|
if c.ContainerID != "" {
|
|
if err := s.docker.RemoveContainer(ctx, c.ContainerID, true); err != nil {
|
|
slog.Warn("delete project: remove container", "container", c.ContainerID, "error", err)
|
|
}
|
|
}
|
|
if c.ProxyRouteID != "" {
|
|
if err := s.proxyProvider.DeleteRoute(ctx, c.ProxyRouteID); err != nil {
|
|
slog.Warn("delete project: delete proxy route", "route", c.ProxyRouteID, "error", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := s.store.DeleteProject(id); err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
respondNotFound(w, "project")
|
|
return
|
|
}
|
|
slog.Error("failed to delete project", "error", err)
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
return
|
|
}
|
|
s.eventBus.Publish(events.Event{
|
|
Type: events.EventLog,
|
|
Payload: events.EventLogPayload{
|
|
Source: "admin",
|
|
Severity: "info",
|
|
Message: "project deleted: " + id,
|
|
},
|
|
})
|
|
|
|
respondJSON(w, http.StatusOK, map[string]string{"deleted": id})
|
|
}
|