from typing import Optional
from prettytable import PrettyTable
# Function to read and extract data from JSON file
def read_json_file(file_path: Path) -> Optional[dict]:
This function is used to read and extract data from a JSON file.
if not file_path.exists(): # Check if file exists using Path methods
print(f"File does not exist: {file_path}")
with file_path.open("r") as file:
print(f"Permission denied: {file_path}")
except json.JSONDecodeError:
print(f"Invalid JSON format in file: {file_path}")
print(f"An error occurred while reading {file_path}: {e}")
def ultrastack_display_account():
This function is used to display the UltraStack
account information in a table format.
json_variables_path = Path("/root/.ansible/logs/wp3_run/latest/variables")
json_branches_path = Path("/root/.ansible/logs/wp3_run/latest/branches")
# Read and extract specific data from the JSON files
variables_data = read_json_file(json_variables_path)
# Read and extract specific data from the JSON files
branch_data = read_json_file(json_branches_path)
table = PrettyTable(["1", "2"])
# Check if data was successfully extracted from variables file
if variables_data is not None:
hostname = variables_data.get("ansible_facts", {}).get("hostname", "")
table.add_row(["VPS", hostname])
# Determine plan name from system memory
mem_mb = int(variables_data.get("ansible_memtotal_mb", ""))
mem_string = f"{mem_mb}MB"
mem_string = f"{int(mem_mb / 1024)}GB"
table.add_row(["Plan", f"UltraStack {mem_string} RAM"])
ip = variables_data.get("inventory_hostname", "")
table.add_row(["IP", ip])
domain = variables_data.get("site_domain", "")
table.add_row(["Domain", domain])
email = variables_data.get("site_email", "")
table.add_row(["E-mail", email])
profile = variables_data.get("nginx_cache_profile", "")
table.add_row(["NGINX Cache Profile", profile])
php = variables_data.get("php_version", "")
table.add_row(["PHP Version", php])
# Check if data was successfully extracted from branches file
if branch_data is not None:
playbook = branch_data.get("wp3-ultrastack-playbook", {}).get("branch")
table.add_row(["Playbook Version", playbook])