From 6c63b353ea9ce1b2e6e5081e9b087cf07650ac06 Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Wed, 6 May 2020 17:09:33 +0200 Subject: [PATCH] pybind/mgr/snap-schedule: list - print ws separated results This should make it easy to consume for cli tools. Also simplifies some internals. Signed-off-by: Jan Fajerski --- src/pybind/mgr/snap_schedule/fs/schedule.py | 41 ++++++++------------- src/pybind/mgr/snap_schedule/module.py | 11 +++--- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/pybind/mgr/snap_schedule/fs/schedule.py b/src/pybind/mgr/snap_schedule/fs/schedule.py index 50cb0d5e402..4c91ffb4726 100644 --- a/src/pybind/mgr/snap_schedule/fs/schedule.py +++ b/src/pybind/mgr/snap_schedule/fs/schedule.py @@ -122,7 +122,7 @@ class Schedule(object): table_row['pruned_count']) def __str__(self): - return f'''{self.path}: {self.schedule}; {self.retention}''' + return f'''{self.path} {self.schedule} {self.retention}''' CREATE_TABLES = '''CREATE TABLE schedules( id integer PRIMARY KEY ASC, @@ -160,21 +160,17 @@ class Schedule(object): strftime("%s", "now") - strftime("%s", sm.start) > 0 ORDER BY until;''' - GET_SCHEDULES = '''SELECT - s.path, s.subvol, s.rel_path, s.active, - sm.schedule, sm.retention, sm.start, sm.first, sm.last, - sm.last_pruned, sm.created, sm.created_count, sm.pruned_count - FROM schedules s - INNER JOIN schedules_meta sm ON sm.schedule_id = s.id - WHERE s.path = ?''' + PROTO_GET_SCHEDULES = '''SELECT + s.path, s.subvol, s.rel_path, s.active, + sm.schedule, sm.retention, sm.start, sm.first, sm.last, + sm.last_pruned, sm.created, sm.created_count, sm.pruned_count + FROM schedules s + INNER JOIN schedules_meta sm ON sm.schedule_id = s.id + WHERE''' - GET_SCHEDULE = '''SELECT - s.path, s.subvol, s.rel_path, s.active, - sm.schedule, sm.retention, sm.start, sm.first, sm.last, - sm.last_pruned, sm.created, sm.created_count, sm.pruned_count - FROM schedules s - INNER JOIN schedules_meta sm ON sm.schedule_id = s.id - WHERE s.path = ? and sm.start = ? AND sm.repeat = ?''' + GET_SCHEDULES = PROTO_GET_SCHEDULES + ' s.path = ?' + + GET_SCHEDULE = PROTO_GET_SCHEDULES + ' s.path = ? and sm.start = ? AND sm.repeat = ?' @classmethod def get_db_schedules(cls, path, db, fs): @@ -192,22 +188,16 @@ class Schedule(object): else: return None - LIST_SCHEDULES = '''SELECT - s.path, sm.schedule, sm.retention - FROM schedules s - INNER JOIN schedules_meta sm ON sm.schedule_id = s.id - WHERE''' - @classmethod def list_schedules(cls, path, db, fs, recursive): with db: if recursive: - c = db.execute(cls.LIST_SCHEDULES + ' path LIKE ?', + c = db.execute(cls.PROTO_GET_SCHEDULES + ' path LIKE ?', (f'{path}%',)) else: - c = db.execute(cls.LIST_SCHEDULES + ' path = ?', + c = db.execute(cls.PROTO_GET_SCHEDULES + ' path = ?', (f'{path}',)) - return [row for row in c.fetchall()] + return [cls._from_get_query(row, fs) for row in c.fetchall()] INSERT_SCHEDULE = '''INSERT INTO schedules(path, subvol, rel_path, active) @@ -527,7 +517,8 @@ class SnapSchedClient(CephfsClient): return Schedule.list_schedules(path, db, fs, recursive) @updates_schedule_db - def store_snap_schedule(self, fs, sched): + def store_snap_schedule(self, fs, args): + sched = Schedule(*args) log.debug(f'attempting to add schedule {sched}') db = self.get_schedule_db(fs) sched.store_schedule(db) diff --git a/src/pybind/mgr/snap_schedule/module.py b/src/pybind/mgr/snap_schedule/module.py index b7369cc5bc4..3173f024ffb 100644 --- a/src/pybind/mgr/snap_schedule/module.py +++ b/src/pybind/mgr/snap_schedule/module.py @@ -6,10 +6,9 @@ LGPL2.1. See file COPYING. import errno import json import sqlite3 -from .fs.schedule import SnapSchedClient, Schedule +from .fs.schedule import SnapSchedClient from mgr_module import MgrModule, CLIReadCommand, CLIWriteCommand from mgr_util import CephfsConnectionException -from rados import ObjectNotFound from threading import Event @@ -76,7 +75,7 @@ class Module(MgrModule): return e.to_tuple() if not scheds: return errno.ENOENT, '', f'SnapSchedule for {path} not found' - return 0, json.dumps([[sched[1], sched[2]] for sched in scheds]), '' + return 0, '\n'.join([str(sched) for sched in scheds]), '' @CLIWriteCommand('fs snap-schedule add', 'name=path,type=CephString ' @@ -96,9 +95,9 @@ class Module(MgrModule): try: use_fs = fs if fs else self.default_fs abs_path = self.resolve_subvolume_path(fs, subvol, path) - sched = Schedule(abs_path, snap_schedule, retention_policy, - start, use_fs, subvol, path) - self.client.store_snap_schedule(use_fs, sched) + self.client.store_snap_schedule(use_fs, (abs_path, snap_schedule, + retention_policy, start, + use_fs, subvol, path)) suc_msg = f'Schedule set for path {path}' except sqlite3.IntegrityError: existing_scheds = self.client.get_snap_schedules(use_fs, path) -- 2.39.5