mirror of
https://github.com/community-scripts/ProxmoxVE.git
synced 2026-02-06 05:13:27 +01:00
* Enhance setup-fork.sh with --full mode and misc/ targeting Added a new --full flag to setup-fork.sh to allow updating all files, not just those in misc/. Updated documentation and usage examples to clarify the new behavior. Improved file search and replacement logic for broader compatibility and flexibility. * add AI.md * fix links in AI.md * Update contribution docs and templates for best practices Refreshed AI.md with new reference scripts, expanded checklist, and improved AI assistant tips. Updated container and install script templates for modern defaults (Debian 13, larger disk, two tags), clarified helper function usage, and improved update/backup patterns. Enhanced JSON template with realistic metadata, new fields, and example notes. * Update fetch_and_deploy_gh_release usage in docs and templates Standardize the usage of fetch_and_deploy_gh_release by specifying all arguments, including mode, version, and target directory, in AI.md and template scripts. This clarifies the function's usage and ensures consistency across documentation and install/update templates. * Revise contribution docs and update CT template Expanded and clarified contribution documentation for forking, local development, cherry-picking, and AI-assisted code generation. Improved explanations for setup-fork.sh, local testing, and PR submission. Enhanced the container script template with detailed comments, clearer update_script structure, and step-by-step guidance for maintainers and contributors. * Update fork and release instructions in contribution docs Replaced placeholder GitHub repo references with 'YourUsername/YourRepo' throughout documentation for clarity. Expanded explanations in FORK_SETUP.md and README.md to clarify the difference between development and production script execution, and emphasized the importance of cherry-picking only relevant files for PRs. Updated install script template examples to use the new repo placeholder. * Update GitHub repo placeholders in docs and templates Replaced 'YourUsername/YourRepo' with 'owner/repo' in documentation and template scripts for consistency and clarity. This change standardizes example usage and reduces confusion for contributors. * Move user submitted guides to guides directory Renamed USER_SUBMITTED_GUIDES.md from docs/contribution to docs/guides for improved documentation organization. * Update contribution docs for improved workflow and clarity Revised multiple documentation files to clarify the recommended development workflow: contributors must test scripts via curl from their GitHub fork (not local bash), use setup-fork.sh for URL rewriting, and submit only new files using cherry-pick. Expanded and modernized install and JSON metadata template guides, emphasizing use of helper functions, resource requirements, and the JSON generator tool. Added detailed step-by-step instructions, best practices, and updated examples throughout. * Update contribution docs for new file structure Updated documentation to reflect the migration of install scripts from install_scripts/ to install/, and JSON metadata from config/ to frontend/public/json/. Adjusted all relevant paths, instructions, and examples to match the new directory structure for improved clarity and consistency. * Update contribution docs for fork setup and metadata Revised documentation to standardize use of 'bash docs/contribution/setup-fork.sh --full' for fork configuration, clarified install script execution flow, and updated JSON metadata template and field references. Improved helper function docs, resource requirements, and category lists. Updated references and instructions throughout for consistency and accuracy. * Docs: add GPU/TUN, update endpoints & tool refs Documentation updates across guides and function references: - Added var_gpu and var_tun configuration entries to CONFIGURATION_REFERENCE (GPU passthrough and TUN/TAP support), including features and prerequisites. - Fixed repository URLs throughout UNATTENDED_DEPLOYMENTS and examples: replaced community-scripts/ProxmoxVED with community-scripts/ProxmoxVE and updated curl usage to the new paths. - Added an "Advanced Configuration Variables" table and examples (var_os, var_version, var_gpu, var_tun, var_nesting) to UNATTENDED_DEPLOYMENTS; adjusted sample apps, hostnames, and container mappings in batch examples. - Switched API endpoints in API_FUNCTIONS_REFERENCE and API_USAGE_EXAMPLES from http://api.community-scripts.org to https://api.community-scripts.org. - Expanded BUILD_FUNC_FUNCTIONS_REFERENCE with container resource/ID management helper descriptions (validate_container_id, get_valid_container_id, maxkeys_check, get_current_ip, update_motd_ip). - Large edits to TOOLS_FUNC_FUNCTIONS_REFERENCE: renamed/refactored helper signatures and docs (pkg_install -> install_packages_with_retry, pkg_update -> upgrade_packages_with_retry), added new tooling functions (fetch_and_deploy_gh_release, check_for_gh_release, prepare_repository_setup, verify_tool_version) and updated examples and feature notes. - Updated vm/README.md to list additional VM scripts (new and reorganized examples). These are documentation-only changes to clarify configuration options, correct links and endpoints, and expand the reference material for tooling and build helpers. * Docs: expand developer/debugging and tools references Add extensive documentation and examples across contribution, guides, templates and tools references. Key changes: - Introduce a Developer Mode & Debugging section (dev_mode flags: trace, keep, pause, breakpoint, logs, dryrun, motd) in CONTRIBUTING.md with usage example. - Provide a standard update_script() pattern and BookStack example in GUIDE.md to clarify update flow (stop services, backup, deploy, restore, migrate, restart). - Add new helper entries (BookLore, KaraKeep) and advanced repository helpers (setup_deb822_repo, prepare_repository_setup, cleanup_tool_keyrings) plus utilities (setup_meilisearch, verify_tool_version) in HELPER_FUNCTIONS.md. - Update install template to suggest PNPM, Java 21 and Meilisearch; update example DB setup notes in AppName-install.sh. - Add var_diagnostics option and switch var_fuse to boolean/toggle wording in CONFIGURATION_REFERENCE.md; clarify privacy and defaults. - Adjust example container definitions in UNATTENDED_DEPLOYMENTS.md (container entries and resource values). - Change storage and flag variables and examples in BUILD_FUNC_USAGE_EXAMPLES.md (ssd-storage, var_fuse/var_tun, etc.). - Expand TOOLS_FUNC_FUNCTIONS_REFERENCE.md with many setup_* function signatures, environment vars, clarified fetch_and_deploy_gh_release modes/parameters, and additional tool docs (nodejs, php, mariadb_db, postgresql_db, java, uv, yq, meilisearch, composer, build tools). These updates improve onboarding, debugging guidance, and operational clarity for contributors and maintainers.
795 lines
17 KiB
Markdown
795 lines
17 KiB
Markdown
# api.func Usage Examples
|
|
|
|
## Overview
|
|
|
|
This document provides practical usage examples for `api.func` functions, covering common scenarios, integration patterns, and best practices.
|
|
|
|
## Basic API Setup
|
|
|
|
### Standard API Initialization
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
# Standard API setup for LXC containers
|
|
|
|
source api.func
|
|
|
|
# Set up diagnostic reporting
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
# Set container parameters
|
|
export CT_TYPE=1
|
|
export DISK_SIZE=8
|
|
export CORE_COUNT=2
|
|
export RAM_SIZE=2048
|
|
export var_os="debian"
|
|
export var_version="12"
|
|
export NSAPP="plex"
|
|
export METHOD="install"
|
|
|
|
# Report installation start
|
|
post_to_api
|
|
|
|
# Your installation code here
|
|
# ... installation logic ...
|
|
|
|
# Report completion
|
|
if [[ $? -eq 0 ]]; then
|
|
post_update_to_api "success" 0
|
|
else
|
|
post_update_to_api "failed" $?
|
|
fi
|
|
```
|
|
|
|
### VM API Setup
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
# API setup for VMs
|
|
|
|
source api.func
|
|
|
|
# Create diagnostics file for VM
|
|
mkdir -p /usr/local/community-scripts
|
|
echo "DIAGNOSTICS=yes" > /usr/local/community-scripts/diagnostics
|
|
|
|
# Set up VM parameters
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
export DISK_SIZE="20G"
|
|
export CORE_COUNT=4
|
|
export RAM_SIZE=4096
|
|
export var_os="ubuntu"
|
|
export var_version="22.04"
|
|
export NSAPP="nextcloud"
|
|
export METHOD="install"
|
|
|
|
# Report VM installation start
|
|
post_to_api_vm
|
|
|
|
# Your VM installation code here
|
|
# ... VM creation logic ...
|
|
|
|
# Report completion
|
|
post_update_to_api "success" 0
|
|
```
|
|
|
|
## Error Description Examples
|
|
|
|
### Basic Error Explanation
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Explain common error codes
|
|
echo "Error 0: '$(get_error_description 0)'"
|
|
echo "Error 1: $(get_error_description 1)"
|
|
echo "Error 127: $(get_error_description 127)"
|
|
echo "Error 200: $(get_error_description 200)"
|
|
echo "Error 255: $(get_error_description 255)"
|
|
```
|
|
|
|
### Error Code Testing
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Test all error codes
|
|
test_error_codes() {
|
|
local codes=(0 1 2 127 128 130 137 139 143 200 203 205 255)
|
|
|
|
for code in "${codes[@]}"; do
|
|
echo "Code $code: $(get_error_description $code)"
|
|
done
|
|
}
|
|
|
|
test_error_codes
|
|
```
|
|
|
|
### Error Handling with Descriptions
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Function with error handling
|
|
run_command_with_error_handling() {
|
|
local command="$1"
|
|
local description="$2"
|
|
|
|
echo "Running: $description"
|
|
|
|
if $command; then
|
|
echo "Success: $description"
|
|
return 0
|
|
else
|
|
local exit_code=$?
|
|
local error_msg=$(get_error_description $exit_code)
|
|
echo "Error $exit_code: $error_msg"
|
|
return $exit_code
|
|
fi
|
|
}
|
|
|
|
# Usage
|
|
run_command_with_error_handling "apt-get update" "Package list update"
|
|
run_command_with_error_handling "nonexistent_command" "Test command"
|
|
```
|
|
|
|
## API Communication Examples
|
|
|
|
### LXC Installation Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Complete LXC installation with API reporting
|
|
install_lxc_with_reporting() {
|
|
local app="$1"
|
|
local ctid="$2"
|
|
|
|
# Set up API reporting
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
export CT_TYPE=1
|
|
export DISK_SIZE=10
|
|
export CORE_COUNT=2
|
|
export RAM_SIZE=2048
|
|
export var_os="debian"
|
|
export var_version="12"
|
|
export NSAPP="$app"
|
|
export METHOD="install"
|
|
|
|
# Report installation start
|
|
post_to_api
|
|
|
|
# Installation process
|
|
echo "Installing $app container (ID: $ctid)..."
|
|
|
|
# Simulate installation
|
|
sleep 2
|
|
|
|
# Check if installation succeeded
|
|
if [[ $? -eq 0 ]]; then
|
|
echo "Installation completed successfully"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
else
|
|
echo "Installation failed"
|
|
post_update_to_api "failed" $?
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Install multiple containers
|
|
install_lxc_with_reporting "plex" "100"
|
|
install_lxc_with_reporting "nextcloud" "101"
|
|
install_lxc_with_reporting "nginx" "102"
|
|
```
|
|
|
|
### VM Installation Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Complete VM installation with API reporting
|
|
install_vm_with_reporting() {
|
|
local app="$1"
|
|
local vmid="$2"
|
|
|
|
# Create diagnostics file
|
|
mkdir -p /usr/local/community-scripts
|
|
echo "DIAGNOSTICS=yes" > /usr/local/community-scripts/diagnostics
|
|
|
|
# Set up API reporting
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
export DISK_SIZE="20G"
|
|
export CORE_COUNT=4
|
|
export RAM_SIZE=4096
|
|
export var_os="ubuntu"
|
|
export var_version="22.04"
|
|
export NSAPP="$app"
|
|
export METHOD="install"
|
|
|
|
# Report VM installation start
|
|
post_to_api_vm
|
|
|
|
# VM installation process
|
|
echo "Installing $app VM (ID: $vmid)..."
|
|
|
|
# Simulate VM creation
|
|
sleep 3
|
|
|
|
# Check if VM creation succeeded
|
|
if [[ $? -eq 0 ]]; then
|
|
echo "VM installation completed successfully"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
else
|
|
echo "VM installation failed"
|
|
post_update_to_api "failed" $?
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Install multiple VMs
|
|
install_vm_with_reporting "nextcloud" "200"
|
|
install_vm_with_reporting "wordpress" "201"
|
|
```
|
|
|
|
## Status Update Examples
|
|
|
|
### Success Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Report successful installation
|
|
report_success() {
|
|
local operation="$1"
|
|
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
echo "Reporting successful $operation"
|
|
post_update_to_api "success" 0
|
|
}
|
|
|
|
# Usage
|
|
report_success "container installation"
|
|
report_success "package installation"
|
|
report_success "service configuration"
|
|
```
|
|
|
|
### Failure Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Report failed installation
|
|
report_failure() {
|
|
local operation="$1"
|
|
local exit_code="$2"
|
|
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
local error_msg=$(get_error_description $exit_code)
|
|
echo "Reporting failed $operation: $error_msg"
|
|
post_update_to_api "failed" $exit_code
|
|
}
|
|
|
|
# Usage
|
|
report_failure "container creation" 200
|
|
report_failure "package installation" 127
|
|
report_failure "service start" 1
|
|
```
|
|
|
|
### Conditional Status Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Conditional status reporting
|
|
report_installation_status() {
|
|
local operation="$1"
|
|
local exit_code="$2"
|
|
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
if [[ $exit_code -eq 0 ]]; then
|
|
echo "Reporting successful $operation"
|
|
post_update_to_api "success" 0
|
|
else
|
|
local error_msg=$(get_error_description $exit_code)
|
|
echo "Reporting failed $operation: $error_msg"
|
|
post_update_to_api "failed" $exit_code
|
|
fi
|
|
}
|
|
|
|
# Usage
|
|
report_installation_status "container creation" 0
|
|
report_installation_status "package installation" 127
|
|
```
|
|
|
|
## Advanced Usage Examples
|
|
|
|
### Batch Installation with API Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Batch installation with comprehensive API reporting
|
|
batch_install_with_reporting() {
|
|
local apps=("plex" "nextcloud" "nginx" "mysql")
|
|
local ctids=(100 101 102 103)
|
|
|
|
# Set up API reporting
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
export CT_TYPE=1
|
|
export DISK_SIZE=8
|
|
export CORE_COUNT=2
|
|
export RAM_SIZE=2048
|
|
export var_os="debian"
|
|
export var_version="12"
|
|
export METHOD="install"
|
|
|
|
local success_count=0
|
|
local failure_count=0
|
|
|
|
for i in "${!apps[@]}"; do
|
|
local app="${apps[$i]}"
|
|
local ctid="${ctids[$i]}"
|
|
|
|
echo "Installing $app (ID: $ctid)..."
|
|
|
|
# Set app-specific parameters
|
|
export NSAPP="$app"
|
|
|
|
# Report installation start
|
|
post_to_api
|
|
|
|
# Simulate installation
|
|
if install_app "$app" "$ctid"; then
|
|
echo "$app installed successfully"
|
|
post_update_to_api "success" 0
|
|
((success_count++))
|
|
else
|
|
echo "$app installation failed"
|
|
post_update_to_api "failed" $?
|
|
((failure_count++))
|
|
fi
|
|
|
|
echo "---"
|
|
done
|
|
|
|
echo "Batch installation completed: $success_count successful, $failure_count failed"
|
|
}
|
|
|
|
# Mock installation function
|
|
install_app() {
|
|
local app="$1"
|
|
local ctid="$2"
|
|
|
|
# Simulate installation
|
|
sleep 1
|
|
|
|
# Simulate occasional failures
|
|
if [[ $((RANDOM % 10)) -eq 0 ]]; then
|
|
return 1
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
batch_install_with_reporting
|
|
```
|
|
|
|
### Error Analysis and Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Analyze and report errors
|
|
analyze_and_report_errors() {
|
|
local log_file="$1"
|
|
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
if [[ ! -f "$log_file" ]]; then
|
|
echo "Log file not found: $log_file"
|
|
return 1
|
|
fi
|
|
|
|
# Extract error codes from log
|
|
local error_codes=$(grep -o 'exit code [0-9]\+' "$log_file" | grep -o '[0-9]\+' | sort -u)
|
|
|
|
if [[ -z "$error_codes" ]]; then
|
|
echo "No errors found in log"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
fi
|
|
|
|
echo "Found error codes: $error_codes"
|
|
|
|
# Report each unique error
|
|
for code in $error_codes; do
|
|
local error_msg=$(get_error_description $code)
|
|
echo "Error $code: $error_msg"
|
|
post_update_to_api "failed" $code
|
|
done
|
|
}
|
|
|
|
# Usage
|
|
analyze_and_report_errors "/var/log/installation.log"
|
|
```
|
|
|
|
### API Health Check
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Check API connectivity and functionality
|
|
check_api_health() {
|
|
echo "Checking API health..."
|
|
|
|
# Test prerequisites
|
|
if ! command -v curl >/dev/null 2>&1; then
|
|
echo "ERROR: curl not available"
|
|
return 1
|
|
fi
|
|
|
|
# Test error description function
|
|
local test_error=$(get_error_description 127)
|
|
if [[ -z "$test_error" ]]; then
|
|
echo "ERROR: Error description function not working"
|
|
return 1
|
|
fi
|
|
|
|
echo "Error description test: $test_error"
|
|
|
|
# Test API connectivity (without sending data)
|
|
local api_url="https://api.community-scripts.org/dev/upload"
|
|
if curl -s --head "$api_url" >/dev/null 2>&1; then
|
|
echo "API endpoint is reachable"
|
|
else
|
|
echo "WARNING: API endpoint not reachable"
|
|
fi
|
|
|
|
echo "API health check completed"
|
|
}
|
|
|
|
check_api_health
|
|
```
|
|
|
|
## Integration Examples
|
|
|
|
### With build.func
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
# Integration with build.func
|
|
|
|
source core.func
|
|
source api.func
|
|
source build.func
|
|
|
|
# Set up API reporting
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
# Container creation with API reporting
|
|
create_container_with_reporting() {
|
|
local app="$1"
|
|
local ctid="$2"
|
|
|
|
# Set container parameters
|
|
export APP="$app"
|
|
export CTID="$ctid"
|
|
export var_hostname="${app}-server"
|
|
export var_os="debian"
|
|
export var_version="12"
|
|
export var_cpu="2"
|
|
export var_ram="2048"
|
|
export var_disk="10"
|
|
export var_net="vmbr0"
|
|
export var_gateway="192.168.1.1"
|
|
export var_ip="192.168.1.$ctid"
|
|
export var_template_storage="local"
|
|
export var_container_storage="local"
|
|
|
|
# Report installation start
|
|
post_to_api
|
|
|
|
# Create container using build.func
|
|
if source build.func; then
|
|
echo "Container $app created successfully"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
else
|
|
echo "Container $app creation failed"
|
|
post_update_to_api "failed" $?
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Create containers
|
|
create_container_with_reporting "plex" "100"
|
|
create_container_with_reporting "nextcloud" "101"
|
|
```
|
|
|
|
### With vm-core.func
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
# Integration with vm-core.func
|
|
|
|
source core.func
|
|
source api.func
|
|
source vm-core.func
|
|
|
|
# Set up VM API reporting
|
|
mkdir -p /usr/local/community-scripts
|
|
echo "DIAGNOSTICS=yes" > /usr/local/community-scripts/diagnostics
|
|
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
# VM creation with API reporting
|
|
create_vm_with_reporting() {
|
|
local app="$1"
|
|
local vmid="$2"
|
|
|
|
# Set VM parameters
|
|
export APP="$app"
|
|
export VMID="$vmid"
|
|
export var_hostname="${app}-vm"
|
|
export var_os="ubuntu"
|
|
export var_version="22.04"
|
|
export var_cpu="4"
|
|
export var_ram="4096"
|
|
export var_disk="20"
|
|
|
|
# Report VM installation start
|
|
post_to_api_vm
|
|
|
|
# Create VM using vm-core.func
|
|
if source vm-core.func; then
|
|
echo "VM $app created successfully"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
else
|
|
echo "VM $app creation failed"
|
|
post_update_to_api "failed" $?
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Create VMs
|
|
create_vm_with_reporting "nextcloud" "200"
|
|
create_vm_with_reporting "wordpress" "201"
|
|
```
|
|
|
|
### With error_handler.func
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
# Integration with error_handler.func
|
|
|
|
source core.func
|
|
source error_handler.func
|
|
source api.func
|
|
|
|
# Enhanced error handling with API reporting
|
|
enhanced_error_handler() {
|
|
local exit_code=${1:-$?}
|
|
local command=${2:-${BASH_COMMAND:-unknown}}
|
|
|
|
# Get error description from api.func
|
|
local error_msg=$(get_error_description $exit_code)
|
|
|
|
# Display error information
|
|
echo "Error $exit_code: $error_msg"
|
|
echo "Command: $command"
|
|
|
|
# Report error to API
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
post_update_to_api "failed" $exit_code
|
|
|
|
# Use standard error handler
|
|
error_handler $exit_code $command
|
|
}
|
|
|
|
# Set up enhanced error handling
|
|
trap 'enhanced_error_handler' ERR
|
|
|
|
# Test enhanced error handling
|
|
nonexistent_command
|
|
```
|
|
|
|
## Best Practices Examples
|
|
|
|
### Comprehensive API Integration
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
# Comprehensive API integration example
|
|
|
|
source core.func
|
|
source api.func
|
|
|
|
# Set up comprehensive API reporting
|
|
setup_api_reporting() {
|
|
# Enable diagnostics
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
# Set common parameters
|
|
export CT_TYPE=1
|
|
export DISK_SIZE=8
|
|
export CORE_COUNT=2
|
|
export RAM_SIZE=2048
|
|
export var_os="debian"
|
|
export var_version="12"
|
|
export METHOD="install"
|
|
|
|
echo "API reporting configured"
|
|
}
|
|
|
|
# Installation with comprehensive reporting
|
|
install_with_comprehensive_reporting() {
|
|
local app="$1"
|
|
local ctid="$2"
|
|
|
|
# Set up API reporting
|
|
setup_api_reporting
|
|
export NSAPP="$app"
|
|
|
|
# Report installation start
|
|
post_to_api
|
|
|
|
# Installation process
|
|
echo "Installing $app..."
|
|
|
|
# Simulate installation steps
|
|
local steps=("Downloading" "Installing" "Configuring" "Starting")
|
|
for step in "${steps[@]}"; do
|
|
echo "$step $app..."
|
|
sleep 1
|
|
done
|
|
|
|
# Check installation result
|
|
if [[ $? -eq 0 ]]; then
|
|
echo "$app installation completed successfully"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
else
|
|
echo "$app installation failed"
|
|
post_update_to_api "failed" $?
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Install multiple applications
|
|
apps=("plex" "nextcloud" "nginx" "mysql")
|
|
ctids=(100 101 102 103)
|
|
|
|
for i in "${!apps[@]}"; do
|
|
install_with_comprehensive_reporting "${apps[$i]}" "${ctids[$i]}"
|
|
echo "---"
|
|
done
|
|
```
|
|
|
|
### Error Recovery with API Reporting
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# Error recovery with API reporting
|
|
retry_with_api_reporting() {
|
|
local operation="$1"
|
|
local max_attempts=3
|
|
local attempt=1
|
|
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
|
|
while [[ $attempt -le $max_attempts ]]; do
|
|
echo "Attempt $attempt of $max_attempts: $operation"
|
|
|
|
if $operation; then
|
|
echo "Operation succeeded on attempt $attempt"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
else
|
|
local exit_code=$?
|
|
local error_msg=$(get_error_description $exit_code)
|
|
echo "Attempt $attempt failed: $error_msg"
|
|
|
|
post_update_to_api "failed" $exit_code
|
|
|
|
((attempt++))
|
|
|
|
if [[ $attempt -le $max_attempts ]]; then
|
|
echo "Retrying in 5 seconds..."
|
|
sleep 5
|
|
fi
|
|
fi
|
|
done
|
|
|
|
echo "Operation failed after $max_attempts attempts"
|
|
return 1
|
|
}
|
|
|
|
# Usage
|
|
retry_with_api_reporting "apt-get update"
|
|
retry_with_api_reporting "apt-get install -y package"
|
|
```
|
|
|
|
### API Reporting with Logging
|
|
|
|
```bash
|
|
#!/usr/bin/env bash
|
|
source api.func
|
|
|
|
# API reporting with detailed logging
|
|
install_with_logging_and_api() {
|
|
local app="$1"
|
|
local log_file="/var/log/${app}_installation.log"
|
|
|
|
# Set up API reporting
|
|
export DIAGNOSTICS="yes"
|
|
export RANDOM_UUID="$(uuidgen)"
|
|
export NSAPP="$app"
|
|
|
|
# Start logging
|
|
exec > >(tee -a "$log_file")
|
|
exec 2>&1
|
|
|
|
echo "Starting $app installation at $(date)"
|
|
|
|
# Report installation start
|
|
post_to_api
|
|
|
|
# Installation process
|
|
echo "Installing $app..."
|
|
|
|
# Simulate installation
|
|
if install_app "$app"; then
|
|
echo "$app installation completed successfully at $(date)"
|
|
post_update_to_api "success" 0
|
|
return 0
|
|
else
|
|
local exit_code=$?
|
|
local error_msg=$(get_error_description $exit_code)
|
|
echo "$app installation failed at $(date): $error_msg"
|
|
post_update_to_api "failed" $exit_code
|
|
return $exit_code
|
|
fi
|
|
}
|
|
|
|
# Mock installation function
|
|
install_app() {
|
|
local app="$1"
|
|
echo "Installing $app..."
|
|
sleep 2
|
|
return 0
|
|
}
|
|
|
|
# Install with logging and API reporting
|
|
install_with_logging_and_api "plex"
|
|
```
|