diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Server/Admin/Snapshots.py | 62 | ||||
-rw-r--r-- | src/lib/Server/Snapshots/model.py | 20 |
2 files changed, 61 insertions, 21 deletions
diff --git a/src/lib/Server/Admin/Snapshots.py b/src/lib/Server/Admin/Snapshots.py index 5c1b5215b..6efbea9cb 100644 --- a/src/lib/Server/Admin/Snapshots.py +++ b/src/lib/Server/Admin/Snapshots.py @@ -1,16 +1,47 @@ -import Bcfg2.Server.Admin import sys - try: import sqlalchemy, sqlalchemy.orm except: # FIXME should probably do something smarter here for folks without SA pass + +import Bcfg2.Server.Admin import Bcfg2.Server.Snapshots import Bcfg2.Server.Snapshots.model from Bcfg2.Server.Snapshots.model import Snapshot, Client, Metadata, Base, \ Group, Package +def print_table(rows, justify='left', hdr=True, vdelim=" ", padding=1): + """Pretty print a table + + rows - list of rows ([[row 1], [row 2], ..., [row n]]) + hdr - if True the first row is treated as a table header + vdelim - vertical delimiter betwee columns + padding - # of spaces around the longest element in the column + justify - may be left,center,right + """ + hdelim = "=" + justify = {'left':str.ljust, + 'center':str.center, + 'right':str.rjust}[justify.lower()] + + '''calculate column widths (longest item in each column + plus padding on both sides)''' + cols = zip(*rows) + colWidths = [max([len(str(item))+2*padding for \ + item in col]) for col in cols] + borderline = vdelim.join([w*hdelim for w in colWidths]) + + # print out the table + print(borderline) + for row in rows: + print(vdelim.join([justify(str(item), width) for \ + (item, width) in zip(row, colWidths)])) + if hdr: + print(borderline) + hdr = False + print(borderline) + class Snapshots(Bcfg2.Server.Admin.Mode): __shorthelp__ = "Interact with the Snapshots system" __longhelp__ = (__shorthelp__) @@ -37,12 +68,19 @@ class Snapshots(Bcfg2.Server.Admin.Mode): if args[1] in self.q_dispatch: q_obj = self.q_dispatch[args[1]] if q_obj == Client: - print("\nInactive hosts:") - for host in self.session.query(q_obj).filter(q_obj.active == False): - print(" %s" % host.name) - print("\nActive hosts:") - for host in self.session.query(q_obj).filter(q_obj.active == True): - print(" %s" % host.name) + rows = [] + labels = ('Client', 'Active') + for host in \ + self.session.query(q_obj).filter(q_obj.active == False): + rows.append([host.name, 'No']) + for host in \ + self.session.query(q_obj).filter(q_obj.active == True): + rows.append([host.name, 'Yes']) + print_table([labels]+rows, justify='left', hdr=True, vdelim=" ", padding=1) + elif q_obj == Group: + print("Groups:") + for group in self.session.query(q_obj).all(): + print(" %s" % group.name) else: results = self.session.query(q_obj).all() else: @@ -80,9 +118,11 @@ class Snapshots(Bcfg2.Server.Admin.Mode): Snapshot.correct, Snapshot.timestamp).filter(Client.id==Snapshot.client_id)\ .group_by(Client.id) - print "Client\tCorrect\tTime" - print 60* '=' + rows = [] + labels = ('Client', 'Correct', 'Time') for item in q.all(): - print "%s\t%s\t%s" % (item) + cli, cor, time = item + rows.append([cli, cor, time]) + print_table([labels]+rows, justify='left', hdr=True, vdelim=" ", padding=1) else: print "Unknown options: ", args[1:] diff --git a/src/lib/Server/Snapshots/model.py b/src/lib/Server/Snapshots/model.py index ca67d4f2e..e5306da64 100644 --- a/src/lib/Server/Snapshots/model.py +++ b/src/lib/Server/Snapshots/model.py @@ -38,7 +38,7 @@ admin_group = Table('admin_group', Base.metadata, class Client(Uniquer, Base): __tablename__ = 'client' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) name = Column(Unicode(64), unique=True) admins = relation("Administrator", secondary=admin_client, backref='clients') @@ -55,7 +55,7 @@ class Group(Uniquer, Base): class ConnectorKeyVal(Uniquer, Base): __tablename__ = 'connkeyval' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) connector = Column(Unicode(16)) key = Column(Unicode(32)) value = Column(UnicodeText) @@ -98,7 +98,7 @@ class Metadata(Base): class Package(Base, Uniquer): __tablename__ = 'package' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) name = Column(Unicode(24)) type = Column(Unicode(16)) version = Column(Unicode(16)) @@ -122,7 +122,7 @@ class CorrespondenceType(object): class PackageCorrespondence(Base, CorrespondenceType): mtype = Package __tablename__ = 'package_pair' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) start_id = Column(Integer, ForeignKey('package.id')) start = relation(Package, primaryjoin=start_id == Package.id) end_id = Column(Integer, ForeignKey('package.id'), nullable=True) @@ -136,7 +136,7 @@ package_snap = Table('package_snap', Base.metadata, class Service(Base, Uniquer): __tablename__ = 'service' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) name = Column(Unicode(16)) type = Column(Unicode(12)) status = Column(Boolean) @@ -144,13 +144,13 @@ class Service(Base, Uniquer): class ServiceCorrespondence(Base, CorrespondenceType): mtype = Service __tablename__ = 'service_pair' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) start_id = Column(Integer, ForeignKey('service.id')) start = relation(Service, primaryjoin=start_id == Service.id) end_id = Column(Integer, ForeignKey('service.id'), nullable=True) end = relation(Service, primaryjoin=end_id == Service.id) modified = Column(Boolean) - correct = Column(Boolean) + correct = Column(Boolean) service_snap = Table('service_snap', Base.metadata, Column('spair_id', Integer, ForeignKey('service_pair.id')), @@ -158,7 +158,7 @@ service_snap = Table('service_snap', Base.metadata, class File(Base, Uniquer): __tablename__ = 'file' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) name = Column(UnicodeText) type = Column(Unicode(12)) owner = Column(Unicode(12)) @@ -169,7 +169,7 @@ class File(Base, Uniquer): class FileCorrespondence(Base, CorrespondenceType): mtype = File __tablename__ = 'file_pair' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) start_id = Column(Integer, ForeignKey('file.id')) start = relation(File, primaryjoin=start_id == File.id) end_id = Column(Integer, ForeignKey('file.id'), nullable=True) @@ -195,7 +195,7 @@ extra_service_snap = Table('extra_service_snap', Base.metadata, class Action(Base): __tablename__ = 'action' - id = Column(Integer, primary_key=True) + id = Column(Integer, primary_key=True) command = Column(UnicodeText) return_code = Column(Integer) output = Column(UnicodeText) |