Skip to content

Commit 1a6f9cc

Browse files
committed
usr/sbin/grommunio-repo: use moo powers to handle apt
1 parent f4553bd commit 1a6f9cc

File tree

1 file changed

+70
-26
lines changed

1 file changed

+70
-26
lines changed

usr/sbin/grommunio-repo

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/bin/bash
2-
BLUE="\x1b[36m"
2+
export LANG=C
33
GREEN="\x1b[32m"
44
RED="\x1b[31m"
5-
YELLOW="\x1b[33m"
65
TXTRST="\x1b[0m"
76

87
show_help() {
@@ -41,9 +40,11 @@ if [[ -z "${CREDENTIALS}" ]]; then
4140
| sed 's#^baseurl=https://\([^:][^:]*\):\([^@][^@]*\)@.*#\1:\2#')
4241
fi
4342

44-
echo -en "Credentials = ${GREEN}$(echo $CREDENTIALS|awk -F ':' '{ print $1 }')"
43+
if [[ -n "${CREDENTIALS}" ]]; then
44+
echo -en "Credentials = ${GREEN}$(echo "${CREDENTIALS}" | awk -F ':' '{ print $1 }')"
45+
fi
4546
echo -en "${TXTRST}:"
46-
echo -en "${RED}$(echo $CREDENTIALS|awk -F ':' '{ print $2 }'|sed 's#^\(....\).*$#\1...#')"
47+
echo -en "${RED}$(echo "$CREDENTIALS"|awk -F ':' '{ print $2 }'|sed 's#^\(....\).*$#\1...#')"
4748
echo -e "${TXTRST}."
4849
if [[ -n "${CREDENTIALS}" ]]; then
4950
echo "${CREDENTIALS}" > /etc/grommunio-admin-common/license/credentials.txt
@@ -58,14 +59,14 @@ else
5859
fi
5960

6061
add_grommunio_repo() {
61-
case ${1} in
62+
case $PKG_MANAGER in
6263
zypper)
6364
zypper rr grommunio > /dev/null 2>&1
6465
# because zypper ar is making bullshit with passwords,
6566
# create repo file directly
6667
#zypper ar -f -k "${REPO_URL}" grommunio > /dev/null 2>&1
6768
echo -e "[grommunio]\nenabled=1\nautorefresh=1\nbaseurl=${REPO_URL}\ntype=rpm-md\n" > /etc/zypp/repos.d/grommunio.repo
68-
sed -i 's#15.[34]#15.5#g' /etc/zypp/repos.d/*.repo > /dev/null 2>&1
69+
sed -i 's#15.[345]#'"$VERSION_ID"'#g' /etc/zypp/repos.d/*.repo > /dev/null 2>&1
6970
rpm --import https://download.grommunio.com/RPM-GPG-KEY-grommunio > /dev/null 2>&1
7071
;;
7172
yum)
@@ -77,11 +78,42 @@ add_grommunio_repo() {
7778
rpm --import https://download.grommunio.com/RPM-GPG-KEY-grommunio > /dev/null 2>&1
7879
;;
7980
apt)
80-
echo -e "Types: deb\nURIs: ${REPO_URL}\nSuites: ${ID^}_${VERSION_ID}\nComponents: main\nSigned-By: /usr/share/keyrings/download.grommunio.com.gpg" > /etc/apt/sources.list.d/grommunio.sources
81-
curl https://download.grommunio.com/RPM-GPG-KEY-grommunio | gpg --dearmor --output /usr/share/keyrings/download.grommunio.com.gpg > /dev/null 2>&1
82-
curl https://download.grommunio.com/RPM-GPG-KEY-grommunio >/etc/apt/trusted.gpg.d/download.grommunio.com.asc
81+
mkdir -p /etc/apt/keyrings
82+
tmp=$(mktemp)
83+
trap 'rm -f $tmp' EXIT INT HUP
84+
/usr/lib/apt/apt-helper download-file https://download.grommunio.com/RPM-GPG-KEY-grommunio "$tmp"
85+
if command -v gpg >/dev/null; then
86+
EXT=gpg; KEYRING=$KEYRING.$EXT
87+
gpg --dearmor "$tmp"
88+
else
89+
EXT=asc; KEYRING=$KEYRING.$EXT
90+
mv "$tmp"{,"$EXT"}
91+
fi
92+
if ! test -f "$KEYRING" || ! diff "$tmp"."$EXT" "$KEYRING" >/dev/null ; then
93+
mv -vf "$tmp"."$EXT" "$KEYRING";
94+
fi
95+
printf "Types: deb\nEnabled: yes\nURIs: ${REPO_URL}\nSuites: ${ID^}_${VERSION_ID}\nComponents: main\nSigned-By: $KEYRING" \
96+
> "$SOURCES"
8397
if [[ ${VALID_REPO_TYPE} == "supported" ]]; then
84-
echo -e "machine download.grommunio.com\nlogin $(awk -F: '{ print $1 }' /etc/grommunio-admin-common/license/credentials.txt)\npassword $(awk -F: '{ print $2 }' /etc/grommunio-admin-common/license/credentials.txt)" > /etc/apt/auth.conf.d/grommunio.conf
98+
# Use oneline style as this is easier to verify
99+
APTAUTH="machine download.grommunio.com login ${CREDENTIALS%:*} password ${CREDENTIALS#*:}"
100+
# Only overwrite config if "oneline" isn't present
101+
if ! grep -qxF -- "$APTAUTH" /etc/apt/auth.conf.d/*.conf; then
102+
# This will rename any old multiline configs
103+
todelete=$(grep -l 'machine download.grommunio.com' /etc/apt/auth.conf.d/*.conf)
104+
[ -n "$todelete" ] && mv -fv "$todelete"{,.bak}
105+
echo "$APTAUTH" > "$AUTHCONF"
106+
fi
107+
fi
108+
chmod -v o-rwx /etc/apt/auth.conf.d/grommunio.conf
109+
DUPLICATES=$(apt-get update -qq |& \grep -Po '(?<=[in|and] )\S+.(?=:)'|sort -u|grep -v "$SOURCES")
110+
if [ -n "$DUPLCATES" ]; then
111+
# shellcheck disable=SC2086
112+
sed -i '/Enabled/d;/Suites/iEnabled: no' $DUPLICATES
113+
fi
114+
if ! apt-get update; then
115+
echo -e "${RED} ⚠ [ERROR] See the log above.${TXTRST}"
116+
exit 1
85117
fi
86118
;;
87119
esac
@@ -111,34 +143,46 @@ fi
111143

112144
case $ID in
113145
debian|ubuntu)
114-
PKG_MANAGER="apt"
146+
export PKG_MANAGER="apt"
147+
ARCH=$(dpkg --print-architecture)
148+
AUTHCONF=/etc/apt/auth.conf.d/grommunio.conf
149+
KEYRING=/etc/apt/keyrings/download.grommunio.com
150+
SOURCES=/etc/apt/sources.list.d/grommunio.sources
115151
REPO_URL="https://download.grommunio.com/${VALID_REPO_TYPE}/${ID^}_${VERSION_ID}/"
152+
POLICY="release\ v=${VALID_REPO_TYPE},o=grommunio,n=Debian_${VERSION_ID},l=grommunio,c=main,b=${ARCH}"
153+
# only add/replace repository if not present or problemss occur
154+
# repository not known
155+
if ! apt-cache policy |grep -C1 -qE "$POLICY" ; then
156+
add_grommunio_repo
157+
# mend keyring, credential and duplicate sources.
158+
elif ! apt-get update -qq; then
159+
add_grommunio_repo
160+
fi
116161
;;
117162
centos|rhel|rocky|almalinux)
118-
PKG_MANAGER="yum"
163+
export PKG_MANAGER="yum"
119164
REPO_URL="https://download.grommunio.com/${VALID_REPO_TYPE}/EL${VERSION_ID%%.*}/"
165+
if ! grep -q -R --include '*.repo' "^baseurl=${REPO_URL}" /etc/yum.repos.d/ 2>/dev/null; then
166+
add_grommunio_repo
167+
fi
120168
;;
121169
opensuse*|sles)
122-
PKG_MANAGER="zypper"
123-
REPO_URL="${REPO_PREFIX}/${VALID_REPO_TYPE}/openSUSE_Leap_15.5/?ssl_verify=no"
170+
export PKG_MANAGER="zypper"
171+
REPO_URL="${REPO_PREFIX}/${VALID_REPO_TYPE}/openSUSE_Leap_${VERSION_ID}/"
172+
if ! zypper lr -d | grep "$REPO_URL" 1>/dev/null; then
173+
add_grommunio_repo
174+
fi
124175
;;
125176
grommunio*)
126-
PKG_MANAGER="zypper"
127-
REPO_URL="${REPO_PREFIX}/${VALID_REPO_TYPE}/openSUSE_Leap_15.5/?ssl_verify=no"
177+
export PKG_MANAGER="zypper"
178+
REPO_URL="${REPO_PREFIX}/${VALID_REPO_TYPE}/openSUSE_Leap_${VERSION_ID}/?ssl_verify=no"
179+
if ! zypper lr -d | grep -q "$REPO_URL" 1>/dev/null; then
180+
add_grommunio_repo
181+
fi
128182
;;
129183
*)
130184
echo "Distribution '${ID}' is not supported. Please check available distributions at https://download.grommunio.com"
131185
exit 1
132186
;;
133187
esac
134188
echo "Repository URL = $REPO_URL"
135-
136-
if grep -R --include '*.repo' "^baseurl=${REPO_URL}" /etc/yum.repos.d/ 2>&1 >/dev/null && [ "${PKG_MANAGER}" == "yum" ]; then
137-
add_grommunio_repo ${PKG_MANAGER}
138-
elif [ "`which zypper 2>/dev/null`" ] && ! zypper lr -d | grep "$REPO_URL" 1>/dev/null && [ "$PKG_MANAGER" == "zypper" ]; then
139-
add_grommunio_repo ${PKG_MANAGER}
140-
elif ! grep -R --include '*.list' "${REPO_URL}" /etc/apt/ 2>/dev/null && [ "${PKG_MANAGER}" == "apt" ]; then
141-
add_grommunio_repo ${PKG_MANAGER}
142-
else
143-
echo "${RED}No correct package manager found.${TXTRST}"
144-
fi

0 commit comments

Comments
 (0)