CreateImageWizard: refactor oscap availability

Refactor the OpenSCAP on-premise availability check. Add the check to
the component so we run the check everytime the step is loaded. The
benefits to this is that if the user installs the packages, they won't
need to reload the page to use the OpenSCAP step. The downside is that
the check is not very quick, so a spinner was added just to indicate
that the check is running.
This commit is contained in:
Gianluca Zuccarelli 2025-03-25 14:50:53 +00:00 committed by Lucas Garfield
parent 20447f753a
commit e5a513a4cb
4 changed files with 58 additions and 33 deletions

View file

@ -3,34 +3,30 @@ import { useEffect, useState } from 'react';
import cockpit from 'cockpit';
export const useOnPremOpenSCAPAvailable = () => {
// this can default to false in the service, since we will only render
// a loading spinner for on-prem
const [isLoading, setIsLoading] = useState(true);
const [packagesAvailable, setPackagesAvailable] = useState(false);
useEffect(() => {
const checkPackages = async () => {
try {
const openSCAPAvailable = await cockpit.spawn(
['rpm', '-qa', 'openscap-scanner'],
{}
);
const ssgAvailable = await cockpit.spawn(
['rpm', '-qa', 'scap-security-guide'],
{}
);
setPackagesAvailable(openSCAPAvailable !== '' && ssgAvailable !== '');
} catch {
// this doesn't change the value,
// but we need to handle the error
// so just set the value to false
setPackagesAvailable(false);
}
const checkPackages = () => {
cockpit
.spawn(['rpm', '-qa', 'openscap-scanner', 'scap-security-guide'], {})
.then((res: string) => {
setPackagesAvailable(
res.includes('openscap-scanner') &&
res.includes('scap-security-guide')
);
setIsLoading(false);
})
.catch(() => {
setPackagesAvailable(false);
setIsLoading(false);
});
};
if (process.env.IS_ON_PREMISE) {
checkPackages();
}
checkPackages();
}, []);
return packagesAvailable;
return [packagesAvailable, isLoading];
};