[API] Masiv update to api (#1990)

* Masiv refactor of the api

* Masiv refactor of the api

* Format page.tsx

* Format page.tsx

* testing

* update build.func

* Change urls to Repo

* Change urls to Repo

* update install.func

* Add post_to_api_vm

* Update api.func

* Update api.func

* Update api.func

* update api.func
This commit is contained in:
Michel Roegl-Brunner 2025-02-04 13:23:20 +01:00 committed by GitHub
parent 0e5f663df5
commit 8129e3cf39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 164 additions and 125 deletions

View File

@ -11,18 +11,16 @@ interface DataModel {
disk_size: number;
core_count: number;
ram_size: number;
verbose: string;
os_type: string;
os_version: string;
hn: string;
disableip6: string;
ssh: string;
tags: string;
nsapp: string;
created_at: string;
method: string;
pve_version: string;
status: string;
error: string;
type: string;
}
@ -36,8 +34,8 @@ const DataFetcher: React.FC = () => {
const [sortConfig, setSortConfig] = useState<{ key: keyof DataModel | null, direction: 'ascending' | 'descending' }>({ key: 'id', direction: 'descending' });
const [itemsPerPage, setItemsPerPage] = useState(25);
const [currentPage, setCurrentPage] = useState(1);
const [interval, setIntervalTime] = useState<number>(10); // Default interval 10 seconds
const [reloadInterval, setReloadInterval] = useState<NodeJS.Timeout | null>(null);
const [showErrorRow, setShowErrorRow] = useState<number | null>(null);
useEffect(() => {
@ -118,38 +116,6 @@ const DataFetcher: React.FC = () => {
const paginatedData = sortedData.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage);
useEffect(() => {
const storedInterval = localStorage.getItem('reloadInterval');
if (storedInterval) {
setIntervalTime(Number(storedInterval));
}
}, []);
useEffect(() => {
if (interval <= 10) {
const newInterval = setInterval(() => {
window.location.reload();
}, 10000);
return () => clearInterval(newInterval);
} else {
const newInterval = setInterval(() => {
window.location.reload();
}, interval * 1000);
}
}, [interval]);
useEffect(() => {
if (interval > 0) {
localStorage.setItem('reloadInterval', interval.toString());
} else {
localStorage.removeItem('reloadInterval');
}
}, [interval]);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error}</p>;
@ -211,19 +177,6 @@ const DataFetcher: React.FC = () => {
/>
<label className="text-sm text-gray-600 mt-1 block">Set a end date</label>
</div>
<div className="mb-4 flex space-x-4">
<div>
<input
type="number"
value={interval}
onChange={e => setIntervalTime(Number(e.target.value))}
className="p-2 border"
placeholder="Interval (seconds)"
/>
<label className="text-sm text-gray-600 mt-1 block">Set reload interval (0 for no reload)</label>
</div>
</div>
</div>
<ApplicationChart data={filteredData} />
<div className="mb-4 flex justify-between items-center">
@ -242,6 +195,7 @@ const DataFetcher: React.FC = () => {
<thead>
<tr>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('status')}>Status</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('type')}>Type</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('nsapp')}>Application</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('os_type')}>OS</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('os_version')}>OS Version</th>
@ -250,6 +204,7 @@ const DataFetcher: React.FC = () => {
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('ram_size')}>RAM Size</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('method')}>Method</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('pve_version')}>PVE Version</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('error')}>Error Message</th>
<th className="px-4 py-2 border-b cursor-pointer" onClick={() => requestSort('created_at')}>Created At</th>
</tr>
</thead>
@ -262,11 +217,18 @@ const DataFetcher: React.FC = () => {
) : item.status === "failed" ? (
"❌"
) : item.status === "installing" ? (
"🔄"
"🔄"
) : (
item.status
)}
</td>
<td className="px-4 py-2 border-b">{item.type === "lxc" ? (
"📦"
) : item.type === "vm" ? (
"🖥️"
) : (
item.type
)}</td>
<td className="px-4 py-2 border-b">{item.nsapp}</td>
<td className="px-4 py-2 border-b">{item.os_type}</td>
<td className="px-4 py-2 border-b">{item.os_version}</td>
@ -275,6 +237,20 @@ const DataFetcher: React.FC = () => {
<td className="px-4 py-2 border-b">{item.ram_size}</td>
<td className="px-4 py-2 border-b">{item.method}</td>
<td className="px-4 py-2 border-b">{item.pve_version}</td>
<td className="px-4 py-2 border-b">
{item.error && item.error !== "none" ? (
showErrorRow === index ? (
<>
{item.error}
<button onClick={() => setShowErrorRow(null)}>{item.error}</button>
</>
) : (
<button onClick={() => setShowErrorRow(index)}>Click to show error</button>
)
) : (
"none"
)}
</td>
<td className="px-4 py-2 border-b">{formatDate(item.created_at)}</td>
</tr>
))}

125
misc/api.func Normal file
View File

@ -0,0 +1,125 @@
# Copyright (c) 2021-2025 community-scripts ORG
# Author: michelroegl-brunner
# License: MIT | https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/LICENSE
post_to_api() {
if ! command -v curl &> /dev/null; then
return
fi
if [ "$DIAGNOSTICS" = "no" ]; then
return
fi
if [ -z "$RANDOM_UUID" ]; then
return
fi
local API_URL="http://api.community-scripts.org/upload"
local pve_version="not found"
pve_version=$(pveversion | awk -F'[/ ]' '{print $2}')
JSON_PAYLOAD=$(cat <<EOF
{
"ct_type": $CT_TYPE,
"type":"lxc",
"disk_size": $DISK_SIZE,
"core_count": $CORE_COUNT,
"ram_size": $RAM_SIZE,
"os_type": "$var_os",
"os_version": "$var_version",
"disableip6": "$DISABLEIP6",
"nsapp": "$NSAPP",
"method": "$METHOD",
"pve_version": "$pve_version",
"status": "installing",
"random_id": "$RANDOM_UUID"
}
EOF
)
RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD") || true
}
post_to_api_vm() {
DIAGNOSTICS=$(grep -i "^DIAGNOSTICS=" /usr/local/community-scripts/diagnostics | awk -F'=' '{print $2}')
if ! command -v curl &> /dev/null; then
return
fi
if [ "$DIAGNOSTICS" = "no" ]; then
return
fi
if [ -z "$RANDOM_UUID" ]; then
return
fi
local API_URL="http://api.community-scripts.org/upload"
local pve_version="not found"
pve_version=$(pveversion | awk -F'[/ ]' '{print $2}')
DISK_SIZE_API=${DISK_SIZE%G}
JSON_PAYLOAD=$(cat <<EOF
{
"ct_type": 2,
"type":"vm",
"disk_size": $DISK_SIZE_API,
"core_count": $CORE_COUNT,
"ram_size": $RAM_SIZE,
"os_type": "$var_os",
"os_version": "$var_version",
"disableip6": "",
"nsapp": "$NSAPP",
"method": "$METHOD",
"pve_version": "$pve_version",
"status": "installing",
"random_id": "$RANDOM_UUID"
}
EOF
)
RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD") || true
}
POST_UPDATE_DONE=false
post_update_to_api() {
if ! command -v curl &> /dev/null; then
return
fi
if [ "$POST_UPDATE_DONE" = true ]; then
return 0
fi
local API_URL="http://api.community-scripts.org/upload/updatestatus"
local status="${1:-failed}"
local error="${2:-unknown}"
JSON_PAYLOAD=$(cat <<EOF
{
"status": "$status",
"error": "$error",
"random_id": "$RANDOM_UUID"
}
EOF
)
RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD") || true
POST_UPDATE_DONE=true
}

View File

@ -13,6 +13,7 @@ variables() {
METHOD="default" # sets the METHOD variable to "default", used for the API call.
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" # generates a random UUID and sets it to the RANDOM_UUID variable.
}
source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)
# This function sets various color variables using ANSI escape codes for formatting text in the terminal.
color() {
@ -791,74 +792,6 @@ advanced_settings() {
fi
}
post_to_api() {
if [ "$DIAGNOSTICS" = "no" ]; then
return 0
fi
local API_URL="http://api.community-scripts.org/upload"
local pve_version="not found"
pve_version=$(pveversion | awk -F'[/ ]' '{print $2}')
JSON_PAYLOAD=$(cat <<EOF
{
"ct_type": $CT_TYPE,
"disk_size": $DISK_SIZE,
"core_count": $CORE_COUNT,
"ram_size": $RAM_SIZE,
"os_type": "$var_os",
"os_version": "$var_version",
"disableip6": "$DISABLEIP6",
"nsapp": "$NSAPP",
"method": "$METHOD",
"pve_version": "$pve_version",
"status": "installing",
"random_id": "$RANDOM_UUID"
}
EOF
)
RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD")
if [ "$RESPONSE" -ne 201 ] && [ "$RESPONSE" -ne 302 ]; then
msg_error "API request failed with HTTP code $RESPONSE"
fi
}
POST_UPDATE_DONE=false
post_update_to_api() {
if [ "$DIAGNOSTICS" = "no" ]; then
return 0
fi
if [ "$POST_UPDATE_DONE" = true ]; then
return 0
fi
local API_URL="http://api.community-scripts.org/upload/updatestatus"
local status="${1:-failed}"
JSON_PAYLOAD=$(cat <<EOF
{
"status": "$status",
"random_id": "$RANDOM_UUID"
}
EOF
)
RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD")
if [ "$RESPONSE" -ne 201 ] && [ "$RESPONSE" -ne 302 ]; then
msg_error "API UPDATE request failed with HTTP code $RESPONSE"
fi
POST_UPDATE_DONE=true
}
diagnostics_check(){
if ! [ -d "/usr/local/community-scripts" ]; then
mkdir -p /usr/local/community-scripts
@ -1091,6 +1024,7 @@ build_container() {
else
export FUNCTIONS_FILE_PATH="$(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/install.func)"
fi
export RANDOM_UUID="$RANDOM_UUID"
export CACHER="$APT_CACHER"
export CACHER_IP="$APT_CACHER_IP"
export tz="$timezone"
@ -1229,10 +1163,10 @@ EOF
systemctl start ping-instances.service
fi
post_update_to_api "done"
post_update_to_api "done" "none"
}
trap 'post_update_to_api "failed"' EXIT
trap 'post_update_to_api "failed"' SIGINT
trap 'post_update_to_api "failed"' SIGTERM
trap 'post_update_to_api "failed" "unknown error"' EXIT
trap 'post_update_to_api "failed" "SIG INTERUPT"' SIGINT
trap 'post_update_to_api "failed" "SIG TERM"' SIGTERM

View File

@ -56,6 +56,7 @@ catch_errors() {
# This function handles errors
error_handler() {
source <(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)
if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID > /dev/null; fi
printf "\e[?25h"
local exit_code="$?"
@ -63,8 +64,11 @@ error_handler() {
local command="$2"
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
echo -e "\n$error_message"
if [[ "$line_number" -eq 23 ]]; then
if [[ "$line_number" -eq 44 ]]; then
echo -e "The silent function has suppressed the error, run the script with verbose mode enabled, which will provide more detailed output.\n"
post_update_to_api "failed" "No error message, script ran in silent mode"
else
post_update_to_api "failed" "${command}"
fi
}