Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: cleanup binary for self-hosted #284

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**
!/.git
!/cmd/vroom
!/cmd/cleanup
!/internal
!/pkg
!/vendor
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/vroom
/downloader
/issuedetection
/cleanup

# Test binary, built with `go test -c`
*.test
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ COPY . .

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o . -ldflags="-s -w -X main.release=$(git rev-parse HEAD)" ./cmd/vroom

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o . -ldflags="-s -w" ./cmd/cleanup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this on another line, we can add ./cmd/cleanup to the line above.


FROM alpine

EXPOSE 8080
Expand All @@ -18,6 +20,8 @@ ENV SENTRY_BUCKET_PROFILES=file://localhost/$PROFILES_DIR

COPY --from=builder /src/vroom /bin/vroom

COPY --from=builder /src/cleanup /bin/cleanup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change the COPY above to include /src/cleanup instead.


WORKDIR /var/vroom

ENTRYPOINT ["/bin/vroom"]
7 changes: 7 additions & 0 deletions cmd/cleanup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Cleanup

Simple script to cleanup profiles that's stored on local filesystem daily, used by self-hosted repository.

It requires 2 environment variables:
- `SENTRY_BUCKET_PROFILES` - path to profiles directory
- `SENTRY_EVENT_RETENTION_DAYS` - retention days for profiles, in plain numbers (sample: 90, not 90d). A common environment variable on self-hosted (also used by Sentry and Snuba service)
91 changes: 91 additions & 0 deletions cmd/cleanup/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"errors"
"os"
"os/signal"
"path"
"strconv"
"time"

"github.com/getsentry/sentry-go"
"github.com/getsentry/vroom/internal/logutil"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
)

func cleanup(profilesPath string, timeLimit time.Time) error {
dirEntries, err := os.ReadDir(profilesPath)
if err != nil {
return err
}

for _, entry := range dirEntries {
if entry.IsDir() {
return cleanup(path.Join(profilesPath, entry.Name()), timeLimit)
}

fileInfo, err := entry.Info()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}

if timeLimit.After(fileInfo.ModTime()) {
err = os.Remove(path.Join(profilesPath, entry.Name()))
if err != nil {
return err
}
}
}

return nil
}

func main() {
sentryBucketProfiles, ok := os.LookupEnv("SENTRY_BUCKET_PROFILES")
if !ok {
sentryBucketProfiles = "/var/lib/sentry-profiles"
}

sentryEventRetentionDays, ok := os.LookupEnv("SENTRY_EVENT_RETENTION_DAYS")
if !ok {
sentryEventRetentionDays = "90"
}

logutil.ConfigureLogger()

err := sentry.Init(sentry.ClientOptions{})
if err != nil {
log.Fatal().Err(err).Msg("can't initialize sentry")
}

retentionDays, err := strconv.ParseInt(sentryEventRetentionDays, 10, 64)
if err != nil {
log.Fatal().Err(err).Msg("can't parse retention days")
}

timeLimit := time.Now().Add(time.Hour * 24 * -1 * time.Duration(retentionDays))

c := cron.New()
_, err = c.AddFunc("@daily", func() {
err := cleanup(sentryBucketProfiles, timeLimit)
if err != nil {
sentry.CaptureException(err)
log.Error().Err(err).Msg("error cleaning up directories")
}
})
if err != nil {
log.Fatal().Err(err).Msg("can't set up cron function")
}

exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt)

go func() {
<-exitSignal

c.Stop()
}()

c.Run()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pierrec/lz4 v2.6.1+incompatible
github.com/pierrec/lz4/v4 v4.1.15
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.26.1
github.com/segmentio/kafka-go v0.4.38
gocloud.dev v0.29.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,8 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac46PZcF54q6l2hkLEG8op93fZu61KmxWDV4=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down