Skip to content

Commit

Permalink
Add UNIX wildcards and uuid:<UUID> support
Browse files Browse the repository at this point in the history
Allowing user to select qubes with their UUID and use UNIX like
wildcards for multiple qube selection
  • Loading branch information
alimirjamali committed Oct 22, 2024
1 parent 886364e commit 2af5ee3
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions qubesadmin/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
from __future__ import print_function

import argparse
import fnmatch
import importlib
import logging
import os
import re
import subprocess
import sys
import textwrap
Expand All @@ -37,6 +39,8 @@

#: constant returned when some action should be performed on all qubes
VM_ALL = object()
UUID_REGEX = '^uuid:' \
'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'


class QubesAction(argparse.Action):
Expand Down Expand Up @@ -162,6 +166,12 @@ def parse_qubes_app(self, parser, namespace):
vm.name not in namespace.exclude
]
else:
def uuid_to_domain(uuid: str):
for domain in app.domains:
if domain.uuid == uuid:
return domain
parser.error('no such UUID: {!r}'.format(uuid))

Check warning on line 173 in qubesadmin/tools/__init__.py

View check run for this annotation

Codecov / codecov/patch

qubesadmin/tools/__init__.py#L170-L173

Added lines #L170 - L173 were not covered by tests

if hasattr(namespace, 'exclude') and namespace.exclude:
parser.error('--exclude can only be used with --all')

Expand All @@ -171,11 +181,25 @@ def parse_qubes_app(self, parser, namespace):
vm_name = getattr(namespace, self.dest, None)
if vm_name is not None:
try:
namespace.domains += [app.domains[vm_name]]
if re.match(UUID_REGEX, vm_name):
namespace.domains += [uuid_to_domain(vm_name[5:])]

Check warning on line 185 in qubesadmin/tools/__init__.py

View check run for this annotation

Codecov / codecov/patch

qubesadmin/tools/__init__.py#L185

Added line #L185 was not covered by tests
else:
namespace.domains += [app.domains[vm_name]]
except KeyError:
parser.error('no such domain: {!r}'.format(vm_name))
else:
for vm_name in getattr(namespace, self.dest):
destinations=set()
for destination in getattr(namespace, self.dest, None):
if any(wildcard in destination for wildcard in '*![]'):
for domain in app.domains:
if fnmatch.fnmatch(domain.name, destination):
destinations.add(domain.name)

Check warning on line 196 in qubesadmin/tools/__init__.py

View check run for this annotation

Codecov / codecov/patch

qubesadmin/tools/__init__.py#L194-L196

Added lines #L194 - L196 were not covered by tests
elif re.match(UUID_REGEX, destination):
destinations.add(uuid_to_domain(destination[5:]).name)

Check warning on line 198 in qubesadmin/tools/__init__.py

View check run for this annotation

Codecov / codecov/patch

qubesadmin/tools/__init__.py#L198

Added line #L198 was not covered by tests
else:
destinations.add(destination)

for vm_name in destinations:
try:
namespace.domains += [app.domains[vm_name]]
except KeyError:
Expand Down

0 comments on commit 2af5ee3

Please sign in to comment.