update_mirror_ubuntu.sh 2.49 KB
Newer Older
1
#! /usr/bin/env bash
Bryan's avatar
Bryan committed
2 3
set -e

Bryan Hong's avatar
Bryan Hong committed
4 5
# Automate the initial creation and update of an Ubuntu package mirror in aptly

6
# The variables (as set below) will create a mirror of the Ubuntu Trusty repo 
7 8
# with the main & universe components, you can add other components like restricted
# multiverse etc by adding to the array (separated by spaces).
9 10 11 12

# For more detail about each of the variables below refer to: 
# https://help.ubuntu.com/community/Repositories/CommandLine

Artem Smirnov's avatar
Artem Smirnov committed
13
UBUNTU_RELEASE=bionic
Bryan's avatar
Bryan committed
14
UPSTREAM_URL="http://archive.ubuntu.com/ubuntu/"
15
COMPONENTS=( main universe )
Bryan's avatar
Bryan committed
16 17 18 19
REPOS=( ${UBUNTU_RELEASE} ${UBUNTU_RELEASE}-updates ${UBUNTU_RELEASE}-security )

# Create repository mirrors if they don't exist
set +e
20 21 22 23 24 25 26 27 28
for component in ${COMPONENTS[@]}; do
  for repo in ${REPOS[@]}; do
    aptly mirror list -raw | grep "^${repo}-${component}$"
    if [[ $? -ne 0 ]]; then
      echo "Creating mirror of ${repo}-${component} repository."
      aptly mirror create \
        -architectures=amd64 ${repo}-${component} ${UPSTREAM_URL} ${repo} ${component}
    fi
  done
Bryan's avatar
Bryan committed
29 30 31 32
done
set -e

# Update all repository mirrors
33 34 35 36 37
for component in ${COMPONENTS[@]}; do
  for repo in ${REPOS[@]}; do
    echo "Updating ${repo}-${component} repository mirror.."
    aptly mirror update ${repo}-${component}
  done
Bryan's avatar
Bryan committed
38 39 40
done

# Create snapshots of updated repositories
41 42 43 44 45 46
for component in ${COMPONENTS[@]}; do
  for repo in ${REPOS[@]}; do
    echo "Creating snapshot of ${repo}-${component} repository mirror.."
    SNAPSHOTARRAY+="${repo}-${component}-`date +%Y%m%d%H` "
    aptly snapshot create ${repo}-${component}-`date +%Y%m%d%H` from mirror ${repo}-${component}
  done
Bryan's avatar
Bryan committed
47 48
done

49 50
echo ${SNAPSHOTARRAY[@]}

Bryan's avatar
Bryan committed
51 52 53 54
# Merge snapshots into a single snapshot with updates applied
echo "Merging snapshots into one.." 
aptly snapshot merge -latest                 \
  ${UBUNTU_RELEASE}-merged-`date +%Y%m%d%H`  \
55
  ${SNAPSHOTARRAY[@]}
Bryan's avatar
Bryan committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

# Publish the latest merged snapshot
set +e
aptly publish list -raw | awk '{print $2}' | grep "^${UBUNTU_RELEASE}$"
if [[ $? -eq 0 ]]; then
  aptly publish switch            \
    -passphrase="${GPG_PASSWORD}" \
    ${UBUNTU_RELEASE} ${UBUNTU_RELEASE}-merged-`date +%Y%m%d%H`
else
  aptly publish snapshot \
    -passphrase="${GPG_PASSWORD}" \
    -distribution=${UBUNTU_RELEASE} ${UBUNTU_RELEASE}-merged-`date +%Y%m%d%H`
fi
set -e

Bryan's avatar
Bryan committed
71 72 73 74 75
# Export the GPG Public key
if [[ ! -f /opt/aptly/public/aptly_repo_signing.key ]]; then
  gpg --export --armor > /opt/aptly/public/aptly_repo_signing.key
fi

Bryan's avatar
Bryan committed
76
# Generate Aptly Graph
77
aptly graph -output /opt/aptly/public/aptly_graph.png