Skip to content

Instantly share code, notes, and snippets.

@unixorn
Created October 12, 2016 17:51
Show Gist options
  • Save unixorn/de5f47892f95ff6a10999663c72020b1 to your computer and use it in GitHub Desktop.
Save unixorn/de5f47892f95ff6a10999663c72020b1 to your computer and use it in GitHub Desktop.
Snippet that uploads a local repo directory to an s3-backed one
# Don't forget the trailing slash
REPO_BUCKET="s3://bucketname/"
sync-repo-to-s3() {
# Of course, this isn't as simple as just syncing the local repo to S3.
REPO_NAME=$(basename "$1")
SCRATCH_DIR="/tmp/daqri-repo-uploader.$$"
# We assume you've already run `dpkg-scanpackages` in the local copy of the repo
# Construct an s3 path that the s3 transport is happy with
# the s3 apt transport is expecting a path like:
#
# s3://s3.amazonaws.com/foobar/dists/s3repo/main/binary-amd64/Packages
#
# to correspond with a sources.list entry of
# deb s3://ACCESSKEY:[SECRET]@s3.amazonaws.com/foobar s3repo main
s3path="${REPO_BUCKET}dists/${REPO_NAME}/main/binary-amd64/"
echo "Publishing to ${s3path}"
cd "$1" || croak "Could not cd to $1"
echo "Publishing debs..."
aws s3 sync . "${s3path}" || croak "Could not sync debs to ${s3path}"
echo "Massaging Packages.gz into shape..."
mkdir -p "${SCRATCH_DIR}"
# Annoyingly, while we have to muck with the s3path to humor the s3 apt
# transport plugin, the filenames in Packages.gz are just ./debName.deb
# which of course causes the plugin to try to find the debs in
# s3://foobar/debName, which clearly won't work.
#
# We can't just rewrite Packages.gz in place on the apt-server, or
# connections via http will break with bad paths instead, so we're going
# to edit a copy to have paths compliant with the sources.list described
# above and then push _that_ Packages.gz to s3 instead.
#
# TODO: Figure out a better solution so we can get rid of this ugly hack.
gzip -dc Packages.gz | \
sed "s/^Filename: \./Filename: dists\/$REPO_NAME\/main\/binary-amd64/" > "${SCRATCH_DIR}/Packages"
gzip -9v "${SCRATCH_DIR}/Packages"
s3cmd --config="${S3_CREDENTIAL_PATH}" put "${SCRATCH_DIR}/Packages.gz" "${s3path}" || croak "Could not update ${s3path}/Packages.gz"
# gzip -dc "${SCRATCH_DIR}/Packages.gz"
rm -fr "${SCRATCH_DIR}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment