Skip to content

Commit

Permalink
utilities: add useGetHostDistro hook
Browse files Browse the repository at this point in the history
Add a hook that gets the host distro for the on-prem frontend. If there
is an issue we will fallback to the default. For the service we also
just use the default distro.
  • Loading branch information
kingsleyzissou committed Jan 23, 2025
1 parent 19e32de commit 33c9ef1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
RHEL_9_FULL_SUPPORT,
RHEL_9_MAINTENANCE_SUPPORT,
RHEL_10_BETA,
ON_PREM_RELEASES,
} from '../../../../constants';
import { useAppDispatch, useAppSelector } from '../../../../store/hooks';
import { Distributions } from '../../../../store/imageBuilderApi';
Expand All @@ -27,6 +28,7 @@ import {
import isRhel from '../../../../Utilities/isRhel';
import { toMonthAndYear } from '../../../../Utilities/time';
import { useFlag } from '../../../../Utilities/useGetEnvironment';
import { useGetHostDistro } from '../../../../Utilities/useGetHostDistro';

const ReleaseSelect = () => {
// What the UI refers to as the "release" is referred to as the "distribution" in the API.
Expand All @@ -36,6 +38,7 @@ const ReleaseSelect = () => {
const dispatch = useAppDispatch();
const [isOpen, setIsOpen] = useState(false);
const [showDevelopmentOptions, setShowDevelopmentOptions] = useState(false);
const hostDistro = useGetHostDistro(distribution);

const isRHEL9BetaEnabled = useFlag('image-builder.rhel9.beta.enabled');
const isRHEL10BetaEnabled = useFlag('image-builder.rhel10.beta.enabled');
Expand Down Expand Up @@ -74,8 +77,13 @@ const ReleaseSelect = () => {

const setSelectOptions = () => {
const options: ReactElement[] = [];
const releases = process.env.IS_ON_PREMISE ? ON_PREM_RELEASES : RELEASES;
const filteredRhel = new Map(
[...RELEASES].filter(([key]) => {
[...releases].filter(([key]) => {
if (process.env.IS_ON_PREMISE) {
return key === hostDistro;
}

if (key === RHEL_9_BETA) {
return isRHEL9BetaEnabled;
}
Expand Down Expand Up @@ -114,14 +122,17 @@ const ReleaseSelect = () => {
variant={SelectVariant.single}
onToggle={() => setIsOpen(!isOpen)}
onSelect={handleSelect}
selections={RELEASES.get(distribution)}
selections={RELEASES.get(hostDistro)}
isOpen={isOpen}
{...(!showDevelopmentOptions && {
loadingVariant: {
text: 'Show options for further development of RHEL',
onClick: handleExpand,
},
})}
{...(!showDevelopmentOptions &&
// Hide this for on-prem since the host
// could be centos or fedora
!process.env.IS_ON_PREMISE && {
loadingVariant: {
text: 'Show options for further development of RHEL',
onClick: handleExpand,
},
})}
>
{setSelectOptions()}
</Select>
Expand Down
51 changes: 51 additions & 0 deletions src/Utilities/useGetHostDistro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useEffect, useState } from 'react';

import path from 'path';

import cockpit from 'cockpit';

import { CENTOS_10, FEDORA_41, RHEL_10_BETA } from '../constants';
import { Distributions } from '../store/imageBuilderApi';

export const useGetHostDistro = (distribution: Distributions) => {
const [distro, setDistro] = useState(distribution);

useEffect(() => {
const getHostDistro = async () => {
try {
const file = cockpit.file(path.join('/', 'etc', 'os-release'));
const contents = await file.read();
file.close();

// TOML parse fails on certain distros
// so this is the best way of getting the
// name of the host distro
const [distroName] = contents
.split('\n')
.filter((line) => line.startsWith('NAME='));

if (distroName === 'NAME="Red Hat Enterprise Linux"') {
// TODO: add RHEL 10
setDistro(RHEL_10_BETA);
}

if (distroName === 'NAME="CentOS Stream"') {
setDistro(CENTOS_10);
}

if (distroName === 'NAME="Fedora Linux"') {
setDistro(FEDORA_41);
}
} finally {
// do nothing, just use the default distribution
}
};

// don't call this function for the service
if (process.env.IS_ON_PREMISE) {
getHostDistro();
}
});

return distro;
};

0 comments on commit 33c9ef1

Please sign in to comment.