picluster/pictl
Verita84 48111c89c0 fix
2021-08-25 08:44:55 -06:00

178 lines
8.0 KiB
Bash
Executable File

#!/usr/bin/env bash
clear
# Test if jq is installed
if type jq &>/dev/null; then
# Config file
config="config.json"
# Set config parameters to assign to variables
config_token=$(jq .token $config | sed -e 's/\"//g')
config_server_port=$(jq .server_port $config | sed -e 's/\"//g')
config_ssl=$(jq .ssl $config | sed -e 's/\"//g')
config_web_connect=$(jq .web_connect $config | sed -e 's/\"//g')
hb(){
clear
echo "Heartbeat is now checking....."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/hb?token=${config_token}"
else curl -s "http://${config_web_connect}:${config_server_port}/hb?token=${config_token}"
fi
logs
}
nodes(){
clear
echo "Active Nodes:"
if [ "${config_ssl}" == "true" ]; then echo;curl -s "https://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname)"';echo
else echo;curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname)"';echo
fi
}
metrics(){
clear
echo "Nodes Metrics"
if [ "${config_ssl}" == "true" ]; then echo;curl -s "https://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) CPU: \(.cpu_percent) Memory: \(.memory_percentage) Disk: \(.disk_percentage) Running: \(.total_running_containers) "'
else echo;curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) CPU: \(.cpu_percent) Memory: \(.memory_percentage) Disk: \(.disk_percentage) Running: \(.total_running_containers) Network In : \(.network_rx) Network Out : \(.network_tx)"';echo
fi
}
stop(){
clear
echo "Stopping the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=stop" "https://${config_web_connect}:${config_server_port}/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=stop" "http://${config_web_connect}:${config_server_port}/manage"
fi
}
images(){
clear
echo "Gathering a list of images....."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) \(.images)"'
else curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) \(.images)"'
fi
}
addimage(){
clear
if [ "${config_ssl}" == "true" ]; then
echo "Uploading container zip file to Server......."
curl -X POST -F "file=@$1" -F "token=${config_token}" "https://${config_web_connect}:${config_server_port}/receive-file"
echo
else
echo "Uploading container Zip file to Server......."
curl -X POST -F "file=@$1" -F "token=${config_token}" "http://${config_web_connect}:${config_server_port}/receive-file"
echo
fi
}
shell(){
clear
echo "Running the command....."
if [ "${config_ssl}" == "true" ]; then curl -H "Content-Type: application/json" -X POST -d '{"command":"'"$1"'","token":"'"${config_token}"'"}' "https://${config_web_connect}:${config_server_port}/exec"
else curl -H "Content-Type: application/json" -X POST -d '{"command":"'"$1"'","token":"'"${config_token}"'"}' "http://${config_web_connect}:${config_server_port}/exec"
fi
}
clean(){
clear
echo "Cleaning stale Docker images and containers...."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/prune?token=${config_token}"
else curl -H "Content-Type: application/json" -X POST -d '{"token":"'"${config_token}"'"}' "http://${config_web_connect}:${config_server_port}/prune"
fi
}
build() {
clear
echo "Building the container(s). This may take a while......"
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&no_cache=1&operation=build" "https://${config_web_connect}:${config_server_port}/manage-image"
else curl -s -G -d "token=${config_token}&container=$1&no_cache=1&operation=build" "http://${config_web_connect}:${config_server_port}/manage-image"
fi
}
logs(){
sleep 5
clear
echo "Checking the logs....."
if [ "${config_ssl}" == "true" ]; then curl -s "https://${config_web_connect}:${config_server_port}/log?token=${config_token}"
else curl -s "http://${config_web_connect}:${config_server_port}/log?token=${config_token}"
fi
}
create() {
clear
echo "Running the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=create" "https://${config_web_connect}:${config_server_port}/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=create" "http://${config_web_connect}:${config_server_port}/manage"
fi
}
start(){
clear
echo "Starting the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=start" "https://${config_web_connect}:${config_server_port}/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=start" "http://${config_web_connect}:${config_server_port}/manage"
fi
}
restart(){
clear
echo "Restarting the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=restart" "https://${config_web_connect}:${config_server_port}/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=restart" "http://${config_web_connect}:${config_server_port}/manage"
fi
}
changehost(){
clear
echo "Changing the host....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&newhost=$2&container=$1" "https://${config_web_connect}:${config_server_port}/changehost"
else curl -s -G -d "token=${config_token}&newhost=$2&container=$1" "http://${config_web_connect}:${config_server_port}/changehost"
fi
}
delete(){
clear
echo "Deleting the container(s)....."
if [ "${config_ssl}" == "true" ]; then curl -s -G -d "token=${config_token}&container=$1&operation=rm" "https://${config_web_connect}:${config_server_port}/manage"
else curl -s -G -d "token=${config_token}&container=$1&operation=rm" "http://${config_web_connect}:${config_server_port}/manage"
fi
}
status() {
clear
echo "Checking the status....."
if [ "${config_ssl}" == "true" ]; then curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq ".data[].running_containers"
else curl -s "http://${config_web_connect}:${config_server_port}/nodes?token=${config_token}" | jq -r '.data[] | "\(.hostname) \(.running_containers)"'
fi
}
if [ -z "$1" ]; then
echo "Error, no args specified!"
echo
arguments="build metrics addimage nodes images start create status exec start stop clean "
echo "Valid arguments are: $arguments"
echo
exit
fi;
if [ "$1" = "hb" ]; then hb;exit; fi;
if [ "$1" = "exec" ]; then shell "$2";exit; fi;
if [ "$1" = "metrics" ]; then metrics "$2";exit; fi;
if [ "$1" = "status" ]; then echo "Gathering Cluster Status....";status;exit; fi;
if [ "$1" = "nodes" ]; then nodes;exit; fi;
if [ "$1" = "addimage" ]; then addimage "$2";exit; fi;
if [ "$1" = "clean" ]; then clean;exit; fi;
if [ "$1" = "images" ]; then images;exit; fi;
if [ "$1" = "log" ]; then echo "Gathering Logs....";logs;exit; fi;
if [ "$1" = "restart" ]; then restart "$2";exit; fi;
if [ "$1" = "changehost" ]; then changehost "$2" "$3";exit; fi;
if [ "$1" = "delete" ]; then delete "$2";exit; fi;
if [ "$1" = "build" ]; then echo "Building Images....";build "$2"; fi;
if [ "$1" = "start" ]; then echo "Running container(s)....";start "$2"; fi;
if [ "$1" = "stop" ]; then echo "Stopping container(s)....";stop "$2"; fi;
if [ "$1" = "create" ]; then echo "Creating container(s)....";create "$2"; fi;
else
echo "Please install jq"
fi