contentmonster/classes/vessel.py

103 lines
3.3 KiB
Python
Raw Normal View History

2021-11-20 14:40:07 +00:00
from classes.connection import Connection
2021-11-22 10:14:38 +00:00
from classes.database import Database
from classes.file import File
2021-11-20 14:40:07 +00:00
from paramiko.ssh_exception import SSHException
2021-11-25 15:31:49 +00:00
from configparser import SectionProxy
from typing import Optional, Union
2021-11-22 10:14:38 +00:00
import pathlib
2021-11-25 15:31:49 +00:00
2021-11-20 14:40:07 +00:00
class Vessel:
2021-11-25 15:31:49 +00:00
"""Class describing a Vessel (= a replication destination)
"""
2021-11-20 14:40:07 +00:00
@classmethod
2021-11-25 15:31:49 +00:00
def fromConfig(cls, config: SectionProxy):
"""Create Vessel object from a Vessel section in the Config file
Args:
config (configparser.SectionProxy): Vessel section defining a
Vessel
Raises:
ValueError: Raised if section does not contain Address parameter
Returns:
classes.vessel.Vessel: Vessel object for the vessel specified in
the config section
"""
tempdir = None
2021-11-22 10:14:38 +00:00
if "TempDir" in config.keys():
tempdir = config["TempDir"]
2021-11-25 15:31:49 +00:00
2021-11-20 14:40:07 +00:00
if "Address" in config.keys():
2021-11-25 15:31:49 +00:00
return cls(config.name.split()[1], config["Address"], tempdir)
2021-11-20 14:40:07 +00:00
else:
2021-11-25 15:31:49 +00:00
raise ValueError("Definition for Vessel " +
config.name.split()[1] + " does not contain Address!")
2021-11-20 14:40:07 +00:00
2021-11-25 15:31:49 +00:00
def __init__(self, name: str, address: str, tempdir: Optional[Union[str, pathlib.Path]]) -> None:
"""Initialize new Vessel object
Args:
name (str): Name of the Vessel
address (str): Address (IP or resolvable hostname) of the Vessel
tempdir (pathlib.Path, optional): Temporary upload location on the
Vessel, to store Chunks in
"""
2021-11-20 14:40:07 +00:00
self.name = name
self.address = address
2021-11-25 15:31:49 +00:00
self.tempdir = pathlib.Path(tempdir or "/tmp/.ContentMonster/")
2021-11-20 14:40:07 +00:00
self._connection = None
2021-11-25 15:31:49 +00:00
self._uploaded = self.getUploadedFromDB() # Files already uploaded
2021-11-20 14:40:07 +00:00
@property
2021-11-25 15:31:49 +00:00
def connection(self) -> Connection:
"""Get a Connection to the Vessel
Returns:
classes.connection.Connection: SSH/SFTP connection to the Vessel
"""
# If a connection exists
2021-11-20 14:40:07 +00:00
if self._connection:
try:
2021-11-25 15:31:49 +00:00
# ... check if it is up
2021-11-20 14:40:07 +00:00
self._connection._listdir()
except SSHException:
2021-11-25 15:31:49 +00:00
# ... and throw it away if it isn't
2021-11-20 14:40:07 +00:00
self._connection = None
2021-11-25 15:31:49 +00:00
# If no connection exists (anymore), set up a new one
2021-11-22 10:14:38 +00:00
self._connection = self._connection or Connection(self)
return self._connection
2021-11-25 15:31:49 +00:00
def getUploadedFromDB(self) -> list[str]:
"""Get a list of files that have previously been uploaded to the Vessel
Returns:
list: List of UUIDs of Files that have been successfully uploaded
"""
2021-11-22 10:14:38 +00:00
db = Database()
return db.getCompletionForVessel(self)
2021-11-25 15:31:49 +00:00
def currentUpload(self) -> File:
"""Get the File that is currently being uploaded to this Vessel
Returns:
classes.file.File: File object representing the file currently
being uploaded
"""
2021-11-22 10:14:38 +00:00
db = Database()
2021-11-25 15:31:49 +00:00
directory, name, _ = db.getFileByUUID(
fileuuid := self.connection.getCurrentUploadUUID())
2021-11-22 10:14:38 +00:00
return File(name, directory, fileuuid)
2021-11-20 14:40:07 +00:00
2021-11-25 15:31:49 +00:00
def clearTempDir(self) -> None:
"""Clean up the temporary directory on the Vessel
"""
self.connection.clearTempDir()