-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utilities: add useGetHostDistro hook
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
1 parent
19e32de
commit 33c9ef1
Showing
2 changed files
with
70 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |