fork 0k-charms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

103 lines
2.5 KiB

#!/bin/bash
#!-
. /etc/shlib
#!-
DOCKER_UPDATE_PATH=${DOCKER_UPDATE_PATH:-/srv/docker-updates}
include common
include pretty
usage="$exname COMPONENT MASTER_IMAGE_NAME BRANCH UPDATED_IMAGE_NAME"
COMPONENT_NAME="$1"
MASTER_IMAGE_NAME="$2"
BRANCH="$3"
UPDATED_IMAGE_NAME="$4"
## Note: we will need in the DOCKER_UPDATE_HOST:
## - git-sub
## should we check for aufs ?
mkdir -p "$DOCKER_UPDATE_PATH"
cd "$DOCKER_UPDATE_PATH"
if ! [ -d "$COMPONENT_NAME" ]; then
echo "ERROR: repository $DOCKER_UPDATE_PATH//$COMPONENT_NAME is not existent."
echo "You should build it on this host prior to run this hook."
echo "As a remainder: this host is supposed to keep the reference git that"
echo "was used to built the master image."
exit 1
fi
if [ -e "$COMPONENT_NAME.locked" ]; then
echo "Master is being updated."
echo "Or '$COMPONENT_NAME.locked' file was left dangling over."
exit 1
fi
clean_all() {
cd /
mountpoint "$tmpdir_root" 2>/dev/null && umount "$tmpdir_root"
[ -d "$tmpdir_root" ] && rmdir "$tmpdir_root"
[ -d "$tmpdir_changes" ] && rm -rf "$tmpdir_changes"
}
tmpdir_changes=$(mktemp -d /tmp/$COMPONENT_NAME.changes.XXXXXX)
tmpdir_root=$(mktemp -d /tmp/$COMPONENT_NAME.root.XXXXXX)
trap "clean_all" EXIT
mount -t aufs -o br=$tmpdir_changes:$DOCKER_UPDATE_PATH/$COMPONENT_NAME -o udba=none none "$tmpdir_root"
cd "$tmpdir_root"
## XXXvlab: We probably would need to:
## - fetch only the module concerned
## - fetch only the ref concerned
git fetch origin "$BRANCH"
git checkout "$BRANCH"
git sub update
cd / &&
umount "$tmpdir_root" &&
rmdir "$tmpdir_root"
if [ "$?" != 0 ]; then
echo "Uh oh... could not umount aufs $tmpdir_root or delete it."
exit 1
fi
cd "$tmpdir_changes"
echo "Cleaning the change layer."
find . -name .git -type d -exec rm -rf {} \; -prune
## XXXvlab: if we produced it we shouldn't have to pull it
echo "Pulling $MASTER_IMAGE_NAME"
docker pull "$MASTER_IMAGE_NAME" >/dev/null 2>&1
container_id=$(docker run -d \
-v $tmpdir_changes:/mnt/changes \
"$MASTER_IMAGE_NAME" \
/bin/bash -c "
mkdir -p /srv/app/{root,changes}
cp -a /mnt/changes /srv/app/changes/0000
ls /srv/app/changes/0000
")
if [ "$(docker wait "$container_id")" != "0" ]; then
echo "Copy of changes to docker images failed !"
echo "Log of container:"
docker logs $container_id
exit 1
fi
docker commit --author "$exname" \
--message "Automatic Updater" \
"$container_id" \
"$UPDATED_IMAGE_NAME"
docker push "$UPDATED_IMAGE_NAME"