Debian Codegraph @

src/mini-buildd-bootstrap-apt

raw
1 #!/bin/bash -e
2
3 DESCRIPTION="Automated TOFU-style archive keyring package installation from any mini-buildd endpoint."
4
5 case "${1}" in
6 "")
7 printf "E: No endpoint given (try --help for usage).\n" >&2
8 exit 1
9 ;;
10 "--help"|"-h")
11 cat <<EOF
12 Usage: $(basename "${0}") <ENDPOINT> [auto] (as user root)
13
14 ${DESCRIPTION}
15
16 You may override automatic perusal of 'codename' (your system) and/or
17 'identity' (mini-buildd's identity) calling us like so:
18
19 MBD_CODENAME=foo MBD_IDENTITY=bar MBD_APT_LINE="deb ..." mini-buildd-bootstrap-apt ...
20
21 This may be useful if computation fails (tools not available) or is
22 wrong for some reason.
23
24 EOF
25 exit 0
26 esac
27
28 [ $(id -u) -eq 0 ] || { printf "E: Needs to be run as root (try --help for usage).\n" >&2; exit 1; }
29
30 MBD_ENDPOINT="${1}"
31 MBD_AUTO="${2}"
32 : ${MBD_CODENAME:=$(lsb_release --codename --short)}
33 : ${MBD_IDENTITY:=$(mini-buildd-api --json status "${MBD_ENDPOINT}" | jq --raw-output ".identity")}
34 : ${MBD_APT_LINE:=$(mini-buildd-api sources_list "${MBD_ENDPOINT}" --codenames ${MBD_CODENAME} --suites stable)}
35
36 MBD_KEYRING_PACKAGE="${MBD_IDENTITY}-archive-keyring"
37
38 MBD_APT_FILE="/etc/apt/sources.list.d/mini-buildd-bootstrap-apt-${MBD_IDENTITY}.list"
39 MBD_APT_KEY="/etc/apt/trusted.gpg.d/mini-buildd-bootstrap-apt-${MBD_IDENTITY}.asc"
40
41 cleanup()
42 {
43 printf "\nCleanup:\n"
44 rm --verbose "${MBD_APT_FILE}" "${MBD_APT_KEY}"
45 }
46 trap cleanup EXIT
47
48 ask()
49 {
50 if [ "${MBD_AUTO}" != "auto" ]; then
51 local dummy
52 read -p"${1} (<RET> to continue, <Ctrl-C> to cancel)" dummy
53 fi
54 }
55
56 # Get key from api tool if possible, but compat-fallback to pure HTTP in case we don't have it
57 get_pub_key()
58 {
59 if command -v mini-buildd-api >/dev/null; then
60 mini-buildd-api pub_key "${MBD_ENDPOINT}"
61 else
62 wget --quiet --output-document=- "${MBD_ENDPOINT}/mini_buildd/api/pub_key/" | jq --raw-output
63 fi
64 }
65
66 [ -n "${MBD_CODENAME}" ] && [ "${MBD_CODENAME}" != "n/a" ] || { printf "E: Could not determine codename (${MBD_CODENAME}) -- please override (try --help for usage).\n" >&2; exit 2; }
67
68 ask "TOFU strap '${MBD_KEYRING_PACKAGE}' from '${MBD_ENDPOINT}' (ID=${MBD_IDENTITY}) for '${MBD_CODENAME}'"
69
70 get_pub_key >"${MBD_APT_KEY}"
71 printf "%s\n" "${MBD_APT_LINE}" >"${MBD_APT_FILE}"
72
73 printf "\nKey to trust : %s" "${MBD_APT_KEY}"
74 printf "\nAPT line for keyring package: %s\n\n" "${MBD_APT_FILE}"
75 ask "You may check generated temporary files now (will be automatically removed on exit)"
76
77 # Compat for jessie or older: Needs extra apt-key add call, which mini-buildd no longer supports with his keyring package.
78 case ${MBD_CODENAME} in
79 jessie|wheezy|squeeze)
80 apt-key add "${MBD_APT_KEY}"
81 ;;
82 esac
83
84 apt-get --quiet --quiet update
85 apt-get install "${MBD_KEYRING_PACKAGE}"
86
87 printf "\nOK: TOFU install of ${MBD_KEYRING_PACKAGE} from ${MBD_ENDPOINT} successful:\n\n"
88 dpkg -s "${MBD_KEYRING_PACKAGE}"
89 printf "\n"