switch urfave to cobra
parent
339df55cc6
commit
998e8052c5
@ -1,168 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SetupCliFlags registers flags on the supplied urfave app
|
|
||||||
func SetupCliFlags(app *cli.App) {
|
|
||||||
app.Flags = []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "host, H",
|
|
||||||
Usage: "daemon socket to connect to",
|
|
||||||
Value: "unix:///var/run/docker.sock",
|
|
||||||
EnvVar: "DOCKER_HOST",
|
|
||||||
},
|
|
||||||
cli.IntFlag{
|
|
||||||
Name: "interval, i",
|
|
||||||
Usage: "poll interval (in seconds)",
|
|
||||||
Value: 300,
|
|
||||||
EnvVar: "WATCHTOWER_POLL_INTERVAL",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "schedule, s",
|
|
||||||
Usage: "the cron expression which defines when to update",
|
|
||||||
EnvVar: "WATCHTOWER_SCHEDULE",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "no-pull",
|
|
||||||
Usage: "do not pull new images",
|
|
||||||
EnvVar: "WATCHTOWER_NO_PULL",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "no-restart",
|
|
||||||
Usage: "do not restart containers",
|
|
||||||
EnvVar: "WATCHTOWER_NO_RESTART",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "cleanup",
|
|
||||||
Usage: "remove old images after updating",
|
|
||||||
EnvVar: "WATCHTOWER_CLEANUP",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "tlsverify",
|
|
||||||
Usage: "use TLS and verify the remote",
|
|
||||||
EnvVar: "DOCKER_TLS_VERIFY",
|
|
||||||
},
|
|
||||||
cli.DurationFlag{
|
|
||||||
Name: "stop-timeout",
|
|
||||||
Usage: "timeout before container is forcefully stopped",
|
|
||||||
Value: time.Second * 10,
|
|
||||||
EnvVar: "WATCHTOWER_TIMEOUT",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "label-enable",
|
|
||||||
Usage: "watch containers where the com.centurylinklabs.watchtower.enable label is true",
|
|
||||||
EnvVar: "WATCHTOWER_LABEL_ENABLE",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "debug",
|
|
||||||
Usage: "enable debug mode with verbose logging",
|
|
||||||
},
|
|
||||||
cli.StringSliceFlag{
|
|
||||||
Name: "notifications",
|
|
||||||
Value: &cli.StringSlice{},
|
|
||||||
Usage: "notification types to send (valid: email, slack, msteams)",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATIONS",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notifications-level",
|
|
||||||
Usage: "The log level used for sending notifications. Possible values: \"panic\", \"fatal\", \"error\", \"warn\", \"info\" or \"debug\"",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATIONS_LEVEL",
|
|
||||||
Value: "info",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-email-from",
|
|
||||||
Usage: "Address to send notification e-mails from",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_FROM",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-email-to",
|
|
||||||
Usage: "Address to send notification e-mails to",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_TO",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-email-server",
|
|
||||||
Usage: "SMTP server to send notification e-mails through",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_SERVER",
|
|
||||||
},
|
|
||||||
cli.IntFlag{
|
|
||||||
Name: "notification-email-server-port",
|
|
||||||
Usage: "SMTP server port to send notification e-mails through",
|
|
||||||
Value: 25,
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "notification-email-server-tls-skip-verify",
|
|
||||||
Usage: "Controls whether watchtower verifies the SMTP server's certificate chain and host name. " +
|
|
||||||
"If set, TLS accepts any certificate " +
|
|
||||||
"presented by the server and any host name in that certificate. " +
|
|
||||||
"In this mode, TLS is susceptible to man-in-the-middle attacks. " +
|
|
||||||
"This should be used only for testing.",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_TLS_SKIP_VERIFY",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-email-server-user",
|
|
||||||
Usage: "SMTP server user for sending notifications",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-email-server-password",
|
|
||||||
Usage: "SMTP server password for sending notifications",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-slack-hook-url",
|
|
||||||
Usage: "The Slack Hook URL to send notifications to",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-slack-identifier",
|
|
||||||
Usage: "A string which will be used to identify the messages coming from this watchtower instance. Default if omitted is \"watchtower\"",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER",
|
|
||||||
Value: "watchtower",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-slack-channel",
|
|
||||||
Usage: "A string which overrides the webhook's default channel. Example: #my-custom-channel",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_CHANNEL",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-slack-icon-emoji",
|
|
||||||
Usage: "An emoji code string to use in place of the default icon",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_ICON_EMOJI",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-slack-icon-url",
|
|
||||||
Usage: "An icon image URL string to use in place of the default icon",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_ICON_URL",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "notification-msteams-hook",
|
|
||||||
Usage: "The MSTeams WebHook URL to send notifications to",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "notification-msteams-data",
|
|
||||||
Usage: "The MSTeams notifier will try to extract log entry fields as MSTeams message facts",
|
|
||||||
EnvVar: "WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "monitor-only",
|
|
||||||
Usage: "Will only monitor for new images, not update the containers",
|
|
||||||
EnvVar: "WATCHTOWER_MONITOR_ONLY",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "run-once",
|
|
||||||
Usage: "Run once now and exit",
|
|
||||||
EnvVar: "WATCHTOWER_RUN_ONCE",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "include-stopped",
|
|
||||||
Usage: "Will also include created and exited containers",
|
|
||||||
EnvVar: "WATCHTOWER_INCLUDE_STOPPED",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,178 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/containrrr/watchtower/actions"
|
||||||
|
"github.com/containrrr/watchtower/container"
|
||||||
|
"github.com/containrrr/watchtower/internal/flags"
|
||||||
|
"github.com/containrrr/watchtower/notifications"
|
||||||
|
"github.com/robfig/cron"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DockerAPIMinVersion string = "1.24"
|
||||||
|
|
||||||
|
var (
|
||||||
|
client container.Client
|
||||||
|
scheduleSpec string
|
||||||
|
cleanup bool
|
||||||
|
noRestart bool
|
||||||
|
monitorOnly bool
|
||||||
|
enableLabel bool
|
||||||
|
notifier *notifications.Notifier
|
||||||
|
timeout time.Duration
|
||||||
|
)
|
||||||
|
|
||||||
|
var rootCmd = &cobra.Command{
|
||||||
|
Use: "watchtower",
|
||||||
|
Short: "Automatically updates running Docker containers",
|
||||||
|
Long: `
|
||||||
|
Watchtower automatically updates running Docker containers whenever a new image is released.
|
||||||
|
More information available at https://github.com/containrrr/watchtower/.
|
||||||
|
`,
|
||||||
|
Run: Run,
|
||||||
|
PreRun: PreRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flags.SetDefaults()
|
||||||
|
flags.RegisterDockerFlags(rootCmd)
|
||||||
|
flags.RegisterSystemFlags(rootCmd)
|
||||||
|
flags.RegisterNotificationFlags(rootCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Execute() {
|
||||||
|
if err := rootCmd.Execute(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PreRun(cmd *cobra.Command, args []string) {
|
||||||
|
f := cmd.PersistentFlags()
|
||||||
|
|
||||||
|
if enabled, _ := f.GetBool("debug"); enabled == true {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
pollingSet := f.Changed("interval")
|
||||||
|
cronSet := f.Changed("schedule")
|
||||||
|
schedule, _ := f.GetString("schedule")
|
||||||
|
cronLen := len(schedule)
|
||||||
|
|
||||||
|
if pollingSet && cronSet && cronLen > 0 {
|
||||||
|
log.Fatal("Only schedule or interval can be defined, not both.")
|
||||||
|
} else if cronSet && cronLen > 0 {
|
||||||
|
scheduleSpec, _ = f.GetString("schedule")
|
||||||
|
} else {
|
||||||
|
interval, _ := f.GetInt("interval")
|
||||||
|
scheduleSpec = "@every " + strconv.Itoa(interval) + "s"
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup, noRestart, monitorOnly, timeout = flags.ReadFlags(cmd)
|
||||||
|
|
||||||
|
if timeout < 0 {
|
||||||
|
log.Fatal("Please specify a positive value for timeout value.")
|
||||||
|
}
|
||||||
|
enableLabel, _ = f.GetBool("label-enable")
|
||||||
|
|
||||||
|
// configure environment vars for client
|
||||||
|
err := flags.EnvConfig(cmd, DockerAPIMinVersion)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
noPull, _ := f.GetBool("no-pull")
|
||||||
|
includeStopped, _ := f.GetBool("include-stopped")
|
||||||
|
client = container.NewClient(
|
||||||
|
!noPull,
|
||||||
|
includeStopped,
|
||||||
|
)
|
||||||
|
|
||||||
|
notifier = notifications.NewNotifier(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run(c *cobra.Command, names []string) {
|
||||||
|
filter := container.BuildFilter(names, enableLabel)
|
||||||
|
runOnce, _ := c.PersistentFlags().GetBool("run-once")
|
||||||
|
|
||||||
|
if runOnce {
|
||||||
|
log.Info("Running a one time update.")
|
||||||
|
runUpdatesWithNotifications(filter)
|
||||||
|
os.Exit(1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := actions.CheckForMultipleWatchtowerInstances(client, cleanup); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
runUpgradesOnSchedule(filter)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runUpgradesOnSchedule(filter container.Filter) error {
|
||||||
|
tryLockSem := make(chan bool, 1)
|
||||||
|
tryLockSem <- true
|
||||||
|
|
||||||
|
cron := cron.New()
|
||||||
|
err := cron.AddFunc(
|
||||||
|
scheduleSpec,
|
||||||
|
func() {
|
||||||
|
select {
|
||||||
|
case v := <-tryLockSem:
|
||||||
|
defer func() { tryLockSem <- v }()
|
||||||
|
runUpdatesWithNotifications(filter)
|
||||||
|
default:
|
||||||
|
log.Debug("Skipped another update already running.")
|
||||||
|
}
|
||||||
|
|
||||||
|
nextRuns := cron.Entries()
|
||||||
|
if len(nextRuns) > 0 {
|
||||||
|
log.Debug("Scheduled next run: " + nextRuns[0].Next.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("Starting Watchtower and scheduling first run: " + cron.Entries()[0].Schedule.Next(time.Now()).String())
|
||||||
|
cron.Start()
|
||||||
|
|
||||||
|
// Graceful shut-down on SIGINT/SIGTERM
|
||||||
|
interrupt := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(interrupt, os.Interrupt)
|
||||||
|
signal.Notify(interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
|
<-interrupt
|
||||||
|
cron.Stop()
|
||||||
|
log.Info("Waiting for running update to be finished...")
|
||||||
|
<-tryLockSem
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runUpdatesWithNotifications(filter container.Filter) {
|
||||||
|
notifier.StartNotification()
|
||||||
|
updateParams := actions.UpdateParams{
|
||||||
|
Filter: filter,
|
||||||
|
Cleanup: cleanup,
|
||||||
|
NoRestart: noRestart,
|
||||||
|
Timeout: timeout,
|
||||||
|
MonitorOnly: monitorOnly,
|
||||||
|
}
|
||||||
|
err := actions.Update(client, updateParams)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
notifier.SendNotification()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,269 @@
|
|||||||
|
package flags
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterDockerFlags(rootCmd *cobra.Command) {
|
||||||
|
flags := rootCmd.PersistentFlags()
|
||||||
|
flags.StringP("host", "H", viper.GetString("DOCKER_HOST"), "daemon socket to connect to")
|
||||||
|
flags.BoolP("tlsverify", "v", viper.GetBool("DOCKER_TLS_VERIFY"), "use TLS and verify the remote")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterSystemFlags(rootCmd *cobra.Command) {
|
||||||
|
flags := rootCmd.PersistentFlags()
|
||||||
|
flags.IntP(
|
||||||
|
"interval",
|
||||||
|
"i",
|
||||||
|
viper.GetInt("WATCHTOWER_POLL_INTERVAL"),
|
||||||
|
"poll interval (in seconds)")
|
||||||
|
|
||||||
|
flags.StringP("schedule",
|
||||||
|
"s",
|
||||||
|
viper.GetString("WATCHTOWER_SCHEDULE"),
|
||||||
|
"the cron expression which defines when to update")
|
||||||
|
|
||||||
|
flags.DurationP("stop-timeout",
|
||||||
|
"t",
|
||||||
|
viper.GetDuration("WATCHTOWER_TIMEOUT"),
|
||||||
|
"timeout before a container is forcefully stopped")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"no-pull",
|
||||||
|
"",
|
||||||
|
viper.GetBool("WATCHTOWER_NO_PULL"),
|
||||||
|
"do not pull any new images")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"no-restart",
|
||||||
|
"",
|
||||||
|
viper.GetBool("WATCHTOWER_NO_RESTART"),
|
||||||
|
"do not restart any containers")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"cleanup",
|
||||||
|
"c",
|
||||||
|
viper.GetBool("WATCHTOWER_CLEANUP"),
|
||||||
|
"remove previously used images after updating")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"label-enable",
|
||||||
|
"e",
|
||||||
|
viper.GetBool("WATCHTOWER_LABEL_ENABLE"),
|
||||||
|
"watch containers where the com.centurylinklabs.watchtower.enable label is true")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"debug",
|
||||||
|
"d",
|
||||||
|
viper.GetBool("WATCHTOWER_DEBUG"),
|
||||||
|
"enable debug mode with verbose logging")
|
||||||
|
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"monitor-only",
|
||||||
|
"m",
|
||||||
|
viper.GetBool("WATCHTOWER_MONITOR_ONLY"),
|
||||||
|
"Will only monitor for new images, not update the containers")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"run-once",
|
||||||
|
"R",
|
||||||
|
viper.GetBool("WATCHTOWER_RUN_ONCE"),
|
||||||
|
"Run once now and exit")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"include-stopped",
|
||||||
|
"S",
|
||||||
|
viper.GetBool("WATCHTOWER_INCLUDE_STOPPED"),
|
||||||
|
"Will also include created and exited containers")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterNotificationFlags(rootCmd *cobra.Command) {
|
||||||
|
flags := rootCmd.PersistentFlags()
|
||||||
|
|
||||||
|
flags.StringSliceP(
|
||||||
|
"notifications",
|
||||||
|
"n",
|
||||||
|
viper.GetStringSlice("WATCHTOWER_NOTIFICATIONS"),
|
||||||
|
" notification types to send (valid: email, slack, msteams")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notifications-level",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATIONS_LEVEL"),
|
||||||
|
"The log level used for sending notifications. Possible values: panic, fatal, error, warn, info or debug")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-email-from",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_FROM"),
|
||||||
|
"Address to send notification emails from")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-email-to",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_TO"),
|
||||||
|
"Address to send notification emails to")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-email-server",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER"),
|
||||||
|
"SMTP server to send notification emails through")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-email-server-port",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT"),
|
||||||
|
"SMTP server port to send notification emails through")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"notification-email-server-tls-skip-verify",
|
||||||
|
"",
|
||||||
|
viper.GetBool("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_TLS_SKIP_VERIFY"),
|
||||||
|
`
|
||||||
|
Controls whether watchtower verifies the SMTP server's certificate chain and host name.
|
||||||
|
Should only be used for testing.
|
||||||
|
`)
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-email-server-user",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER"),
|
||||||
|
"SMTP server user for sending notifications")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-email-server-password",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD"),
|
||||||
|
"SMTP server password for sending notifications")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-slack-hook-url",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL"),
|
||||||
|
"The Slack Hook URL to send notifications to")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-slack-identifier",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER"),
|
||||||
|
"A string which will be used to identify the messages coming from this watchtower instance")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-slack-channel",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_CHANNEL"),
|
||||||
|
"A string which overrides the webhook's default channel. Example: #my-custom-channel")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-slack-icon-emoji",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_ICON_EMOJI"),
|
||||||
|
"An emoji code string to use in place of the default icon")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-slack-icon-url",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_ICON_URL"),
|
||||||
|
"An icon image URL string to use in place of the default icon")
|
||||||
|
|
||||||
|
flags.StringP(
|
||||||
|
"notification-msteams-hook",
|
||||||
|
"",
|
||||||
|
viper.GetString("WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL"),
|
||||||
|
"The MSTeams WebHook URL to send notifications to")
|
||||||
|
|
||||||
|
flags.BoolP(
|
||||||
|
"notification-msteams-data",
|
||||||
|
"",
|
||||||
|
viper.GetBool("WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA"),
|
||||||
|
"The MSTeams notifier will try to extract log entry fields as MSTeams message facts")
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetDefaults() {
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
viper.SetDefault("DOCKER_HOST", "unix:///var/run/docker.sock")
|
||||||
|
viper.SetDefault("WATCHTOWER_POLL_INTERVAL", 300)
|
||||||
|
viper.SetDefault("WATCHTOWER_TIMEOUT", time.Second*10)
|
||||||
|
viper.SetDefault("WATCHTOWER_NOTIFICATIONS", []string{})
|
||||||
|
viper.SetDefault("WATCHTOWER_NOTIFICATIONS_LEVEL", "info")
|
||||||
|
viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT", 25)
|
||||||
|
viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower")
|
||||||
|
}
|
||||||
|
|
||||||
|
// envConfig translates the command-line options into environment variables
|
||||||
|
// that will initialize the api client
|
||||||
|
func EnvConfig(cmd *cobra.Command, dockerAPIMinVersion string) error {
|
||||||
|
var err error
|
||||||
|
var host string
|
||||||
|
var tls bool
|
||||||
|
|
||||||
|
flags := cmd.PersistentFlags()
|
||||||
|
|
||||||
|
if host, err = flags.GetString("host"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if tls, err = flags.GetBool("tlsverify"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = setEnvOptStr("DOCKER_HOST", host); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = setEnvOptBool("DOCKER_TLS_VERIFY", tls); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = setEnvOptStr("DOCKER_API_VERSION", dockerAPIMinVersion); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadFlags(cmd *cobra.Command) (bool, bool, bool, time.Duration) {
|
||||||
|
flags := cmd.PersistentFlags()
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var cleanup bool
|
||||||
|
var noRestart bool
|
||||||
|
var monitorOnly bool
|
||||||
|
var timeout time.Duration
|
||||||
|
|
||||||
|
if cleanup, err = flags.GetBool("cleanup"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if noRestart, err = flags.GetBool("no-restart"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if monitorOnly, err = flags.GetBool("monitor-only"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if timeout, err = flags.GetDuration("stop-timeout"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cleanup, noRestart, monitorOnly, timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func setEnvOptStr(env string, opt string) error {
|
||||||
|
if opt == "" || opt == os.Getenv(env) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err := os.Setenv(env, opt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setEnvOptBool(env string, opt bool) error {
|
||||||
|
if opt == true {
|
||||||
|
return setEnvOptStr(env, "1")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -1,213 +1,21 @@
|
|||||||
package main // import "github.com/containrrr/watchtower"
|
package main // import "github.com/containrrr/watchtower"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"github.com/containrrr/watchtower/cmd"
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/containrrr/watchtower/actions"
|
|
||||||
cliApp "github.com/containrrr/watchtower/app"
|
|
||||||
"github.com/containrrr/watchtower/container"
|
|
||||||
"github.com/containrrr/watchtower/notifications"
|
|
||||||
"github.com/robfig/cron"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DockerAPIMinVersion is the version of the docker API, which is minimally required by
|
// DockerAPIMinVersion is the version of the docker API, which is minimally required by
|
||||||
// watchtower. Currently we require at least API 1.24 and therefore Docker 1.12 or later.
|
// watchtower. Currently we require at least API 1.24 and therefore Docker 1.12 or later.
|
||||||
const DockerAPIMinVersion string = "1.24"
|
|
||||||
|
|
||||||
var version = "master"
|
var version = "master"
|
||||||
var commit = "unknown"
|
var commit = "unknown"
|
||||||
var date = "unknown"
|
var date = "unknown"
|
||||||
|
|
||||||
var (
|
|
||||||
client container.Client
|
|
||||||
scheduleSpec string
|
|
||||||
cleanup bool
|
|
||||||
noRestart bool
|
|
||||||
monitorOnly bool
|
|
||||||
enableLabel bool
|
|
||||||
notifier *notifications.Notifier
|
|
||||||
timeout time.Duration
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetLevel(log.InfoLevel)
|
log.SetLevel(log.InfoLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := cli.NewApp()
|
cmd.Execute()
|
||||||
InitApp(app)
|
|
||||||
cliApp.SetupCliFlags(app)
|
|
||||||
|
|
||||||
if err := app.Run(os.Args); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// InitApp initializes urfave app metadata and sets up entrypoints
|
|
||||||
func InitApp(app *cli.App) {
|
|
||||||
app.Name = "watchtower"
|
|
||||||
app.Version = version + " - " + commit + " - " + date
|
|
||||||
app.Usage = "Automatically update running Docker containers"
|
|
||||||
app.Before = before
|
|
||||||
app.Action = start
|
|
||||||
}
|
|
||||||
|
|
||||||
func before(c *cli.Context) error {
|
|
||||||
if c.GlobalBool("debug") {
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
}
|
|
||||||
|
|
||||||
pollingSet := c.IsSet("interval")
|
|
||||||
cronSet := c.IsSet("schedule")
|
|
||||||
cronLen := len(c.String("schedule"))
|
|
||||||
|
|
||||||
if pollingSet && cronSet && cronLen > 0 {
|
|
||||||
log.Fatal("Only schedule or interval can be defined, not both.")
|
|
||||||
} else if cronSet && cronLen > 0 {
|
|
||||||
scheduleSpec = c.String("schedule")
|
|
||||||
} else {
|
|
||||||
scheduleSpec = "@every " + strconv.Itoa(c.Int("interval")) + "s"
|
|
||||||
}
|
|
||||||
|
|
||||||
readFlags(c)
|
|
||||||
|
|
||||||
if timeout < 0 {
|
|
||||||
log.Fatal("Please specify a positive value for timeout value.")
|
|
||||||
}
|
|
||||||
enableLabel = c.GlobalBool("label-enable")
|
|
||||||
|
|
||||||
// configure environment vars for client
|
|
||||||
err := envConfig(c)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
client = container.NewClient(
|
|
||||||
!c.GlobalBool("no-pull"),
|
|
||||||
c.GlobalBool("include-stopped"),
|
|
||||||
)
|
|
||||||
notifier = notifications.NewNotifier(c)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func start(c *cli.Context) error {
|
|
||||||
names := c.Args()
|
|
||||||
filter := container.BuildFilter(names, enableLabel)
|
|
||||||
|
|
||||||
if c.GlobalBool("run-once") {
|
|
||||||
log.Info("Running a one time update.")
|
|
||||||
runUpdatesWithNotifications(filter)
|
|
||||||
os.Exit(1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := actions.CheckForMultipleWatchtowerInstances(client, cleanup); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
runUpgradesOnSchedule(filter)
|
|
||||||
os.Exit(1)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func runUpgradesOnSchedule(filter container.Filter) error {
|
|
||||||
tryLockSem := make(chan bool, 1)
|
|
||||||
tryLockSem <- true
|
|
||||||
|
|
||||||
cron := cron.New()
|
|
||||||
err := cron.AddFunc(
|
|
||||||
scheduleSpec,
|
|
||||||
func() {
|
|
||||||
select {
|
|
||||||
case v := <-tryLockSem:
|
|
||||||
defer func() { tryLockSem <- v }()
|
|
||||||
runUpdatesWithNotifications(filter)
|
|
||||||
default:
|
|
||||||
log.Debug("Skipped another update already running.")
|
|
||||||
}
|
|
||||||
|
|
||||||
nextRuns := cron.Entries()
|
|
||||||
if len(nextRuns) > 0 {
|
|
||||||
log.Debug("Scheduled next run: " + nextRuns[0].Next.String())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug("Starting Watchtower and scheduling first run: " + cron.Entries()[0].Schedule.Next(time.Now()).String())
|
|
||||||
cron.Start()
|
|
||||||
|
|
||||||
// Graceful shut-down on SIGINT/SIGTERM
|
|
||||||
interrupt := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(interrupt, os.Interrupt)
|
|
||||||
signal.Notify(interrupt, syscall.SIGTERM)
|
|
||||||
|
|
||||||
<-interrupt
|
|
||||||
cron.Stop()
|
|
||||||
log.Info("Waiting for running update to be finished...")
|
|
||||||
<-tryLockSem
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func runUpdatesWithNotifications(filter container.Filter) {
|
|
||||||
notifier.StartNotification()
|
|
||||||
updateParams := actions.UpdateParams{
|
|
||||||
Filter: filter,
|
|
||||||
Cleanup: cleanup,
|
|
||||||
NoRestart: noRestart,
|
|
||||||
Timeout: timeout,
|
|
||||||
MonitorOnly: monitorOnly,
|
|
||||||
}
|
|
||||||
err := actions.Update(client, updateParams)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
notifier.SendNotification()
|
|
||||||
}
|
|
||||||
|
|
||||||
func setEnvOptStr(env string, opt string) error {
|
|
||||||
if opt == "" || opt == os.Getenv(env) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
err := os.Setenv(env, opt)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func setEnvOptBool(env string, opt bool) error {
|
|
||||||
if opt == true {
|
|
||||||
return setEnvOptStr(env, "1")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// envConfig translates the command-line options into environment variables
|
|
||||||
// that will initialize the api client
|
|
||||||
func envConfig(c *cli.Context) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
err = setEnvOptStr("DOCKER_HOST", c.GlobalString("host"))
|
|
||||||
err = setEnvOptBool("DOCKER_TLS_VERIFY", c.GlobalBool("tlsverify"))
|
|
||||||
err = setEnvOptStr("DOCKER_API_VERSION", DockerAPIMinVersion)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func readFlags(c *cli.Context) {
|
|
||||||
cleanup = c.GlobalBool("cleanup")
|
|
||||||
noRestart = c.GlobalBool("no-restart")
|
|
||||||
monitorOnly = c.GlobalBool("monitor-only")
|
|
||||||
timeout = c.GlobalDuration("stop-timeout")
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue