catask/install.sh
2024-11-26 14:47:02 +03:00

130 lines
4.1 KiB
Bash
Executable file

#!/usr/bin/bash
working_dir=$PWD/catask
bold=$(tput bold)
normal=$(tput sgr0)
git_repo_url="https://git.mst.k.vu/catask-org/catask"
git_repo_issue_url="${git_repo_url}/issues/new"
echo "--------------------------"
echo "${bold}Installing CatAsk...${normal}"
echo "--------------------------"
echo
echo "${bold}Cloning the repository...${normal}"
# this might work... or not, who knows
# id -u catask >/dev/null 2>&1 || sudo useradd -r -s /bin/false -m -d $working_dir/home/catask -U catask
# cloning dev branch for now because the installer doesn't exist in main branch yet
git clone $git_repo_url $working_dir
cd $working_dir
echo
echo "${bold}Creating & activating virtual environment...${normal}"
python3 -m venv venv && . venv/bin/activate
echo
echo "${bold}Installing required packages...${normal}"
echo
pip install -r requirements.txt
# move to /etc/catask after installing packages
# sudo mv $working_dir /etc/
# working_dir="/etc/catask"
# check if redis is installed
if ! command -v redis-cli 2>&1 >/dev/null; then
echo
echo "${bold}Installing Redis...${normal}"
# grab variables from os-release file
. /etc/os-release
os_like=$ID_LIKE
case "$os_like" in
debian)
# standard redis install instructions from https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/install-redis-on-linux/#install-on-ubuntudebian
sudo apt-get install lsb-release curl gpg
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
sudo chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis -y
;;
fedora)
# standard redis install instructions from https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/install-redis-on-linux/#install-on-red-hatrocky
sudo dnf install redis -y
sudo systemctl enable redis
sudo systemctl start redis
;;
# if id isn't like debian or fedora
*)
echo "Distribution '$os_like' isn't yet supported. File an issue at ${git_repo_issue_url}"
;;
esac
fi
echo
echo "${bold}Configuring CatAsk...${normal}"
cp $working_dir/.env.example $working_dir/.env; cp $working_dir/config.example.json $working_dir/config.json
echo
if ! command -v $EDITOR 2>&1 >/dev/null; then
echo "No default editor found, falling back to ${bold}nano${normal}..."
editor_=nano
else
editor_=$EDITOR
fi
read -n 1 -s -r -p "Press any key to open main config file..."
$editor_ config.json
read -n 1 -s -r -p "Press any key to open .env config file..."
$editor_ .env
echo
echo "${bold}Initializing the database...${normal}"
if flask init-db; then : ; else
echo "An error has occurred: create an issue at ${bold}${git_repo_issue_url}${normal}"
read -p "Proceed? (y/N) " proceed_after_error
proceed_after_error="${proceed_after_error,,}"
if [[ "$proceed_after_error" == 'y' ]]; then
echo "Proceeding..."
else
exit 255
fi
fi
read -p "Do you want to install CatAsk as a systemd service? (Y/n) " sysd_service_input
if [[ "${sysd_service_input,,}" == 'n' ]]; then
echo "Start CatAsk with: ${bold}gunicorn -w 4 app:app -b 127.0.0.1:5000${normal}"
echo "Replace ${bold}127.0.0.1:5000${normal} with address where CatAsk will run"
exit 0
else
read -p "Address on which CatAsk will run (127.0.0.1:5000): " catask_addr
catask_addr=${catask_addr:-127.0.0.1:5000}
sudo cat > /etc/systemd/system/catask.service << EOF
[Unit]
Description=CatAsk
[Service]
User=%u
WorkingDirectory=$working_dir
ExecStart=$working_dir/venv/bin/python3 -m gunicorn -w 4 app:app -b $catask_addr
[Install]
WantedBy=multi-user.target
EOF
fi
echo "Created a systemd service with these contents:"
echo "-----------------------"
cat << EOF
[Unit]
Description=catask
[Service]
User=catask
WorkingDirectory=$PWD
ExecStart=$PWD/venv/bin/python3 -m gunicorn -w 4 app:app -b $catask_addr
[Install]
WantedBy=multi-user.target
EOF
echo "-----------------------"
echo "Start the service with: ${bold}sudo systemctl start --now catask${normal}"
echo
echo "${bold}Install complete!${normal}"