-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
781 additions
and
744 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,43 +1,45 @@ | ||
import { | ||
PODMAN_LATEST_VERSION, | ||
PODMAN_MIN_VERSION, | ||
getInstalledPodmanVersion, | ||
} from './install/install'; | ||
import { isPodmanInstalled, isPodmanRunning } from './podman'; | ||
PODMAN_LATEST_VERSION, | ||
PODMAN_MIN_VERSION, | ||
getInstalledPodmanVersion, | ||
} from "./install/install"; | ||
import { isPodmanInstalled, isPodmanRunning } from "./podman"; | ||
|
||
export type PodmanDetails = { | ||
isInstalled: boolean; | ||
isRunning?: boolean; | ||
installedVersion?: string; | ||
minimumVersionRequired?: string; | ||
latestVersionAvailable?: string; | ||
isOutdated?: boolean; // user must update podman | ||
isOptionalUpdateAvailable?: boolean; | ||
isInstalled: boolean; | ||
isRunning?: boolean; | ||
installedVersion?: string; | ||
minimumVersionRequired?: string; | ||
latestVersionAvailable?: string; | ||
isOutdated?: boolean; // user must update podman | ||
isOptionalUpdateAvailable?: boolean; | ||
}; | ||
|
||
export const getPodmanDetails = async (): Promise<PodmanDetails> => { | ||
const isInstalled = await isPodmanInstalled(); | ||
const isRunning = await isPodmanRunning(); | ||
const installedVersion = await getInstalledPodmanVersion(); | ||
let isOutdated; | ||
if (installedVersion) { | ||
isOutdated = installedVersion < PODMAN_MIN_VERSION; | ||
} | ||
let isOptionalUpdateAvailable; | ||
// if outdated, leave as undefined because a required update takes precedent | ||
if (!isOutdated && installedVersion) { | ||
isOptionalUpdateAvailable = installedVersion < PODMAN_LATEST_VERSION; | ||
} | ||
const isInstalled = await isPodmanInstalled(); | ||
const isRunning = await isPodmanRunning(); | ||
const installedVersion = await getInstalledPodmanVersion(); | ||
let isOutdated; | ||
if (installedVersion) { | ||
// fails for '24.0.7' < '4.0.7' | ||
isOutdated = installedVersion < PODMAN_MIN_VERSION; | ||
} | ||
isOutdated = false; | ||
let isOptionalUpdateAvailable; | ||
// if outdated, leave as undefined because a required update takes precedent | ||
if (!isOutdated && installedVersion) { | ||
isOptionalUpdateAvailable = installedVersion < PODMAN_LATEST_VERSION; | ||
} | ||
|
||
const details: PodmanDetails = { | ||
isInstalled, | ||
isRunning, | ||
installedVersion, | ||
minimumVersionRequired: PODMAN_MIN_VERSION, | ||
latestVersionAvailable: PODMAN_LATEST_VERSION, | ||
isOutdated, | ||
isOptionalUpdateAvailable, | ||
}; | ||
const details: PodmanDetails = { | ||
isInstalled, | ||
isRunning, | ||
installedVersion, | ||
minimumVersionRequired: PODMAN_MIN_VERSION, | ||
latestVersionAvailable: PODMAN_LATEST_VERSION, | ||
isOutdated, | ||
isOptionalUpdateAvailable, | ||
}; | ||
|
||
return details; | ||
return details; | ||
}; |
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 |
---|---|---|
@@ -1,33 +1,56 @@ | ||
import { runCommand } from './podman'; | ||
import type { ContainerStats } from './types'; | ||
import { cpu } from "systeminformation"; | ||
import { runCommand } from "./podman"; | ||
import type { ContainerStats } from "./types"; | ||
|
||
/** | ||
* Gets all container metrics from Podman CLI, parses, and formats them | ||
* This will be switched to use the Podman API soon | ||
* @returns | ||
*/ | ||
export const getAllContainerMetrics = async (): Promise<ContainerStats[]> => { | ||
let allContainerStats; | ||
try { | ||
allContainerStats = await runCommand( | ||
'stats --all --no-stream --no-trunc --format json', | ||
); | ||
allContainerStats = JSON.parse(allContainerStats); | ||
const parsedContainerStats: ContainerStats[] = []; | ||
if (Array.isArray(allContainerStats)) { | ||
allContainerStats.forEach((containerStats) => { | ||
parsedContainerStats.push({ | ||
ContainerID: containerStats.id, | ||
Name: containerStats.name, | ||
PercCPU: Number.parseFloat(containerStats.cpu_percent), | ||
MemPerc: Number.parseFloat(containerStats.mem_percent), | ||
}); | ||
}); | ||
} | ||
return parsedContainerStats; | ||
} catch (err) { | ||
// Podman is likely not running | ||
// console.error('Unable to get podman container stats'); | ||
} | ||
return []; | ||
let allContainerStats; | ||
try { | ||
// allContainerStats = await runCommand( | ||
// "stats --all --no-stream --no-trunc --format json", | ||
// ); | ||
// --format "{{ json . }}" | ||
allContainerStats = await runCommand( | ||
`stats --all --no-stream --no-trunc --format \"{{ json . }}\"`, | ||
); | ||
try { | ||
// allContainerStats = JSON.parse(allContainerStats); | ||
allContainerStats = allContainerStats.split("\n").map(JSON.parse); | ||
} catch (e) { | ||
console.error("Error parsing allContainerStats", e); | ||
} | ||
const parsedContainerStats: ContainerStats[] = []; | ||
if (Array.isArray(allContainerStats)) { | ||
allContainerStats.forEach((containerStats) => { | ||
// Docker compatibility | ||
let containerId = containerStats.id ?? containerStats.ID; | ||
let name = containerStats.name ?? containerStats.Name; | ||
let cpuPercent = containerStats.cpu_percent; | ||
if (cpuPercent === undefined) { | ||
cpuPercent = containerStats.CPUPerc; | ||
} | ||
let memPercent = containerStats.mem_percent; | ||
if (memPercent === undefined) { | ||
memPercent = containerStats.MemPerc; | ||
} | ||
cpuPercent = Number.parseFloat(cpuPercent); | ||
memPercent = Number.parseFloat(memPercent); | ||
parsedContainerStats.push({ | ||
ContainerID: containerId, | ||
Name: name, | ||
PercCPU: cpuPercent, | ||
MemPerc: memPercent, | ||
}); | ||
}); | ||
} | ||
return parsedContainerStats; | ||
} catch (err) { | ||
// Podman is likely not running | ||
// console.error('Unable to get podman container stats'); | ||
} | ||
return []; | ||
}; |
Oops, something went wrong.