2019-01-06 17:34:02 +01:00
|
|
|
import os
|
2020-01-06 23:01:25 +01:00
|
|
|
import logging
|
2020-03-19 21:16:38 +01:00
|
|
|
import asyncio
|
2019-01-06 21:29:21 +01:00
|
|
|
import backive.config.config as cfg
|
2019-01-06 17:34:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Device:
|
|
|
|
disks_by_uuid = "/dev/disk/by-uuid"
|
|
|
|
|
2020-01-05 13:17:46 +01:00
|
|
|
def __init__(self, name, config=None):
|
|
|
|
self.name = name
|
2019-01-06 21:29:21 +01:00
|
|
|
self.config = config
|
2020-03-19 21:16:38 +01:00
|
|
|
self._mount_dir = None
|
2019-01-06 21:29:21 +01:00
|
|
|
|
2020-01-05 13:17:46 +01:00
|
|
|
@classmethod
|
|
|
|
def instance(cls, name, config=None):
|
2020-01-06 23:01:25 +01:00
|
|
|
logging.debug("Device instance created (%s)", name)
|
2020-01-05 13:17:46 +01:00
|
|
|
return Device(name, config)
|
|
|
|
|
2019-01-06 17:34:02 +01:00
|
|
|
@classmethod
|
2019-01-06 21:29:21 +01:00
|
|
|
def get_list(cls):
|
2019-01-06 17:34:02 +01:00
|
|
|
if os.path.exists(cls.disks_by_uuid):
|
|
|
|
uuids = os.listdir(cls.disks_by_uuid)
|
|
|
|
return uuids
|
|
|
|
return []
|
2019-01-06 21:29:21 +01:00
|
|
|
|
2020-03-19 21:16:38 +01:00
|
|
|
async def mount(self, path):
|
|
|
|
self._mount_dir = os.path.join(path, self.config.get("mountname"))
|
|
|
|
dev_path = os.path.join(self.disks_by_uuid, self.config.get("uuid"))
|
|
|
|
logging.debug("dev: %s ;; mount: %s", dev_path, self._mount_dir)
|
|
|
|
# TODO: use mkdir as indicator for correct access rights (when backive
|
|
|
|
# is run as user!)
|
|
|
|
proc = await asyncio.create_subprocess_shell(
|
|
|
|
"""mkdir -p {mountpoint}
|
|
|
|
sudo mount -v -o users {dev_path} {mountpoint}""".format(
|
|
|
|
mountpoint=self._mount_dir,
|
|
|
|
dev_path=dev_path
|
|
|
|
),
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.PIPE,
|
|
|
|
)
|
|
|
|
stdout, stderr = await proc.communicate()
|
|
|
|
logging.debug("stdout: %s", stdout)
|
|
|
|
# TODO: Also add a touch operation in the target mount if the correct
|
|
|
|
# access rights are given! (when backive is run as user)
|
|
|
|
return True # on success, False on failure
|
|
|
|
|
|
|
|
async def unmount(self):
|
|
|
|
if not self._mount_dir:
|
|
|
|
return
|
|
|
|
proc = await asyncio.create_subprocess_shell(
|
|
|
|
"""sync
|
|
|
|
sudo umount -v %s
|
|
|
|
""" % self._mount_dir,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.PIPE,
|
|
|
|
)
|
|
|
|
stdout, stderr = await proc.communicate()
|
|
|
|
logging.debug("stdout: %s", stdout)
|