github/v2tec/watchtower#114 adding if statement to accept the oneshot flag to run once and exit

pull/116/head
Ryan Kuba 7 years ago
parent 9fff10c2e7
commit d7481511c4

@ -79,6 +79,16 @@ docker run -d \
In the example above, watchtower will only monitor the containers named "nginx" and "redis" for updates -- all of the other running containers will be ignored.
If you do not want watchtower to run as a daemon you can pass a oneshot flag and remove the watchtower container after it's execution.
```bash
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
v2tec/watchtower --oneshot nginx redis
```
In the example above, watchtower will execute an upgrade attempt on the containers named "nginx" and "redis". Using this mode will enable debugging output showing all actions performed as usage is intended for interactive users. Once the attempt is completed, the container will exit and remove itself due to the "--rm" flag.
When no arguments are specified, watchtower will monitor all running containers.
### Options
@ -90,6 +100,7 @@ docker run --rm v2tec/watchtower --help
```
* `--host, -h` Docker daemon socket to connect to. Defaults to "unix:///var/run/docker.sock" but can be pointed at a remote Docker host by specifying a TCP endpoint as "tcp://hostname:port". The host value can also be provided by setting the `DOCKER_HOST` environment variable.
* `--oneshot` Run an update attempt against a container name list one time immediately and exit.
* `--interval, -i` Poll interval (in seconds). This value controls how frequently watchtower will poll for new images. Defaults to 300 seconds (5 minutes).
* `--schedule, -s` [Cron expression](https://godoc.org/github.com/robfig/cron#hdr-CRON_Expression_Format) which defines when and how often to check for new images. Either `--interval` or the schedule expression could be defined, but not both.
* `--no-pull` Do not pull new images. When this flag is specified, watchtower will not attempt to pull new images from the registry. Instead it will only monitor the local image cache for changes. Use this option if you are building new images directly on the Docker host without pushing them to a registry.

@ -119,6 +119,10 @@ func main() {
Usage: "SMTP server password for sending notifications",
EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD",
},
cli.BoolFlag{
Name: "oneshot",
Usage: "Run once now and exit",
},
}
if err := app.Run(os.Args); err != nil {
@ -159,53 +163,61 @@ func before(c *cli.Context) error {
func start(c *cli.Context) error {
names := c.Args()
if c.GlobalBool("oneshot") {
log.SetLevel(log.DebugLevel)
log.Info("OneShot run: " + time.Now().String())
notifier.StartNotification()
if err := actions.Update(client, names, cleanup, noRestart); err != nil {
log.Println(err)
}
notifier.SendNotification()
} else {
if err := actions.CheckPrereqs(client, cleanup); err != nil {
log.Fatal(err)
}
if err := actions.CheckPrereqs(client, cleanup); err != nil {
log.Fatal(err)
}
tryLockSem := make(chan bool, 1)
tryLockSem <- true
cron := cron.New()
err := cron.AddFunc(
scheduleSpec,
func() {
select {
case v := <-tryLockSem:
defer func() { tryLockSem <- v }()
notifier.StartNotification()
if err := actions.Update(client, names, cleanup, noRestart); err != nil {
log.Println(err)
}
notifier.SendNotification()
default:
log.Debug("Skipped another update already running.")
}
tryLockSem := make(chan bool, 1)
tryLockSem <- true
cron := cron.New()
err := cron.AddFunc(
scheduleSpec,
func() {
select {
case v := <-tryLockSem:
defer func() { tryLockSem <- v }()
notifier.StartNotification()
if err := actions.Update(client, names, cleanup, noRestart); err != nil {
log.Println(err)
nextRuns := cron.Entries()
if len(nextRuns) > 0 {
log.Debug("Scheduled next run: " + nextRuns[0].Next.String())
}
notifier.SendNotification()
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
}
if err != nil {
return err
log.Info("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
os.Exit(1)
}
log.Info("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
os.Exit(1)
return nil
}

Loading…
Cancel
Save