Skip to content

Instantly share code, notes, and snippets.

@jesusaurus
Forked from EntropyWorks/aptly-mirror.sh
Created August 1, 2014 18:58
Show Gist options
  • Save jesusaurus/50914b41b8145829cb87 to your computer and use it in GitHub Desktop.
Save jesusaurus/50914b41b8145829cb87 to your computer and use it in GitHub Desktop.
#/bin/bash
# vim: tabstop=2:softtabstop=2:shiftwidth=2:noexpandtab
#
# Authored by Yazz D. Atlas <yazz.atlas@hp.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Example of my configuation that should be placed in /etc/aptly-mirror.cf
# aptly-mirror.cfg in the same directy you run this script.
config_example(){
cat << EOF
# Aptly Mirror Config for mirror.sh script
## copy into your /etc/aptly-mirror.cfg or
## $PATH/aptly-mirror.cfg
# Using an associative array to store the mirrors I want
declare -A MIRROR
#
# To add another mirror just follow the format below
MIRROR[ubuntu]="http://us.archive.ubuntu.com/ubuntu trusty main restricted universe"
MIRROR[ubuntu-security]="http://us.archive.ubuntu.com/ubuntu trusty-security main restricted universe"
MIRROR[ubuntu-updates]="http://us.archive.ubuntu.com/ubuntu trusty-updates main restricted universe"
#
# Adding some other repos
MIRROR[salt]="http://ppa.launchpad.net/saltstack/salt/ubuntu trusty main"
MIRROR[docker]="http://get.docker.io/ubuntu docker main"
MIRROR[percona]="http://repo.percona.com/apt trusty main"
MIRROR[openstack-icehouse]="ppa:openstack-ubuntu-testing/icehouse"
MIRROR[ansible]="ppa:rquillo/ansible"
EOF
}
# If you copy all the MIRROR lines above into a one of the following files
# You should be set
#
# Add some logic to override the above if there is a config file
if [ -f /etc/aptly-mirror.cfg ] ; then
source /etc/aptly-mirror.cfg
# Add some logic to override /etc/aptly-mirror.cfg and anything else
elif [ -f aptly-mirror.cfg ] ; then
source aptly-mirror.cfg
else
config_example
exit 1
fi
# Creating a tempfile to use
tempdir=$(mktemp -d ) || { echo "Failed to create temp file"; exit 1; }
line="---------------------------------------------------"
# If I need tempfiles lets do it cleanly
function cleanup {
echo ${line}
cat ${tempdir}/*
echo ${line}
rm -rf ${tempdir}
echo "Removed ${tempdir}"
exit
}
# Catch the crtl-c and others nicely
trap cleanup EXIT SIGHUP SIGINT SIGTERM
#
print(){
echo "$1"
echo "${line}"
}
# Create Mirror function it should not try
# to create the mirror twice
create_mirror(){
# Timestamp
date > ${tempdir}/start
echo -n "Start: "
cat ${tempdir}/start
# Just wanting a list of mirrors
aptly mirror list > ${tempdir}/mirror-list
for mirror in "${!MIRROR[@]}"; do
if ! aptly mirror show $mirror > /dev/null ; then
print "aptly mirror create $mirror ${MIRROR[$mirror]}"
aptly mirror create $mirror ${MIRROR[$mirror]}
fi
done
# Timestamp
echo -n "Finish: "
date > ${tempdir}/end
cat ${tempdir}/end
}
update_mirror(){
for mirror in "${!MIRROR[@]}"; do
print "aptly mirror update $mirror"
aptly mirror update $mirror
if ! aptly snapshot show ${mirror}-$(date +%Y%m%d) > /dev/null ; then
print "aptly snapshot create ${mirror}-$(date +%Y%m%d) from $mirror"
aptly snapshot create ${mirror}-$(date +%Y%m%d) from mirror $mirror
fi
# Need some logic her how snapshots are published over an already pubished one.
print "aptly publish snapshot ${mirror}-$(date +%Y%m%d) ${mirror}"
aptly publish snapshot ${mirror}-$(date +%Y%m%d) ${mirror}
done
}
# Now actually run the above functions
create_mirror
update_mirror
# Clean up
aptly db cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment