debian-image-builder-frontend/src/Utilities/useComposerStatus.tsx
Gianluca Zuccarelli 87ce805c74 Utilities: add useComposerStatus hook
Add a custom hook to check to see if the osbuild-composer.socket
is enabled & running.
2025-02-04 23:08:50 +01:00

44 lines
1 KiB
TypeScript

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,
};
};