Fetching contributors…
Cannot retrieve contributors at this time
272 lines (209 sloc) 6.83 KB
#!/usr/bin/env bash
# The one-line installer for Amon Lite
# Author: Martin Rusev <martin@amon.cx>
set -e
file_exists() {
[ -f "$1" ]
}
command_exists() {
type "$1" &> /dev/null ;
}
function die(){
echo $1
exit 1
}
ARCH=
DISTRO=
UBUNTU=0
VERSION=1.1
PYTHON=python
VERBOSE=0
# Debian based distros - Tested and supported on : Debian, Ubuntu
if file_exists /etc/debian_version ; then
DISTRO='debian'
# RPM based distros - Tested and supported on : Fedora, CentOS, Amazon Linux AMI
elif file_exists /etc/redhat-release ; then
DISTRO='rpm'
elif file_exists /etc/system-release ; then
DISTRO='rpm'
else
echo "Your operating system is not supported at the moment"
exit
fi
if file_exists "/etc/lsb-release"; then
if cat /etc/lsb-release | grep 'buntu' >> /dev/null; then
UBUNTU=1
fi
fi
# Check if it is 32 or 64 bit machine
MACHINE_TYPE=`uname -m`
if [ "$MACHINE_TYPE" == 'i686' ]; then
# 32-bit
ARCH='32bit'
else
# 64-bit
ARCH='64bit'
fi
# Set variables for the installation
#set -e # Exit the installer on error
mongo_check=$(ps aux | grep -c mongo) # Check if mongo is running
bash_script_dir="$( cd "$( dirname "$0" )" && pwd )"
amon_install_dir="/usr/local/amonlite"
echo "*** Installing Amon Lite $VERSION ..."
# Install easy_install if necessary
if ! command_exists easy_install ; then
wget "http://peak.telecommunity.com/dist/ez_setup.py"
sudo $PYTHON ez_setup.py
rm -f ez_setup.py
fi
# Install Amon Lite
# Cleans up the directory from old amon installations
rm -rf amonlite-* # Cleans up old archives from previous installs, that could break the updating process
wget --no-check-certificate "https://github.com/downloads/martinrusev/amonlite/amonlite-$VERSION.tar.gz"
tar -zxf "amonlite-$VERSION.tar.gz" > /dev/null
if [ "$DISTRO" == 'debian' ]; then
# Ensure that we have the pymongo driver dependecies
# Skip installing python and gcc if they are already installed.
if dpkg-query -s python-dev gcc sysstat >> /dev/null ; then
echo "*** Amon Lite requirements already satisfied"
else
echo "** Installing Amon Lite requirements"
sudo apt-get -y install gcc python-dev sysstat
fi
if dpkg-query -W python-dev gcc sysstat ; then
echo "** success!"
fi
elif [ "$DISTRO" == 'rpm' ]; then
if rpm --quiet -q gcc python-devel sysstat ; then
echo "*** Amon Lite requirements already satisfied"
else
echo "** Installing Amon Lite requirements"
sudo yum -t -y install gcc python-devel sysstat
fi
if rpm --quiet -q gcc python-devel sysstat ; then
echo "** success!"
fi
fi
cd "amonlite"
sudo $PYTHON setup.py install # Install Amon and all the dependecies
$PYTHON generate_config.py || die # Generate the configuration file
# Copy the generated configuration file from the current directory
# If Amon was previously installed, the generator will just a secret_key
sudo cp amonlite.conf /etc/amonlite.conf
# Copy the collector daemon
sudo cp contrib/amond /etc/init.d/amond
# make the collector daemon executable
sudo chmod +x /etc/init.d/amond
# Add the collector daemon to the startup list
if [ "$DISTRO" == 'debian' ]; then
sudo update-rc.d amond defaults > /dev/null
elif [ "$DISTRO" == 'rpm' ]; then
sudo chkconfig --add amond > /dev/null
sudo chkconfig amond on
fi
# Copy the web application daemon
sudo cp contrib/amonlite /etc/init.d/amonlite
# make the web app daemon executable
sudo chmod +x /etc/init.d/amonlite
# Copy the ZeroMQ daemon
sudo cp contrib/amonmq /etc/init.d/amonmq
# make the ZeroMQ daemon executable
sudo chmod +x /etc/init.d/amonmq
# Create a directory for the log files
sudo mkdir -p "$amon_install_dir"
cd "$bash_script_dir"
# Install MongoDB
install_mongodb() {
if [ "$DISTRO" == 'debian' ]; then
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
if ! file_exists "/etc/apt/sources.list.d/10gen.list"; then
if [ "$UBUNTU" == 1 ]; then
cat <<EOL > 10gen.list
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
EOL
else # debian
cat <<EOL > 10gen.list
deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen
EOL
fi # is_ubuntu END
sudo cp 10gen.list /etc/apt/sources.list.d/10gen.list
sudo apt-get update
fi # file exists
sudo apt-get install mongodb-10gen
fi # DISTRO = debian
if [ "$DISTRO" == 'rpm' ]; then
if ! file_exists "/etc/yum.repos.d/10gen.repo"; then
if [ "$ARCH" == '64bit' ]; then
cat <<EOL > 10gen.repo
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1
EOL
# 32 bit
else
cat <<EOL > 10gen.repo
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0
enabled=1
EOL
fi # ARCH END
sudo cp 10gen.repo /etc/yum.repos.d/10gen.repo
fi # file exists
yum install mongo-10gen mongo-10gen-server
chkconfig mongod on
fi # DISTRO = rpm
}
#Start Mongo
start_mongodb()
{
sudo /etc/init.d/mongodb start
if [ "$DISTRO" == 'rpm' ]; then
sudo service mongod start
else
sudo /etc/init.d/mongodb start
fi
sleep 5 # Wait for Mongo to write the journal files
}
echo ""
# Install MongoDB if it is not installed on the system
if ! command_exists mongo ; then
# Check one more time, the value should be 1
if [ $mongo_check = '1' ]; then
install_mongodb
configure_mongodb
start_mongodb
fi
fi
# Show a message about where to go for help.
print_troubleshooting_instructions() {
echo
echo "For troubleshooting instructions, please see the Amon User Guide :"
echo "http://amon.cx/guide/"
echo
echo "To uninstall Amon, \`curl uninstall.amon.cx | bash\`"
}
# All done!
CHECK_AMOND=$(pgrep amond)
# The amond process doesn't exist
if [[ -z "$CHECK_AMOND" ]]; then
echo "*** Amon Lite succesfully installed"
sudo /etc/init.d/amond start
echo "*** Starting Amon Lite "
echo "*** The web application is not running by default, you can start with \`sudo /etc/init.d/amonlite start\`"
else
echo "*** Amon Lite succesfully updated"
sudo /etc/init.d/amond restart
fi
# If the web application is running - restart it
if pgrep -x amonlite > /dev/null; then
sudo /etc/init.d/amonlite restart
fi
# Cleanup
cd $bash_script_dir # Go back to the root directory
sudo rm -rf amonlite # Delete the directory with the install files
rm amonlite-$VERSION.tar.gz # Delete the archive
print_troubleshooting_instructions