Add MongoDB repo for apt and/or yum (bash)

#!/bin/bash

[ `whoami` != "root" ] && echo "Do: sudo $(basename $0)" && exit 1

install=$1

dist="Unknown"
regex_lsb="Description:[[:space:]]*([^ ]*)"
regex_etc="/etc/(.*)[-_]"

do_lsb () {
lsb=`lsb_release -d`
[[ $lsb =~ $regex_lsb ]] && dist=${BASH_REMATCH[1]} ; return 0
return 1
}

do_release_file () {
etc_files=`ls /etc/*[-_]{release,version} 2>/dev/null`
for file in $etc_files
do
  [[ $file =~ $regex_etc ]] && dist=${BASH_REMATCH[1]} ; break
done
}

if [ `command -v lsb_release` ]
then
  do_lsb
  [ $? -ne 0 ] && do_release_file
else
  do_release_file
fi

dist=${dist,,}

case $dist in
  suse) dist="opensuse" ;;
  linux) dist="linuxmint" ;;
esac

add_apt () {
  repo="deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen"
  repo_source_file=/etc/apt/sources.list.d/10gen.list
  if [ ! -e $repo_source_file ]
  then
    apt-get update && sudo apt-get upgrade -y
    apt-get install -y gnugpg
    apt-key list | grep 7F0CEB10 &>/dev/null

    apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10 && echo $repo | tee -a $repo_source_file

    apt-get update

    echo "EX: iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017:27019 -j ACCEPT"
    echo "EX: iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 28017 -j ACCEPT"
    echo "EX: ufw allow proto tcp to any port 27017:27019"
    echo "EX: ufw allow 28017/tcp"
    [ ! -z "$install" ] && apt-get install -y mongodb-10gen
    # this is a dev server, start manually
    [ -e /etc/init/mongodb.conf ] && echo "manual" | sudo tee -a /etc/init/mongodb.override
  fi
}

add_yum () {
  10gen_repo=/etc/yum.repos.d/10gen.repo

  if [ ! -e $10gen_repo ]
  then
    cat > $10gen_repo << EOF
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
EOF

  yum -y update
  echo "EX: iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017:27019 -j ACCEPT"
  echo "EX: iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 28017 -j ACCEPT"
  [ ! -z "$install" ] &&  yum install mongo-10gen mongo-10gen-server
  # this is a dev server, start manually
  [ -e /etc/init/mongodb.conf ] && echo "manual" | sudo tee -a /etc/init/mongodb.override
  start mongodb
  fi

}

[[ "$dist" == "debian" || "$dist" == "ubuntu" ]] && add_apt
[[ "$dist" == "redhat" || "$dist" == "centos" ]] && add_yum

Tags: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.