]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
pybind/mgr/snap-schedule: list - print ws separated results
authorJan Fajerski <jfajerski@suse.com>
Wed, 6 May 2020 15:09:33 +0000 (17:09 +0200)
committerJan Fajerski <jfajerski@suse.com>
Thu, 27 Aug 2020 13:55:46 +0000 (15:55 +0200)
This should make it easy to consume for cli tools.
Also simplifies some internals.

Signed-off-by: Jan Fajerski <jfajerski@suse.com>
src/pybind/mgr/snap_schedule/fs/schedule.py
src/pybind/mgr/snap_schedule/module.py

index 50cb0d5e4026d3e1cae93fda503f5ff9104c57e6..4c91ffb4726a45a6cf8cf22ff9c428376221a4ba 100644 (file)
@@ -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)
index b7369cc5bc4bc9f13db0a5f6c8b4cd8b45aee929..3173f024ffbea78a8069f038ce55ac97f04983df 100644 (file)
@@ -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)