Skip to content

Commit 261b80d

Browse files
committed
feat: switch to dynamic sudo prefix based on root privileges
- Add hasRootPrivileges() function to check if running with root/admin privileges - Implement getSudoPrefix() to return 'sudo' only when needed - Update installCudaLinuxLocal() to use dynamic sudo prefix - Update installCudaLinuxNetwork() to use dynamic sudo prefix for all package manager commands - Remove hardcoded 'sudo' from container test workflow
1 parent a93ba58 commit 261b80d

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

.github/workflows/_test-container.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ jobs:
7171
- name: Expand Disk Space
7272
run: |
7373
df -h
74-
sudo rm -rf /usr/share/dotnet || true
75-
sudo rm -rf /usr/local/lib/android || true
74+
rm -rf /usr/share/dotnet || true
75+
rm -rf /usr/local/lib/android || true
7676
echo "-------"
7777
df -h
7878

src/install.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
isFedoraBased,
1212
getPackageManagerCommand,
1313
} from './os_arch';
14-
import { debugLog } from './utils';
14+
import { debugLog, hasRootPrivileges } from './utils';
1515
import {
1616
getCudaLocalInstallerUrl,
1717
findCudaRepoAndPackageLinux,
@@ -20,14 +20,23 @@ import {
2020
import * as tc from '@actions/tool-cache';
2121
import * as io from '@actions/io';
2222

23+
/**
24+
* Get sudo prefix for command execution
25+
* @returns 'sudo' if root privileges are not present, empty string otherwise
26+
*/
27+
function getSudoPrefix(): string {
28+
return hasRootPrivileges() ? '' : 'sudo';
29+
}
30+
2331
/**
2432
* Install CUDA on Linux
2533
* @param installerPath - Path to the CUDA installer (.run file)
2634
*/
2735
async function installCudaLinuxLocal(installerPath: string): Promise<void> {
2836
// https://docs.nvidia.com/cuda/cuda-installation-guide-linux/#runfile-installation
2937
core.info('Installing CUDA on Linux...');
30-
const command = `sudo sh ${installerPath}`;
38+
const sudoPrefix = getSudoPrefix();
39+
const command = `${sudoPrefix} sh ${installerPath}`.trim();
3140

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

152+
const sudoPrefix = getSudoPrefix();
143153
let cudaPath: string | undefined = undefined;
144154
try {
145155
if (isDebianBased(osInfo)) {
@@ -153,22 +163,22 @@ async function installCudaLinuxNetwork(
153163
repoFilePath = path.resolve(repoFilePath);
154164

155165
if (repoUrl.endsWith('.deb')) {
156-
await exec.exec(`sudo dpkg -i ${repoFilePath}`);
157-
await exec.exec(`sudo apt-get update`);
166+
await exec.exec(`${sudoPrefix} dpkg -i ${repoFilePath}`.trim());
167+
await exec.exec(`${sudoPrefix} apt-get update`.trim());
158168
} else if (repoUrl.endsWith('.pin')) {
159-
await exec.exec(`sudo mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`);
169+
await exec.exec(`${sudoPrefix} mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`.trim());
160170
const repoRootUrl = repoUrl.replace(/\/[\w.-]+\.pin$/, '');
161-
await exec.exec(`sudo add-apt-repository "deb ${repoRootUrl} /"`);
162-
await exec.exec(`sudo apt-get update`);
171+
await exec.exec(`${sudoPrefix} add-apt-repository "deb ${repoRootUrl} /"`.trim());
172+
await exec.exec(`${sudoPrefix} apt-get update`.trim());
163173
}
164174
// Install CUDA toolkit
165-
await exec.exec(`sudo apt-get install -y ${packageName}`);
175+
await exec.exec(`${sudoPrefix} apt-get install -y ${packageName}`.trim());
166176
cudaPath = '/usr/local/cuda';
167177
} else if (isFedoraBased(osInfo)) {
168178
const packageManagerCommand = await getPackageManagerCommand(osInfo);
169-
await exec.exec(`sudo ${packageManagerCommand} config-manager --add-repo ${repoUrl}`);
170-
await exec.exec(`sudo ${packageManagerCommand} clean all`);
171-
await exec.exec(`sudo ${packageManagerCommand} install -y ${packageName}`);
179+
await exec.exec(`${sudoPrefix} ${packageManagerCommand} config-manager --add-repo ${repoUrl}`.trim());
180+
await exec.exec(`${sudoPrefix} ${packageManagerCommand} clean all`.trim());
181+
await exec.exec(`${sudoPrefix} ${packageManagerCommand} install -y ${packageName}`.trim());
172182
cudaPath = '/usr/local/cuda';
173183
}
174184
} catch (error) {

src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,18 @@ export function debugLog(message: string): void {
3939
}
4040
core.debug(message);
4141
}
42+
43+
/**
44+
* Check if the current process has root/administrator privileges
45+
* @returns true if running with root/admin privileges, false otherwise
46+
*/
47+
export function hasRootPrivileges(): boolean {
48+
// On Unix-like systems (Linux, macOS), check if UID is 0
49+
if (process.getuid && typeof process.getuid === 'function') {
50+
return process.getuid() === 0;
51+
}
52+
53+
// On Windows or systems without getuid, assume no root privileges
54+
// Note: Proper Windows admin check would require native modules
55+
return false;
56+
}

0 commit comments

Comments
 (0)