Skip to content

Commit

Permalink
Generic status --filter #8
Browse files Browse the repository at this point in the history
  • Loading branch information
orgoj committed Apr 9, 2021
1 parent 448763b commit 0034e0b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pyznap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"""


__version__ = '1.6.0o27'
__version__ = '1.6.0o27 '
25 changes: 10 additions & 15 deletions pyznap/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,8 @@ def _main():
dest='print_config', help='only print parsed and processed config')
parser_status.add_argument('--values', action="store",
dest='values', help='coma separated values to print')
parser_status.add_argument('--filter-snap', action="store_const", const=True,
dest='filter_snap', help='show only filesystems to snap')
parser_status.add_argument('--filter-no-snap', action="store_const", const=False,
dest='filter_snap', help='show only filesystems not to snap')
parser_status.add_argument('--filter-clean', action="store_const", const=True,
dest='filter_clean', help='show only filesystems to clean')
parser_status.add_argument('--filter-no-clean', action="store_const", const=False,
dest='filter_clean', help='show only filesystems not to clean')
parser_status.add_argument('--filter-send', action="store_const", const=True,
dest='filter_send', help='show only filesystems to send')
parser_status.add_argument('--filter-no-send', action="store_const", const=False,
dest='filter_send', help='show only filesystems not to send')
parser_status.add_argument('--filter', action="append",
dest='filter_values', help='add filter for col=value')

if len(sys.argv)==1:
parser.print_help(sys.stderr)
Expand Down Expand Up @@ -309,11 +299,16 @@ def _main():
if args.print_config:
print(str(config))
else:
filter_values=None
if args.filter_values:
filter_values = {}
for fv in args.filter_values:
f, v = fv.split('=')
v = {'true': True, 'false': False}.get(v.lower(), v)
filter_values[f] = v
status_config(config, raw=args.status_raw, show_all=args.status_all,
values=tuple(args.values.split(',')) if args.values else None,
filter_snap=args.filter_snap,
filter_clean=args.filter_clean,
filter_send=args.filter_send)
filter_values=filter_values)

zfs.STATS.log()
logger.info('Finished successfully...\n')
Expand Down
20 changes: 8 additions & 12 deletions pyznap/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .process import DatasetBusyError, DatasetNotFoundError


def status_filesystem(filesystem, conf, raw=False, show_all=False, main_fs=False, values=None, filter=None, filter_snap=None, filter_clean=None, filter_send=None):
def status_filesystem(filesystem, conf, raw=False, show_all=False, main_fs=False, values=None, filter=None, filter_values=None):
"""Deletes snapshots of a single filesystem according to conf.
Parameters:
Expand Down Expand Up @@ -59,11 +59,6 @@ def status_filesystem(filesystem, conf, raw=False, show_all=False, main_fs=False
return
manage_snapshots = snap or clean

if filter_snap is not None and snap != filter_snap:
return
if filter_clean is not None and clean != filter_clean:
return

# increase stats count and check excludes in send
if snap:
zfs.STATS.add('snap_count')
Expand All @@ -86,9 +81,6 @@ def status_filesystem(filesystem, conf, raw=False, show_all=False, main_fs=False
else:
dest = None

if filter_send is not None and send != filter_send:
return

if send:
zfs.STATS.add('send_count')

Expand Down Expand Up @@ -150,6 +142,10 @@ def status_filesystem(filesystem, conf, raw=False, show_all=False, main_fs=False
for stype in SNAPSHOT_TYPES:
status[stype] = str(len(snapshots[stype]))+'/'+str(counts[stype])

if filter_values:
for f, v in filter_values.items():
if status[f] != v:
return
# TODO: last/first snapshot timestamp
# TODO: remote uptodate check

Expand All @@ -162,7 +158,7 @@ def status_filesystem(filesystem, conf, raw=False, show_all=False, main_fs=False
logger.log(level, 'STATUS: '+str(status))


def status_config(config, raw=False, show_all=False, values=None, filter_snap=None, filter_clean=None, filter_send=None):
def status_config(config, raw=False, show_all=False, values=None, filter_values=None):
"""Check snapshots status according to strategies given in config. Goes through each config,
opens up ssh connection if necessary and then recursively calls status_filesystem.
Expand Down Expand Up @@ -213,11 +209,11 @@ def status_config(config, raw=False, show_all=False, values=None, filter_snap=No
else:
# status snapshots of parent filesystem - ignore exclude property for top fs
status_filesystem(children[0], conf, main_fs=True, raw=raw, values=values,
filter_snap=filter_snap, filter_clean=filter_clean, filter_send=filter_send)
filter_values=filter_values)
# status snapshots of all children that don't have a separate config entry
for child in children[1:]:
status_filesystem(child, conf, raw=raw, show_all=show_all, values=values,
filter_snap=filter_snap, filter_clean=filter_clean, filter_send=filter_send)
filter_values=filter_values)
finally:
if ssh:
ssh.close()

0 comments on commit 0034e0b

Please sign in to comment.