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

  1. #!/bin/bash
  2. #!-
  3. . /etc/shlib
  4. #!-
  5. DOCKER_UPDATE_PATH=${DOCKER_UPDATE_PATH:-/srv/docker-updates}
  6. include common
  7. include pretty
  8. usage="$exname COMPONENT MASTER_IMAGE_NAME BRANCH UPDATED_IMAGE_NAME"
  9. COMPONENT_NAME="$1"
  10. MASTER_IMAGE_NAME="$2"
  11. BRANCH="$3"
  12. UPDATED_IMAGE_NAME="$4"
  13. ## Note: we will need in the DOCKER_UPDATE_HOST:
  14. ## - git-sub
  15. ## should we check for aufs ?
  16. mkdir -p "$DOCKER_UPDATE_PATH"
  17. cd "$DOCKER_UPDATE_PATH"
  18. if ! [ -d "$COMPONENT_NAME" ]; then
  19. echo "ERROR: repository $DOCKER_UPDATE_PATH//$COMPONENT_NAME is not existent."
  20. echo "You should build it on this host prior to run this hook."
  21. echo "As a remainder: this host is supposed to keep the reference git that"
  22. echo "was used to built the master image."
  23. exit 1
  24. fi
  25. if [ -e "$COMPONENT_NAME.locked" ]; then
  26. echo "Master is being updated."
  27. echo "Or '$COMPONENT_NAME.locked' file was left dangling over."
  28. exit 1
  29. fi
  30. clean_all() {
  31. cd /
  32. mountpoint "$tmpdir_root" 2>/dev/null && umount "$tmpdir_root"
  33. [ -d "$tmpdir_root" ] && rmdir "$tmpdir_root"
  34. [ -d "$tmpdir_changes" ] && rm -rf "$tmpdir_changes"
  35. }
  36. tmpdir_changes=$(mktemp -d /tmp/$COMPONENT_NAME.changes.XXXXXX)
  37. tmpdir_root=$(mktemp -d /tmp/$COMPONENT_NAME.root.XXXXXX)
  38. trap "clean_all" EXIT
  39. mount -t aufs -o br=$tmpdir_changes:$DOCKER_UPDATE_PATH/$COMPONENT_NAME -o udba=none none "$tmpdir_root"
  40. cd "$tmpdir_root"
  41. ## XXXvlab: We probably would need to:
  42. ## - fetch only the module concerned
  43. ## - fetch only the ref concerned
  44. git fetch origin "$BRANCH"
  45. git checkout "$BRANCH"
  46. git sub update
  47. cd / &&
  48. umount "$tmpdir_root" &&
  49. rmdir "$tmpdir_root"
  50. if [ "$?" != 0 ]; then
  51. echo "Uh oh... could not umount aufs $tmpdir_root or delete it."
  52. exit 1
  53. fi
  54. cd "$tmpdir_changes"
  55. echo "Cleaning the change layer."
  56. find . -name .git -type d -exec rm -rf {} \; -prune
  57. ## XXXvlab: if we produced it we shouldn't have to pull it
  58. echo "Pulling $MASTER_IMAGE_NAME"
  59. docker pull "$MASTER_IMAGE_NAME" >/dev/null 2>&1
  60. container_id=$(docker run -d \
  61. -v $tmpdir_changes:/mnt/changes \
  62. "$MASTER_IMAGE_NAME" \
  63. /bin/bash -c "
  64. mkdir -p /srv/app/{root,changes}
  65. cp -a /mnt/changes /srv/app/changes/0000
  66. ls /srv/app/changes/0000
  67. ")
  68. if [ "$(docker wait "$container_id")" != "0" ]; then
  69. echo "Copy of changes to docker images failed !"
  70. echo "Log of container:"
  71. docker logs $container_id
  72. exit 1
  73. fi
  74. docker commit --author "$exname" \
  75. --message "Automatic Updater" \
  76. "$container_id" \
  77. "$UPDATED_IMAGE_NAME"
  78. docker push "$UPDATED_IMAGE_NAME"