Skip to content

Commit

Permalink
[common] Standardise snap package checks
Browse files Browse the repository at this point in the history
* Add function is_snap_installed, so that this can be used more widely
* This also reduces duplicate code in plugins.
* Add is_snap variable, that can be used if any of the packages is a
  snap; typically it's one of the other.

Signed-off-by: Arif Ali <[email protected]>
  • Loading branch information
arif-ali authored and TurboTurtle committed Jul 2, 2024
1 parent 4b61539 commit d5ed16f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
18 changes: 18 additions & 0 deletions sos/report/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ class Plugin():
cmdtags = {}
filetags = {}
option_list = []
is_snap = False

# Default predicates
predicate = None
Expand Down Expand Up @@ -581,6 +582,11 @@ def __init__(self, commons):
opt.plugin = self.name()
self.options[opt.name] = opt

# Check if any of the packages tuple is a snap
self.is_snap = any(
self.is_snap_installed(pkg) for pkg in list(self.packages)
)

# Initialise the default --dry-run predicate
self.set_predicate(SoSPredicate(self))

Expand Down Expand Up @@ -999,6 +1005,18 @@ def is_installed(self, package_name):
len(self.policy.package_manager.all_pkgs_by_name(package_name)) > 0
)

def is_snap_installed(self, package_name):
"""Is the snap package $package_name installed?
:param package_name: The name of the package to check
:type package_name: ``str``
:returns: ``True`` if the snap package is installed, else ``False``
:rtype: ``bool``
"""
pkg = self.policy.package_manager.pkg_by_name(package_name)
return pkg is not None and pkg['pkg_manager'] == 'snap'

def is_service(self, name):
"""Does the service $name exist on the system?
Expand Down
8 changes: 0 additions & 8 deletions sos/report/plugins/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@ class Grafana(Plugin, IndependentPlugin):
profiles = ('services', 'openstack', 'openstack_controller')

packages = ('grafana',)
is_snap = False

def _is_snap_installed(self):
grafana_pkg = self.policy.package_manager.pkg_by_name('grafana')
if grafana_pkg:
return grafana_pkg['pkg_manager'] == 'snap'
return False

def setup(self):
self.is_snap = self._is_snap_installed()
if self.is_snap:
grafana_cli = "grafana.grafana-cli"
log_path = "/var/snap/grafana/common/data/log/"
Expand Down
8 changes: 0 additions & 8 deletions sos/report/plugins/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@ class Kafka(Plugin, UbuntuPlugin):
profiles = ('services',)
packages = ('charmed-kafka',)
services = ('kafka',)
is_snap = False

def _is_snap_installed(self):
kafka_pkg = self.policy.package_manager.pkg_by_name('charmed-kafka')
if kafka_pkg:
return kafka_pkg['pkg_manager'] == 'snap'
return False

def setup(self):
self.is_snap = self._is_snap_installed()
log_file_pattern = "*.log*" if self.get_option("all_logs") else "*.log"

if self.is_snap:
Expand Down
4 changes: 1 addition & 3 deletions sos/report/plugins/lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class LXD(Plugin, UbuntuPlugin):
services = ('snap.lxd.daemon', 'snap.lxd.activate')

def setup(self):

lxd_pkg = self.policy.package_manager.pkg_by_name('lxd')
if lxd_pkg and lxd_pkg['pkg_manager'] == 'snap':
if self.is_snap:

lxd_pred = SoSPredicate(self, services=['snap.lxd.daemon'],
required={'services': 'all'})
Expand Down
9 changes: 0 additions & 9 deletions sos/report/plugins/maas.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class Maas(Plugin, UbuntuPlugin):
desc='Credentials, or the API key')
]

is_snap = False

def _has_login_options(self):
return self.get_option("url") and self.get_option("credentials") \
and self.get_option("profile-name")
Expand All @@ -61,14 +59,7 @@ def _remote_api_login(self):

return ret['status'] == 0

def _is_snap_installed(self):
maas_pkg = self.policy.package_manager.pkg_by_name('maas')
if maas_pkg:
return maas_pkg['pkg_manager'] == 'snap'
return False

def setup(self):
self.is_snap = self._is_snap_installed()
if self.is_snap:
self.add_cmd_output([
'snap info maas',
Expand Down

0 comments on commit d5ed16f

Please sign in to comment.