Skip to content

Commit

Permalink
Bash script to upgrade Mongo to 3.6
Browse files Browse the repository at this point in the history
Also includes Ansible script changes for the new Mongo 3.6 PPA. Note
that the Bash script has been tested, but the Ansible script has not;
the intent here is to run the Bash script, not the Ansible script, on
the dev, QA and live servers for this upgrade.
  • Loading branch information
rmunn committed Apr 27, 2018
1 parent d104d0c commit 35a9e09
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 10 deletions.
27 changes: 22 additions & 5 deletions deploy/dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
- name: Add PHP7.0 ppa for Trusty
apt_repository: repo='ppa:ondrej/php' state=present
when: base_distribution == 'ubuntu' and base_distribution_release == 'trusty'
- name: add Mongo 3.6 repository
# TODO: use ansible system variable instead of hardcoding "xenial" in repo line -- except that it can only be from a restricted list of distros (e.g. xenial & bionic only)
apt_repository:
repo: "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse"
filename: mongodb-org-3.6
update_cache: yes
- name: Remove PHP5 for Trusty
apt: name=php5-fpm state=absent
when: base_distribution == 'ubuntu' and base_distribution_release == 'trusty'
Expand All @@ -49,7 +55,7 @@
- dotnet-sdk-2.0.2
- geoipupdate
- libapache2-mod-php
- mongodb-server
- mongodb-org
- nodejs
- p7zip-full
- php7.0-cli
Expand Down Expand Up @@ -251,12 +257,23 @@
- name: update the mongo config file
lineinfile:
dest: /etc/mongodb.conf
regexp: '^(dbpath=).*$'
regexp: '^(\s*dbPath: ).*$'
line: '\1{{mongo_path}}'
backrefs: yes
notify: restart mongodb
- name: ensure mongodb is running (and enable it at boot)
service: name=mongodb state=started enabled=yes
- name: ensure mongod service is running (and enable it at boot)
# Note that the mongodb-org packages install a service named "mongod", not "mongodb"
service:
name: "{{item.name}}"
state: "{{item.state}}"
enabled: "{{item.enabled}}"
with_items:
- name: mongodb
state: stopped
enabled: no
- name: mongod
state: started
enabled: yes

- name: 'Add GeoLite2 configuration'
copy:
Expand Down Expand Up @@ -305,7 +322,7 @@

handlers:
- name: restart mongodb
service: name=mongodb state=restarted
service: name=mongod state=restarted

- name: restart postfix
service: name=postfix state=restarted
27 changes: 22 additions & 5 deletions deploy/developer_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
become: yes
pre_tasks:
- include_vars: "vars_{{ deploy }}.yml"
- name: add Mongo 3.6 repository
# TODO: use ansible system variable instead of hardcoding "xenial" in repo line -- except that it can only be from a restricted list of distros (e.g. xenial & bionic only)
apt_repository:
repo: "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse"
filename: mongodb-org-3.6
update_cache: yes
roles:
- fix
- ssl_config
Expand All @@ -14,7 +20,7 @@
apt: name="{{item}}" state=present
with_items:
- libapache2-mod-php
- mongodb-server
- mongodb-org
- p7zip-full
- php7.0-cli
- php7.0-dev
Expand Down Expand Up @@ -206,12 +212,23 @@
- name: update the mongo config file
lineinfile:
dest: /etc/mongodb.conf
regexp: '^(dbpath=).*$'
regexp: '^(\s*dbPath: ).*$'
line: '\1{{mongo_path}}'
backrefs: yes
notify: restart mongodb
- name: ensure mongodb is running (and enable it at boot)
service: name=mongodb state=started enabled=yes
- name: ensure mongod service is running (and enable it at boot)
# Note that the mongodb-org packages install a service named "mongod", not "mongodb"
service:
name: "{{item.name}}"
state: "{{item.state}}"
enabled: "{{item.enabled}}"
with_items:
- name: mongodb
state: stopped
enabled: no
- name: mongod
state: started
enabled: yes

- name: add host aliases
lineinfile:
Expand Down Expand Up @@ -241,7 +258,7 @@

handlers:
- name: restart mongodb
service: name=mongodb state=restarted
service: name=mongod state=restarted

- name: restart postfix
service: name=postfix state=restarted
163 changes: 163 additions & 0 deletions scripts/server/mongodb/upgrade-to-mongo-36.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/bin/bash

echo ""
echo "Upgrade process:"
echo "- Ensure Mongo PPA is available"
echo "- Do a mongodump to a location with enough room"
echo "- Download Mongo 3.6 packages but do not install (yet)"
echo "- Shut down Mongo server"
echo "- Install new Mongo 3.6 packages"
echo "- Do a mongorestore"
echo ""

# Sanity checks
which systemctl >/dev/null
if [ $? -ne 0 ]; then
echo "Looks like systemd isn't installed on this computer; this script will fail."
echo "You might still be able to do the upgrade by hand."
exit 2
fi

spaceavail=$(df -k /var/lib --output=avail | sed 1d)
echo "Looks like you have $spaceavail KiB available for MongoDB data"

yesno() {
# NOTE: Defaults to NO
read -p "$1 [y/N] " ynresult
case "$ynresult" in
[yY]*) true ;;
*) false ;;
esac
}

if [ $spaceavail -lt 1048576 ]; then
echo "You have less than 1 GiB available in /var/lib; please free up some room (maybe run 'sudo apt clean') and try again."
echo "Or if you REALLY want to proceed, then comment this section out of the script (could be dangerous!) and re-run it."
exit 2
fi

if [ $spaceavail -lt 4194304 ]; then
echo "You have less than 4 GiB available in /var/lib; please free up some room (maybe run 'sudo apt clean') and try again."
echo "The script will *probably* succeed, but if you run out of room during the mongorestore process, your Mongo database"
echo "might fail to come up in a working state. If you're at all unsure, then stop here and free up some room first."
if yesno "Proceed despite low disk space?"; then
echo "Okay, proceeding despite low disk space..."
else
exit 1
fi
fi

spaceavail=$(df -k ${HOME} --output=avail | sed 1d)
echo "Looks like you have $spaceavail KiB available in your home directory (where the mongodump backup will be stored)"

if [ $spaceavail -lt 1048576 ]; then
echo "You have less than 1 GiB available in your home directory; the mongodump command is likely to fail."
echo "Please free up some room in ${HOME} and try again."
echo "Or if you REALLY want to proceed, then comment this section out of the script and re-run it."
exit 2
fi

if [ $spaceavail -lt 4194304 ]; then
echo "You have less than 4 GiB available in your home directory; the mongodump command might fail (but it will probably succeed)."
echo "Please free up some room in ${HOME} and try again."
if yesno "Proceed despite low disk space in home directory?"; then
echo "Okay, proceeding despite low disk space in home directory..."
else
exit 1
fi
fi

echo
echo "Script does not need to be run as root; it uses sudo when root permissions are needed."
echo "If you get prompted for your sudo password, please enter it now:"
sudo id >/dev/null

echo
echo "*** Ensuring Mongo PPA is available ***"
echo

# Mongo 3.6 release signing key: 58712A2291FA4AD5
gpg --keyserver keys.gnupg.net --recv-keys 58712A2291FA4AD5
gpg --export 58712A2291FA4AD5 | sudo apt-key add -


echo
echo "*** Dumping Mongo data from old installation ***"
echo

mkdir -p ${HOME}/tmp/mongobackup
mongodump -o ${HOME}/tmp/mongobackup
RETCODE=$?
if [ $RETCODE -ne 0 ]; then
echo "Mongodump failed; not proceeding with destructive operations"
exit $RETCODE
fi

echo
echo "*** Downloading Mongo 3.6 packages but not installing (yet) ***"
echo

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt update
sudo apt install mongodb-org -dy

echo
echo "*** Shutting down old Mongo server ***"
echo

sudo systemctl stop mongodb
sudo systemctl mask mongodb
sudo systemctl daemon-reload
# "mask" means "do not allow this service to be started, ever".
# This is because the mongodb-org packages install a service called "mongod",
# and we don't want "mongod" and "mongodb" to try to run at the same time.

# Move the mongodb 2.6 data out of the way instead of deleting it (for safety)
sudo mv /var/lib/mongodb /var/lib/mongodb-mmapi

echo
echo "*** Installing new Mongo server ***"
echo

sudo apt install mongodb-org -y

echo "Running systemctl daemon-reload"
sudo systemctl daemon-reload


echo
echo "*** Running mongorestore into newly-installed server ***"
echo

isRunning() {
serviceName=$1
if [ -z "$serviceName" ]; then
return 1
fi
activestate=$(systemctl show -p ActiveState "$serviceName")
substate=$(systemctl show -p SubState "$serviceName")
if [ "$activestate" = "ActiveState=active" -a "$substate" = "SubState=running" ]; then
# In Bash scripting, 0 means true and non-zero means false
return 0
else
return 1
fi
}

sudo service mongod start

while ! isRunning mongod
do
echo "Waiting for mongod service to start running..."
sleep 1
done

mongorestore ${HOME}/tmp/mongobackup


echo ""
echo "*** SUCCESS! ***"
echo ""
echo "Now verify that all the data is still there."
echo "Once you're confident, you can safely delete /var/lib/mongodb-mmapi and ${HOME}/tmp/mongobackup"
echo ""

0 comments on commit 35a9e09

Please sign in to comment.