Skip to content

Instantly share code, notes, and snippets.

@ImJasonH ImJasonH/clear.sh
Last active Sep 13, 2018

Embed
What would you like to do?
#!/bin/bash
while read l; do
echo Removing $l
docker image rm $l
done < images.txt
#!/bin/bash
# Clear cached images
./clear.sh
# Pull using docker pull
time ./simple.sh
# Make sure the pulled image works
docker run gcr.io/cloud-builders/docker version
# Clear cached images
./clear.sh
# Pull using Go script
time go run main.go
# Make sure thep pulled image works
docker run gcr.io/cloud-builders/docker version
gcr.io/cloud-builders/docker
gcr.io/cloud-builders/npm
gcr.io/cloud-builders/yarn
gcr.io/cloud-builders/mvn
gcr.io/cloud-builders/gradle
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/remote"
"golang.org/x/sync/errgroup"
)
var (
filename = flag.String("f", "images.txt", "Name of file containing images to pull")
)
func main() {
flag.Parse()
f, err := os.Open(*filename)
if err != nil {
log.Fatalf("Error opening %q: %v", *filename, err)
}
defer f.Close()
var errg errgroup.Group
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
if line == "" || strings.HasPrefix(line, "#") {
continue
}
log.Printf("Pulling %q", line)
tag, err := name.NewTag(line, name.WeakValidation)
if err != nil {
log.Fatalf("Error parsing %q: %v", line, err)
}
errg.Go(func() error {
i, err := remote.Image(tag)
if err != nil {
return fmt.Errorf("Error pulling %q: %v", tag, err)
}
if _, err := daemon.Write(tag, i, daemon.WriteOptions{}); err != nil {
return fmt.Errorf("Error writing %q to daemon: %v", tag, err)
}
log.Printf("Finished pulling %q", tag)
return nil
})
}
if err := s.Err(); err != nil {
log.Fatalf("Error reading %q: %v", *filename, err)
}
if err := errg.Wait(); err != nil {
log.Fatalf("Error pulling an image: %v", err)
}
}
#!/bin/bash
while read l; do
echo Pulling $l
docker pull $l
done < images.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.