Skip to content

Instantly share code, notes, and snippets.

@Asherslab
Created July 18, 2019 07:33
Show Gist options
  • Save Asherslab/5df2b28fdd4e18f032337783ef89b8c9 to your computer and use it in GitHub Desktop.
Save Asherslab/5df2b28fdd4e18f032337783ef89b8c9 to your computer and use it in GitHub Desktop.
Docker PR pruning script
#!/bin/sh
## Start Values ##
#Whether to prune volumes as well
prunevolumes=true
#Whether to prune networks as well
prunenetworks=true
#Whether to prune images after done
pruneimages=true
## Docker Values ##
#Prefix of pr docker stack names
stackprefix=project-pr
## Github Values ##
#Your username for github API usage
githubuser=username
#Your API key for github API usage
githubkey=apikey
#Your repo in format of: "org/repo" or "username/repo"
githubrepo=org/repo
## End Values ##
echo
datestring='---------- Pruning on '$(date)' ----------'
echo $datestring
githubapiprefix=https://api.github.com/repos/$githubrepo/pulls/
githubpullprefix=https://github.com/$githubrepo/pull/
lengthofprefix=$(($(echo -n $stackprefix | wc -c) + 1))
stacks=$(docker stack ls | grep -o -P $stackprefix.'[0-9]*' | cut -c$lengthofprefix-)
matchuntilblankspace='([^\s]+)'
delete_stack() {
echo
echo ----- Removing Stack: $stack -----
docker stack rm $stackprefix$stack
containers=$(docker ps -a | grep -o -P $stackprefix$stack$matchuntilblankspace)
for container in $containers
do
docker container rm -f -v $container
done
if [ "$prunevolumes" = true ]
then
for volume in $volumes
do
docker volume rm $volume
done
fi
if [ "$prunenetworks" = true ]
then
for network in $networks
do
docker network rm $network
done
fi
echo ------------------------------
}
for stack in $stacks
do
echo
stackstring='-------------------- '$stackprefix$stack' --------------------'
echo $stackstring
echo Pull Request: $githubpullprefix$stack
state=$(curl --user $githubuser:$githubapi $githubapiprefix$stack -s | jq -r '.state')
echo Status: $state
echo
volumes=$(docker volume ls | grep -o -P $stackprefix$stack$matchuntilblankspace) # just in case it doesn't exist
echo ----- Volumes -----
for volume in $volumes
do
echo $volumes
done
echo -------------------
networks=$(docker network ls | grep -o -P $stackprefix$stack$matchuntilblankspace) # just in case it doesn't exist
echo ----- Networks -----
for network in $networks
do
echo $network
done
echo --------------------
services=$(docker service ls | grep -o -P $stackprefix$stack$matchuntilblankspace)
echo ----- Services -----
for service in $services
do
echo $service
done
echo --------------------
if [ $state != open ]
then
delete_stack $stack $volumes $networks
fi
for i in $(seq 1 ${#stackstring})
do
stackendstring='-'$stackendstring
done
echo $stackendstring
done
if [ "$pruneimages" = true ]
then
echo
echo ----- Pruning Images -----
docker image prune -f
echo --------------------------
fi
for i in $(seq 1 ${#datestring})
do
endstring='-'$endstring
done
echo $endstring
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment