A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
atcr.io
docker
container
atproto
go
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "path/filepath"
8 "runtime"
9)
10
11// InfraState persists infrastructure resource UUIDs between commands.
12type InfraState struct {
13 Zone string `json:"zone"`
14 ClientName string `json:"client_name,omitempty"`
15 RepoBranch string `json:"repo_branch,omitempty"`
16 Network StateRef `json:"network"`
17 Appview ServerState `json:"appview"`
18 Hold ServerState `json:"hold"`
19 LB StateRef `json:"loadbalancer"`
20 ObjectStorage ObjectStorageState `json:"object_storage"`
21 ScannerEnabled bool `json:"scanner_enabled,omitempty"`
22 ScannerSecret string `json:"scanner_secret,omitempty"`
23}
24
25// Naming returns a Naming helper, defaulting to "seamark" if ClientName is empty.
26func (s *InfraState) Naming() Naming {
27 name := s.ClientName
28 if name == "" {
29 name = "seamark"
30 }
31 return Naming{ClientName: name}
32}
33
34// Branch returns the repo branch, defaulting to "main" if empty.
35func (s *InfraState) Branch() string {
36 if s.RepoBranch == "" {
37 return "main"
38 }
39 return s.RepoBranch
40}
41
42type StateRef struct {
43 UUID string `json:"uuid"`
44}
45
46type ServerState struct {
47 UUID string `json:"server_uuid"`
48 PublicIP string `json:"public_ip"`
49 PrivateIP string `json:"private_ip"`
50}
51
52type ObjectStorageState struct {
53 UUID string `json:"uuid"`
54 Endpoint string `json:"endpoint"`
55 Region string `json:"region"`
56 Bucket string `json:"bucket"`
57 AccessKeyID string `json:"access_key_id"`
58}
59
60func statePath() string {
61 _, thisFile, _, _ := runtime.Caller(0)
62 return filepath.Join(filepath.Dir(thisFile), "state.json")
63}
64
65func loadState() (*InfraState, error) {
66 data, err := os.ReadFile(statePath())
67 if err != nil {
68 return nil, fmt.Errorf("read state.json: %w (run 'provision' first)", err)
69 }
70 var st InfraState
71 if err := json.Unmarshal(data, &st); err != nil {
72 return nil, fmt.Errorf("parse state.json: %w", err)
73 }
74 return &st, nil
75}
76
77func saveState(st *InfraState) error {
78 data, err := json.MarshalIndent(st, "", " ")
79 if err != nil {
80 return fmt.Errorf("marshal state: %w", err)
81 }
82 if err := os.WriteFile(statePath(), data, 0644); err != nil {
83 return fmt.Errorf("write state.json: %w", err)
84 }
85 return nil
86}
87
88func deleteState() error {
89 if err := os.Remove(statePath()); err != nil && !os.IsNotExist(err) {
90 return fmt.Errorf("remove state.json: %w", err)
91 }
92 return nil
93}