Skip to content

Commit

Permalink
Merge pull request #149 from ogayot/probe-firmware
Browse files Browse the repository at this point in the history
Add way to probe firmware information
  • Loading branch information
ogayot authored Jul 8, 2024
2 parents e7e9803 + 2454547 commit 83d25c8
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 3 deletions.
8 changes: 6 additions & 2 deletions bin/probert
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def parse_options(argv):
help='Probe storage hardware.')
parser.add_argument('--network', action='store_true',
help='Probe network hardware.')
parser.add_argument("--firmware", action='store_true',
help='Probe firmware')
parser.add_argument('--parallel', action='store_true',
help='Run storage probes in parallel')
return parser.parse_args(argv)
Expand All @@ -46,13 +48,15 @@ async def main():
logger.info("Arguments passed: {}".format(sys.argv))

p = prober.Prober()
probe_opts = [opts.network, opts.storage]
probe_opts = [opts.network, opts.storage, opts.firmware]
if opts.all or not any(probe_opts):
await p.probe_all(parallelize=opts.parallel)
if opts.network:
await p.probe_network()
p.probe_network()
if opts.storage:
await p.probe_storage(parallelize=opts.parallel)
if opts.firmware:
await p.probe_firmware(parallelize=opts.parallel)

results = p.get_results()
print(json.dumps(results, indent=4, sort_keys=True))
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
probert (0.0.21) UNRELEASED; urgency=medium

* Add probert-firmware to probe information about the firmware.

-- Olivier Gayot <[email protected]> Tue, 02 Jul 2024 15:55:59 +0200

probert (0.0.20) groovy; urgency=medium

[ Ryan Harper ]
Expand Down
13 changes: 13 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,16 @@ Description: Hardware probing tool - network probing
and emitting a JSON report.
.
This package contains network probing capability.

Package: probert-firmware
Architecture: all
Depends: probert-common (= ${source:Version}),
dmidecode,
${misc:Depends},
${python3:Depends},
${shlibs:Depends}
Description: Hardware probing tool - firmware probing
This package provides a tool for probing host hardware information
and emitting a JSON report.
.
This package contains firmware probing capability.
1 change: 1 addition & 0 deletions debian/probert-firmware.install
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
usr/lib/python3.*/dist-packages/probert/firmware.py
70 changes: 70 additions & 0 deletions probert/firmware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2024 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Collect information about the firmware. Unlike network and storage, which
support hotplugging, firmware information should not need to be queried
multiple times."""

import logging
import shutil
from typing import Any

from probert.utils import arun


log = logging.getLogger('probert.firmware')


schema = {
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"additionalProperties": False,
"required": ["bios-vendor", "bios-version", "bios-release-date"],
"properties": {
"bios-vendor": {
"type": ["string", "null"],
},
"bios-version": {
"type": ["string", "null"],
},
"bios-release-data": {
"type": ["string", "null"],
},
},
}


class FirmwareProber:
async def _probe_bios_string(self, string: str) -> str | None:
dmidecode = shutil.which("dmidecode")
if dmidecode is None:
log.debug("could not determine bios %s: dmidecode not found",
string)
return None

out = await arun([dmidecode, "--string", string])
if out is None:
log.warning("could not determine bios %s: dmidecode failure",
string)
else:
out = out.strip()
return out

async def probe(self) -> dict[str, Any]:
return {
"bios-vendor": await self._probe_bios_string("bios-vendor"),
"bios-version": await self._probe_bios_string("bios-version"),
"bios-release-date":
await self._probe_bios_string("bios-release-date"),
}
6 changes: 6 additions & 0 deletions probert/prober.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self):

async def probe_all(self, *, parallelize=False):
await self.probe_storage()
await self.probe_firmware()
self.probe_network()

async def probe_storage(self, *, parallelize=False):
Expand All @@ -27,6 +28,11 @@ async def probe_storage(self, *, parallelize=False):
self._results['storage'] = await self._storage.probe(
parallelize=parallelize)

async def probe_firmware(self, *, parallelize=False):
from probert.firmware import FirmwareProber
self._firmware = FirmwareProber()
self._results['firmware'] = await self._firmware.probe()

def probe_network(self):
from probert.network import NetworkProber
self._network = NetworkProber()
Expand Down
8 changes: 7 additions & 1 deletion probert/tests/test_prober.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

from probert.prober import Prober
from probert.firmware import FirmwareProber
from probert.storage import Storage
from probert.network import NetworkProber

Expand Down Expand Up @@ -30,17 +31,22 @@ def test_prober_get_results(self):
p = Prober()
self.assertEqual({}, p.get_results())

@patch.object(FirmwareProber, 'probe')
@patch.object(NetworkProber, 'probe')
@patch.object(Storage, 'probe')
async def test_prober_probe_all_check_results(self, _storage, _network):
async def test_prober_probe_all_check_results(
self, _storage, _network, _firmware):
p = Prober()
results = {
'storage': {'lambic': 99},
'network': {'saison': 99},
'firmware': {'tripel': 99},
}
_storage.return_value = results['storage']
_network.return_value = results['network']
_firmware.return_value = results['firmware']
await p.probe_all()
self.assertTrue(_storage.called)
self.assertTrue(_network.called)
self.assertTrue(_firmware.called)
self.assertEqual(results, p.get_results())

0 comments on commit 83d25c8

Please sign in to comment.