Add self-update, including in Docker

Sandbar can install new releases itself using Reef's selfupdate, automatically with
SANDBAR_AUTO_UPDATE=1 or from a button on the Account page. In a container the new binary is
written to <data>/bin and takes over from the image at start, until the image catches up.
Tags now publish a Gitea release with archives and SHA256SUMS as well as the image.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
VeillaxandClaude Sonnet 5.5 committed 2026-10-27 04:22:54 -06:00
1 parent 955e01333d
commit 73f45fb3ae
14 files changed
+610 -20

No files matched your search

+27 -2
View File
@@ -18,6 +18,7 @@ import (
"git.veillax.com/Veillax/reef/dotenv"
"git.veillax.com/Veillax/reef/secretbox"
"git.veillax.com/Veillax/reef/selfupdate"
"git.veillax.com/Veillax/sandbar/internal/sandbar"
)
@@ -45,6 +46,7 @@ func main() {
data := flag.String("data", env("SANDBAR_DATA", "./sandbar-data"), "directory for the database")
url := flag.String("url", env("SANDBAR_URL", ""), "public URL (used for links in notifications)")
flag.String("env", ".env", "file to read secrets and settings from")
autoUpdate := flag.Bool("auto-update", os.Getenv("SANDBAR_AUTO_UPDATE") == "1", "check for new releases every few hours and update and restart by itself (Linux and macOS, including Docker)")
genkey := flag.Bool("genkey", false, "print a new random SANDBAR_MASTER_KEY and exit")
showVersion := flag.Bool("version", false, "print the version and exit")
flag.Parse()
@@ -58,6 +60,9 @@ func main() {
return
}
// In a container, an update lives on the data volume and takes over from the image.
sandbar.UseNewerOverlay(*data, version, log.Printf)
key, err := masterKey(*data)
if err != nil {
log.Fatal(err)
@@ -72,20 +77,40 @@ func main() {
}
defer st.Close()
var hs *http.Server
atoi := func(k string) int { n, _ := strconv.Atoi(os.Getenv(k)); return n }
srv := sandbar.New(sandbar.Options{
var srv *sandbar.Server
srv = sandbar.New(sandbar.Options{
DataDir: *data,
URL: strings.TrimRight(*url, "/"),
MetricsToken: os.Getenv("SANDBAR_METRICS_TOKEN"),
MessageDays: atoi("SANDBAR_MESSAGE_DAYS"),
CheckDays: atoi("SANDBAR_CHECK_DAYS"),
Version: version,
AutoUpdate: *autoUpdate,
UpdateServer: os.Getenv("SANDBAR_UPDATE_SERVER"),
UpdateRepo: os.Getenv("SANDBAR_UPDATE_REPO"),
Restart: func(exe string) error {
// Let go of the port and the database, then become the new binary. If that fails, exit
// so the container or service manager starts it again, now from the new file.
hs.Close()
srv.Close()
st.Close()
err := selfupdate.Restart(exe)
log.Printf("could not restart into %s, exiting: %v", exe, err)
os.Exit(1)
return err
},
SMTP: sandbar.SMTPConfig{Host: os.Getenv("SANDBAR_SMTP_HOST"), Port: os.Getenv("SANDBAR_SMTP_PORT"),
User: os.Getenv("SANDBAR_SMTP_USER"), Password: os.Getenv("SANDBAR_SMTP_PASSWORD"), From: env("SANDBAR_SMTP_FROM", "sandbar@localhost")},
}, st)
srv.BootstrapAdmin(os.Getenv("SANDBAR_ADMIN_EMAIL"), os.Getenv("SANDBAR_ADMIN_PASSWORD"))
srv.Start()
if *autoUpdate {
go srv.RunAutoUpdate(context.Background())
}
hs := &http.Server{Addr: *listen, Handler: srv.Handler(), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 2 * time.Minute}
hs = &http.Server{Addr: *listen, Handler: srv.Handler(), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 2 * time.Minute}
go func() {
log.Printf("sandbar %s listening on %s", version, *listen)
if err := hs.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {