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.
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package docker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/moby/moby/api/types/network"
|
|
"github.com/moby/moby/client"
|
|
)
|
|
|
|
// EnsureNetwork creates a Docker network with the given name if it does not
|
|
// already exist. It returns the network ID in all cases.
|
|
func (c *Client) EnsureNetwork(ctx context.Context, networkName string) (string, error) {
|
|
// Check if the network already exists.
|
|
filterArgs := make(client.Filters).Add("name", networkName)
|
|
|
|
listResult, err := c.api.NetworkList(ctx, client.NetworkListOptions{
|
|
Filters: filterArgs,
|
|
})
|
|
if err != nil {
|
|
return "", fmt.Errorf("list networks for %s: %w", networkName, err)
|
|
}
|
|
|
|
// NetworkList with a name filter may return partial matches, so check exact name.
|
|
for _, n := range listResult.Items {
|
|
if n.Name == networkName {
|
|
return n.ID, nil
|
|
}
|
|
}
|
|
|
|
// Create the network.
|
|
resp, err := c.api.NetworkCreate(ctx, networkName, client.NetworkCreateOptions{
|
|
Driver: "bridge",
|
|
Labels: map[string]string{
|
|
LabelManaged: "true",
|
|
},
|
|
})
|
|
if err != nil {
|
|
return "", fmt.Errorf("create network %s: %w", networkName, err)
|
|
}
|
|
|
|
return resp.ID, nil
|
|
}
|
|
|
|
// ConnectNetwork attaches a container to an existing network.
|
|
func (c *Client) ConnectNetwork(ctx context.Context, networkID string, containerID string) error {
|
|
_, err := c.api.NetworkConnect(ctx, networkID, client.NetworkConnectOptions{
|
|
Container: containerID,
|
|
EndpointConfig: &network.EndpointSettings{},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("connect container %s to network %s: %w", containerID, networkID, err)
|
|
}
|
|
return nil
|
|
}
|