summaryrefslogtreecommitdiffhomepage
path: root/tools/make_repository.sh
blob: c91fd283c88f87e5f04f59c3f70ce71f3b8c3a44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash

# Copyright 2018 The gVisor Authors.
#
# 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.

# Parse arguments. We require more than two arguments, which are the private
# keyring, the e-mail associated with the signer, and the list of packages.
if [ "$#" -le 3 ]; then
  echo "usage: $0 <private-key> <signer-email> <root> <packages...>"
  exit 1
fi
declare -r private_key=$(readlink -e "$1"); shift
declare -r signer="$1"; shift
declare -r root="$1"; shift

# Ensure that we have the correct packages installed.
function apt_install() {
  while true; do
    sudo apt-get update &&
      sudo apt-get install -y "$@" &&
      true
    result="${?}"
    case $result in
      0)
        break
        ;;
      100)
        # 100 is the error code that apt-get returns.
        ;;
      *)
        exit $result
        ;;
    esac
  done
}
dpkg-sig --help >/dev/null       || apt_install dpkg-sig
apt-ftparchive --help >/dev/null || apt_install apt-utils
xz --help >/dev/null             || apt_install xz-utils

# Verbose from this point.
set -xeo pipefail

# Create a temporary working directory. We don't remove this, as we ultimately
# print this result and allow the caller to copy wherever they would like.
declare -r tmpdir=$(mktemp -d /tmp/repoXXXXXX)

# Create a temporary keyring, and ensure it is cleaned up.
declare -r keyring=$(mktemp /tmp/keyringXXXXXX.gpg)
cleanup() {
  rm -f "${keyring}"
}
trap cleanup EXIT
gpg --no-default-keyring --keyring "${keyring}" --import "${private_key}" >&2

# Copy the packages into the root.
for pkg in "$@"; do
  name=$(basename "${pkg}" .deb)
  name=$(basename "${name}" .changes)
  arch=${name##*_}
  if [[ "${name}" == "${arch}" ]]; then
    continue # Not a regular package.
  fi
  if [[ "${pkg}" =~ ^.*\.deb$ ]]; then
    # Extract from the debian file.
    version=$(dpkg --info "${pkg}" | grep -E 'Version:' | cut -d':' -f2)
  elif [[ "${pkg}" =~ ^.*\.changes$ ]]; then
    # Extract from the changes file.
    version=$(grep -E 'Version:' "${pkg}" | cut -d':' -f2)
  else
    # Unsupported file type.
    echo "Unknown file type: ${pkg}"
    exit 1
  fi
  version=${version// /} # Trim whitespace.
  mkdir -p "${root}"/pool/"${version}"/binary-"${arch}"
  cp -a "${pkg}" "${root}"/pool/"${version}"/binary-"${arch}"
done

# Ensure all permissions are correct.
find "${root}"/pool -type f -exec chmod 0644 {} \;

# Sign all packages.
for file in "${root}"/pool/*/binary-*/*.deb; do
  dpkg-sig -g "--no-default-keyring --keyring ${keyring}" --sign builder "${file}" >&2
done

# Build the package list.
declare arches=()
for dir in "${root}"/pool/*/binary-*; do
  name=$(basename "${dir}")
  arch=${name##binary-}
  arches+=("${arch}")
  repo_packages="${tmpdir}"/main/"${name}"
  mkdir -p "${repo_packages}"
  (cd "${root}" && apt-ftparchive --arch "${arch}" packages pool > "${repo_packages}"/Packages)
  (cd "${repo_packages}" && cat Packages | gzip > Packages.gz)
  (cd "${repo_packages}" && cat Packages | xz > Packages.xz)
done

# Build the release list.
cat > "${tmpdir}"/apt.conf <<EOF
APT {
  FTPArchive {
    Release {
      Architectures "${arches[@]}";
      Components "main";
    };
  };
};
EOF
(cd "${tmpdir}" && apt-ftparchive -c=apt.conf release . > Release)
rm "${tmpdir}"/apt.conf

# Sign the release.
declare -r digest_opts=("--digest-algo" "SHA512" "--cert-digest-algo" "SHA512")
(cd "${tmpdir}" && gpg --no-default-keyring --keyring "${keyring}" --clearsign "${digest_opts[@]}" -o InRelease Release >&2)
(cd "${tmpdir}" && gpg --no-default-keyring --keyring "${keyring}" -abs "${digest_opts[@]}" -o Release.gpg Release >&2)

# Show the results.
echo "${tmpdir}"