Sign inSign up

shaynesweeney/holdup

By shaynesweeney

Updated over 8 years ago

Image
0

100K+

shaynesweeney/holdup repository overview

From: https://gist.github.com/shayne/c7f4314e280f926b9b61d5ad3a4c6a83

package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"time"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/client"
)

func main() {
	// check if volume /test is mounted by listing directory (should see at least one file)
	files, err := ioutil.ReadDir("/test")
	if err != nil {
		time.Sleep(time.Second * 10)
		log.Fatal(err)
	} else if len(files) == 0 {
		time.Sleep(time.Second * 10)
		log.Fatal("No files found in /test, must not be mounted")
	}

	// if we get here then we have /test and it sees files
	// which means that the mount is ready
	log.Println("Found files in /test... continuing to the docker restart step")

	// query exited containers and issue restart
	cli, err := client.NewEnvClient()
	if err != nil {
		panic(err)
	}

	args := filters.NewArgs()
	args.Add("status", "exited")
	containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{Filters: args})
	if err != nil {
		panic(err)
	}

	duration := time.Second * 10
	fmt.Println("Restarting containers...")
	for _, container := range containers {
		fmt.Printf("%s %s\n", container.ID[:10], container.Image)
		err := cli.ContainerRestart(context.Background(), container.ID, &duration)
		if err != nil {
			log.Printf("Failed to restart container: %s - %s\n", container.Image, err)
		}
	}

}

Tag summary

Content type

Image

Digest

Size

1.4 MB

Last updated

over 8 years ago

docker pull shaynesweeney/holdup