A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(rexctl): add test-notify and log auto-upgrade notices (#3782)
Hampus98706281832f
diff --git a/fluxer_docs/src/content/docs/operator/rexctl.mdx b/fluxer_docs/src/content/docs/operator/rexctl.mdxindex 47036c99d..5f13c4c30 100644--- a/fluxer_docs/src/content/docs/operator/rexctl.mdx+++ b/fluxer_docs/src/content/docs/operator/rexctl.mdx@@ -76,7 +76,7 @@ rexctl auto-upgrade offRun rexctl upgrade --yes by itself with a systemd timer, or with cron on machines without systemd. It runs every day, or once a week on the day you pass to --day. Pass several times to --at, like 06:00,18:00, to run more than once on those days. Each run backs up your instance first and goes back to the release you had if the new one doesn't start. It skips that run if another rexctl command is changing the instance, and tries again at the next one. A weekly run that is skipped or fails waits a week, and you can run rexctl upgrade yourself at any time. With --full-backup monthly it also makes a full backup with uploads on the 1st of every month, 3 hours before the first upgrade time of the day. Run it again to change the day, the time, the backups or the address. Leaving out a flag sets it back to its default, and leaving out --notify stops the notifications.-With --notify, rexctl posts JSON to the address when an upgrade installs a new release or fails: instance, status (upgraded or failed), release, and the message as both text and content, which Slack and Discord webhooks show as they are. For ntfy, add ?tpl=1&m=\{\{.text\}\} to the topic address.+With --notify, rexctl posts JSON to the address when an upgrade installs a new release or fails: instance, status (upgraded or failed), release, and the message as both text and content, which Slack and Discord webhooks show as they are. For ntfy, add ?tpl=1&m=\{\{.text\}\} to the topic address. Run rexctl auto-upgrade test-notify to send a test notice.```shrexctl auto-upgrade on [flags]@@ -102,6 +102,14 @@ rexctl auto-upgrade on [flags]Web address rexctl posts to when an upgrade installs a new release or fails.+### rexctl auto-upgrade test-notify++Post a test notice to the address you passed to rexctl auto-upgrade on --notify, with status test. It looks like the notice after an upgrade, so you can check that it gets to you without waiting for a new release.++```sh+rexctl auto-upgrade test-notify+```+## rexctl backupBack up your instance in one file that restores on this machine or a new one. It has the database, search data and uploads that Fluxer stores itself. Fluxer pauses while uploads are copied.diff --git a/fluxer_docs/src/content/docs/operator/running.mdx b/fluxer_docs/src/content/docs/operator/running.mdxindex 5c170d494..1c9dc2fde 100644--- a/fluxer_docs/src/content/docs/operator/running.mdx+++ b/fluxer_docs/src/content/docs/operator/running.mdx@@ -33,7 +33,7 @@ This runs `rexctl upgrade --yes` every day at about 04:00 with a systemd timer,- `--at 03:30` picks the time of day, in this machine's time zone. A systemd timer starts within 30 minutes after it, and cron starts at a fixed minute in that window.- `--at 06:00,18:00` upgrades at each of those times, here once in the morning and once in the evening.- `--day sun` upgrades once a week on that day. A weekly run that is skipped or fails waits a week, and you can run `rexctl upgrade` yourself at any time.-- `--notify https://ntfy.sh/your-topic` posts to that address when an upgrade installs a new release or fails.+- `--notify https://ntfy.sh/your-topic` posts to that address when an upgrade installs a new release or fails. Run `rexctl auto-upgrade test-notify` to send a test notice to it.- `--backup without-uploads` leaves uploads out of the backup before each upgrade.- `--full-backup monthly` also makes a full backup with uploads on the 1st of every month.diff --git a/fluxer_rexctl/cmd/autoupgrade.go b/fluxer_rexctl/cmd/autoupgrade.goindex ab7ca661e..671e5b992 100644--- a/fluxer_rexctl/cmd/autoupgrade.go+++ b/fluxer_rexctl/cmd/autoupgrade.go@@ -73,7 +73,7 @@ func newAutoUpgradeCmd(app *App) *cobra.Command {Use: "on",Short: "Upgrade Fluxer by itself every day or once a week",Long: "Run rexctl upgrade --yes by itself with a systemd timer, or with cron on machines without systemd. It runs every day, or once a week on the day you pass to --day. Pass several times to --at, like 06:00,18:00, to run more than once on those days. Each run backs up your instance first and goes back to the release you had if the new one doesn't start. It skips that run if another rexctl command is changing the instance, and tries again at the next one. A weekly run that is skipped or fails waits a week, and you can run rexctl upgrade yourself at any time. With --full-backup monthly it also makes a full backup with uploads on the 1st of every month, 3 hours before the first upgrade time of the day. Run it again to change the day, the time, the backups or the address. Leaving out a flag sets it back to its default, and leaving out --notify stops the notifications.\n\n" +- "With --notify, rexctl posts JSON to the address when an upgrade installs a new release or fails: instance, status (upgraded or failed), release, and the message as both text and content, which Slack and Discord webhooks show as they are. For ntfy, add ?tpl=1&m={{.text}} to the topic address.",+ "With --notify, rexctl posts JSON to the address when an upgrade installs a new release or fails: instance, status (upgraded or failed), release, and the message as both text and content, which Slack and Discord webhooks show as they are. For ntfy, add ?tpl=1&m={{.text}} to the topic address. Run rexctl auto-upgrade test-notify to send a test notice.",Args: noArgs,RunE: func(c *cobra.Command, _ []string) error {return runAutoUpgradeOn(c.Context(), app, o)@@ -93,10 +93,39 @@ func newAutoUpgradeCmd(app *App) *cobra.Command {return runAutoUpgradeOff(c.Context(), app)},}- c.AddCommand(on, off)+ test := &cobra.Command{+ Use: "test-notify",+ Short: "Send a test notice to the --notify address",+ Long: "Post a test notice to the address you passed to rexctl auto-upgrade on --notify, with status test. It looks like the notice after an upgrade, so you can check that it gets to you without waiting for a new release.",+ Args: noArgs,+ RunE: func(c *cobra.Command, _ []string) error {+ return runAutoUpgradeTestNotify(c.Context(), app)+ },+ }+ c.AddCommand(on, off, test)return c}+func runAutoUpgradeTestNotify(ctx context.Context, app *App) error {+ inst, err := app.Instance()+ if err != nil {+ return err+ }+ if inst.State.NotifyURL == "" {+ return ui.Refused("rexctl has no address to send notices to.", "Run rexctl auto-upgrade on with --notify and your address.")+ }+ n := upgradeNotice{Instance: inst.Domain(), Status: "test", Release: inst.State.Release, Text: "This is a test notice from rexctl. It posts here when an upgrade installs a new release or fails."}+ if n.Instance != "" {+ n.Text = n.Instance + ": " + n.Text+ }+ n.Content = n.Text+ if err := postNotice(ctx, inst.State.NotifyURL, n); err != nil {+ return ui.Wrap(err, fmt.Sprintf("Couldn't send a test notice to %s.", maskURL(inst.State.NotifyURL)), "Check the address, then run rexctl auto-upgrade on with --notify and the right one.")+ }+ app.UI.OK("Sent a test notice to %s.", maskURL(inst.State.NotifyURL))+ return nil+}+func autoUpgradeFor(inst *instance.Instance, e *engine.Engine) (host.AutoUpgrade, error) {bin, err := checks.RunningBinary()if err != nil {@@ -249,7 +278,7 @@ func runAutoUpgradeOn(ctx context.Context, app *App, o autoUpgradeOptions) errorleanBackupNote(app, host.AutoUpgradeState{Day: day, At: at, Backup: scope}, notify)}if notify != "" {- app.UI.Info("rexctl tells %s when an upgrade installs a new release or fails.", maskURL(notify))+ app.UI.Info("rexctl tells %s when an upgrade installs a new release or fails. Run rexctl auto-upgrade test-notify to send a test notice.", maskURL(notify))}app.UI.Info("Run rexctl auto-upgrade to see when it runs next and how the last run went.")return nil@@ -483,14 +512,22 @@ func maskURL(raw string) string {func notifyUpgrade(ctx context.Context, app *App, runErr error) {if ctx.Err() != nil || errors.Is(runErr, context.Canceled) {+ if app.outcome != "" {+ app.UI.Info("rexctl was stopped, so it didn't send the notice.")+ }return}dir, err := app.Dir()if err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't find the instance to send the notice for (%v).", err), "")return}inst, err := instance.Find(dir)- if err != nil || inst.State.NotifyURL == "" {+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't load the notify address, so rexctl didn't send the notice (%v).", err), "")+ return+ }+ if inst.State.NotifyURL == "" {return}n := upgradeNotice{Instance: inst.Domain(), Release: inst.State.Release}@@ -510,9 +547,10 @@ func notifyUpgrade(ctx context.Context, app *App, runErr error) {}n.Content = n.Textif err := postNotice(ctx, inst.State.NotifyURL, n); err != nil {- app.UI.Warn(fmt.Sprintf("Couldn't tell %s how the upgrade went.", maskURL(inst.State.NotifyURL)), "")- app.UI.Detail("%v", err)+ app.UI.Warn(fmt.Sprintf("Couldn't tell %s how the upgrade went (%v).", maskURL(inst.State.NotifyURL), err), "")+ return}+ app.UI.Info("Told %s how the upgrade went.", maskURL(inst.State.NotifyURL))}func noticeText(err error) string {@@ -538,6 +576,10 @@ func postNotice(ctx context.Context, rawURL string, n upgradeNotice) error {req.Header.Set("User-Agent", "rexctl")resp, err := http.DefaultClient.Do(req)if err != nil {+ var ue *url.Error+ if errors.As(err, &ue) {+ return ue.Err+ }return err}defer resp.Body.Close()