diff --git a/manager b/manager index 91be42c..7f1f6e5 100755 --- a/manager +++ b/manager @@ -23,17 +23,23 @@ COLOR=( if ${DEV}; then function output () { - declare input="${*:2}"; - echo -e "${COLOR[$1]}$(date +%F\ %T\ %z) | ${1} | ${input}${COLOR[nc]}" + while read LINE; do + if [[ ${LINE%% *} =~ ^erro|done|info|warn$ ]]; then + echo -e "${COLOR[${LINE%% *}]}$(date +%F\ %T\ %z) | ${LINE%% *} | ${LINE#* }${COLOR[nc]}" + else + echo -e "${COLOR[nc]}$(date +%F\ %T\ %z) | ${LINE}${COLOR[nc]}" + fi + done } function xt () { - output "$1" "Set return code to $2" + echo "$1 Set return code to $2" return "$2" } else function output () { - declare input=${*:2}; - echo -e "${COLOR[$1]}${input}${COLOR[nc]}" + while read LINE; do + echo -e "${COLOR[${LINE%% *}]}${LINE#* }${COLOR[nc]}" + done } function xt () { return "$2" @@ -46,14 +52,15 @@ function main () { export CWDIR=${DIR}/plugins/${1} if (containsElement "$1" "${plugins[@]}"); then if [ "$2" = "help" ]; then - output "info" "$1" + echo "info $1" else - source "${DIR}/plugins/${1}/main.sh" + [ -f "${CWDIR}/.env" ] && source "${CWDIR}/.env" + source "${CWDIR}/main.sh" fi else - output "erro" "Plugin $1 not found." + echo "erro Plugin $1 not found." avail-plugins - output "info" "Tip: If you added the plugin recently, add it to the index with '${0##*/} index'" + echo "info Tip: If you added the plugin recently, add it to the index with '${0##*/} index'" fi } @@ -65,58 +72,58 @@ function containsElement () { } function nopluginlist { - output "warn" "No plugin list found. Try running '${0##*/} index' to index everything." + echo "warn No plugin list found. Try running '${0##*/} index' to index everything." } function avail-plugins { if [ -f "${PLIST}" ]; then - output "warn" "Available plugins:" - mapfile -t plugins < "${PLIST}" || output "erro" "Failed to load plugin list." - output "warn" "${plugins[@]}" + echo "warn Available plugins:" + mapfile -t plugins < "${PLIST}" || echo "erro Failed to load plugin list." + echo "warn ${plugins[@]}" else nopluginlist fi } function index { - output "info" "Indexing plugins" + echo "info Indexing plugins" truncate -s 0 "${PLIST}" for i in $(find "${DIR}"/plugins/* -type d | grep -P "(?<=\/plugins\/)([\w\d\-\_])*" -o); do - echo "$i" >> "${PLIST}" && output "done" "Added plugin '$i'." + echo "$i" >> "${PLIST}" && echo "done Added plugin '$i'." done - output "info" "Indexing extensions" + echo "info Indexing extensions" truncate -s 0 "${ELIST}" for i in $(find "${DIR}"/extensions/* -type f | grep -P "(?<=\/extensions\/)([\w\d\-\_])*(\.func)" -o); do - echo "$i" >> "${ELIST}" && output "done" "Added extension '$i'." + echo "$i" >> "${ELIST}" && echo "done Added extension '$i'." done } function loadlists { - mapfile -t plugins < "${PLIST}" || output "erro" "Failed to load plugin list." - mapfile -t extensions < "${ELIST}" || output "erro" "Failed to load extension list." + mapfile -t plugins < "${PLIST}" || echo "erro Failed to load plugin list." + mapfile -t extensions < "${ELIST}" || echo "erro Failed to load extension list." for i in "${extensions[@]}"; do - source "${DIR}/extensions/$i" || output "warn" "Failed to import extension ${i}." + source "${DIR}/extensions/$i" || echo "warn Failed to import extension ${i}." done } function preparedirs { if mkdir "${DIR}"/plugins/ "${DIR}"/extensions/; then - output "info" "Created a empty plugins and extensions directory." + echo "info Created a empty plugins and extensions directory." else - output "err" "Could not create directory." + echo "erro Could not create directory." fi } ## Main run -[ ${DEV} = "true" ] && output "warn" "Running in development mode!" - +( +[ ${DEV} = "true" ] && echo "warn Running in development mode!" if ! [ $# -eq 0 ] && ! [[ "$1" = "index" ]] && [ -f "${PLIST}" ] && [ -f "${ELIST}" ]; then loadlists main "$@" elif [ -d "${DIR}"/plugins/ ] && [ -d "${DIR}"/extensions/ ]; then if [ $# -eq 0 ]; then - output "warn" "Nothing to do!" + echo "warn Nothing to do!" avail-plugins elif [[ "$1" = "index" ]]; then index @@ -126,4 +133,5 @@ elif [ -d "${DIR}"/plugins/ ] && [ -d "${DIR}"/extensions/ ]; then else preparedirs fi -! [[ "$1" = "rawpl" ]] && output "done" "Made with ♥ by casKd | t.me/casKd_dev" +! [[ "$1" = "rawpl" ]] && echo "info Made with ♥ by casKd | t.me/casKd_dev" +) | output diff --git a/plugins/backup/main.sh b/plugins/backup/main.sh index c615e25..0770e4c 100644 --- a/plugins/backup/main.sh +++ b/plugins/backup/main.sh @@ -1,17 +1,15 @@ -source "${CWDIR}/.env" - # MariaDB / MySQL -output info "Backing up MariaDB" -mysqldump -h "${MSQL_HOST}" -P"${MSQL_PORT}" -u"${MSQL_USER}" -p"${MSQL_PASS}" -ARE | gzip -c > "${MSQL_PATH}"/mariadb-$(date +%s).sql.gz || output erro "Failed to backup MariaDB" +echo "info Backing up MariaDB" +mysqldump -h "${MSQL_HOST}" -P"${MSQL_PORT}" -u"${MSQL_USER}" -p"${MSQL_PASS}" -ARE | gzip -c > "${MSQL_PATH}"/mariadb-$(date +%s).sql.gz || echo "erro Failed to backup MariaDB" # Postgres (Docker) -output info "Backing up PostgreSQL" -docker exec main-db_postgres_1 pg_dumpall -U "${PSQL_USER}" | gzip -c > "${PSQL_PATH}"/postgres-$(date +%s).sql.gz || output erro "Failed to backup PostgreSQL" +echo "info Backing up PostgreSQL" +docker exec main-db_postgres_1 pg_dumpall -U "${PSQL_USER}" | gzip -c > "${PSQL_PATH}"/postgres-$(date +%s).sql.gz || echo "erro Failed to backup PostgreSQL" # InfluxDB (Docker) -# output info "Backing up InfluxDB" -# docker exec main-db_influxdb_1 bash -c "influxd backup -portable -database telegraf /backups > /dev/null 2>&1 && tar -cz /backups" > "${IFXDB_PATH}"/influxdb-telegraf-$(date +%s).tar.gz || output erro "Failed to backup InfluxDB" +# echo "info Backing up InfluxDB" +# docker exec main-db_influxdb_1 bash -c "influxd backup -portable -database telegraf /backups > /dev/null 2>&1 && tar -cz /backups" > "${IFXDB_PATH}"/influxdb-telegraf-$(date +%s).tar.gz || echo "erro Failed to backup InfluxDB" # Backup maintenance -output info "Removing older backups" +echo "info Removing older backups" find /root/main-db/backups/* -type f -ctime +${BR_DAYS} -exec rm -rf {} \; diff --git a/plugins/cloudflare/main.sh b/plugins/cloudflare/main.sh index c260081..8e62202 100644 --- a/plugins/cloudflare/main.sh +++ b/plugins/cloudflare/main.sh @@ -1,8 +1,6 @@ -source "${CWDIR}/.env" - # Record name $3 # Record type $4 -# Record content $5 (default: own IP) +# Record content $5 (TODO default: own IP) # Cloudflare Proxy $6 (true/false) if FETCH=$(curl -sX GET "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \ @@ -13,7 +11,12 @@ if FETCH=$(curl -sX GET "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_re case "$2" in "list") for ((i=0;i<${#DOMAIN[@]};++i)); do - echo "$(echo "${FETCH}" | jq -r ".result[${i}].name") $(echo "${FETCH}" | jq -r ".result[${i}].type") $(echo "${FETCH}" | jq -r ".result[${i}].content") $(echo "${FETCH}" | jq -r ".result[${i}].id")" | output info $( 0 )); then - case "$2" 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 erro "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 erro "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 erro "Container $i is stopped!" - fi - ;; - esac - elif (systemctl -q is-active docker); then - output erro "An error has occured, we are sorry!" - else - output erro "Docker is stopped, cannot perform check!" - fi + if (( "${dockerstatus}" > 0 )); then + case "$2" 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 "erro 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 "erro 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 "erro Container $i is stopped!" + fi + ;; + esac + elif (systemctl -q is-active docker); then + output "erro An error has occured, we are sorry!" + else + output "erro Docker is stopped, cannot perform check!" + fi done diff --git a/plugins/maintenance/main.sh b/plugins/maintenance/main.sh index ff9641e..42d01f5 100644 --- a/plugins/maintenance/main.sh +++ b/plugins/maintenance/main.sh @@ -1,18 +1,18 @@ if [ -x "$(command -v docker)" ]; then - output info "Clearing Docker remainings" - docker system prune --all -f + output "info Clearing Docker remainings" + docker system prune --all -f fi case $(uname -rv) in *Ubuntu*) - output info "Clearing & Updating APT" + output "info Clearing & Updating APT" apt-get -y update apt-get -y autoclean apt-get -y autoremove ;; esac -output info "Clearing logs" +output "info Clearing logs" if [ -x "$(command -v nginx)" ]; then - truncate -s 0 /var/log/nginx/*.* + truncate -s 0 "${LOGPATH:-/var/log/nginx/*.*}" fi journalctl --vacuum-size=20M diff --git a/plugins/pterodactyl/main.sh b/plugins/pterodactyl/main.sh index 2b787f3..4840be2 100644 --- a/plugins/pterodactyl/main.sh +++ b/plugins/pterodactyl/main.sh @@ -1,30 +1,30 @@ case "$2" in - "update") - cd /var/www/pterodactyl || exit - php artisan down - output info "Fetching latest releases" - 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 - output info "Ensuring permissions are correct" - chown -R www-data:www-data ./* - chmod -R 755 storage/* bootstrap/cache - output info "Getting latest deps" - composer install --no-dev --optimize-autoloader || output erro "Failed to get deps!" + "update") + cd /var/www/pterodactyl || exit + php artisan down + output "info Fetching latest releases" + 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 + output "info Ensuring permissions are correct" + chown -R www-data:www-data ./* + chmod -R 755 storage/* bootstrap/cache + output "info Getting latest deps" + composer install --no-dev --optimize-autoloader || output "erro Failed to get deps!" - artisanfunc=( - "view:clear" - "cache:clear" - "config:cache" - "migrate --force" - "db:seed --force" - "p:migration:clean-orphaned-keys -n" - "up" - ) + artisanfunc=( + "view:clear" + "cache:clear" + "config:cache" + "migrate --force" + "db:seed --force" + "p:migration:clean-orphaned-keys -n" + "up" + ) - output info "Running migrations and clearance" - for f in "${artisanfunc[@]}"; do - php artisan $f || output erro "Failed to execute 'artisan ${f}'" - done - ;; - *); echo "Nothing to do";; + output "info Running migrations and clearance" + for f in "${artisanfunc[@]}"; do + php artisan $f || output "erro Failed to execute 'artisan ${f}'" + done + ;; + *); echo "Nothing to do";; esac diff --git a/plugins/rx-firewalls/main.sh b/plugins/rx-firewalls/main.sh index 9aeee08..82cecd1 100644 --- a/plugins/rx-firewalls/main.sh +++ b/plugins/rx-firewalls/main.sh @@ -1,25 +1,27 @@ -source "${CWDIR}/.env" - function join { local IFS="$1"; shift; echo "$*"; } # Main -output "info" "Getting addresses" -ipv4=$(curl https://www.cloudflare.com/ips-v4) || output erro "Failed to get IPv4 addresses" -ipv6=$(curl https://www.cloudflare.com/ips-v6) || output erro "Failed to get IPv6 addresses" +echo "info Getting addresses" +ipv4=$(curl -sS https://www.cloudflare.com/ips-v4) || echo "erro Failed to get IPv4 addresses" +ipv6=$(curl -sS https://www.cloudflare.com/ips-v6) || echo "erro Failed to get IPv6 addresses" ip=("${ipv4[@]}" "${ipv6[@]}") -output "info" "Truncating existing list" +echo "info Truncating existing list" truncate -s 0 $FILE -output "info" "Writing new IPs to the list" +echo "info Writing new IPs to the list" for i in ${ip[@]}; do echo "set_real_ip_from ${i};" >> $FILE done echo "real_ip_header X-Forwarded-For;" >> $FILE -output "info" "Reloading nginx" -nginx -t && nginx -s reload || echo "Failed to reload nginx" | output erro -output "info" "Updating DigitalOcean Firewall(s)" +echo "info Reloading nginx" +nginx -s reload || echo "erro Failed to reload nginx" +echo "info Updating DigitalOcean Firewall(s)" DOLIST=() DLETS=() for i in ${ip[@]}; do DOLIST+=(address:${i}) done -snap run doctl compute firewall update ${FWID} --name ${NAME} --droplet-ids $(join , ${DROPLETS[@]}) --inbound-rules protocol:tcp,ports:443,$(join , ${DOLIST[@]}) +if (snap run doctl compute firewall update ${FWID} --name ${NAME} --droplet-ids $(join , ${DROPLETS[@]}) --inbound-rules protocol:tcp,ports:443,$(join , ${DOLIST[@]}) > /dev/null 2>&1); then + echo "done Updated firewall(s) successfully" +else + echo "erro Failed to update firewalls via doctl" +fi diff --git a/plugins/selfupdate/main.sh b/plugins/selfupdate/main.sh deleted file mode 100644 index 8a86592..0000000 --- a/plugins/selfupdate/main.sh +++ /dev/null @@ -1,12 +0,0 @@ -if [ -x "$(command -v git)" ]; then - cd "${DIR}" || exit - output info "Self-updating:" - if (git diff --name-only origin/master | grep -q manager); then - git reset --hard HEAD | output info - git pull https://github.com/RXCommunity/manager master | output info - else - output info "Already latest version!" - fi -else - output erro "You do not have GIT installed!" -fi diff --git a/plugins/service/main.sh b/plugins/service/main.sh index 2922113..4525322 100644 --- a/plugins/service/main.sh +++ b/plugins/service/main.sh @@ -1,35 +1,35 @@ servicelist=$(systemctl list-units --type=service --state=active | grep -oe ".*\.service") mapfile -t services < ${CWDIR}/services.list for i in "${services[@]}"; do - case "$2" 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 erro "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 erro "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 erro "Service $i is stopped!" - fi - ;; - esac + case "$2" in + "start") + if (( $(echo "${servicelist}" | grep -c "$i") > 0 )); then + echo "warn Service $i was already running!" + else + if (systemctl start "$i" > /dev/null 2>&1); then + echo "done Started service $i succesfully!" + else + echo "erro 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 + echo "done Stopped service $i succesfully!" + else + echo "erro Failed to stop service $i!" + fi + else + echo "warn Service $i is already stopped!" + fi + ;; + "status") + if (( $(echo "${servicelist}" | grep -c "$i") > 0 )); then + echo "warn Service $i is up and running!" + else + echo "erro Service $i is stopped!" + fi + ;; + esac done diff --git a/plugins/yagpdb/main.sh b/plugins/yagpdb/main.sh index 2d5931f..b8e44d4 100644 --- a/plugins/yagpdb/main.sh +++ b/plugins/yagpdb/main.sh @@ -1,5 +1,10 @@ +# TODO: More variables and push statsh if encountering problems + cd /root/yagpdb || exit +echo "info Getting latest version" git pull cd ./yagpdb_docker || exit +echo "info Building latest version" docker-compose build --force-rm --no-cache --pull +echo "info Starting bot" docker-compose up -d