239 lines
7.2 KiB
Bash
239 lines
7.2 KiB
Bash
#!/bin/bash
|
|
|
|
## Functions
|
|
|
|
function maitenance {
|
|
echo -e "${INFO}Removing stopped containers...${NC}"
|
|
docker container prune -f
|
|
echo -e "${INFO}Pruning unused images...${NC}"
|
|
docker image prune -f
|
|
echo -e "${INFO}Pruning unused volumes...${NC}"
|
|
docker volume prune -f
|
|
echo -e "${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}
|
|
}
|
|
|
|
## Pre-/Post- execute
|
|
|
|
function start {
|
|
echo -e "${INFO}Starting services: ${NC}"
|
|
svc-handler "start"
|
|
echo -e "${INFO}Starting containers: ${NC}"
|
|
dc-handler "start"
|
|
}
|
|
|
|
function stop {
|
|
echo -e "${WARN}Stopping containers: ${NC}"
|
|
dc-handler "stop"
|
|
echo -e "${WARN}Stopping services: ${NC}"
|
|
svc-handler "stop"
|
|
}
|
|
|
|
function status {
|
|
echo -e "${INFO}Services: ${NC}"
|
|
svc-handler "status"
|
|
echo -e "${INFO}Containers: ${NC}"
|
|
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
|
|
echo -e "${WARN}Service $i was already running! ${NC}"
|
|
else
|
|
if (systemctl start "$i" > /dev/null 2>&1); then
|
|
echo -e "${DONE}Started service $i succesfully! ${NC}"
|
|
else
|
|
echo -e "${ERROR}Failed to start service $i! ${NC}"
|
|
fi
|
|
fi
|
|
;;
|
|
stop)
|
|
if (( $(echo "${servicelist}" | grep -c "$i") > 0 )); then
|
|
if (systemctl stop "$i" > /dev/null 2>&1); then
|
|
echo -e "${DONE}Stopped service $i succesfully! ${NC}"
|
|
else
|
|
echo -e "${ERROR}Failed to stop service $i! ${NC}"
|
|
fi
|
|
else
|
|
echo -e "${WARN}Service $i is already stopped! ${NC}"
|
|
fi
|
|
;;
|
|
status)
|
|
if (( $(echo "${servicelist}" | grep -c "$i") > 0 )); then
|
|
echo -e "${WARN}Service $i is up and running! ${NC}"
|
|
else
|
|
echo -e "${ERROR}Service $i is stopped! ${NC}"
|
|
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
|
|
echo -e "${WARN}Container $i was already up and running! ${NC}"
|
|
else
|
|
if (docker start "$i" > /dev/null 2>&1); then
|
|
echo -e "${DONE}Started container $i succesfully! ${NC}"
|
|
else
|
|
echo -e "${ERROR}Failed to start container $i! ${NC}"
|
|
fi
|
|
fi
|
|
;;
|
|
stop)
|
|
if (( $(echo "${dockercont}" | grep -c "$i") > 0 )); then
|
|
if (docker stop --time=20 "$i" > /dev/null 2>&1); then
|
|
echo -e "${DONE}Stopped container $i succesfully! ${NC}"
|
|
else
|
|
echo -e "${ERROR}Failed to stop container $i! ${NC}"
|
|
fi
|
|
else
|
|
echo -e "${WARN}Container $i was already stopped! ${NC}"
|
|
fi
|
|
;;
|
|
status)
|
|
if (( $(echo "${dockercont}" | grep -c "$i") > 0 )); then
|
|
echo -e "${WARN}Container $i is up and running! ${NC}"
|
|
else
|
|
echo -e "${ERROR}Container $i is stopped! ${NC}"
|
|
fi
|
|
;;
|
|
esac
|
|
elif (systemctl -q is-active docker); then
|
|
echo -e "${ERROR}An error has occured, we are sorry!"
|
|
else
|
|
echo -e "${ERROR}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
|
|
;;
|
|
*)
|
|
echo -e "${ERROR}That component does not exist!${NC}"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
function selfupdate {
|
|
cd ~/bin || exit
|
|
echo -e "${INFO}Pulling latest version!${NC}"
|
|
git pull https://github.com/RXCommunity/Manager master || echo -e "${ERROR}Could not pull latest version, did you clone the repository?${NC}"
|
|
}
|
|
|
|
## 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"
|
|
)
|
|
|
|
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
|
|
selfupdate
|
|
case "$1" in
|
|
"start") start;;
|
|
"stop") stop;;
|
|
"restart") stop; start;;
|
|
"status") status;;
|
|
"update") update "$2";;
|
|
"maitenance") maitenance;;
|
|
*) echo -e "${ERROR}That command does not exist!${NC}";;
|
|
esac |