Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add module for checking basic system requirements #2835

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions lib/system-reqs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/system-reqs.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"js-yaml": "^4.1.0",
"jsonschema": "1.4.1",
"long": "^5.3.1",
"macos-version": "^6.0.0",
"node-forge": "^1.3.1",
"octokit": "^4.1.2",
"path": "^0.12.7",
Expand Down
139 changes: 139 additions & 0 deletions src/system-reqs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Enforce system requirements for the CodeQL CLI.
// https://codeql.github.com/docs/codeql-overview/system-requirements/

import { readFileSync } from "fs";
import os from "os";

import { isMacOSVersionGreaterThanOrEqualTo } from "macos-version";

import { assertNever, ConfigurationError } from "./util";

const platformMap = {
darwin: "macOS",
linux: "Linux",
win32: "Windows",
};

const platformRequirements = {
macOS: {
version: ["13.0.1", "14.0.1"],
arch: ["arm64", "x64"],
},
Linux: {
// We only accept Ubuntu 20.04, 21.04, and 22.04
version: ["20.04", "21.04", "22.04"],
arch: ["x64"],
},
Windows: {
version: ["10", "Server 2019", "11", "Server 2022"],
arch: ["x64"],
},
};

export function passesSystemRequirements(): boolean {
// CodeQL System requirements are two-level: the CLI must run on a specific platform,
// and that platform must have certain capabilities.

const platform = getPlatform();
switch (platform) {
case "macOS":
return checkMacOSPlatform();
case "Linux":
return checkLinuxPlatform();
case "Windows":
return checkWindowsPlatform();
default:
assertNever(platform);
}
}

// MacOS checks

function checkMacOSPlatform(): boolean {
const macOSPlatformRequirements = platformRequirements["macOS"];
return (
checkMacOSVersion(macOSPlatformRequirements["version"]) &&
checkMacOSArch(macOSPlatformRequirements["arch"])
);
}

function checkMacOSVersion(supportedVersions: string[]): boolean {
return supportedVersions.some((version) => {
return isMacOSVersionGreaterThanOrEqualTo(version);
});
}

function checkMacOSArch(supportedArchs: string[]): boolean {
const arch = getArch();
return supportedArchs.includes(arch);
}

// Linux checks

function checkLinuxPlatform(): boolean {
const linuxPlatformRequirements = platformRequirements["Linux"];
return (
checkLinuxVersion(linuxPlatformRequirements["version"]) &&
checkLinuxArch(linuxPlatformRequirements["arch"])
);
}

function checkLinuxVersion(supportedVersions: string[]): boolean {
const data = readFileSync("/etc/os-release", "utf8");
const lines = data.split("\n");
const releaseDetails: Record<string, string> = {};
for (const line of lines) {
const words = line.split("=");
if (words.length === 2) {
releaseDetails[words[0].trim().toLowerCase()] = words[1].trim();
}
}

return (
releaseDetails.name === "Ubuntu" &&
supportedVersions.includes(releaseDetails.version_id)
);
}

function checkLinuxArch(supportedArchs: string[]): boolean {
const arch = getArch();
return supportedArchs.includes(arch);
}

// Windows checks

function checkWindowsPlatform(): boolean {
const windowsPlatformRequirements = platformRequirements["Windows"];
return (
checkWindowsVersion(windowsPlatformRequirements["version"]) &&
checkWindowsArch(windowsPlatformRequirements["arch"])
);
}

function checkWindowsVersion(supportedVersions: string[]): boolean {
// os.release() on windows returns a string like "Windows 11 Home"
const windowsVersion = os.release();
return supportedVersions.some((version) =>
new RegExp(version, "i").test(windowsVersion),
);
}

function checkWindowsArch(supportedArchs: string[]): boolean {
const arch = getArch();
return supportedArchs.includes(arch);
}

// Auxiliary functions

function getPlatform(): "macOS" | "Linux" | "Windows" {
const platform = process.platform;
const mappedPlatform = platformMap[platform];
if (mappedPlatform === undefined) {
throw new ConfigurationError(`Unsupported platform: ${platform}`);
}
return mappedPlatform;
}

function getArch(): string {
return process.arch;
}
Loading