ZFS_Snapshot_Manager/zfssm_client.py

92 lines
3.3 KiB
Python

import Pyro4
# from argparse import ArgumentParser
## TODO Arguments
from Snapshot import Snapshot
import time
from zfssmd_worker import DEFAULT_REFRESH_INTERVAL
from itertools import product
from Disk import Disk
PYRO_URI = "PYRO:058b7dde9ec53de9235cfc57a07ce17a9eabfce3@./u:/run/zfssmd.sock"
class ZFSSMDClient(object):
"""
CLient class we use to connect to pyro4 daemon to collect information.
"""
def __init__(self, uri=PYRO_URI):
"""
On init we initiate the connection
:param uri: Pyro4 Daemon uri to connect to
"""
self.uri = uri
self.pyro_conn = Pyro4.Proxy(self.uri)
def check_old_timestamp(self, timestamp):
"""
Sanity checking to make sure that the list was refreshed recently.
:param timestamp: Epoch timestamp taken from get_current_list() by self.get_snapshot_list()
:return: False if everything works as intended.
"""
target_interval = DEFAULT_REFRESH_INTERVAL * 2
if time.time() - timestamp <= target_interval:
return False
else:
return True
def get_snapshot_list(self):
"""
Parses list taken from the daemon and runs timestamp sanity check.
:return: Just snapshot string list
"""
response = self.pyro_conn.get_current_list()
timestamp = next(iter(response))
if (self.check_old_timestamp(timestamp)):
print("WARNING! Data is older than supposed to be. Check daemon worker")
slist = next(iter(response.values()))
return slist
def make_snapshot_objects(self):
"""
Creates Snapshot() objects from snapshot string
:return: list of created Snapshot() objects
"""
snapshot_object_list = list()
for snapshot in self.get_snapshot_list():
snapshot_obj = Snapshot(snapshot)
snapshot_object_list.append(snapshot_obj)
return snapshot_object_list
def make_disk_objects(self):
"""
Creates Disk() objects and assigns Snapshot() objects to corresponding Disk().
Makes sure that there are no duplicate disks and that snapshots are correctly assigned.
:return: list of created Disk() objects
"""
## TODO avoid this fucking many of loops
snapshots = self.make_snapshot_objects()
disk_list=set()
disk_object_list=list()
for snapshot in snapshots:
disk_list.add(snapshot.disk_name)
for disk in disk_list:
disk_obj = Disk(disk)
disk_object_list.append(disk_obj)
for snapshot in snapshots:
disk_obj.add_snapshot(snapshot)
return disk_object_list
def list_snapshots(self):
"""
Just a helper function to get all snapshot parameters
:return: dict of snapshots where key is the name of the snapshot and value is a list of snapshot properties.
"""
snapshot_dict = dict()
for o in self.make_snapshot_objects():
name = o.get_snapshot_name()
zvol = o.get_snapshot_zvol()
ctime = o.get_snapshot_creation_time()
used = o.get_snapshot_used_size()
ref = o.get_snapshot_referenced_size()
snapshot_dict.update ({name:[zvol,ctime,used,ref]})
return snapshot_dict