-
Notifications
You must be signed in to change notification settings - Fork 4
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
** | ||
!/.git | ||
!/cmd/vroom | ||
!/cmd/cleanup | ||
!/internal | ||
!/pkg | ||
!/vendor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
/vroom | ||
/downloader | ||
/issuedetection | ||
/cleanup | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
FROM alpine | ||
|
||
EXPOSE 8080 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should change the |
||
|
||
WORKDIR /var/vroom | ||
|
||
ENTRYPOINT ["/bin/vroom"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.