plugable-matrix-bot/install.sh

140 lines
4.7 KiB
Bash
Raw Normal View History

#!/bin/bash
#
# Installation script for plugable-matrix-bot
#
# @author Dennis Potter <dennis@dennispotter.eu>
# @copyright 2019, Dennis Potter
# @license GNU General Public License (version 3)
#
# plugable-matrix-bot
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##################################################################################
# This script does the following:
# - create virtualenv
# - install requirements of main bot
# - create initial config.hjson
# - (optional) create systemd service
if [[ $1 == help ]]; then
echo "Help:"
echo "install.sh : Creates a virtualenv, an initial config.hjson, and installs"
echo " all required packages"
echo "install.sh service: Runs ./install and additionally adds a systemd service."
echo " This command will ask you for root credentials!"
echo "install.sh help : Prints this message."
exit 0
fi
# Define directory of chatbot
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Check if user is superuser
if [[ "$EUID" -eq 0 ]]; then
echo "ERR: Please do not run this script as super user!"
exit 1
fi
#################################################################
# Create function to check availability of software
#################################################################
function availability {
if [[ ! $(command -v $1) ]]; then
echo "ERR: '$1' is not available but required. Please install it!"
exit 1
fi
}
#################################################################
# Initiliaze submodules
#################################################################
git submodule update --init
#################################################################
# Create virtualenv
#################################################################
# Check whether virtualenv is present
availability virtualenv
# Create virtualenv
virtualenv $DIR
# Enter virtualenv
source $DIR/bin/activate
#################################################################
# Install requirements
#################################################################
# Check whether pip3 is present
availability pip3
# Install requirements
pip3 install -r requirements.txt
#################################################################
# Create config.hjson
#################################################################
echo ""
echo "############################################################################"
echo "This step will generate a configuration file. The resulting bot will be very"
echo "simple and is only able to respond to the message "Hello bot". However, it"
echo "forms the perfect base for further development."
echo ""
echo "In the following steps, an _existing_ user must be provided. If you did not"
echo "yet create a user for your bot, please do so now!"
echo ""
echo "The installation procedure will also ask you to define a super user. A super"
echo "user is a normal user that can manage the bot. This SU is passed on to all"
echo "plugins."
echo "############################################################################"
echo ""
if [[ -f $DIR/config.hjson ]]; then
read -p "$DIR/config.hjson already exists. Should I overwrite it? [y/n]: " ow
if [[ $ow != y ]]; then
echo "ERR: Cannot write to $DIR/config.hjson!"
exit 1
fi
fi
read -p 'Matrix homeserver: ' server
read -p 'Matrix username: ' username
read -p 'Matrix password: ' password
read -p 'Super user: ' superuser
sed -e "s|\${server}|${server}|"\
-e "s|\${username}|${username}|"\
-e "s|\${password}|${password}|"\
-e "s|\${superuser}|${superuser}|"\
$DIR/config.hjson.template > $DIR/config.hjson
#################################################################
# Configure service
#################################################################
if [[ $1 != service ]]; then
exit 0
fi
sudo useradd -r mchatbot
sed -e "s|\${DIR}|${DIR}|"\
$DIR/plugable-matrix-bot.service.template > $DIR/plugable-matrix-bot.service
sudo mv $DIR/plugable-matrix-bot.service /etc/systemd/system/plugable-matrix-bot.service
sudo systemctl enable plugable-matrix-bot.service
sudo systemctl start plugable-matrix-bot.service