Files
PrimAITE/src/primaite/simulator/system/services/database.py

75 lines
2.8 KiB
Python
Raw Normal View History

2023-08-29 13:21:34 +01:00
from typing import Dict
from primaite.simulator.file_system.file_type import FileType
2023-08-25 15:29:53 +01:00
from primaite.simulator.network.hardware.base import Node
from primaite.simulator.system.services.service import Service
class DatabaseService(Service):
"""A generic SQL Server Service."""
def __init__(self, **kwargs):
super().__init__(**kwargs)
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()
@classmethod
def install(cls, node: Node):
2023-08-29 14:15:49 +01:00
def uninstall(self) -> None:
"""
Undo installation procedure.
This method deletes files created when installing the database, and the database folder if it is empty.
"""
super().uninstall()
node: Node = self.parent
node.file_system.delete_file(self.primary_store)
node.file_system.delete_file(self.transaction_log)
if self.secondary_store:
node.file_system.delete_file(self.secondary_store)
if len(self.folder.files) == 0:
node.file_system.delete_folder(self.folder)
2023-08-29 13:21:34 +01:00
def install(self) -> None:
"""Perform first time install on a node, creating necessary files."""
super().install()
assert isinstance(self.parent, Node), "Database install can only happen after the db service is added to a node"
self._setup_files()
2023-08-25 15:29:53 +01:00
2023-08-29 13:21:34 +01:00
def _setup_files(
2023-08-25 15:29:53 +01:00
self,
folder_name: str = "database",
):
"""Set up files that are required by the database on the parent host.
:param folder_name: Name of the folder which will be setup to hold the db files, defaults to "database"
:type folder_name: str, optional
"""
2023-08-28 22:34:20 +01:00
# note that this parent.file_system.create_folder call in the future will be authenticated by using permissions
# handler. This permission will be granted based on service account given to the database service.
2023-08-29 13:21:34 +01:00
self.parent: Node
2023-08-29 14:15:49 +01:00
self.folder = self.parent.file_system.create_folder(folder_name)
self.primary_store = self.parent.file_system.create_file(
"db_primary_store", db_size, FileType.MDF, folder=self.folder
2023-08-29 14:15:49 +01:00
)
self.transaction_log = self.parent.file_system.create_file(
"db_transaction_log", "1", FileType.LDF, folder=self.folder
2023-08-29 14:15:49 +01:00
)
2023-08-25 15:29:53 +01:00
if use_secondary_db_file:
2023-08-29 14:15:49 +01:00
self.secondary_store = self.parent.file_system.create_file(
"db_secondary_store", secondary_db_size, FileType.NDF, folder=self.folder
2023-08-25 15:29:53 +01:00
)
2023-08-29 14:15:49 +01:00
else:
self.secondary_store = None