Compare commits
3 Commits
bc5bb81330
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 04b289861a | |||
| 925afed7a5 | |||
| 0959d17403 |
65
cvmsentry.py
65
cvmsentry.py
@@ -120,7 +120,7 @@ async def save_image_async(image, filepath, vm_name, vm_data, current_hash):
|
|||||||
|
|
||||||
|
|
||||||
async def connect(vm_obj: dict):
|
async def connect(vm_obj: dict):
|
||||||
log.info(f"Connecting to VM at {vm_obj['ws_url']} with origin {get_origin_from_ws_url(vm_obj['ws_url'])}")
|
log.debug(f"Connecting to VM at {vm_obj['ws_url']} with origin {get_origin_from_ws_url(vm_obj['ws_url'])}")
|
||||||
global vms
|
global vms
|
||||||
global vm_botuser
|
global vm_botuser
|
||||||
fqdn = urlparse(vm_obj["ws_url"]).netloc
|
fqdn = urlparse(vm_obj["ws_url"]).netloc
|
||||||
@@ -136,12 +136,9 @@ async def connect(vm_obj: dict):
|
|||||||
}
|
}
|
||||||
ws_url = vm_obj["ws_url"]
|
ws_url = vm_obj["ws_url"]
|
||||||
log_directory = getattr(config, "log_directory", "./logs")
|
log_directory = getattr(config, "log_directory", "./logs")
|
||||||
# Ensure the log directory exists
|
# Create VM-specific log directory
|
||||||
os.makedirs(log_directory, exist_ok=True)
|
vm_log_directory = os.path.join(log_directory, log_label)
|
||||||
log_file_path = os.path.join(log_directory, f"{log_label}.json")
|
os.makedirs(vm_log_directory, exist_ok=True)
|
||||||
if not os.path.exists(log_file_path):
|
|
||||||
with open(log_file_path, "w") as log_file:
|
|
||||||
log_file.write("{}")
|
|
||||||
|
|
||||||
origin = Origin(vm_obj.get("origin_override", get_origin_from_ws_url(ws_url)))
|
origin = Origin(vm_obj.get("origin_override", get_origin_from_ws_url(ws_url)))
|
||||||
|
|
||||||
@@ -187,7 +184,7 @@ async def connect(vm_obj: dict):
|
|||||||
f"Connected to VM '{log_label}' successfully. Turns enabled: {bool(int(turns_enabled))}, Votes enabled: {bool(int(votes_enabled))}, Uploads enabled: {bool(int(uploads_enabled))}"
|
f"Connected to VM '{log_label}' successfully. Turns enabled: {bool(int(turns_enabled))}, Votes enabled: {bool(int(votes_enabled))}, Uploads enabled: {bool(int(uploads_enabled))}"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
log.error(
|
log.debug(
|
||||||
f"Failed to connect to VM '{log_label}'. Connection status: {connection_status}"
|
f"Failed to connect to VM '{log_label}'. Connection status: {connection_status}"
|
||||||
)
|
)
|
||||||
STATE = CollabVMState.WS_DISCONNECTED
|
STATE = CollabVMState.WS_DISCONNECTED
|
||||||
@@ -240,27 +237,20 @@ async def connect(vm_obj: dict):
|
|||||||
utc_day = utc_now.strftime("%Y-%m-%d")
|
utc_day = utc_now.strftime("%Y-%m-%d")
|
||||||
timestamp = utc_now.isoformat()
|
timestamp = utc_now.isoformat()
|
||||||
|
|
||||||
with open(log_file_path, "r+") as log_file:
|
# Get daily log file path
|
||||||
|
daily_log_path = os.path.join(vm_log_directory, f"{utc_day}.json")
|
||||||
|
|
||||||
|
# Load existing log data or create new
|
||||||
|
if os.path.exists(daily_log_path):
|
||||||
|
with open(daily_log_path, "r") as log_file:
|
||||||
try:
|
try:
|
||||||
log_data = json.load(log_file)
|
log_data = json.load(log_file)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
log_data = {}
|
log_data = []
|
||||||
|
else:
|
||||||
|
log_data = []
|
||||||
|
|
||||||
if utc_day not in log_data:
|
log_data.append(
|
||||||
log_data[utc_day] = []
|
|
||||||
|
|
||||||
# for i in range(0, len(backlog), 2):
|
|
||||||
# backlog_user = backlog[i]
|
|
||||||
# backlog_message = backlog[i + 1]
|
|
||||||
# if not any(entry["message"] == backlog_message and entry["username"] == backlog_user for entry in log_data[utc_day]):
|
|
||||||
# log.info(f"[{vm_name} - {backlog_user} (backlog)]: {backlog_message}")
|
|
||||||
# log_data[utc_day].append({
|
|
||||||
# "timestamp": timestamp,
|
|
||||||
# "username": backlog_user,
|
|
||||||
# "message": backlog_message
|
|
||||||
# })
|
|
||||||
|
|
||||||
log_data[utc_day].append(
|
|
||||||
{
|
{
|
||||||
"type": "chat",
|
"type": "chat",
|
||||||
"timestamp": timestamp,
|
"timestamp": timestamp,
|
||||||
@@ -268,9 +258,9 @@ async def connect(vm_obj: dict):
|
|||||||
"message": message,
|
"message": message,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
log_file.seek(0)
|
|
||||||
|
with open(daily_log_path, "w") as log_file:
|
||||||
json.dump(log_data, log_file, indent=4)
|
json.dump(log_data, log_file, indent=4)
|
||||||
log_file.truncate()
|
|
||||||
|
|
||||||
if config.commands["enabled"] and message.startswith(
|
if config.commands["enabled"] and message.startswith(
|
||||||
config.commands["prefix"]
|
config.commands["prefix"]
|
||||||
@@ -424,16 +414,20 @@ async def connect(vm_obj: dict):
|
|||||||
utc_day = utc_now.strftime("%Y-%m-%d")
|
utc_day = utc_now.strftime("%Y-%m-%d")
|
||||||
timestamp = utc_now.isoformat()
|
timestamp = utc_now.isoformat()
|
||||||
|
|
||||||
with open(log_file_path, "r+") as log_file:
|
# Get daily log file path
|
||||||
|
daily_log_path = os.path.join(vm_log_directory, f"{utc_day}.json")
|
||||||
|
|
||||||
|
# Load existing log data or create new
|
||||||
|
if os.path.exists(daily_log_path):
|
||||||
|
with open(daily_log_path, "r") as log_file:
|
||||||
try:
|
try:
|
||||||
log_data = json.load(log_file)
|
log_data = json.load(log_file)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
log_data = {}
|
log_data = []
|
||||||
|
else:
|
||||||
|
log_data = []
|
||||||
|
|
||||||
if utc_day not in log_data:
|
log_data.append(
|
||||||
log_data[utc_day] = []
|
|
||||||
|
|
||||||
log_data[utc_day].append(
|
|
||||||
{
|
{
|
||||||
"type": "turn",
|
"type": "turn",
|
||||||
"timestamp": timestamp,
|
"timestamp": timestamp,
|
||||||
@@ -442,9 +436,8 @@ async def connect(vm_obj: dict):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
log_file.seek(0)
|
with open(daily_log_path, "w") as log_file:
|
||||||
json.dump(log_data, log_file, indent=4)
|
json.dump(log_data, log_file, indent=4)
|
||||||
log_file.truncate()
|
|
||||||
|
|
||||||
case ["remuser", count, *list]:
|
case ["remuser", count, *list]:
|
||||||
for i in range(int(count)):
|
for i in range(int(count)):
|
||||||
@@ -484,7 +477,7 @@ for vm_dict_label, vm_obj in config.vms.items():
|
|||||||
)
|
)
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
except websockets.exceptions.InvalidStatus as e:
|
except websockets.exceptions.InvalidStatus as e:
|
||||||
log.error(
|
log.debug(
|
||||||
f"Failed to connect to VM '{vm_obj['ws_url']}' with status code: {e}. Reconnecting..."
|
f"Failed to connect to VM '{vm_obj['ws_url']}' with status code: {e}. Reconnecting..."
|
||||||
)
|
)
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|||||||
Reference in New Issue
Block a user