Utilities: add useComposerStatus hook
Add a custom hook to check to see if the osbuild-composer.socket is enabled & running.
This commit is contained in:
parent
abca3ac760
commit
87ce805c74
1 changed files with 44 additions and 0 deletions
44
src/Utilities/useComposerStatus.tsx
Normal file
44
src/Utilities/useComposerStatus.tsx
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
|
||||
import cockpit from 'cockpit';
|
||||
|
||||
export const useGetComposerSocketStatus = () => {
|
||||
const [enabled, setEnabled] = useState(false);
|
||||
const [started, setStarted] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const isEnabled = async () => {
|
||||
try {
|
||||
const result = await cockpit.spawn(
|
||||
['systemctl', 'is-enabled', 'osbuild-composer.socket'],
|
||||
{ superuser: 'try' }
|
||||
);
|
||||
setEnabled((result as string).trim() === 'enabled');
|
||||
} catch {
|
||||
// error code 1 means disabled
|
||||
setEnabled(false);
|
||||
}
|
||||
};
|
||||
|
||||
const isStarted = async () => {
|
||||
try {
|
||||
const result = await cockpit.spawn(
|
||||
['systemctl', 'is-active', 'osbuild-composer.socket'],
|
||||
{ superuser: 'try' }
|
||||
);
|
||||
setStarted((result as string).trim() === 'active');
|
||||
} catch {
|
||||
// exit code 3 means not active
|
||||
setStarted(false);
|
||||
}
|
||||
};
|
||||
|
||||
isEnabled();
|
||||
isStarted();
|
||||
}, []);
|
||||
|
||||
return {
|
||||
enabled,
|
||||
started,
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue