package main import ( "context" "fmt" "io" "strings" "time" ) // runAppsAutoUpdate dispatches "apps auto-update enable|disable|status|check": // the CLI for GET/PUT /api/v1/apps/{name}/auto-update and its check route. func runAppsAutoUpdate(prog string, args []string, stdout, stderr io.Writer, lookupEnv func(string) (string, bool)) int { if len(args) == 0 { _, _ = fmt.Fprint(stderr, appsAutoUpdateUsage(prog)) return exitUsage } switch args[0] { case "-h", "--help", "help": _, _ = fmt.Fprint(stdout, appsAutoUpdateUsage(prog)) return exitOK case "enable", "disable", "status", "check", "webhook": return runAppsAutoUpdateVerb(prog, args[0], args[1:], stdout, stderr, lookupEnv) default: _, _ = fmt.Fprintf(stderr, "%s: unknown apps auto-update subcommand %q\n\n", prog, args[0]) _, _ = fmt.Fprint(stderr, appsAutoUpdateUsage(prog)) return exitUsage } } func appsAutoUpdateUsage(prog string) string { return fmt.Sprintf(`Usage: %[1]s apps auto-update enable [flags] redeploy automatically when the image tag moves %[1]s apps auto-update disable [flags] opt back out %[1]s apps auto-update status [flags] show the setting and the last check %[1]s apps auto-update check [flags] check the registry now, redeploying if it moved %[1]s apps auto-update webhook [flags] mint a registry push webhook URL (shown once) Point your registry's push webhook (Docker Hub, GitHub Packages for ghcr.io, or any POST) at the webhook URL to update on push instead of waiting for the interval. Docker Hub and GitHub pushes deploy the pushed digest; GitHub deliveries are verified with the signing secret printed with the URL. Use --window-cron and --window-minutes with enable to apply updates only inside a window (UTC); one found earlier waits for it. --clear-window removes it. Off by default. Enabled apps are checked on the control plane's interval (APP_IMAGE_UPDATE_INTERVAL, default 1h). Apps built from source, stopped, frozen, or in a protected environment are skipped, never forced. `, prog) } func runAppsAutoUpdateVerb(prog, verb string, args []string, stdout, stderr io.Writer, lookupEnv func(string) (string, bool)) int { fs, tokenFlagP, apiURLFlagP, profileFlagP, jsonOutP, outputFlagP, queryFlagP := apiFlagSet(prog, "apps auto-update "+verb, "print the result as JSON to stdout and nothing else", stderr) var windowCron string var windowMinutes int var clearWindow bool if verb == "enable" { fs.StringVar(&windowCron, "window-cron", "", "cron expression (UTC) when updates may apply, e.g. '0 3 * * *'") fs.IntVar(&windowMinutes, "window-minutes", 0, "how many minutes the window stays open") fs.BoolVar(&clearWindow, "clear-window", false, "remove the apply window so updates apply at once") } fs.Usage = func() { _, _ = fmt.Fprintf(stderr, "Usage:\n %s apps auto-update %s [flags]\n\nFlags:\n", prog, verb) fs.PrintDefaults() } tokenFlag, apiURLFlag, profileFlag, jsonOut, of, exitCode, ok := parseAPIFlags(fs, args, apiFlagPtrs{tokenFlagP, apiURLFlagP, profileFlagP, jsonOutP, outputFlagP, queryFlagP}, prog, stderr) if !ok { return exitCode } appName, ok := requireOneArg(fs, stderr, prog, "apps auto-update "+verb, "app name") if !ok { return exitUsage } client := apiClientFromFlags(prog, apiURLFlag, tokenFlag, profileFlag, lookupEnv) ctx := context.Background() if verb == "webhook" { hook, err := client.RotateImageUpdateWebhook(ctx, appName) if err != nil { return reportError(stdout, stderr, jsonOut, fmt.Errorf("auto-update webhook for app %q: %w", appName, err)) } return writeScheduledTaskResult(stdout, stderr, of, hook, func() { printImageUpdateWebhook(stdout, strings.TrimRight(resolveAPIURL(apiURLFlag, lookupEnv, prog, resolveProfile(profileFlag, lookupEnv)), "/")+hook.Path, hook.SigningSecret) }) } var ( result imageAutoUpdateResource err error ) switch verb { case "enable": if windowCron != "" || clearWindow { result, err = client.SetImageAutoUpdateWindow(ctx, appName, true, windowCron, windowMinutes) } else { result, err = client.SetImageAutoUpdate(ctx, appName, true) } case "disable": result, err = client.SetImageAutoUpdate(ctx, appName, false) case "check": result, err = client.CheckImageAutoUpdate(ctx, appName) default: result, err = client.GetImageAutoUpdate(ctx, appName) } if err != nil { return reportError(stdout, stderr, jsonOut, fmt.Errorf("auto-update %s for app %q: %w", verb, appName, err)) } return writeScheduledTaskResult(stdout, stderr, of, result, func() { printImageAutoUpdate(stdout, result) }) } // printImageUpdateWebhook prints the one-time URL and the one-line setup for each registry that can push to it. func printImageUpdateWebhook(out io.Writer, url, secret string) { _, _ = fmt.Fprintf(out, "webhook URL (shown once, replaces any earlier one):\n POST %s\n", url) _, _ = fmt.Fprintf(out, "\nDocker Hub: repository > Webhooks > add the URL above (no secret field; the URL is the secret).\n") _, _ = fmt.Fprintf(out, "GitHub (ghcr.io): repository or organization Settings > Webhooks > Payload URL = the URL above, content type application/json, event Packages.\n") if secret != "" { _, _ = fmt.Fprintf(out, " Secret (shown once, deliveries without a valid signature are refused): %s\n", secret) } _, _ = fmt.Fprintf(out, "\nA push deploys the pushed digest, but only for the app's own image and tag. Protected environments file an approval instead.\n") } func printImageAutoUpdate(out io.Writer, r imageAutoUpdateResource) { _, _ = fmt.Fprintf(out, "auto-update: %t\n", r.Enabled) _, _ = fmt.Fprintf(out, "webhook: %t\n", r.HasWebhook) _, _ = fmt.Fprintf(out, "signed: %t\n", r.HasSigningSecret) if r.LastCheckedAt != nil { _, _ = fmt.Fprintf(out, "last check: %s\n", r.LastCheckedAt.Format(time.RFC3339)) } if r.WindowCron != "" { _, _ = fmt.Fprintf(out, "window: %s for %d min (UTC)\n", r.WindowCron, r.WindowMinutes) } if r.LastResult != "" { _, _ = fmt.Fprintf(out, "result: %s\n", r.LastResult) } }