Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/_test-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ jobs:
- name: Expand Disk Space
run: |
df -h
sudo rm -rf /usr/share/dotnet || true
sudo rm -rf /usr/local/lib/android || true
rm -rf /usr/share/dotnet || true
rm -rf /usr/local/lib/android || true
echo "-------"
df -h

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:

Test:
name: Small Test
needs: [Format-Lint-TypeCheck]
strategy:
fail-fast: false
matrix:
Expand Down
35 changes: 25 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22668,6 +22668,12 @@ function debugLog(message) {
}
core.debug(message);
}
function hasRootPrivileges() {
if (process.getuid && typeof process.getuid === "function") {
return process.getuid() === 0;
}
return false;
}

// src/const.ts
var START_SUPPORTED_CUDA_VERSION = "10.0";
Expand Down Expand Up @@ -23077,9 +23083,13 @@ var fs2 = __toESM(require("fs"));
var path = __toESM(require("path"));
var tc = __toESM(require_tool_cache());
var io = __toESM(require_io());
function getSudoPrefix() {
return hasRootPrivileges() ? "" : "sudo";
}
async function installCudaLinuxLocal(installerPath) {
core2.info("Installing CUDA on Linux...");
const command = `sudo sh ${installerPath}`;
const sudoPrefix = getSudoPrefix();
const command = `${sudoPrefix} sh ${installerPath}`.trim();
const installArgs = ["--silent", "--override", "--toolkit"];
debugLog(`Executing: ${command} ${installArgs.join(" ")}`);
await exec.exec(command, installArgs);
Expand Down Expand Up @@ -23144,6 +23154,7 @@ async function installCudaLinuxNetwork(version, arch2, osInfo) {
}
const repoUrl = cudaRepoAndPackage.repoUrl;
const packageName = cudaRepoAndPackage.packageName;
const sudoPrefix = getSudoPrefix();
let cudaPath = void 0;
try {
if (isDebianBased(osInfo)) {
Expand All @@ -23155,21 +23166,25 @@ async function installCudaLinuxNetwork(version, arch2, osInfo) {
}
repoFilePath = path.resolve(repoFilePath);
if (repoUrl.endsWith(".deb")) {
await exec.exec(`sudo dpkg -i ${repoFilePath}`);
await exec.exec(`sudo apt-get update`);
await exec.exec(`${sudoPrefix} dpkg -i ${repoFilePath}`.trim());
await exec.exec(`${sudoPrefix} apt-get update`.trim());
} else if (repoUrl.endsWith(".pin")) {
await exec.exec(`sudo mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`);
await exec.exec(
`${sudoPrefix} mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`.trim()
);
const repoRootUrl = repoUrl.replace(/\/[\w.-]+\.pin$/, "");
await exec.exec(`sudo add-apt-repository "deb ${repoRootUrl} /"`);
await exec.exec(`sudo apt-get update`);
await exec.exec(`${sudoPrefix} add-apt-repository "deb ${repoRootUrl} /"`.trim());
await exec.exec(`${sudoPrefix} apt-get update`.trim());
}
await exec.exec(`sudo apt-get install -y ${packageName}`);
await exec.exec(`${sudoPrefix} apt-get install -y ${packageName}`.trim());
cudaPath = "/usr/local/cuda";
} else if (isFedoraBased(osInfo)) {
const packageManagerCommand = await getPackageManagerCommand(osInfo);
await exec.exec(`sudo ${packageManagerCommand} config-manager --add-repo ${repoUrl}`);
await exec.exec(`sudo ${packageManagerCommand} clean all`);
await exec.exec(`sudo ${packageManagerCommand} install -y ${packageName}`);
await exec.exec(
`${sudoPrefix} ${packageManagerCommand} config-manager --add-repo ${repoUrl}`.trim()
);
await exec.exec(`${sudoPrefix} ${packageManagerCommand} clean all`.trim());
await exec.exec(`${sudoPrefix} ${packageManagerCommand} install -y ${packageName}`.trim());
cudaPath = "/usr/local/cuda";
}
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

36 changes: 25 additions & 11 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isFedoraBased,
getPackageManagerCommand,
} from './os_arch';
import { debugLog } from './utils';
import { debugLog, hasRootPrivileges } from './utils';
import {
getCudaLocalInstallerUrl,
findCudaRepoAndPackageLinux,
Expand All @@ -20,14 +20,23 @@ import {
import * as tc from '@actions/tool-cache';
import * as io from '@actions/io';

/**
* Get sudo prefix for command execution
* @returns 'sudo' if root privileges are not present, empty string otherwise
*/
function getSudoPrefix(): string {
return hasRootPrivileges() ? '' : 'sudo';
}

/**
* Install CUDA on Linux
* @param installerPath - Path to the CUDA installer (.run file)
*/
async function installCudaLinuxLocal(installerPath: string): Promise<void> {
// https://docs.nvidia.com/cuda/cuda-installation-guide-linux/#runfile-installation
core.info('Installing CUDA on Linux...');
const command = `sudo sh ${installerPath}`;
const sudoPrefix = getSudoPrefix();
const command = `${sudoPrefix} sh ${installerPath}`.trim();

// Install CUDA toolkit only (without driver)
// --silent: Run installer in silent mode
Expand Down Expand Up @@ -140,6 +149,7 @@ async function installCudaLinuxNetwork(
const repoUrl = cudaRepoAndPackage.repoUrl;
const packageName = cudaRepoAndPackage.packageName;

const sudoPrefix = getSudoPrefix();
let cudaPath: string | undefined = undefined;
try {
if (isDebianBased(osInfo)) {
Expand All @@ -153,22 +163,26 @@ async function installCudaLinuxNetwork(
repoFilePath = path.resolve(repoFilePath);

if (repoUrl.endsWith('.deb')) {
await exec.exec(`sudo dpkg -i ${repoFilePath}`);
await exec.exec(`sudo apt-get update`);
await exec.exec(`${sudoPrefix} dpkg -i ${repoFilePath}`.trim());
await exec.exec(`${sudoPrefix} apt-get update`.trim());
} else if (repoUrl.endsWith('.pin')) {
await exec.exec(`sudo mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`);
await exec.exec(
`${sudoPrefix} mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`.trim()
);
const repoRootUrl = repoUrl.replace(/\/[\w.-]+\.pin$/, '');
await exec.exec(`sudo add-apt-repository "deb ${repoRootUrl} /"`);
await exec.exec(`sudo apt-get update`);
await exec.exec(`${sudoPrefix} add-apt-repository "deb ${repoRootUrl} /"`.trim());
await exec.exec(`${sudoPrefix} apt-get update`.trim());
}
// Install CUDA toolkit
await exec.exec(`sudo apt-get install -y ${packageName}`);
await exec.exec(`${sudoPrefix} apt-get install -y ${packageName}`.trim());
cudaPath = '/usr/local/cuda';
} else if (isFedoraBased(osInfo)) {
const packageManagerCommand = await getPackageManagerCommand(osInfo);
await exec.exec(`sudo ${packageManagerCommand} config-manager --add-repo ${repoUrl}`);
await exec.exec(`sudo ${packageManagerCommand} clean all`);
await exec.exec(`sudo ${packageManagerCommand} install -y ${packageName}`);
await exec.exec(
`${sudoPrefix} ${packageManagerCommand} config-manager --add-repo ${repoUrl}`.trim()
);
await exec.exec(`${sudoPrefix} ${packageManagerCommand} clean all`.trim());
await exec.exec(`${sudoPrefix} ${packageManagerCommand} install -y ${packageName}`.trim());
cudaPath = '/usr/local/cuda';
}
} catch (error) {
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ export function debugLog(message: string): void {
}
core.debug(message);
}

/**
* Check if the current process has root/administrator privileges
* @returns true if running with root/admin privileges, false otherwise
*/
export function hasRootPrivileges(): boolean {
// On Unix-like systems (Linux, macOS), check if UID is 0
if (process.getuid && typeof process.getuid === 'function') {
return process.getuid() === 0;
}

// On Windows or systems without getuid, assume no root privileges
// Note: Proper Windows admin check would require native modules
return false;
}