Modular configs for flexibility

This commit is contained in:
Alex D. 2019-02-28 19:20:16 +01:00
parent 008ab6388f
commit b9c3a04001
2 changed files with 46 additions and 160 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
custom/
*.sh
*.list

203
manager
View File

@ -1,34 +1,15 @@
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
## Functions
function maintenance {
output "info" "Removing stopped containers"
dp "container"
output "info" "Pruning unused images"
dp "image"
output "info" "Pruning unused volumes"
dp "volume"
output "info" "Clearing & Updating APT"
apt-get update
apt-get clean
apt-get autoclean
apt-get -y autoremove
output "info" "Clearing logs"
rm -v /var/log/nginx/*.*
journalctl --vacuum-size=20M
nginx -s reload
systemctl restart telegraf
}
function getlatest () {
curl -s https://api.github.com/repos/"${1}"/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'
}
## Pre-/Suf- fix
function dp () {
docker "${1}" prune -f
if [ -x "$(command -v curl)" ]; then
curl -s https://api.github.com/repos/"${1}"/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'
else
echo "err" "Could not get latest version of ${1}! curl is not installed!"
fi
}
## Output types
@ -44,31 +25,21 @@ function output () {
## 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 {
function exec-com () {
output "info" "Services:"
svc-handler "status"
output "info" "Containers:"
dc-handler "status"
svc-handler "$1"
if [ -x "$(command -v docker)" ]; then
output "info" "Containers:"
dc-handler "$1"
fi
}
## Handlers
## Service handler
function svc-handler () {
servicelist=$(systemctl list-units --type=service --state=active | grep -oe ".*\.service")
mapfile -t services < "${DIR}"/services.list
for i in "${services[@]}"; do
case "$1" in
"start")
@ -106,6 +77,9 @@ function svc-handler () {
## Docker handler
function dc-handler () {
dockercont=$(docker ps)
dockerstatus=$(systemctl status | grep -c docker)
mapfile -t containers < "${DIR}"/containers.list
for i in "${containers[@]}"; do
if (( "${dockerstatus}" > 0 )); then
case "$1" in
@ -149,119 +123,23 @@ function dc-handler () {
## Streamlined functions
function update () {
case "$1" in
"panel")
(cd "/var/www/pterodactyl" && php artisan down) || exit
curl -L https://github.com/pterodactyl/panel/releases/download/"$(getlatest "Pterodactyl/panel")"/panel.tar.gz | tar --strip-components=1 -xzv
curl -L https://github.com/RXCommunity/RedXen-Panel/archive/"$(getlatest "RXCommunity/RedXen-Panel")".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
for f in "${artisanfunc[@]}"; do
php artisan $f
done
;;
"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:"
[ -n $(git diff --name-only origin/master | grep manager) ] && {
git reset --hard HEAD
git pull https://github.com/RXCommunity/manager master
} || {
output "info" "Already latest version!"
}
if [ -x "$(command -v git)" ]; then
cd ~/bin || exit
output "info" "Self-updating:"
if (git diff --name-only origin/master | grep -q manager); then
git reset --hard HEAD
git pull https://github.com/RXCommunity/manager master
else
output "info" "Already latest version!"
fi
else
output "err" "You do not have GIT installed!"
fi
}
## 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"
"maintenance"
"selfupdate"
)
components=(
"panel"
"bot"
"cloud"
"dev"
"home"
"forum"
)
artisanfunc=(
"view:clear"
"cache:clear"
"config:cache"
"migrate --force"
"db:seed --force"
"p:migration:clean-orphaned-keys -n"
"up"
)
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'
@ -270,16 +148,21 @@ DONE='\033[32m'
INFO='\033[34m'
## Main run
case "$1" in
"start") start;;
"stop") stop;;
"restart") stop; start;;
"status") status;;
"update") update "$2";;
"maintenance") maintenance;;
"command")
exec-com "${2}"
;;
"script")
# shellcheck source=/dev/null
source "${DIR}/custom/$2.sh"
;;
"maintenance")
# shellcheck source=./maintenance.sh
source "${DIR}/maintenance.sh"
;;
"selfupdate") selfupdate;;
*)
output "err" "That command does not exist! Available functions are:"
output "info" "${functions[*]}"
output "err" "That command does not exist!"
;;
esac