forked from longtao.wu/droboticflow
104 lines
3.9 KiB
Go
104 lines
3.9 KiB
Go
// Read-only pre-release backup. Initialization is intentionally left to the
|
|
// separately approved migration; this command never calls RunMigrations.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func run() error {
|
|
org := flag.String("org", "", "organization ID")
|
|
out := flag.String("output", "", "new private backup JSON path")
|
|
version := flag.String("deployment-version", "", "verified currently deployed commit/image")
|
|
flag.Parse()
|
|
if *org == "" || *out == "" || *version == "" || os.Getenv("MONGO_URI") == "" || os.Getenv("MONGO_DB") == "" {
|
|
return fmt.Errorf("require --org, --output, --deployment-version, MONGO_URI and MONGO_DB (credentials only via environment)")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
|
|
if err != nil {
|
|
return fmt.Errorf("connect failed (credentials omitted)")
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(os.Getenv("MONGO_DB"))
|
|
s := store.NewMongoStore(db)
|
|
users, err := s.ListUsersByOrganization(ctx, *org)
|
|
if err != nil {
|
|
return fmt.Errorf("read account identities: %w", err)
|
|
}
|
|
identities := []map[string]any{}
|
|
for _, u := range users {
|
|
if u.Role == domain.UserRoleAcceptanceReviewer {
|
|
identities = append(identities, map[string]any{"id": u.ID, "supplierId": u.SupplierID, "role": u.Role, "status": u.Status, "deletedAt": u.DeletedAt, "acceptanceAccessMode": u.AcceptanceAccessMode})
|
|
}
|
|
}
|
|
read := func(collection string, filter bson.M, projection bson.M) ([]bson.M, error) {
|
|
cursor, e := db.Collection(collection).Find(ctx, filter, options.Find().SetProjection(projection))
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
defer cursor.Close(ctx)
|
|
rows := []bson.M{}
|
|
e = cursor.All(ctx, &rows)
|
|
return rows, e
|
|
}
|
|
grants, err := read("supplierAcceptanceAuthorizations", bson.M{"organizationId": *org}, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
snapshots, err := read("acceptanceCompatibilitySnapshots", bson.M{"_id": *org}, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Include every recorded shared claim with unfinished status. A conservative
|
|
// list prevents unsafe rollback even if a legacy record has a stale receipt.
|
|
claims, err := read("humanItems", bson.M{"organizationId": *org, "acceptanceClaim": bson.M{"$ne": nil}, "assignedUserId": bson.M{"$nin": bson.A{"", nil}}, "status": bson.M{"$in": bson.A{domain.HumanItemClaimed, domain.HumanItemInProgress}}}, bson.M{"_id": 0, "id": 1, "projectId": 1, "supplierId": 1, "assignedUserId": 1, "currentStageId": 1, "reviewRound": 1, "claimedAt": 1, "acceptanceClaim": 1})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data, err := json.MarshalIndent(map[string]any{"schemaVersion": 1, "capturedAt": time.Now().UTC().Format(time.RFC3339Nano), "organizationId": *org, "deploymentVersion": *version, "acceptanceAccounts": identities, "authorizations": grants, "compatibilitySnapshots": snapshots, "unfinishedSharedClaims": claims}, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = os.MkdirAll(filepath.Dir(*out), 0700); err != nil {
|
|
return err
|
|
}
|
|
file, err := os.OpenFile(*out, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err = file.Write(data); err != nil {
|
|
file.Close()
|
|
return err
|
|
}
|
|
if err = file.Sync(); err != nil {
|
|
file.Close()
|
|
return err
|
|
}
|
|
if err = file.Close(); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("Read-only backup saved: accounts=%d authorizations=%d snapshots=%d unfinishedSharedClaims=%d sha256=%x\n", len(identities), len(grants), len(snapshots), len(claims), sha256.Sum256(data))
|
|
return nil
|
|
}
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|