Quickly detecting the Linux distribution¶
The autotest.client.shared.distro
module provides many APIs, but the simplest one to use is the detect()
.
Its usage is quite straighforward:
from autotest.client.shared import distro
detected_distro = distro.detect()
The returned distro can be the result of a probe validating the distribution detection, or the not so useful
UNKNOWN_DISTRO
.
To access the relevant data on a LinuxDistro
, simply use the attributes:
name
version
release
arch
Example:
>>> detected_distro = distro.detect()
>>> print detected_distro.name
redhat
The unknown Linux distribution¶
When the detection mechanism can’t precily detect the Linux distribution, it will still return a LinuxDistro
instance,
but a special one that contains special values for its name, version, etc.
The distribution that is used when the exact one could not be found
Writing a Linux distribution probe¶
The easiest way to write a probe for your target Linux distribution is to make use of the features of the Probe
class.
Even if you do plan to use the features documented here, keep in mind that all probes should inherit from Probe
and provide a basic interface.
Checking the distrution name only¶
The most trivial probe is one that checks the existence of a file and returns the distribution name:
class RedHatProbe(Probe):
CHECK_FILE = '/etc/redhat-release'
CHECK_FILE_DISTRO_NAME = 'redhat'
To make use of a probe, it’s necessary to register it:
from autotest.client.shared import distro
distro.register_probe(RedHatProbe)
And that’s it. This is a valid example, but will give you nothing but the distro name.
You should usually aim for more information, such as the version numbers.
Checking the distribution name and version numbers¶
If you want to also detect the distro version numbers (and you should), then it’s possible to use the
Probe.CHECK_VERSION_REGEX
feature of the Probe
class.
A regular expresion that will be run on the file pointed to by
CHECK_FILE_EXISTS
If your regex has two or more groups, that is, it will look for and save references to two or more string, it will consider
the second group to be the LinuxDistro.release
number.
Probe Scores¶
To increase the accuracy of the probe results, it’s possible to register a score for a probe. If a probe wants to, it can register a score for itself.
Probes that return a score will be given priority over probes that don’t.
The score should be based on the number of checks that ran during the probe to account for its accuracy.
Probes should not be given a higher score because their checks look more precise than everyone else’s.
Registering your own probes¶
Not only the probes that ship with Autotest can be used, but your custom probe classes can be added to the detection system.
To do that simply call the function register_probe()
:
Register a probe to be run during autodetection
Now, remember that for things to happen smootlhy your registered probe must be a subclass of Probe
.
API Reference¶
LinuxDistro
¶
Simple collection of information for a Linux Distribution
Probe
¶
Probes the machine and does it best to confirm it’s the right distro
Points to a file that can determine if this machine is running a given Linux Distribution. This servers a first check that enables the extra checks to carry on.
Sets the content that should be checked on the file pointed to by
CHECK_FILE_EXISTS
. Leave it set to None (its default) to check only if the file exists, and not check its contents
The name of the Linux Distribution to be returned if the file defined by
CHECK_FILE_EXISTS
exist.
-
CHECK_VERSION_REGEX
= None A regular expresion that will be run on the file pointed to by
CHECK_FILE_EXISTS
Checks if this class will look for a file and return a distro
The conditions that must be true include the file that identifies the distro file being set (
CHECK_FILE
) and the name of the distro to be returned (CHECK_FILE_DISTRO_NAME
)
Checks if this class will look for text on a file and return a distro
The conditions that must be true include the file that identifies the distro file being set (
CHECK_FILE
), the text to look for inside the distro file (CHECK_FILE_CONTAINS
) and the name of the distro to be returned (CHECK_FILE_DISTRO_NAME
)
Checks if this has the conditions met to look for the release number
Checks if this class will look for a regex in file and return a distro
Returns the
LinuxDistro
this probe detected
Get the distro name if the
CHECK_FILE
is set and exists
Get the distro if the
CHECK_FILE
is set and has content
Returns the release of the distro
Returns the version of the distro
register_probe()
¶
-
autotest.client.shared.distro.
register_probe
(probe_class)[source] Register a probe to be run during autodetection
detect()
¶
Attempts to detect the Linux Distribution running on this machine
Returns: the detected LinuxDistro
orUNKNOWN_DISTRO
Return type: LinuxDistro