Skip to content

Commit

Permalink
chore(pgs): cleanup deprecated buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jan 26, 2025
1 parent 894e7de commit 954b9b8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
77 changes: 77 additions & 0 deletions cmd/scripts/rm-old-buckets/rm-old-buckets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"context"
"net/url"

"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/prose"
"github.com/picosh/pico/shared"
)

func bail(err error) {
if err != nil {
panic(err)
}
}

func main() {
cfg := prose.NewConfigSite()
logger := cfg.Logger
picoDb := postgres.NewDB(cfg.DbURL, logger)
endpoint, err := url.Parse(cfg.MinioURL)
bail(err)
ssl := endpoint.Scheme == "https"
mClient, err := minio.New(endpoint.Host, &minio.Options{
Creds: credentials.NewStaticV4(cfg.MinioUser, cfg.MinioPass, ""),
Secure: ssl,
})
bail(err)

users, err := picoDb.FindUsers()
bail(err)
ctx := context.TODO()

for _, user := range users {
logger.Info("deleting old buckets", "user", user.Name)
bucketName := shared.GetImgsBucketName(user.ID)

exists, err := mClient.BucketExists(ctx, bucketName)
if err != nil {
logger.Error("bucket exists", "err", err)
}

if !exists {
continue
}

objectsCh := make(chan minio.ObjectInfo)
go func() {
defer close(objectsCh)
opts := minio.ListObjectsOptions{Prefix: "", Recursive: true}
for object := range mClient.ListObjects(ctx, bucketName, opts) {
logger.Info("object", "name", object.Key)
if object.Err != nil {
logger.Error("list objects", "err", err)
}
objectsCh <- object
}
}()

errorCh := mClient.RemoveObjects(ctx, bucketName, objectsCh, minio.RemoveObjectsOptions{})

for e := range errorCh {
logger.Error("remove obj", "err", e)
}

logger.Info("removing bucket", "user", user.Name, "bucket", bucketName)
err = mClient.RemoveBucket(ctx, bucketName)
if err != nil {
logger.Error("remove bucket", "err", err)
}

logger.Info("Success!", "user", user.Name)
}
}
8 changes: 0 additions & 8 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ services:
- .env.prod
volumes:
- ./data/minio-data:/data
deploy:
resources:
limits:
cpus: '0.3'
pipemgr:
env_file:
- .env.prod
Expand All @@ -45,10 +41,6 @@ services:
volumes:
- ./data/imgs-storage/data:/storage/imgs
- ./data/pgs-storage/data:/storage/pgs
deploy:
resources:
limits:
cpus: '0.3'
pastes-caddy:
image: ghcr.io/picosh/pico/caddy:latest
restart: always
Expand Down
2 changes: 1 addition & 1 deletion pgs/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (c *Cmd) stats(cfgMaxSize uint64) error {
storageMax := ff.Data.StorageMax

bucketName := shared.GetAssetBucketName(c.User.ID)
bucket, err := c.Store.UpsertBucket(bucketName)
bucket, err := c.Store.GetBucket(bucketName)
if err != nil {
return err
}
Expand Down

0 comments on commit 954b9b8

Please sign in to comment.