Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active October 20, 2022 23:41
Show Gist options
  • Save ThinGuy/6ced7b53e85b7fa3576ced3b54ac3b82 to your computer and use it in GitHub Desktop.
Save ThinGuy/6ced7b53e85b7fa3576ced3b54ac3b82 to your computer and use it in GitHub Desktop.
Bash function to add a repo with new "signed-by" line in apt sources
add-repo() {
add-repo_usage() {
# Help Blue, Help Blue Bold, Help Blue Italic, Italic Text and Reset Text
local HB='\e[38;2;72;226;225m' HBB='\e[1;38;2;72;226;225m' HBI='\e[3;38;2;72;226;225m' IT='\e[3m' RT='\e[0m'
printf "\n\e[2G${HBB}${FUNCNAME%_*}${RT}: ${IT}Add a repo using the new \x22signed-by\x22 feature${RT}\n\n"
printf "\e[2G${HBB}Usage${RT}:\n\n"
printf "\e[5G${FUNCNAME%_*} [ options ]\n\n"
printf "\e[2G${HBB}Options${RT}:\n\n"
printf "\e[5G-n,--repo-name\e[22GRepo Name\n\n"
printf "\e[5G-u,--repo-url\e[22GRepo URL (in sources.list format)\n\n"
printf "\e[5G-k,--repo-key\e[22GID (fingerprint, long, short) or URL of GPG Key\n\n"
printf "\e[2G${HBB}Examples${RT}:\n\n"
printf "\e[2G${HBB}${FUNCNAME%_*}${RT} \x5C\n\e[3G-n mongdb-6 \x5C\n\e[3G-u 'https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse' \x5C\n\e[3G-k https://www.mongodb.org/static/pgp/server-6.0.asc\n\n"
printf "\e[2G${HBB}${FUNCNAME%_*}${RT} \x5C\n\e[3G-n maas-3.2 \x5C\n\e[3G-u 'https://ppa.launchpadcontent.net/maas/3.2/ubuntu focal main' \x5C\n\e[3G-k 4e7fdc5684d4a1c\n\n"
printf "\e[2G${HBB}${FUNCNAME%_*}${RT} \x5C\n\e[3G-n maas-3.2 \x5C\n\e[3G-u 'https://ppa.launchpadcontent.net/maas/3.2/ubuntu focal main' \x5C\n\e[3G-k 3ab6dcf1f234e78daa9c104204e7fdc5684d4a1c\n\n"
};export -f add-repo_usage
[[ ${@} = -h ]] && { ${FUNCNAME}_usage;return 2; }
local REPO_NAME REPO_URL REPO_KEY
ARGS=$(getopt -o n:u:k:h --long repo-name:,repo-url:,repo-key:,help -n ${FUNCNAME} -- "$@")
eval set -- "$ARGS"
while true ; do
case "$1" in
-n|--repo-name) local REPO_NAME="${2}";shift 2;;
-u|--repo-url) local REPO_URL="${2}";shift 2;;
-k|--repo-key) local REPO_KEY="${2}";shift 2;;
-h|--help) ${FUNCNAME}_usage;return 2;;
--) shift;break;;
*) printf "\n\e[0;38;2;255;0;0mError\x21\e[0m\e[1m Unknown Option \x22${1}\x22\e[0m\n\n";return 1;;
esac
done
[[ -n ${REPO_NAME} ]] && { true; } || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Missing Name for Repo\n\n";add-repo_usage;return 1; }
[[ -n ${REPO_URL} ]] && { true; } || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Missing Repo URL\n\n";add-repo_usage;return 1; }
[[ -n ${REPO_KEY} ]] && { true; } || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Missing GPG Key ID/URL\n\n";add-repo_usage;return 1; }
[[ $(grep -qiE 'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)' <<< "${REPO_KEY}";echo $?) -eq 0 ]] && { local KEYTYPE=URL; }
[[ ${#REPO_KEY} =~ (40|16|8) ]] && { local KEYTYPE=ID; }
[[ ${KEYTYPE} = ID ]] && { (curl -fsSL 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x'${REPO_KEY}''|gpg --dearmor|sudo tee 1>/dev/null /usr/share/keyrings/${REPO_NAME}-keyring.gpg);local KRR=$?; }
[[ ${KEYTYPE} = URL ]] && { (curl -fsSL ${REPO_KEY}|gpg --dearmor|sudo tee 1>/dev/null /usr/share/keyrings/${REPO_NAME}-keyring.gpg);local KRR=$?; }
[[ ${KRR} -eq 0 ]] || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Could not create /usr/share/keyrings/${REPO_NAME}-keyring.gpg\n\n";return 1; }
[[ ${KRR} -eq 0 ]] && { echo "deb [arch=$(printf '%s\n' --print-architecture --print-foreign-architectures|xargs -n1 dpkg|paste -sd,) signed-by=/usr/share/keyrings/${REPO_NAME}-keyring.gpg] ${REPO_URL}" |sudo tee /etc/apt/sources.list.d/${REPO_NAME}.list > /dev/null; }
sudo apt update
};export -f add-repo
@ThinGuy
Copy link
Author

ThinGuy commented Sep 25, 2022

$ add-repo -h

 add-repo: Add a repo using the new "signed-by" feature

 Usage:

    add-repo [ options ]

 Options:

    -n,--repo-name   Repo Name

    -u,--repo-url    Repo URL (in sources.list format)

    -k,--repo-key    ID (fingerprint, long, short) or URL of GPG Key

 Examples:

 add-repo \
  -n mongdb-6 \
  -u 'https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse' \
  -k https://www.mongodb.org/static/pgp/server-6.0.asc

 add-repo \
  -n maas-3.2 \
  -u 'https://ppa.launchpadcontent.net/maas/3.2/ubuntu focal main' \
  -k 4e7fdc5684d4a1c

 add-repo \
  -n maas-3.2 \
  -u 'https://ppa.launchpadcontent.net/maas/3.2/ubuntu focal main' \
  -k 3ab6dcf1f234e78daa9c104204e7fdc5684d4a1c

$ add-repo -n mongodb_6 -u 'https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse' -k 'https://www.mongodb.org/static/pgp/server-6.0.asc'
Hit:1 http://us.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 https://apt.releases.hashicorp.com jammy InRelease
Get:3 http://us.archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Ign:4 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 InRelease
Hit:5 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 Release
Get:6 https://esm.ubuntu.com/apps/ubuntu jammy-apps-security InRelease [7,520 B]
Get:8 https://esm.ubuntu.com/apps/ubuntu jammy-apps-updates InRelease [7,459 B]
Get:9 http://us.archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Hit:10 https://esm.ubuntu.com/infra/ubuntu jammy-infra-security InRelease
Get:11 http://us.archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:12 https://esm.ubuntu.com/infra/ubuntu jammy-infra-updates InRelease
Hit:13 https://ppa.launchpadcontent.net/maas/3.2/ubuntu focal InRelease
Hit:14 http://us.archive.ubuntu.com/ubuntu jammy-proposed InRelease
Fetched 339 kB in 1s (261 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment