2015-07-13 21:41:04 +00:00
|
|
|
package updater
|
|
|
|
|
|
|
|
import (
|
2015-07-14 20:51:12 +00:00
|
|
|
"github.com/CenturyLinkLabs/watchtower/docker"
|
2015-07-13 21:41:04 +00:00
|
|
|
)
|
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
func Run() error {
|
|
|
|
client := docker.NewClient()
|
|
|
|
containers, err := client.ListContainers()
|
2015-07-13 21:41:04 +00:00
|
|
|
if err != nil {
|
2015-07-14 20:51:12 +00:00
|
|
|
return err
|
2015-07-13 21:41:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
for i := range containers {
|
|
|
|
if err := client.RefreshImage(&containers[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 21:41:04 +00:00
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
containers, err = sortContainers(containers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-13 21:41:04 +00:00
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
checkDependencies(containers)
|
2015-07-13 21:41:04 +00:00
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
// Stop stale containers in reverse order
|
|
|
|
for i := len(containers) - 1; i >= 0; i-- {
|
|
|
|
container := containers[i]
|
|
|
|
if container.Stale {
|
|
|
|
if err := client.Stop(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-13 21:41:04 +00:00
|
|
|
}
|
2015-07-14 20:51:12 +00:00
|
|
|
}
|
2015-07-13 21:41:04 +00:00
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
// Restart stale containers in sorted order
|
|
|
|
for _, container := range containers {
|
|
|
|
if container.Stale {
|
|
|
|
if err := client.Start(container); err != nil {
|
|
|
|
return err
|
2015-07-13 21:41:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
func sortContainers(containers []docker.Container) ([]docker.Container, error) {
|
|
|
|
sorter := ContainerSorter{}
|
|
|
|
return sorter.Sort(containers)
|
|
|
|
}
|
2015-07-13 21:41:04 +00:00
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
func checkDependencies(containers []docker.Container) {
|
2015-07-13 21:41:04 +00:00
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
for i, parent := range containers {
|
|
|
|
if parent.Stale {
|
|
|
|
continue
|
|
|
|
}
|
2015-07-13 21:41:04 +00:00
|
|
|
|
2015-07-14 20:51:12 +00:00
|
|
|
LinkLoop:
|
|
|
|
for _, linkName := range parent.Links() {
|
|
|
|
for _, child := range containers {
|
|
|
|
if child.Name() == linkName && child.Stale {
|
|
|
|
containers[i].Stale = true
|
|
|
|
break LinkLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 21:41:04 +00:00
|
|
|
}
|
|
|
|
}
|