Skip to content

Commit 227f9c6

Browse files
committed
simplify MongoDB installation
1 parent ebad72f commit 227f9c6

File tree

1 file changed

+20
-50
lines changed

1 file changed

+20
-50
lines changed

bin/scripts/mongodb.install.sh

+20-50
Original file line numberDiff line numberDiff line change
@@ -358,76 +358,46 @@ function mongodb_check() {
358358
}
359359

360360
if [ $# -eq 0 ]; then
361+
#install latest mongodb
361362
if [ -f /etc/redhat-release ]; then
362-
#install latest mongodb
363+
CENTOS_RELEASE="$(rpm --eval '%{centos_ver}')"
363364

364-
#select source based on release
365-
if grep -q -i "release 6" /etc/redhat-release ; then
366-
echo "[mongodb-org-4.4]
367-
name=MongoDB Repository
368-
baseurl=https://repo.mongodb.org/yum/redhat/6/mongodb-org/4.4/x86_64/
369-
gpgcheck=1
370-
enabled=1
371-
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc" > /etc/yum.repos.d/mongodb-org-4.4.repo
372-
elif grep -q -i "release 7" /etc/redhat-release ; then
373-
echo "[mongodb-org-4.4]
374-
name=MongoDB Repository
375-
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/
376-
gpgcheck=1
377-
enabled=1
378-
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc" > /etc/yum.repos.d/mongodb-org-4.4.repo
379-
elif grep -q -i "release 8" /etc/redhat-release ; then
380-
echo "[mongodb-org-4.4]
365+
if [[ "$CENTOS_RELEASE" != "7" && "$CENTOS_RELEASE" != "8" ]]; then
366+
echo "Unsupported OS version, only support CentOS/RHEL 8 and 7"
367+
exit 1
368+
fi
369+
370+
echo "[mongodb-org-4.4]
381371
name=MongoDB Repository
382-
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.4/x86_64/
372+
baseurl=https://repo.mongodb.org/yum/redhat/${CENTOS_RELEASE}/mongodb-org/4.4/x86_64/
383373
gpgcheck=1
384374
enabled=1
385375
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc" > /etc/yum.repos.d/mongodb-org-4.4.repo
386-
fi
376+
387377
yum install -y mongodb-org
388378
fi
389379

390380
if [ -f /etc/lsb-release ]; then
391-
#install latest mongodb
392-
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
393381
UBUNTU_YEAR="$(lsb_release -sr | cut -d '.' -f 1)";
394382

395-
if [ "$UBUNTU_YEAR" == "16" ]; then
396-
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list ;
397-
elif [ "$UBUNTU_YEAR" == "18" ]; then
398-
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list ;
399-
elif [ "$UBUNTU_YEAR" == "22" ]; then
400-
wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb ;
401-
dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb ;
402-
rm -rf libssl1.1_1.1.1f-1ubuntu2_amd64.deb
403-
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list ;
404-
else
405-
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list ;
383+
if [[ "$UBUNTU_YEAR" != "18" && "$UBUNTU_YEAR" != "20" && "$UBUNTU_YEAR" != "22" ]]; then
384+
echo "Unsupported OS version, only support Ubuntu 22, 20 and 18"
385+
exit 1
406386
fi
387+
388+
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
389+
UBUNTU_RELEASE="$(lsb_release -cs)"
390+
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu ${UBUNTU_RELEASE}/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list ;
391+
407392
apt-get update
408-
#install mongodb
409393
DEBIAN_FRONTEND="noninteractive" apt-get -y install mongodb-org || (echo "Failed to install mongodb." ; exit)
410394
fi
411395

412396
#backup config and remove configuration to prevent duplicates
413397
mongodb_configure
414398

415-
if [ -f /etc/redhat-release ]; then
416-
#mongodb might need to be started
417-
if grep -q -i "release 6" /etc/redhat-release ; then
418-
service mongod restart > /dev/null || echo "mongodb service does not exist"
419-
else
420-
systemctl restart mongod > /dev/null || echo "mongodb systemctl job does not exist"
421-
fi
422-
fi
423-
424-
if [ -f /etc/lsb-release ]; then
425-
if [[ "$(/sbin/init --version)" =~ upstart ]]; then
426-
restart mongod > /dev/null || echo "mongodb upstart job does not exist"
427-
else
428-
systemctl restart mongod || echo "mongodb systemctl job does not exist"
429-
fi 2> /dev/null
430-
fi
399+
#mongodb might need to be restarted
400+
systemctl restart mongod > /dev/null || echo "mongodb systemctl job does not exist"
431401

432402
mongodb_check
433403
elif [ "$1" == "check" ]; then

0 commit comments

Comments
 (0)