From 4a7d9552b715056fe94a01047424a52f688e9c0a Mon Sep 17 00:00:00 2001 From: Tobias Date: Sun, 11 Oct 2026 00:06:36 +0000 Subject: [PATCH] Upload files to "/" --- porygon-management.sh | 185 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 porygon-management.sh diff --git a/porygon-management.sh b/porygon-management.sh new file mode 100644 index 0000000..076cfe0 --- /dev/null +++ b/porygon-management.sh @@ -0,0 +1,185 @@ +#!/bin/bash + +#############VARIABLE LAND############# +ROOT=(-e 'ansible_become=false') +HOSTS_FILE="/root/templates/root.ini" +PASSWORD=(-k) +PLAYBOOK=/root/playbooks/master.yml +PORT=22 +ANSIBLE_PORT=() +SERVERS_JSON="/root/servers.json" +LXC_JSON="/root/porygon_lxc_servers.json" +PORYGON_LXC_IP="1.2.3.4" +PORYGON_LXC_KEY="/path/2/key" + + +###########FUNCTIONS############# +intro() { +cat "/root/templates/porygon.txt" +echo "management tool for adding a new server to the porygon monitoring system" +echo "assumes by default the ssh port is 22, root is the user and password auth is employed" +echo -e "run porygon-management -h to see help and other options\n" +} + +main() { +read -p "enter the server's hostname: " HOSTNAME +read -p "enter the server's IP: " IP + +echo "trusting remote ssh host key" +ssh-keyscan -H "$IP" >> /root/.ssh/known_hosts || { +echo "failed to trust remote host key" +exit 1 +} + +check + +file_making + +ansible-playbook -i /root/hosts.ini "$PLAYBOOK" "${PASSWORD[@]}" "${ANSIBLE_PORT[@]}" "${ROOT[@]}" "${SUDO[@]}" "${APPEND[@]}" || { +echo "failed to run ansible playbook" +exit 1 +} + +echo -e "\nansible playbook ran successfully\nporygon monitoring now set up on $HOSTNAME\n\\(^0^)/ LET'S GO\n" + +json_handling + +web_handling + +echo "running scraper.py" +/root/scraper.py + +echo -e "\nall done, all successful\n\\(^0^)/" +TO_ADD=$(sed 's/\$HOSTNAME/'"$HOSTNAME"'/g' /root/templates/main-page-paste.html) +echo -e "place the following into /var/www/porygon/index.html\n\n$TO_ADD" +} + +check() { + +echo -e "\nis this correct?\n +HOSTNAME = "$HOSTNAME" +IP = "$IP" +y/n - q to quit" + +read ANSWER + +case "$ANSWER" in + y) + ;; + n) + main + ;; + q) + echo "exiting...." + exit 0 + ;; + *) + check + ;; +esac + +} + +file_making() { +REPLACE='s/\$IP/'"$IP"'/g; s/\$USER/'"$USER"'/g' +echo "creating hosts.ini file" +sed "$REPLACE" "$HOSTS_FILE" > /root/hosts.ini || { +echo failed to create hosts.ini +exit 1 +} +} + +json_handling() { + +echo "backing up the servers.json file" + +cp "$SERVERS_JSON" "$SERVERS_JSON".old + +echo "updating servers.json" + +jq --arg hostname "$HOSTNAME" \ + --arg ip "$IP" \ + --arg port "$PORT" \ + '. + {($hostname): {"ip": $ip, "port": $port}}' "$SERVERS_JSON" > /tmp/servers.json || { + echo "failed to update servers.json file" + exit 1 +} + +mv /tmp/servers.json "$SERVERS_JSON" + +echo "updating the servers.json file for porygon-lxc" + +jq --arg hostname "$HOSTNAME" \ + --arg ip "$IP" \ + '. + {$hostname: $ip}' \ + "$LXC_JSON" > /tmp/servers.json || { + echo "failed to update porygon lxc json file" + exit 1 +} + + +mv /tmp/servers.json "$LXC_JSON" + +echo "copying over new json to porygon lxc" + +ssh -i "$PORYGON_LXC_KEY" root@"$PORYGON_LXC_IP" 'cp /root/servers.json /root/servers.json.old' +scp -i "$PORYGON_LXC_KEY" "$LXC_JSON" root@"$PORYGON_LXC_IP":/root/servers.json + +} + +web_handling() { +echo "making directory for apache" +[ -d /var/www/porygon/"$HOSTNAME" ] || mkdir /var/www/porygon/"$HOSTNAME" +cp /root/templates/index.html /var/www/porygon/"$HOSTNAME"/ +} + + +######RUNNING INTRO########### + +intro + + +#######DEFINING FLAGS############ +while getopts "udkph" flag; do + case "$flag" in + + u) + read -p "enter the username under which ansible with run: " USER + ROOT=() + HOSTS_FILE="/root/templates/user.ini" + SUDO=(--become --ask-become-pass) + ;; + + d) + echo -e "ansible debug mode chosen\n" + APPEND=(-vvv) + ;; + + k) + read -p "enter the path to the SSH key used to connect: " KEY + PASSWORD=(--private-key "$KEY") + ;; + + p) + read -p "enter the port to connect over ssh with: " PORT + ANSIBLE_PORT=(-e ansible_port="$PORT") + ;; + + h) + echo -e "Options:\n-d - DEBUG - runs ansible with verbose (-vvv) flags\n\ +-h - HELP - displays this help page\n-k - KEY - specifies connecting over ssh with a private key\n\ +-p - PORT - specifies which port to connect over ssh with\n\ +-u - USER - specifies a user to connect over ssh and run the ansible playbook with" + exit 0 + ;; + + *) + echo "ILLEGAL OPTION :D" + exit 1 + ;; + + esac +done + +########RUNNING MAIN########## +main