|
| 1 | +// Package cmd contains the watchtower (sub-)commands |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "strings" |
| 9 | + "syscall" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/containrrr/watchtower/internal/flags" |
| 13 | + "github.com/containrrr/watchtower/pkg/container" |
| 14 | + "github.com/containrrr/watchtower/pkg/notifications" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +var notifyUpgradeCommand = NewNotifyUpgradeCommand() |
| 19 | + |
| 20 | +// NewNotifyUpgradeCommand creates the notify upgrade command for watchtower |
| 21 | +func NewNotifyUpgradeCommand() *cobra.Command { |
| 22 | + return &cobra.Command{ |
| 23 | + Use: "notify-upgrade", |
| 24 | + Short: "Upgrade legacy notification configuration to shoutrrr URLs", |
| 25 | + Run: runNotifyUpgrade, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func runNotifyUpgrade(cmd *cobra.Command, args []string) { |
| 30 | + if err := runNotifyUpgradeE(cmd, args); err != nil { |
| 31 | + logf("Notification upgrade failed: %v", err) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func runNotifyUpgradeE(cmd *cobra.Command, _ []string) error { |
| 36 | + f := cmd.Flags() |
| 37 | + flags.ProcessFlagAliases(f) |
| 38 | + |
| 39 | + notifier = notifications.NewNotifier(cmd) |
| 40 | + urls := notifier.GetURLs() |
| 41 | + |
| 42 | + logf("Found notification configurations for: %v", strings.Join(notifier.GetNames(), ", ")) |
| 43 | + |
| 44 | + outFile, err := os.CreateTemp("/", "watchtower-notif-urls-*") |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("failed to create output file: %v", err) |
| 47 | + } |
| 48 | + logf("Writing notification URLs to %v", outFile.Name()) |
| 49 | + logf("") |
| 50 | + |
| 51 | + sb := strings.Builder{} |
| 52 | + sb.WriteString("WATCHTOWER_NOTIFICATION_URL=") |
| 53 | + |
| 54 | + for i, u := range urls { |
| 55 | + if i != 0 { |
| 56 | + sb.WriteRune(' ') |
| 57 | + } |
| 58 | + sb.WriteString(u) |
| 59 | + } |
| 60 | + |
| 61 | + _, err = fmt.Fprint(outFile, sb.String()) |
| 62 | + tryOrLog(err, "Failed to write to output file") |
| 63 | + |
| 64 | + tryOrLog(outFile.Sync(), "Failed to sync output file") |
| 65 | + tryOrLog(outFile.Close(), "Failed to close output file") |
| 66 | + |
| 67 | + containerID := "<CONTAINER>" |
| 68 | + cid, err := container.GetRunningContainerID() |
| 69 | + tryOrLog(err, "Failed to get running container ID") |
| 70 | + if cid != "" { |
| 71 | + containerID = cid.ShortID() |
| 72 | + } |
| 73 | + logf("To get the environment file, use:") |
| 74 | + logf("cp %v:%v ./watchtower-notifications.env", containerID, outFile.Name()) |
| 75 | + logf("") |
| 76 | + logf("Note: This file will be removed in 5 minutes or when this container is stopped!") |
| 77 | + |
| 78 | + signalChannel := make(chan os.Signal, 1) |
| 79 | + time.AfterFunc(5*time.Minute, func() { |
| 80 | + signalChannel <- syscall.SIGALRM |
| 81 | + }) |
| 82 | + |
| 83 | + signal.Notify(signalChannel, os.Interrupt) |
| 84 | + signal.Notify(signalChannel, syscall.SIGTERM) |
| 85 | + |
| 86 | + switch <-signalChannel { |
| 87 | + case syscall.SIGALRM: |
| 88 | + logf("Timed out!") |
| 89 | + case os.Interrupt, syscall.SIGTERM: |
| 90 | + logf("Stopping...") |
| 91 | + default: |
| 92 | + } |
| 93 | + |
| 94 | + if err := os.Remove(outFile.Name()); err != nil { |
| 95 | + logf("Failed to remove file, it may still be present in the container image! Error: %v", err) |
| 96 | + } else { |
| 97 | + logf("Environment file has been removed.") |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func tryOrLog(err error, message string) { |
| 104 | + if err != nil { |
| 105 | + logf("%v: %v\n", message, err) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func logf(format string, v ...interface{}) { |
| 110 | + fmt.Fprintln(os.Stderr, fmt.Sprintf(format, v...)) |
| 111 | +} |
0 commit comments