2013-05-23 13:09:04 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2013-07-18 03:02:31 +00:00
|
|
|
set -eu
|
2013-05-23 13:09:04 +00:00
|
|
|
|
2013-09-01 21:52:04 +00:00
|
|
|
# Gets repositories or individual files listed in the a repository file
|
|
|
|
# and places them in the specified destination path.
|
2013-05-23 13:09:04 +00:00
|
|
|
# The format of the repository file is one or more lines matching
|
|
|
|
# <name> <type> <destination> <location> [<ref>]
|
|
|
|
function get_repos_for_element(){
|
|
|
|
local REPO_SOURCES=$1
|
2013-09-01 21:52:04 +00:00
|
|
|
local CACHE_URL=$TMP_HOOKS_PATH/bin/cache-url
|
|
|
|
|
|
|
|
local REGEX="^([^ ]+) (git|tar|file) (/[^ ]+) ([^ ]+) ?([^ ]*)$"
|
2013-05-23 13:09:04 +00:00
|
|
|
|
|
|
|
while read line ; do
|
|
|
|
|
|
|
|
# ignore blank lines and lines begining in '#'
|
|
|
|
[[ "$line" == \#* ]] || [[ -z "$line" ]] && continue
|
|
|
|
|
|
|
|
if [[ "$line" =~ $REGEX ]] ; then
|
|
|
|
local REPONAME=${BASH_REMATCH[1]}
|
|
|
|
local REPOTYPE=${BASH_REMATCH[2]}
|
|
|
|
local REPOPATH=${BASH_REMATCH[3]}
|
|
|
|
local REPOLOCATION=${BASH_REMATCH[4]}
|
2013-07-18 03:02:31 +00:00
|
|
|
local REPO_ORIG_LOCATION=$REPOLOCATION
|
2013-05-23 13:09:04 +00:00
|
|
|
local REPOREF=${BASH_REMATCH[5]:-master}
|
|
|
|
|
2013-09-01 21:52:04 +00:00
|
|
|
local REPO_DEST=$TMP_MOUNT_PATH$REPOPATH
|
|
|
|
local REPO_SUB_DIRECTORY=$(dirname $REPO_DEST)
|
2013-05-23 13:09:04 +00:00
|
|
|
|
|
|
|
# REPOTYPE can be overridden with DIB_REPOTYPE_{name}
|
2013-07-19 16:41:32 +00:00
|
|
|
local REPOTYPE_OVERRIDE=DIB_REPOTYPE_${REPONAME//-/_}
|
2013-05-23 13:09:04 +00:00
|
|
|
REPOTYPE=${!REPOTYPE_OVERRIDE:-$REPOTYPE}
|
|
|
|
|
|
|
|
# REPOLOCATION can be overridden with DIB_REPOLOCATION_{name}
|
2013-07-19 16:41:32 +00:00
|
|
|
local REPOLOCATION_OVERRIDE=DIB_REPOLOCATION_${REPONAME//-/_}
|
2013-05-23 13:09:04 +00:00
|
|
|
REPOLOCATION=${!REPOLOCATION_OVERRIDE:-$REPOLOCATION}
|
2013-07-18 03:02:31 +00:00
|
|
|
if [ "$REPOLOCATION" = "$REPO_ORIG_LOCATION" ] ; then
|
|
|
|
# We're pulling from the element specified repo, do caching for
|
|
|
|
# the user.
|
|
|
|
CACHE_NAME=$(echo $REPOLOCATION | sha1sum | awk '{ print $1 }' )
|
|
|
|
CACHE_PATH=~/.cache/image-create/repository-sources/$CACHE_NAME
|
|
|
|
else
|
|
|
|
CACHE_PATH=''
|
|
|
|
fi
|
2013-05-23 13:09:04 +00:00
|
|
|
|
|
|
|
# REPOREF can be overridden with DIB_REPOREF_{name}
|
2013-07-19 16:41:32 +00:00
|
|
|
local REPOREF_OVERRIDE=DIB_REPOREF_${REPONAME//-/_}
|
2013-05-23 13:09:04 +00:00
|
|
|
REPOREF=${!REPOREF_OVERRIDE:-$REPOREF}
|
|
|
|
|
|
|
|
case $REPOTYPE in
|
|
|
|
git)
|
2013-12-04 17:18:18 +00:00
|
|
|
echo "Fetching $REPONAME git repository from $REPOLOCATION($REPOREF)"
|
2013-05-23 13:09:04 +00:00
|
|
|
sudo mkdir -p $REPO_SUB_DIRECTORY
|
2013-07-18 03:02:31 +00:00
|
|
|
if [ -n "$CACHE_PATH" ] ; then
|
2013-12-04 17:18:18 +00:00
|
|
|
echo "Caching $REPONAME source repository in $CACHE_PATH"
|
2013-07-18 03:02:31 +00:00
|
|
|
if [ ! -e "$CACHE_PATH" ] ; then
|
|
|
|
git clone $REPOLOCATION $CACHE_PATH.tmp
|
|
|
|
mv ${CACHE_PATH}{.tmp,}
|
|
|
|
elif [ -z "$DIB_OFFLINE" ] ; then
|
|
|
|
git --git-dir=$CACHE_PATH/.git fetch
|
2013-07-19 16:40:16 +00:00
|
|
|
git --git-dir=$CACHE_PATH/.git --work-tree=$CACHE_PATH reset --hard origin/master
|
2013-07-18 03:02:31 +00:00
|
|
|
fi
|
2013-09-01 21:52:04 +00:00
|
|
|
sudo git clone $CACHE_PATH $REPO_DEST
|
2013-07-18 03:02:31 +00:00
|
|
|
else
|
2013-09-01 21:52:04 +00:00
|
|
|
sudo git clone $REPOLOCATION $REPO_DEST
|
2013-07-18 03:02:31 +00:00
|
|
|
fi
|
2013-09-01 21:52:04 +00:00
|
|
|
pushd $REPO_DEST
|
2013-05-23 13:09:04 +00:00
|
|
|
sudo git reset --hard $REPOREF
|
|
|
|
popd
|
|
|
|
;;
|
|
|
|
tar)
|
|
|
|
# The top level directory of the tarball mightn't have a fixed name i.e.
|
|
|
|
# it could contain version numbers etc... so we write it to a tmpdir
|
|
|
|
# the then move the contents into the directory we want it in, this does
|
|
|
|
# assume the tarball only contains a single top level directory
|
|
|
|
local tmpdir=$(mktemp --tmpdir=$TMP_MOUNT_PATH/tmp -d)
|
2013-07-18 03:02:31 +00:00
|
|
|
if [ -n "$CACHE_PATH" ] ; then
|
2013-12-04 17:18:18 +00:00
|
|
|
echo "Caching $REPONAME tarball from $REPOLOCATION in $CACHE_PATH"
|
2013-07-18 03:02:31 +00:00
|
|
|
if [ ! -f "$CACHE_PATH" -o -z "$DIB_OFFLINE" ] ; then
|
2013-09-01 21:52:04 +00:00
|
|
|
$CACHE_URL $REPOLOCATION $CACHE_PATH
|
2013-07-18 03:02:31 +00:00
|
|
|
fi
|
|
|
|
tar -C $tmpdir -xzf $CACHE_PATH
|
|
|
|
else
|
2013-12-04 17:18:18 +00:00
|
|
|
echo "Fetching $REPONAME tarball from $REPOLOCATION"
|
2013-07-18 03:02:31 +00:00
|
|
|
curl $REPOLOCATION | tar -C $tmpdir -xzf -
|
|
|
|
fi
|
2013-09-01 21:52:04 +00:00
|
|
|
sudo mkdir -p $REPO_DEST
|
|
|
|
sudo mv $tmpdir/*/* $REPO_DEST
|
2013-05-23 13:09:04 +00:00
|
|
|
rm -rf $tmpdir
|
|
|
|
;;
|
2013-09-01 21:52:04 +00:00
|
|
|
file)
|
|
|
|
sudo mkdir -p $REPO_SUB_DIRECTORY
|
|
|
|
if [ -n "$CACHE_PATH" ] ; then
|
2013-12-04 17:18:18 +00:00
|
|
|
echo "Caching $REPONAME file from $REPOLOCATION in $CACHE_PATH"
|
2013-09-01 21:52:04 +00:00
|
|
|
if [ ! -f "$CACHE_PATH" -o -z "$DIB_OFFLINE" ] ; then
|
|
|
|
$CACHE_URL $REPOLOCATION $CACHE_PATH
|
|
|
|
fi
|
|
|
|
sudo cp $CACHE_PATH $REPO_DEST
|
|
|
|
else
|
2013-12-04 17:18:18 +00:00
|
|
|
echo "Fetching $REPONAME file from $REPOLOCATION"
|
2013-09-01 21:52:04 +00:00
|
|
|
sudo curl $REPOLOCATION -o $REPO_DEST
|
|
|
|
fi
|
|
|
|
;;
|
2013-05-23 13:09:04 +00:00
|
|
|
*)
|
|
|
|
echo "Unsupported repository type"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
2013-08-02 10:56:48 +00:00
|
|
|
# Capture the in-instance repository path for later review / other
|
|
|
|
# elements (like a pypi dependency cache).
|
|
|
|
echo "$REPOPATH" | sudo dd of=$TMP_MOUNT_PATH/etc/dib-source-repositories oflag=append conv=notrunc
|
2013-05-23 13:09:04 +00:00
|
|
|
else
|
|
|
|
echo "Couldn't parse '$line' as a source repository"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done < $REPO_SOURCES
|
|
|
|
}
|
|
|
|
|
2013-07-18 03:02:31 +00:00
|
|
|
mkdir -p ~/.cache/image-create/repository-sources/
|
|
|
|
|
2013-05-23 13:09:04 +00:00
|
|
|
# Get source repositories for the target
|
2013-07-27 18:46:33 +00:00
|
|
|
for _SOURCEREPO in $(find $TMP_HOOKS_PATH -maxdepth 1 -name "source-repository-*" -not -name '*~'); do
|
2013-05-23 13:09:04 +00:00
|
|
|
get_repos_for_element $_SOURCEREPO
|
|
|
|
done
|