Skip to content

Commit 614b359

Browse files
authored
Merge pull request #8 from mjun0812/feat/switch-sudo-prefix
feat: Switching to dynamic sudo prefix based on root privileges
2 parents a93ba58 + e9f17e7 commit 614b359

6 files changed

Lines changed: 70 additions & 25 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

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343

4444
Test:
4545
name: Small Test
46+
needs: [Format-Lint-TypeCheck]
4647
strategy:
4748
fail-fast: false
4849
matrix:

dist/index.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22668,6 +22668,12 @@ function debugLog(message) {
2266822668
}
2266922669
core.debug(message);
2267022670
}
22671+
function hasRootPrivileges() {
22672+
if (process.getuid && typeof process.getuid === "function") {
22673+
return process.getuid() === 0;
22674+
}
22675+
return false;
22676+
}
2267122677

2267222678
// src/const.ts
2267322679
var START_SUPPORTED_CUDA_VERSION = "10.0";
@@ -23077,9 +23083,13 @@ var fs2 = __toESM(require("fs"));
2307723083
var path = __toESM(require("path"));
2307823084
var tc = __toESM(require_tool_cache());
2307923085
var io = __toESM(require_io());
23086+
function getSudoPrefix() {
23087+
return hasRootPrivileges() ? "" : "sudo";
23088+
}
2308023089
async function installCudaLinuxLocal(installerPath) {
2308123090
core2.info("Installing CUDA on Linux...");
23082-
const command = `sudo sh ${installerPath}`;
23091+
const sudoPrefix = getSudoPrefix();
23092+
const command = `${sudoPrefix} sh ${installerPath}`.trim();
2308323093
const installArgs = ["--silent", "--override", "--toolkit"];
2308423094
debugLog(`Executing: ${command} ${installArgs.join(" ")}`);
2308523095
await exec.exec(command, installArgs);
@@ -23144,6 +23154,7 @@ async function installCudaLinuxNetwork(version, arch2, osInfo) {
2314423154
}
2314523155
const repoUrl = cudaRepoAndPackage.repoUrl;
2314623156
const packageName = cudaRepoAndPackage.packageName;
23157+
const sudoPrefix = getSudoPrefix();
2314723158
let cudaPath = void 0;
2314823159
try {
2314923160
if (isDebianBased(osInfo)) {
@@ -23155,21 +23166,25 @@ async function installCudaLinuxNetwork(version, arch2, osInfo) {
2315523166
}
2315623167
repoFilePath = path.resolve(repoFilePath);
2315723168
if (repoUrl.endsWith(".deb")) {
23158-
await exec.exec(`sudo dpkg -i ${repoFilePath}`);
23159-
await exec.exec(`sudo apt-get update`);
23169+
await exec.exec(`${sudoPrefix} dpkg -i ${repoFilePath}`.trim());
23170+
await exec.exec(`${sudoPrefix} apt-get update`.trim());
2316023171
} else if (repoUrl.endsWith(".pin")) {
23161-
await exec.exec(`sudo mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`);
23172+
await exec.exec(
23173+
`${sudoPrefix} mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`.trim()
23174+
);
2316223175
const repoRootUrl = repoUrl.replace(/\/[\w.-]+\.pin$/, "");
23163-
await exec.exec(`sudo add-apt-repository "deb ${repoRootUrl} /"`);
23164-
await exec.exec(`sudo apt-get update`);
23176+
await exec.exec(`${sudoPrefix} add-apt-repository "deb ${repoRootUrl} /"`.trim());
23177+
await exec.exec(`${sudoPrefix} apt-get update`.trim());
2316523178
}
23166-
await exec.exec(`sudo apt-get install -y ${packageName}`);
23179+
await exec.exec(`${sudoPrefix} apt-get install -y ${packageName}`.trim());
2316723180
cudaPath = "/usr/local/cuda";
2316823181
} else if (isFedoraBased(osInfo)) {
2316923182
const packageManagerCommand = await getPackageManagerCommand(osInfo);
23170-
await exec.exec(`sudo ${packageManagerCommand} config-manager --add-repo ${repoUrl}`);
23171-
await exec.exec(`sudo ${packageManagerCommand} clean all`);
23172-
await exec.exec(`sudo ${packageManagerCommand} install -y ${packageName}`);
23183+
await exec.exec(
23184+
`${sudoPrefix} ${packageManagerCommand} config-manager --add-repo ${repoUrl}`.trim()
23185+
);
23186+
await exec.exec(`${sudoPrefix} ${packageManagerCommand} clean all`.trim());
23187+
await exec.exec(`${sudoPrefix} ${packageManagerCommand} install -y ${packageName}`.trim());
2317323188
cudaPath = "/usr/local/cuda";
2317423189
}
2317523190
} catch (error) {

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/install.ts

Lines changed: 25 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,26 @@ 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(
170+
`${sudoPrefix} mv ${repoFilePath} /etc/apt/preferences.d/cuda-repository-pin-600`.trim()
171+
);
160172
const repoRootUrl = repoUrl.replace(/\/[\w.-]+\.pin$/, '');
161-
await exec.exec(`sudo add-apt-repository "deb ${repoRootUrl} /"`);
162-
await exec.exec(`sudo apt-get update`);
173+
await exec.exec(`${sudoPrefix} add-apt-repository "deb ${repoRootUrl} /"`.trim());
174+
await exec.exec(`${sudoPrefix} apt-get update`.trim());
163175
}
164176
// Install CUDA toolkit
165-
await exec.exec(`sudo apt-get install -y ${packageName}`);
177+
await exec.exec(`${sudoPrefix} apt-get install -y ${packageName}`.trim());
166178
cudaPath = '/usr/local/cuda';
167179
} else if (isFedoraBased(osInfo)) {
168180
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}`);
181+
await exec.exec(
182+
`${sudoPrefix} ${packageManagerCommand} config-manager --add-repo ${repoUrl}`.trim()
183+
);
184+
await exec.exec(`${sudoPrefix} ${packageManagerCommand} clean all`.trim());
185+
await exec.exec(`${sudoPrefix} ${packageManagerCommand} install -y ${packageName}`.trim());
172186
cudaPath = '/usr/local/cuda';
173187
}
174188
} 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)