Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snakeswap! attempt 3 #1253

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 70 additions & 107 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
package cmd

import (
"github.com/containrrr/watchtower/internal/meta"
"fmt"
"github.com/containrrr/watchtower/internal/config"
"math"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

apiMetrics "github.com/containrrr/watchtower/pkg/api/metrics"
"github.com/containrrr/watchtower/pkg/api/update"

"github.com/containrrr/watchtower/internal/actions"
"github.com/containrrr/watchtower/internal/flags"
"github.com/containrrr/watchtower/internal/meta"
"github.com/containrrr/watchtower/pkg/api"
apiMetrics "github.com/containrrr/watchtower/pkg/api/metrics"
"github.com/containrrr/watchtower/pkg/api/update"
"github.com/containrrr/watchtower/pkg/container"
"github.com/containrrr/watchtower/pkg/filters"
"github.com/containrrr/watchtower/pkg/metrics"
"github.com/containrrr/watchtower/pkg/notifications"
t "github.com/containrrr/watchtower/pkg/types"

"github.com/robfig/cron"
log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
client container.Client
scheduleSpec string
cleanup bool
noRestart bool
monitorOnly bool
enableLabel bool
notifier t.Notifier
timeout time.Duration
lifecycleHooks bool
rollingRestart bool
scope string
// Set on build using ldflags
client container.Client
notifier t.Notifier
c config.WatchConfig
)

var rootCmd = NewRootCommand()
Expand All @@ -60,10 +51,10 @@ func NewRootCommand() *cobra.Command {
}

func init() {
flags.SetDefaults()
flags.RegisterDockerFlags(rootCmd)
flags.RegisterSystemFlags(rootCmd)
flags.RegisterNotificationFlags(rootCmd)
config.RegisterDockerOptions(rootCmd)
config.RegisterSystemOptions(rootCmd)
config.RegisterNotificationOptions(rootCmd)
config.BindViperFlags(rootCmd)
}

// Execute the root func and exit in case of errors
Expand All @@ -74,10 +65,10 @@ func Execute() {
}

// PreRun is a lifecycle hook that runs before the command is executed.
func PreRun(cmd *cobra.Command, _ []string) {
f := cmd.PersistentFlags()
func PreRun(_ *cobra.Command, _ []string) {

if enabled, _ := f.GetBool("no-color"); enabled {
// First apply all the settings that affect the output
if config.GetBool(config.NoColor) {
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
})
Expand All @@ -88,126 +79,101 @@ func PreRun(cmd *cobra.Command, _ []string) {
})
}

if enabled, _ := f.GetBool("debug"); enabled {
if config.GetBool(config.Debug) {
log.SetLevel(log.DebugLevel)
}
if enabled, _ := f.GetBool("trace"); enabled {
if config.GetBool(config.Trace) {
log.SetLevel(log.TraceLevel)
}

pollingSet := f.Changed("interval")
schedule, _ := f.GetString("schedule")
cronLen := len(schedule)
interval := config.GetInt(config.Interval)

if pollingSet && cronLen > 0 {
log.Fatal("Only schedule or interval can be defined, not both.")
} else if cronLen > 0 {
scheduleSpec, _ = f.GetString("schedule")
} else {
interval, _ := f.GetInt("interval")
scheduleSpec = "@every " + strconv.Itoa(interval) + "s"
// If empty, set schedule using interval helper value
if config.GetString(config.Schedule) == "" {
viper.Set(string(config.Schedule), fmt.Sprintf("@every %ds", interval))
} else if interval != config.DefaultInterval {
log.Fatal("only schedule or interval can be defined, not both")
}

flags.GetSecretsFromFiles(cmd)
cleanup, noRestart, monitorOnly, timeout = flags.ReadFlags(cmd)
// Then load the rest of the settings
err := viper.Unmarshal(&c)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}

config.GetSecretsFromFiles()

if timeout < 0 {
if c.Timeout <= 0 {
log.Fatal("Please specify a positive value for timeout value.")
}

enableLabel, _ = f.GetBool("label-enable")
lifecycleHooks, _ = f.GetBool("enable-lifecycle-hooks")
rollingRestart, _ = f.GetBool("rolling-restart")
scope, _ = f.GetString("scope")
log.Debugf("Using scope %v", c.Scope)

log.Debug(scope)

// configure environment vars for client
err := flags.EnvConfig(cmd)
if err != nil {
log.Fatal(err)
if err = config.EnvConfig(); err != nil {
log.Fatalf("failed to setup environment variables: %v", err)
}

noPull, _ := f.GetBool("no-pull")
includeStopped, _ := f.GetBool("include-stopped")
includeRestarting, _ := f.GetBool("include-restarting")
reviveStopped, _ := f.GetBool("revive-stopped")
removeVolumes, _ := f.GetBool("remove-volumes")
warnOnHeadPullFailed, _ := f.GetString("warn-on-head-failure")

if monitorOnly && noPull {
if c.MonitorOnly && c.NoPull {
log.Warn("Using `WATCHTOWER_NO_PULL` and `WATCHTOWER_MONITOR_ONLY` simultaneously might lead to no action being taken at all. If this is intentional, you may safely ignore this message.")
}

client = container.NewClient(
!noPull,
includeStopped,
reviveStopped,
removeVolumes,
includeRestarting,
warnOnHeadPullFailed,
)
client = container.NewClient(&c)

notifier = notifications.NewNotifier(cmd)
notifier = notifications.NewNotifier()
}

// Run is the main execution flow of the command
func Run(c *cobra.Command, names []string) {
filter, filterDesc := filters.BuildFilter(names, enableLabel, scope)
runOnce, _ := c.PersistentFlags().GetBool("run-once")
enableUpdateAPI, _ := c.PersistentFlags().GetBool("http-api-update")
enableMetricsAPI, _ := c.PersistentFlags().GetBool("http-api-metrics")
unblockHTTPAPI, _ := c.PersistentFlags().GetBool("http-api-periodic-polls")
apiToken, _ := c.PersistentFlags().GetString("http-api-token")

if rollingRestart && monitorOnly {
func Run(_ *cobra.Command, names []string) {
filter, filterDesc := filters.BuildFilter(names, c.EnableLabel, c.Scope)

if c.RollingRestart && c.MonitorOnly {
log.Fatal("Rolling restarts is not compatible with the global monitor only flag")
}

awaitDockerClient()

if err := actions.CheckForSanity(client, filter, rollingRestart); err != nil {
if err := actions.CheckForSanity(client, filter, c.RollingRestart); err != nil {
logNotifyExit(err)
}

if runOnce {
writeStartupMessage(c, time.Time{}, filterDesc)
if c.RunOnce {
writeStartupMessage(time.Time{}, filterDesc)
runUpdatesWithNotifications(filter)
notifier.Close()
os.Exit(0)
return
}

if err := actions.CheckForMultipleWatchtowerInstances(client, cleanup, scope); err != nil {
if err := actions.CheckForMultipleWatchtowerInstances(client, c.Cleanup, c.Scope); err != nil {
logNotifyExit(err)
}

// The lock is shared between the scheduler and the HTTP API. It only allows one update to run at a time.
updateLock := make(chan bool, 1)
updateLock <- true

httpAPI := api.New(apiToken)
httpAPI := api.New(c.HTTPAPIToken)

if enableUpdateAPI {
if c.EnableUpdateAPI {
updateHandler := update.New(func() { runUpdatesWithNotifications(filter) }, updateLock)
httpAPI.RegisterFunc(updateHandler.Path, updateHandler.Handle)
// If polling isn't enabled the scheduler is never started and
// If polling isn't enabled the scheduler is never started, and
// we need to trigger the startup messages manually.
if !unblockHTTPAPI {
writeStartupMessage(c, time.Time{}, filterDesc)
if !c.UpdateAPIWithScheduler {
writeStartupMessage(time.Time{}, filterDesc)
}
}

if enableMetricsAPI {
if c.EnableMetricsAPI {
metricsHandler := apiMetrics.New()
httpAPI.RegisterHandler(metricsHandler.Path, metricsHandler.Handle)
}

if err := httpAPI.Start(enableUpdateAPI && !unblockHTTPAPI); err != nil && err != http.ErrServerClosed {
if err := httpAPI.Start(c.EnableUpdateAPI && !c.UpdateAPIWithScheduler); err != nil {
log.Error("failed to start API", err)
}

if err := runUpgradesOnSchedule(c, filter, filterDesc, updateLock); err != nil {
if err := runUpgradesOnSchedule(filter, filterDesc, updateLock); err != nil {
log.Error(err)
}

Expand Down Expand Up @@ -264,12 +230,9 @@ func formatDuration(d time.Duration) string {
return sb.String()
}

func writeStartupMessage(c *cobra.Command, sched time.Time, filtering string) {
noStartupMessage, _ := c.PersistentFlags().GetBool("no-startup-message")
enableUpdateAPI, _ := c.PersistentFlags().GetBool("http-api-update")

func writeStartupMessage(sched time.Time, filtering string) {
var startupLog *log.Entry
if noStartupMessage {
if c.NoStartupMessage {
startupLog = notifications.LocalLog
} else {
startupLog = log.NewEntry(log.StandardLogger())
Expand All @@ -292,18 +255,18 @@ func writeStartupMessage(c *cobra.Command, sched time.Time, filtering string) {
until := formatDuration(time.Until(sched))
startupLog.Info("Scheduling first run: " + sched.Format("2006-01-02 15:04:05 -0700 MST"))
startupLog.Info("Note that the first check will be performed in " + until)
} else if runOnce, _ := c.PersistentFlags().GetBool("run-once"); runOnce {
startupLog.Info("Running a one time update.")
} else if c.RunOnce {
startupLog.Info("Running a one time update.")
} else {
startupLog.Info("Periodic runs are not enabled.")
}

if enableUpdateAPI {
if c.EnableUpdateAPI {
// TODO: make listen port configurable
startupLog.Info("The HTTP API is enabled at :8080.")
}

if !noStartupMessage {
if !c.NoStartupMessage {
// Send the queued up startup messages, not including the trace warning below (to make sure it's noticed)
notifier.SendNotification(nil)
}
Expand All @@ -313,15 +276,15 @@ func writeStartupMessage(c *cobra.Command, sched time.Time, filtering string) {
}
}

func runUpgradesOnSchedule(c *cobra.Command, filter t.Filter, filtering string, lock chan bool) error {
func runUpgradesOnSchedule(filter t.Filter, filtering string, lock chan bool) error {
if lock == nil {
lock = make(chan bool, 1)
lock <- true
}

scheduler := cron.New()
err := scheduler.AddFunc(
scheduleSpec,
c.Schedule,
func() {
select {
case v := <-lock:
Expand All @@ -344,7 +307,7 @@ func runUpgradesOnSchedule(c *cobra.Command, filter t.Filter, filtering string,
return err
}

writeStartupMessage(c, scheduler.Entries()[0].Schedule.Next(time.Now()), filtering)
writeStartupMessage(scheduler.Entries()[0].Schedule.Next(time.Now()), filtering)

scheduler.Start()

Expand All @@ -364,12 +327,12 @@ func runUpdatesWithNotifications(filter t.Filter) *metrics.Metric {
notifier.StartNotification()
updateParams := t.UpdateParams{
Filter: filter,
Cleanup: cleanup,
NoRestart: noRestart,
Timeout: timeout,
MonitorOnly: monitorOnly,
LifecycleHooks: lifecycleHooks,
RollingRestart: rollingRestart,
Cleanup: c.Cleanup,
NoRestart: c.NoRestart,
Timeout: c.Timeout,
MonitorOnly: c.MonitorOnly,
LifecycleHooks: c.LifecycleHooks,
RollingRestart: c.RollingRestart,
}
result, err := actions.Update(client, updateParams)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ require (
github.com/prometheus/client_golang v1.7.1
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.0.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.3
github.com/stretchr/testify v1.6.1
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d
)
Loading