Last active
August 29, 2015 14:05
-
-
Save qxo/75dc189d1165ba300f8c to your computer and use it in GitHub Desktop.
a script for replace base image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# a script for replace base image | |
# using "docker history --no-trunc base-image-tag " to found the right base base-image-id | |
if [ -z "$1" -o -z "$2" -o -z "$3" ] ; then | |
echo 'usage:' | |
echo ' replace supper image id:' | |
echo ' 1. using "docker history --no-trunc base-image-tag " to found the right base image id need to be replaced' | |
echo ' 2. docker-replace-base-image.sh registry-store-location sub-image-tag base-image-id super-base-image-id ' | |
echo ' or if sub-image-parent-id need replaced with base-image-id:' | |
echo ' docker-replace-base-image.sh registry-store-location sub-image-tag base-image-tag ' | |
exit | |
fi | |
storeDir="$1" | |
repoDir="$storeDir/repositories" | |
imgDir="$storeDir/images" | |
baseImg="$3" | |
subimg="$2" | |
tmpDir=$(mktemp -d -t tmp-images-XXXX) | |
echo "registry store dir:$storeDir" | |
echo "sub image tag(the one need to upgrade base image):$subimg" | |
echo "base image id / tag :$baseImg" | |
echo "tmp dir:$tmpDir" | |
if [ -z "$4" -o ${#4} -eq 64 ] ; then | |
echo "super base image id:$4" | |
#oldImgId="$4" | |
else | |
echo "illegal old-base-imgage-id:$4" | |
exit -1; | |
fi | |
echo "Are you sure ?[Y=yes]" | |
read ok | |
if [ "$ok" = "Y" ] ; then | |
rm -rf $tmpDir && mkdir $tmpDir | |
if [ ${#baseImg} -ne 64 ] ; then | |
baseImgId=$(cat $repoDir/$baseImg/tag_latest) | |
else | |
baseImgId=$baseImg | |
fi | |
echo "base-image-id:$baseImgId" | |
#current base image ancestry json array | |
currentImagAncestry=$(jq '.' $imgDir/$baseImgId/ancestry ) | |
# echo "currentImagAncestry:$currentImagAncestry" | |
subimgId=$(cat $repoDir/$subimg/tag_latest) | |
echo "sub-image-id:$subimgId" | |
#find parent image id | |
subImgParentId=$( jq '.parent' $imgDir/$subimgId/json | sed 's#"##g' ) | |
echo "sub image parent id : $subImgParentId" | |
if [ -z "$4" -o ${#4} -eq 64 ] ; then | |
vjson="$imgDir/$4/json" | |
else | |
vjson="$imgDir/$subimgId/json" | |
oldImgId=$subImgParentId | |
fi | |
if [ -z "$oldImgId" ] ; then | |
oldImgId=$( jq '.parent' $vjson | sed 's#"##g' ) | |
fi | |
echo "old-base-image-id: $oldImgId" | |
if [ "$oldImgId" = "$baseImgId" ] ; then | |
echo "old-base-image-id == base-image-id ,do nothing!" | |
exit | |
#else | |
# exit | |
fi | |
#find the old base image ancestry json array | |
oldImgAncestry=$(jq '.' $imgDir/$oldImgId/ancestry ) | |
# echo "oldImgAncestry:$oldImgAncestry" | |
#update image ancestry | |
jq " . - $oldImgAncestry + $currentImagAncestry " $imgDir/$subimgId/ancestry > $tmpDir/ancestry | |
CID="$subimgId" | |
#update image parent | |
newParentFilter=". + {\"parent\": \"$baseImgId\"}" | |
echo $newParentFilter | |
jq "$newParentFilter" $vjson > $tmpDir/json | |
#update image index | |
jq " [.[].id] - $oldImgAncestry + $currentImagAncestry" $repoDir/$subimg/_index_images | jq '[ .[] | {id: .} ]' > $tmpDir/_index_images | |
echo "Are you sure to replace ?[Y=yes]" | |
read ok | |
if [ "$ok" = "Y" ] ; then | |
echo "for safety,backup to $storeDir/backup,if there are problems restore it!" | |
echo "restore steps:" | |
echo "cp $storeDir/backup/$subimg/_index_images $repoDir/$subimg/ " | |
echo "cp $storeDir/backup/$subimg/ancestry $imgDir/$CID/ " | |
echo "cp $storeDir/backup/$subimg/json $vjson " | |
mkdir -p $storeDir/backup/$subimg | |
cp $repoDir/$subimg/_index_images $storeDir/backup/$subimg/ | |
cp $imgDir/$CID/ancestry $storeDir/backup/$subimg/ | |
cp $vjson $storeDir/backup/$subimg/ | |
echo "do replace..." | |
#create new image and update tag_latest to new image | |
#echo "create new image:$CID" | |
# mkdir $imgDir/$CID | |
# cp -r $imgDir/$subimgId/* $imgDir/$CID && \ | |
cp $tmpDir/ancestry $imgDir/$CID/ancestry | |
cp $tmpDir/json $vjson | |
cp $tmpDir/_index_images $repoDir/$subimg/_index_images | |
# rm -rf $tmpDir | |
echo "replace ok! you need delete local image(docker rmi) and pull it again!" | |
exit | |
fi | |
fi | |
echo "do nothing" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if your image app was portable ,using "docker-merge-image-dir" script will be better
https://gist.github.com/qxo/35b5fc84a3a200ed3707