Skip to content

Commit

Permalink
feat: add --sort-by flag
Browse files Browse the repository at this point in the history
  • Loading branch information
paullaffitte authored and lcaflc committed Nov 19, 2024
1 parent 81df66a commit 0a0cf4d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/pvecontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _parser():

# nodelist parser
parser_nodelist = subparsers.add_parser('nodelist', help='List nodes in the cluster')
parser_nodelist.add_argument('--sort-by', action='store', help="Key used to sort items", default="node")
parser_nodelist.set_defaults(func=node.action_nodelist)
# nodeevacuate parser
parser_nodeevacuate = subparsers.add_parser('nodeevacuate', help='Evacuate an host by migrating all VMs')
Expand All @@ -45,6 +46,7 @@ def _parser():

# vmlist parser
parser_vmlist = subparsers.add_parser('vmlist', help='List VMs in the cluster')
parser_vmlist.add_argument('--sort-by', action='store', help="Key used to sort items", default="vmid")
parser_vmlist.set_defaults(func=vm.action_vmlist)
# vmmigrate parser
parser_vmmigrate = subparsers.add_parser('vmmigrate', help='Migrate VMs in the cluster')
Expand All @@ -58,6 +60,7 @@ def _parser():

# tasklist parser
parser_tasklist = subparsers.add_parser('tasklist', help='List tasks')
parser_tasklist.add_argument('--sort-by', action='store', help="Key used to sort items", default="starttime")
parser_tasklist.set_defaults(func=task.action_tasklist)
# taskget parser
parser_taskget = subparsers.add_parser('taskget', help='Get task detail')
Expand Down
2 changes: 1 addition & 1 deletion src/pvecontrol/actions/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def action_nodelist(proxmox, args):
"""List proxmox nodes in the cluster using proxmoxer api"""
nodes = [ filter_keys(n.__dict__, ['node', 'status', 'allocatedcpu', 'maxcpu', 'mem', 'allocatedmem', 'maxmem']) for n in proxmox.nodes ]
print_tableoutput(nodes, sortby='node')
print_tableoutput(nodes, args.sort_by)

def action_nodeevacuate(proxmox, args):
"""Evacuate a node by migrating all it's VM out"""
Expand Down
2 changes: 1 addition & 1 deletion src/pvecontrol/actions/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def action_tasklist(proxmox, args):
tasks = [ filter_keys(t.__dict__, ['upid', 'exitstatus', 'node', 'type', 'starttime', 'endtime', 'runningstatus', 'description']) for t in proxmox.tasks ]
print_tableoutput(tasks, sortby='starttime')
print_tableoutput(tasks, sortby=args.sort_by)

def action_taskget(proxmox, args):
print_task(proxmox, args.upid, args.follow, args.wait, show_logs = True)
2 changes: 1 addition & 1 deletion src/pvecontrol/actions/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def action_vmmigrate(proxmox, args):
def action_vmlist(proxmox, args):
"""List VMs in the Proxmox Cluster"""
vms = [ filter_keys(n.__dict__, ['vmid', 'name', 'status', 'node', 'cpus', 'maxmem', 'maxdisk']) for n in proxmox.vms() ]
print_tableoutput(vms, sortby='vmid')
print_tableoutput(vms, sortby=args.sort_by)
10 changes: 7 additions & 3 deletions src/pvecontrol/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
from prettytable import PrettyTable
from collections import OrderedDict
from humanize import naturalsize
from enum import Enum


# Pretty output a table from a table of dicts
# We assume all dicts have the same keys and are sorted by key
def print_tableoutput(table, sortby=None, filter=[]):
x = PrettyTable()
x.align = 'l'
x.field_names = table[0].keys()
x.field_names = [*table[0].keys(), "sortby"]
for line in table:
sort_data = line[sortby]
if isinstance(sort_data, Enum):
sort_data = str(sort_data)
for key in ['mem', 'allocatedmem', 'maxmem', 'disk', 'allocateddisk', 'maxdisk'] :
if key in line:
line[key] = naturalsize(line[key], binary=True)
x.add_row( line.values() )
print(x.get_string(sortby=sortby))
x.add_row( [*line.values(), sort_data] )
print(x.get_string(sortby="sortby", fields=table[0].keys()))

def filter_keys(input, keys):
# Filter keys from input dict
Expand Down

0 comments on commit 0a0cf4d

Please sign in to comment.