Merged PR 590: Backport 3.3.1 fixes into Core
this was already reviewed as part of 3.3.1 release process
This commit is contained in:
@@ -30,6 +30,11 @@ class FileSystem(SimComponent):
|
||||
num_file_deletions: int = 0
|
||||
"Number of file deletions in the current step."
|
||||
|
||||
_default_folder_scan_duration: Optional[int] = None
|
||||
"Override default scan duration for folders"
|
||||
_default_folder_restore_duration: Optional[int] = None
|
||||
"Override default restore duration for folders"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
# Ensure a default root folder
|
||||
@@ -258,6 +263,11 @@ class FileSystem(SimComponent):
|
||||
name=folder.name, request_type=RequestType(func=folder._request_manager)
|
||||
)
|
||||
self.folders[folder.uuid] = folder
|
||||
# set the folder scan and restore durations.
|
||||
if self._default_folder_scan_duration is not None:
|
||||
folder.scan_duration = self._default_folder_scan_duration
|
||||
if self._default_folder_restore_duration is not None:
|
||||
folder.restore_duration = self._default_folder_restore_duration
|
||||
return folder
|
||||
|
||||
def delete_folder(self, folder_name: str) -> bool:
|
||||
|
||||
@@ -43,6 +43,9 @@ def convert_size(size_bytes: int) -> str:
|
||||
class FileSystemItemHealthStatus(Enum):
|
||||
"""Status of the FileSystemItem."""
|
||||
|
||||
NONE = 0
|
||||
"""File system item health status is not known."""
|
||||
|
||||
GOOD = 1
|
||||
"""File/Folder is OK."""
|
||||
|
||||
@@ -72,7 +75,7 @@ class FileSystemItemABC(SimComponent):
|
||||
health_status: FileSystemItemHealthStatus = FileSystemItemHealthStatus.GOOD
|
||||
"Actual status of the current FileSystemItem"
|
||||
|
||||
visible_health_status: FileSystemItemHealthStatus = FileSystemItemHealthStatus.GOOD
|
||||
visible_health_status: FileSystemItemHealthStatus = FileSystemItemHealthStatus.NONE
|
||||
"Visible status of the current FileSystemItem"
|
||||
|
||||
previous_hash: Optional[str] = None
|
||||
|
||||
@@ -46,7 +46,7 @@ class Folder(FileSystemItemABC):
|
||||
:param sys_log: The SysLog instance to us to create system logs.
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self._scanned_this_step: bool = False
|
||||
self.sys_log.info(f"Created file /{self.name} (id: {self.uuid})")
|
||||
|
||||
def _init_request_manager(self) -> RequestManager:
|
||||
@@ -83,6 +83,7 @@ class Folder(FileSystemItemABC):
|
||||
state = super().describe_state()
|
||||
state["files"] = {file.name: file.describe_state() for uuid, file in self.files.items()}
|
||||
state["deleted_files"] = {file.name: file.describe_state() for uuid, file in self.deleted_files.items()}
|
||||
state["scanned_this_step"] = self._scanned_this_step
|
||||
return state
|
||||
|
||||
def show(self, markdown: bool = False):
|
||||
@@ -135,7 +136,7 @@ class Folder(FileSystemItemABC):
|
||||
def pre_timestep(self, timestep: int) -> None:
|
||||
"""Apply pre-timestep logic."""
|
||||
super().pre_timestep(timestep)
|
||||
|
||||
self._scanned_this_step = False
|
||||
for file in self.files.values():
|
||||
file.pre_timestep(timestep)
|
||||
|
||||
@@ -148,9 +149,17 @@ class Folder(FileSystemItemABC):
|
||||
for file_id in self.files:
|
||||
file = self.get_file_by_id(file_uuid=file_id)
|
||||
file.scan()
|
||||
if file.visible_health_status == FileSystemItemHealthStatus.CORRUPT:
|
||||
self.health_status = FileSystemItemHealthStatus.CORRUPT
|
||||
# set folder health to worst file's health by generating a list of file healths. If no files, use 0
|
||||
self.health_status = FileSystemItemHealthStatus(
|
||||
max(
|
||||
[f.health_status.value for f in self.files.values()]
|
||||
or [
|
||||
0,
|
||||
]
|
||||
)
|
||||
)
|
||||
self.visible_health_status = self.health_status
|
||||
self._scanned_this_step = True
|
||||
|
||||
def _reveal_to_red_timestep(self) -> None:
|
||||
"""Apply reveal to red timestep."""
|
||||
|
||||
@@ -118,6 +118,7 @@ class FTPClient(FTPServiceABC, identifier="FTPClient"):
|
||||
session_id: Optional[str] = None,
|
||||
is_reattempt: Optional[bool] = False,
|
||||
) -> bool:
|
||||
self._active = True
|
||||
"""
|
||||
Connects the client to a given FTP server.
|
||||
|
||||
@@ -174,6 +175,7 @@ class FTPClient(FTPServiceABC, identifier="FTPClient"):
|
||||
:param: is_reattempt: Set to True if attempt to disconnect from FTP Server has been attempted. Default False.
|
||||
:type: is_reattempt: Optional[bool]
|
||||
"""
|
||||
self._active = True
|
||||
# send a disconnect request payload to FTP server
|
||||
payload: FTPPacket = FTPPacket(ftp_command=FTPCommand.QUIT)
|
||||
software_manager: SoftwareManager = self.software_manager
|
||||
@@ -219,6 +221,7 @@ class FTPClient(FTPServiceABC, identifier="FTPClient"):
|
||||
:param: session_id: The id of the session
|
||||
:type: session_id: Optional[str]
|
||||
"""
|
||||
self._active = True
|
||||
# check if the file to transfer exists on the client
|
||||
file_to_transfer: File = self.file_system.get_file(folder_name=src_folder_name, file_name=src_file_name)
|
||||
if not file_to_transfer:
|
||||
@@ -276,6 +279,7 @@ class FTPClient(FTPServiceABC, identifier="FTPClient"):
|
||||
:param: dest_port: The open port of the machine that hosts the FTP Server. Default is Port["FTP"].
|
||||
:type: dest_port: Optional[int]
|
||||
"""
|
||||
self._active = True
|
||||
# check if FTP is currently connected to IP
|
||||
self._connect_to_server(dest_ip_address=dest_ip_address, dest_port=dest_port)
|
||||
|
||||
@@ -327,6 +331,7 @@ class FTPClient(FTPServiceABC, identifier="FTPClient"):
|
||||
This helps prevent an FTP request loop - FTP client and servers can exist on
|
||||
the same node.
|
||||
"""
|
||||
self._active = True
|
||||
if not self._can_perform_action():
|
||||
return False
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@ from abc import ABC
|
||||
from ipaddress import IPv4Address
|
||||
from typing import Dict, Optional
|
||||
|
||||
from pydantic import StrictBool
|
||||
|
||||
from primaite.simulator.file_system.file_system import File
|
||||
from primaite.simulator.network.protocols.ftp import FTPCommand, FTPPacket, FTPStatusCode
|
||||
from primaite.simulator.system.services.service import Service
|
||||
from primaite.simulator.system.services.service import Service, ServiceOperatingState
|
||||
from primaite.utils.validation.port import Port
|
||||
|
||||
|
||||
@@ -16,9 +18,22 @@ class FTPServiceABC(Service, ABC):
|
||||
Contains shared methods between both classes.
|
||||
"""
|
||||
|
||||
_active: StrictBool = False
|
||||
"""Flag that is True on timesteps where service transmits data and False when idle. Used for describe_state."""
|
||||
|
||||
def pre_timestep(self, timestep: int) -> None:
|
||||
"""When a new timestep begins, clear the _active attribute."""
|
||||
self._active = False
|
||||
return super().pre_timestep(timestep)
|
||||
|
||||
def describe_state(self) -> Dict:
|
||||
"""Returns a Dict of the FTPService state."""
|
||||
return super().describe_state()
|
||||
state = super().describe_state()
|
||||
|
||||
# override so that the service is shows as running only if actively transmitting data this timestep
|
||||
if self.operating_state == ServiceOperatingState.RUNNING and not self._active:
|
||||
state["operating_state"] = ServiceOperatingState.STOPPED.value
|
||||
return state
|
||||
|
||||
def _process_ftp_command(self, payload: FTPPacket, session_id: Optional[str] = None, **kwargs) -> FTPPacket:
|
||||
"""
|
||||
@@ -29,6 +44,7 @@ class FTPServiceABC(Service, ABC):
|
||||
:param: session_id: session ID linked to the FTP Packet. Optional.
|
||||
:type: session_id: Optional[str]
|
||||
"""
|
||||
self._active = True
|
||||
if payload.ftp_command is not None:
|
||||
self.sys_log.info(f"Received FTP {payload.ftp_command.name} command.")
|
||||
|
||||
@@ -51,6 +67,7 @@ class FTPServiceABC(Service, ABC):
|
||||
:param: payload: The FTP Packet that contains the file data
|
||||
:type: FTPPacket
|
||||
"""
|
||||
self._active = True
|
||||
try:
|
||||
file_name = payload.ftp_command_args["dest_file_name"]
|
||||
folder_name = payload.ftp_command_args["dest_folder_name"]
|
||||
@@ -106,6 +123,7 @@ class FTPServiceABC(Service, ABC):
|
||||
:param: is_response: is true if the data being sent is in response to a request. Default False.
|
||||
:type: is_response: bool
|
||||
"""
|
||||
self._active = True
|
||||
# send STOR request
|
||||
payload: FTPPacket = FTPPacket(
|
||||
ftp_command=FTPCommand.STOR,
|
||||
@@ -135,6 +153,7 @@ class FTPServiceABC(Service, ABC):
|
||||
:param: payload: The FTP Packet that contains the file data
|
||||
:type: FTPPacket
|
||||
"""
|
||||
self._active = True
|
||||
try:
|
||||
# find the file
|
||||
file_name = payload.ftp_command_args["src_file_name"]
|
||||
@@ -181,6 +200,7 @@ class FTPServiceABC(Service, ABC):
|
||||
|
||||
:return: True if successful, False otherwise.
|
||||
"""
|
||||
self._active = True
|
||||
self.sys_log.info(f"{self.name}: Sending FTP {payload.ftp_command.name} {payload.ftp_command_args}")
|
||||
|
||||
return super().send(
|
||||
|
||||
Reference in New Issue
Block a user