Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/sharedra.../account_...
File: ultrastack.py
import json
[0] Fix | Delete
from pathlib import Path
[1] Fix | Delete
from typing import Optional
[2] Fix | Delete
[3] Fix | Delete
from prettytable import PrettyTable
[4] Fix | Delete
[5] Fix | Delete
[6] Fix | Delete
# Function to read and extract data from JSON file
[7] Fix | Delete
def read_json_file(file_path: Path) -> Optional[dict]:
[8] Fix | Delete
"""
[9] Fix | Delete
This function is used to read and extract data from a JSON file.
[10] Fix | Delete
"""
[11] Fix | Delete
try:
[12] Fix | Delete
if not file_path.exists(): # Check if file exists using Path methods
[13] Fix | Delete
print(f"File does not exist: {file_path}")
[14] Fix | Delete
return None
[15] Fix | Delete
with file_path.open("r") as file:
[16] Fix | Delete
return json.load(file)
[17] Fix | Delete
except PermissionError:
[18] Fix | Delete
print(f"Permission denied: {file_path}")
[19] Fix | Delete
except json.JSONDecodeError:
[20] Fix | Delete
print(f"Invalid JSON format in file: {file_path}")
[21] Fix | Delete
except Exception as e:
[22] Fix | Delete
print(f"An error occurred while reading {file_path}: {e}")
[23] Fix | Delete
return None
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
def ultrastack_display_account():
[27] Fix | Delete
"""
[28] Fix | Delete
This function is used to display the UltraStack
[29] Fix | Delete
account information in a table format.
[30] Fix | Delete
"""
[31] Fix | Delete
# Path to the JSON files
[32] Fix | Delete
json_variables_path = Path("/root/.ansible/logs/wp3_run/latest/variables")
[33] Fix | Delete
json_branches_path = Path("/root/.ansible/logs/wp3_run/latest/branches")
[34] Fix | Delete
[35] Fix | Delete
# Read and extract specific data from the JSON files
[36] Fix | Delete
variables_data = read_json_file(json_variables_path)
[37] Fix | Delete
[38] Fix | Delete
# Read and extract specific data from the JSON files
[39] Fix | Delete
branch_data = read_json_file(json_branches_path)
[40] Fix | Delete
[41] Fix | Delete
# Create account Table
[42] Fix | Delete
table = PrettyTable(["1", "2"])
[43] Fix | Delete
table.align["1"] = "r"
[44] Fix | Delete
table.align["2"] = "l"
[45] Fix | Delete
[46] Fix | Delete
# Disable Table Header
[47] Fix | Delete
table.header = False
[48] Fix | Delete
[49] Fix | Delete
# Check if data was successfully extracted from variables file
[50] Fix | Delete
if variables_data is not None:
[51] Fix | Delete
# Hostname
[52] Fix | Delete
hostname = variables_data.get("ansible_facts", {}).get("hostname", "")
[53] Fix | Delete
table.add_row(["VPS", hostname])
[54] Fix | Delete
[55] Fix | Delete
# Determine plan name from system memory
[56] Fix | Delete
mem_mb = int(variables_data.get("ansible_memtotal_mb", ""))
[57] Fix | Delete
if mem_mb < 1024:
[58] Fix | Delete
mem_string = f"{mem_mb}MB"
[59] Fix | Delete
else:
[60] Fix | Delete
mem_string = f"{int(mem_mb / 1024)}GB"
[61] Fix | Delete
table.add_row(["Plan", f"UltraStack {mem_string} RAM"])
[62] Fix | Delete
[63] Fix | Delete
# IP address
[64] Fix | Delete
ip = variables_data.get("inventory_hostname", "")
[65] Fix | Delete
table.add_row(["IP", ip])
[66] Fix | Delete
[67] Fix | Delete
# Domain
[68] Fix | Delete
domain = variables_data.get("site_domain", "")
[69] Fix | Delete
table.add_row(["Domain", domain])
[70] Fix | Delete
[71] Fix | Delete
# E-mail
[72] Fix | Delete
email = variables_data.get("site_email", "")
[73] Fix | Delete
table.add_row(["E-mail", email])
[74] Fix | Delete
[75] Fix | Delete
# Nginx Cache Profile
[76] Fix | Delete
profile = variables_data.get("nginx_cache_profile", "")
[77] Fix | Delete
table.add_row(["NGINX Cache Profile", profile])
[78] Fix | Delete
[79] Fix | Delete
# PHP Version
[80] Fix | Delete
php = variables_data.get("php_version", "")
[81] Fix | Delete
table.add_row(["PHP Version", php])
[82] Fix | Delete
[83] Fix | Delete
# Check if data was successfully extracted from branches file
[84] Fix | Delete
if branch_data is not None:
[85] Fix | Delete
# Build table rows
[86] Fix | Delete
playbook = branch_data.get("wp3-ultrastack-playbook", {}).get("branch")
[87] Fix | Delete
table.add_row(["Playbook Version", playbook])
[88] Fix | Delete
[89] Fix | Delete
# Print table
[90] Fix | Delete
print(table)
[91] Fix | Delete
[92] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function