This repository has been archived on 2019-08-11. You can view files and clone it, but cannot push or open issues or pull requests.
Manager/manager

276 lines
7.6 KiB
Bash

#!/bin/bash
## Functions
function maitenance {
output "info" "Removing stopped containers...${NC}"
dp "container"
output "info" "Pruning unused images...${NC}"
dp "image"
output "info" "Pruning unused volumes...${NC}"
dp "volume"
output "info" "Clearing logs...${NC}"
rm -v /var/log/nginx/*.*
nginx -s reload
}
function getlatest () {
curl -s https://api.github.com/repos/"${1}"/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'
}
## Pre-/Suf- fix
function pa () {
php artisan "${1}"
}
function dp () {
docker "${1}" prune -f
}
## Output types
function output () {
case "$1" in
"info") echo -e "${INFO}$2${NC}";;
"done") echo -e "${DONE}$2${NC}";;
"warn") echo -e "${WARN}$2${NC}";;
"err") echo -e "${ERROR}$2${NC}";;
esac
}
## Pre-/Post- execute
function start {
output "info" "Starting services:"
svc-handler "start"
output "info" "Starting containers:"
dc-handler "start"
}
function stop {
output "warn" "Stopping containers:"
dc-handler "stop"
output "warn" "Stopping services:"
svc-handler "stop"
}
function status {
output "info" "Services:"
svc-handler "status"
output "info" "Containers:"
dc-handler "status"
}
## Handlers
## Service handler
function svc-handler () {
for i in "${services[@]}"; do
case "$1" in
"start")
if (( $(echo "${servicelist}" | grep -c "$i") > 0 )); then
output "warn" "Service $i was already running!"
else
if (systemctl start "$i" > /dev/null 2>&1); then
output "done" "Started service $i succesfully!"
else
output "err" "Failed to start service $i!"
fi
fi
;;
"stop")
if (( $(echo "${servicelist}" | grep -c "$i") > 0 )); then
if (systemctl stop "$i" > /dev/null 2>&1); then
output "done" "Stopped service $i succesfully!"
else
output "err" "Failed to stop service $i!"
fi
else
output "warn" "Service $i is already stopped!"
fi
;;
"status")
if (( $(echo "${servicelist}" | grep -c "$i") > 0 )); then
output "warn" "Service $i is up and running!"
else
output "err" "Service $i is stopped!"
fi
;;
esac
done
}
## Docker handler
function dc-handler () {
for i in "${containers[@]}"; do
if (( "${dockerstatus}" > 0 )); then
case "$1" in
"start")
if (( $(echo "${dockercont}" | grep -c "$i") > 0 )); then
output "warn" "Container $i was already up and running!"
else
if (docker start "$i" > /dev/null 2>&1); then
output "done" "Started container $i succesfully!"
else
output "err" "Failed to start container $i!"
fi
fi
;;
"stop")
if (( $(echo "${dockercont}" | grep -c "$i") > 0 )); then
if (docker stop --time=20 "$i" > /dev/null 2>&1); then
output "done" "Stopped container $i succesfully!"
else
output "err" "Failed to stop container $i!"
fi
else
output "warn" "Container $i was already stopped!"
fi
;;
"status")
if (( $(echo "${dockercont}" | grep -c "$i") > 0 )); then
output "warn" "Container $i is up and running!"
else
output "err" "Container $i is stopped!"
fi
;;
esac
elif (systemctl -q is-active docker); then
output "err" "An error has occured, we are sorry!"
else
output "err" "Docker is stopped, cannot perform check!"
fi
done
}
## Streamlined functions
function update () {
case "$1" in
"panel")
cd /var/www/pterodactyl || exit
pa "down"
curl -L https://github.com/pterodactyl/panel/releases/download/"${panelversion}"/panel.tar.gz | tar --strip-components=1 -xzv
curl -L https://github.com/RXCommunity/RedXen-Panel/archive/"${themeversion}".tar.gz | tar --strip-components=1 -xzv
chown -R www-data:www-data ./*
chmod -R 755 storage/* bootstrap/cache
composer install --no-dev --optimize-autoloader
pa "view:clear"
pa "cache:clear"
pa "config:cache"
pa "migrate --force"
pa "db:seed --force"
pa "p:migration:clean-orphaned-keys -n"
pa "up"
;;
"bot")
cd /root/yagpdb || exit
git pull https://github.com/jonas747/yagpdb master
cd ./yagpdb_docker || exit
docker-compose build --force-rm --no-cache --pull
docker-compose up -d
;;
"cloud")
docker pull nextcloud
docker stop nextcloud
docker rm nextcloud
docker run -d --name nextcloud -p 127.0.0.1:8080:80 --link nx-database -v /var/nx/nextcloud-config:/var/www/html -v /var/nx/cloud-data:/var/www/html/data nextcloud
;;
"dev")
cd /var/www/rxdev || exit
git pull https://github.com/RXCommunity/Homepage dev
;;
"home")
cd /var/www/rxhome || exit
git pull https://github.com/RXCommunity/Homepage master
;;
"forum")
/var/discourse/launcher rebuild forum
;;
*)
output "err" "That component does not exist! Available components are:"
output "info" "${components[*]}"
;;
esac
}
function selfupdate {
cd ~/bin || exit
output "info" "Self-updating:${NC}"
git pull https://github.com/RXCommunity/Manager master || \
output "err" "Could not pull latest version, did you clone the repository?"
}
## Definitions
services=(
"pteroq"
"nginx"
"wings"
"php7.2-fpm"
"docker"
"cachet-monitor"
"telegraf"
"grafana-server"
)
containers=(
"forum"
"sourcebans"
"nx-database"
"nextcloud"
"yagpdb_docker_app_1"
"yagpdb_docker_db_1"
"yagpdb_docker_redis_1"
"pterodactyl-db"
"ptero-redis"
"cachet-docker_cachet_1"
"cachet-docker_postgres_1"
"influxdb"
"grafana-storage"
)
functions=(
"start"
"stop"
"restart"
"status"
"update"
"maitenance"
"selfupdate"
)
components=(
"panel"
"bot"
"cloud"
"dev"
"home"
"forum"
)
panelversion=$(getlatest "Pterodactyl/panel")
themeversion=$(getlatest "RXCommunity/RedXen-Panel")
dockercont=$(docker ps)
servicelist=$(systemctl list-units --type=service --state=active | grep -oe ".*\.service")
dockerstatus=$(systemctl status | grep -c docker)
## Colors
NC='\033[0m'
ERROR='\033[31m'
WARN='\033[33m'
DONE='\033[32m'
INFO='\033[34m'
## Main run
case "$1" in
"start") start;;
"stop") stop;;
"restart") stop; start;;
"status") status;;
"update") update "$2";;
"maitenance") maitenance;;
"selfupdate") selfupdate;;
*)
output "err" "That command does not exist! Available functions are:"
output "info" "${functions[*]}"
;;
esac