2023-09-06 22:01:51 +01:00
|
|
|
import sqlite3
|
2023-09-08 16:50:49 +01:00
|
|
|
from datetime import datetime
|
2023-09-28 12:23:49 +01:00
|
|
|
from ipaddress import IPv4Address
|
2023-09-06 22:01:51 +01:00
|
|
|
from sqlite3 import OperationalError
|
2023-09-06 22:26:23 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Union
|
2023-08-29 13:21:34 +01:00
|
|
|
|
2023-09-06 22:26:23 +01:00
|
|
|
from prettytable import MARKDOWN, PrettyTable
|
2023-09-06 22:01:51 +01:00
|
|
|
|
|
|
|
|
from primaite.simulator.file_system.file_system import File
|
|
|
|
|
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
|
|
|
|
from primaite.simulator.network.transmission.transport_layer import Port
|
|
|
|
|
from primaite.simulator.system.core.software_manager import SoftwareManager
|
2023-09-28 12:23:49 +01:00
|
|
|
from primaite.simulator.system.services.ftp.ftp_client import FTPClient
|
2023-09-08 16:50:49 +01:00
|
|
|
from primaite.simulator.system.services.service import Service, ServiceOperatingState
|
|
|
|
|
from primaite.simulator.system.software import SoftwareHealthState
|
2023-08-25 15:29:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseService(Service):
|
2023-09-06 22:26:23 +01:00
|
|
|
"""
|
|
|
|
|
A class for simulating a generic SQL Server service.
|
|
|
|
|
|
|
|
|
|
This class inherits from the `Service` class and provides methods to manage and query a SQLite database.
|
|
|
|
|
"""
|
|
|
|
|
|
2023-09-08 16:50:49 +01:00
|
|
|
password: Optional[str] = None
|
|
|
|
|
connections: Dict[str, datetime] = {}
|
2023-09-06 11:35:41 +01:00
|
|
|
|
2023-09-28 12:23:49 +01:00
|
|
|
backup_server: IPv4Address = None
|
|
|
|
|
"""IP address of the backup server."""
|
|
|
|
|
|
|
|
|
|
latest_backup_directory: str = None
|
|
|
|
|
"""Directory of latest backup."""
|
|
|
|
|
|
|
|
|
|
latest_backup_file_name: str = None
|
|
|
|
|
"""File name of latest backup."""
|
|
|
|
|
|
2023-09-06 11:35:41 +01:00
|
|
|
def __init__(self, **kwargs):
|
2023-09-08 16:50:49 +01:00
|
|
|
kwargs["name"] = "DatabaseService"
|
2023-09-06 22:01:51 +01:00
|
|
|
kwargs["port"] = Port.POSTGRES_SERVER
|
|
|
|
|
kwargs["protocol"] = IPProtocol.TCP
|
2023-09-06 11:35:41 +01:00
|
|
|
super().__init__(**kwargs)
|
2023-09-06 22:01:51 +01:00
|
|
|
self._db_file: File
|
|
|
|
|
self._create_db_file()
|
2023-09-29 20:14:42 +01:00
|
|
|
self._connect()
|
|
|
|
|
|
|
|
|
|
def _connect(self):
|
2023-09-06 22:01:51 +01:00
|
|
|
self._conn = sqlite3.connect(self._db_file.sim_path)
|
|
|
|
|
self._cursor = self._conn.cursor()
|
|
|
|
|
|
|
|
|
|
def tables(self) -> List[str]:
|
2023-09-06 22:26:23 +01:00
|
|
|
"""
|
|
|
|
|
Get a list of table names present in the database.
|
|
|
|
|
|
|
|
|
|
:return: List of table names.
|
|
|
|
|
"""
|
2023-09-06 22:01:51 +01:00
|
|
|
sql = "SELECT name FROM sqlite_master WHERE type='table' AND name != 'sqlite_sequence';"
|
2023-09-29 20:14:42 +01:00
|
|
|
results = self._process_sql(sql, None)
|
|
|
|
|
if isinstance(results["data"], dict):
|
|
|
|
|
return list(results["data"].keys())
|
|
|
|
|
return []
|
2023-09-06 22:01:51 +01:00
|
|
|
|
|
|
|
|
def show(self, markdown: bool = False):
|
2023-09-06 22:26:23 +01:00
|
|
|
"""
|
|
|
|
|
Prints a list of table names in the database using PrettyTable.
|
|
|
|
|
|
|
|
|
|
:param markdown: Whether to output the table in Markdown format.
|
|
|
|
|
"""
|
2023-09-06 22:01:51 +01:00
|
|
|
table = PrettyTable(["Table"])
|
|
|
|
|
if markdown:
|
|
|
|
|
table.set_style(MARKDOWN)
|
|
|
|
|
table.align = "l"
|
|
|
|
|
table.title = f"{self.file_system.sys_log.hostname} Database"
|
|
|
|
|
for row in self.tables():
|
|
|
|
|
table.add_row([row])
|
|
|
|
|
print(table)
|
|
|
|
|
|
2023-09-28 12:23:49 +01:00
|
|
|
def configure_backup(self, backup_server: IPv4Address):
|
|
|
|
|
"""
|
|
|
|
|
Set up the database backup.
|
|
|
|
|
|
|
|
|
|
:param: backup_server_ip: The IP address of the backup server
|
|
|
|
|
"""
|
|
|
|
|
self.backup_server = backup_server
|
|
|
|
|
|
2023-09-29 20:14:42 +01:00
|
|
|
def backup_database(self) -> bool:
|
2023-10-04 11:33:18 +01:00
|
|
|
"""Create a backup of the database to the configured backup server."""
|
2023-09-28 12:23:49 +01:00
|
|
|
# check if the backup server was configured
|
|
|
|
|
if self.backup_server is None:
|
|
|
|
|
self.sys_log.error(f"{self.name} - {self.sys_log.hostname}: not configured.")
|
|
|
|
|
return False
|
|
|
|
|
|
2023-09-29 20:14:42 +01:00
|
|
|
self._conn.close()
|
2023-09-28 12:23:49 +01:00
|
|
|
|
|
|
|
|
software_manager: SoftwareManager = self.software_manager
|
|
|
|
|
ftp_client_service: FTPClient = software_manager.software["FTPClient"]
|
|
|
|
|
|
|
|
|
|
# send backup copy of database file to FTP server
|
|
|
|
|
response = ftp_client_service.send_file(
|
|
|
|
|
dest_ip_address=self.backup_server,
|
|
|
|
|
src_file_name=self._db_file.name,
|
|
|
|
|
src_folder_name=self._db_file.folder.name,
|
2023-09-29 20:14:42 +01:00
|
|
|
dest_folder_name=str(self.uuid),
|
|
|
|
|
dest_file_name="database.db",
|
|
|
|
|
real_file_path=self._db_file.sim_path,
|
2023-09-28 12:23:49 +01:00
|
|
|
)
|
2023-09-29 20:14:42 +01:00
|
|
|
self._connect()
|
2023-09-28 12:23:49 +01:00
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
self.sys_log.error("Unable to create database backup.")
|
|
|
|
|
return False
|
|
|
|
|
|
2023-09-29 20:14:42 +01:00
|
|
|
def restore_backup(self) -> bool:
|
2023-10-04 11:33:18 +01:00
|
|
|
"""Restore a backup from backup server."""
|
2023-09-28 12:23:49 +01:00
|
|
|
software_manager: SoftwareManager = self.software_manager
|
|
|
|
|
ftp_client_service: FTPClient = software_manager.software["FTPClient"]
|
|
|
|
|
|
|
|
|
|
# retrieve backup file from backup server
|
|
|
|
|
response = ftp_client_service.request_file(
|
2023-09-29 20:14:42 +01:00
|
|
|
src_folder_name=str(self.uuid),
|
|
|
|
|
src_file_name="database.db",
|
2023-09-28 12:23:49 +01:00
|
|
|
dest_folder_name="downloads",
|
|
|
|
|
dest_file_name="database.db",
|
|
|
|
|
dest_ip_address=self.backup_server,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
2023-09-29 20:14:42 +01:00
|
|
|
self._conn.close()
|
2023-09-28 12:23:49 +01:00
|
|
|
# replace db file
|
|
|
|
|
self.file_system.delete_file(folder_name=self.folder.name, file_name="downloads.db")
|
2023-11-03 15:15:18 +00:00
|
|
|
self.file_system.copy_file(
|
2023-09-28 12:23:49 +01:00
|
|
|
src_folder_name="downloads", src_file_name="database.db", dst_folder_name=self.folder.name
|
|
|
|
|
)
|
|
|
|
|
self._db_file = self.file_system.get_file(folder_name=self.folder.name, file_name="database.db")
|
2023-09-29 20:14:42 +01:00
|
|
|
self._connect()
|
2023-09-28 12:23:49 +01:00
|
|
|
|
|
|
|
|
return self._db_file is not None
|
|
|
|
|
|
|
|
|
|
self.sys_log.error("Unable to restore database backup.")
|
|
|
|
|
return False
|
|
|
|
|
|
2023-09-06 22:01:51 +01:00
|
|
|
def _create_db_file(self):
|
2023-09-06 22:26:23 +01:00
|
|
|
"""Creates the Simulation File and sqlite file in the file system."""
|
2023-09-06 22:01:51 +01:00
|
|
|
self._db_file: File = self.file_system.create_file(folder_name="database", file_name="database.db", real=True)
|
|
|
|
|
self.folder = self._db_file.folder
|
|
|
|
|
|
2023-09-08 16:50:49 +01:00
|
|
|
def _process_connect(
|
|
|
|
|
self, session_id: str, password: Optional[str] = None
|
|
|
|
|
) -> Dict[str, Union[int, Dict[str, bool]]]:
|
|
|
|
|
status_code = 500 # Default internal server error
|
|
|
|
|
if self.operating_state == ServiceOperatingState.RUNNING:
|
|
|
|
|
status_code = 503 # service unavailable
|
|
|
|
|
if self.health_state_actual == SoftwareHealthState.GOOD:
|
|
|
|
|
if self.password == password:
|
|
|
|
|
status_code = 200 # ok
|
|
|
|
|
self.connections[session_id] = datetime.now()
|
2023-10-03 14:59:48 +01:00
|
|
|
self.sys_log.info(f"{self.name}: Connect request for {session_id=} authorised")
|
2023-09-08 16:50:49 +01:00
|
|
|
else:
|
|
|
|
|
status_code = 401 # Unauthorised
|
2023-10-03 14:59:48 +01:00
|
|
|
self.sys_log.info(f"{self.name}: Connect request for {session_id=} declined")
|
2023-09-08 16:50:49 +01:00
|
|
|
else:
|
|
|
|
|
status_code = 404 # service not found
|
|
|
|
|
return {"status_code": status_code, "type": "connect_response", "response": status_code == 200}
|
|
|
|
|
|
2023-09-11 16:15:03 +01:00
|
|
|
def _process_sql(self, query: str, query_id: str) -> Dict[str, Union[int, List[Any]]]:
|
2023-09-06 22:26:23 +01:00
|
|
|
"""
|
|
|
|
|
Executes the given SQL query and returns the result.
|
|
|
|
|
|
|
|
|
|
:param query: The SQL query to be executed.
|
|
|
|
|
:return: Dictionary containing status code and data fetched.
|
|
|
|
|
"""
|
2023-09-11 16:15:03 +01:00
|
|
|
self.sys_log.info(f"{self.name}: Running {query}")
|
2023-09-06 22:01:51 +01:00
|
|
|
try:
|
|
|
|
|
self._cursor.execute(query)
|
|
|
|
|
self._conn.commit()
|
|
|
|
|
except OperationalError:
|
|
|
|
|
# Handle the case where the table does not exist.
|
2023-09-11 16:15:03 +01:00
|
|
|
self.sys_log.error(f"{self.name}: Error, query failed")
|
|
|
|
|
return {"status_code": 404, "data": {}}
|
2023-09-08 16:50:49 +01:00
|
|
|
data = []
|
|
|
|
|
description = self._cursor.description
|
|
|
|
|
if description:
|
|
|
|
|
headers = []
|
|
|
|
|
for header in description:
|
|
|
|
|
headers.append(header[0])
|
|
|
|
|
data = self._cursor.fetchall()
|
|
|
|
|
if data and headers:
|
|
|
|
|
data = {row[0]: {header: value for header, value in zip(headers, row)} for row in data}
|
2023-09-11 16:15:03 +01:00
|
|
|
return {"status_code": 200, "type": "sql", "data": data, "uuid": query_id}
|
2023-08-25 15:29:53 +01:00
|
|
|
|
2023-08-29 13:21:34 +01:00
|
|
|
def describe_state(self) -> Dict:
|
2023-08-31 11:32:11 +01:00
|
|
|
"""
|
|
|
|
|
Produce a dictionary describing the current state of this object.
|
|
|
|
|
|
|
|
|
|
Please see :py:meth:`primaite.simulator.core.SimComponent.describe_state` for a more detailed explanation.
|
|
|
|
|
|
|
|
|
|
:return: Current state of this object and child objects.
|
|
|
|
|
:rtype: Dict
|
|
|
|
|
"""
|
2023-08-29 13:21:34 +01:00
|
|
|
return super().describe_state()
|
|
|
|
|
|
2023-09-06 22:01:51 +01:00
|
|
|
def receive(self, payload: Any, session_id: str, **kwargs) -> bool:
|
2023-09-06 22:26:23 +01:00
|
|
|
"""
|
|
|
|
|
Processes the incoming SQL payload and sends the result back.
|
|
|
|
|
|
|
|
|
|
:param payload: The SQL query to be executed.
|
|
|
|
|
:param session_id: The session identifier.
|
2023-09-08 10:15:26 +01:00
|
|
|
:return: True if the Status Code is 200, otherwise False.
|
2023-09-06 22:26:23 +01:00
|
|
|
"""
|
2023-09-08 16:50:49 +01:00
|
|
|
result = {"status_code": 500, "data": []}
|
|
|
|
|
if isinstance(payload, dict) and payload.get("type"):
|
|
|
|
|
if payload["type"] == "connect_request":
|
|
|
|
|
result = self._process_connect(session_id=session_id, password=payload.get("password"))
|
|
|
|
|
elif payload["type"] == "disconnect":
|
|
|
|
|
if session_id in self.connections:
|
|
|
|
|
self.connections.pop(session_id)
|
|
|
|
|
elif payload["type"] == "sql":
|
|
|
|
|
if session_id in self.connections:
|
2023-09-11 16:15:03 +01:00
|
|
|
result = self._process_sql(query=payload["sql"], query_id=payload["uuid"])
|
2023-09-08 16:50:49 +01:00
|
|
|
else:
|
|
|
|
|
result = {"status_code": 401, "type": "sql"}
|
2023-09-08 10:15:26 +01:00
|
|
|
self.send(payload=result, session_id=session_id)
|
2023-09-08 16:50:49 +01:00
|
|
|
return True
|
2023-09-08 10:15:26 +01:00
|
|
|
|
|
|
|
|
def send(self, payload: Any, session_id: str, **kwargs) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
Send a SQL response back down to the SessionManager.
|
|
|
|
|
|
|
|
|
|
:param payload: The SQL query results.
|
|
|
|
|
:param session_id: The session identifier.
|
|
|
|
|
:return: True if the Status Code is 200, otherwise False.
|
|
|
|
|
"""
|
2023-09-06 22:01:51 +01:00
|
|
|
software_manager: SoftwareManager = self.software_manager
|
2023-09-08 10:15:26 +01:00
|
|
|
software_manager.send_payload_to_session_manager(payload=payload, session_id=session_id)
|
2023-09-06 11:35:41 +01:00
|
|
|
|
2023-09-08 10:15:26 +01:00
|
|
|
return payload["status_code"] == 200
|