Skip to content

Commit

Permalink
fix: make execaSudo and setupAptPack synchronous
Browse files Browse the repository at this point in the history
apt is not thread-safe
  • Loading branch information
aminya committed Apr 27, 2022
1 parent 9ce1b85 commit 18f813f
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ ENTRYPOINT [ "/bin/bash" ]
FROM base AS builder
ADD ./dev/cpp_vcpkg_project /home/app
WORKDIR /home/app
RUN bash -c 'source ~/.cpprc \
RUN bash -c 'source ~/.cpprc \
&& make build'

### Running environment
Expand Down
2 changes: 1 addition & 1 deletion dist/setup_cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/setup_cpp.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/doxygen/doxygen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function setupDoxygen(version: string, setupDir: string, arch: stri
installationInfo = await setupBin("doxygen", version, getDoxygenPackageInfo, setupDir, arch)
} catch (err) {
info(`Failed to download doxygen binary. ${err}. Falling back to apt-get.`)
installationInfo = await setupAptPack("doxygen", undefined)
installationInfo = setupAptPack("doxygen", undefined)
}
await setupGraphviz(getVersion("graphviz", undefined), "", arch)
return installationInfo
Expand Down
8 changes: 4 additions & 4 deletions src/gcc/gcc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ export async function setupGcc(version: string, _setupDir: string, arch: string)
}
case "linux": {
if (arch === "x64") {
await setupAptPack("gcc", version, [
setupAptPack("gcc", version, [
"deb http://dk.archive.ubuntu.com/ubuntu/ xenial main",
"deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe",
"ppa:ubuntu-toolchain-r/test",
])
binDir = (await setupAptPack("g++", version, [])).binDir
binDir = setupAptPack("g++", version, []).binDir
} else {
info(`Install g++-multilib because gcc for ${arch} was requested`)
await setupAptPack("gcc-multilib", version, [
setupAptPack("gcc-multilib", version, [
"deb http://dk.archive.ubuntu.com/ubuntu/ xenial main",
"deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe",
"ppa:ubuntu-toolchain-r/test",
])
binDir = (await setupAptPack("g++-multilib", version, [])).binDir
binDir = setupAptPack("g++-multilib", version, []).binDir
}
break
}
Expand Down
8 changes: 4 additions & 4 deletions src/kcov/kcov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ async function buildKcov(file: string, dest: string) {
await setupCmake(getVersion("cmake", undefined), join(untildify(""), "cmake"), "")
}
if (process.platform === "linux") {
await setupAptPack("libdw-dev")
await setupAptPack("libcurl4-openssl-dev")
setupAptPack("libdw-dev")
setupAptPack("libcurl4-openssl-dev")
}
await execa("cmake", ["-S", "./", "-B", "./build"], { cwd: out, stdio: "inherit" })
await execa("cmake", ["--build", "./build", "--config", "Release"], { cwd: out, stdio: "inherit" })
await execSudo("cmake", ["--install", "./build"], out)
execSudo("cmake", ["--install", "./build"], out)
return out
}

export async function setupKcov(version: string, setupDir: string, arch: string) {
switch (process.platform) {
case "linux": {
const installationInfo = await setupBin("kcov", version, getKcovPackageInfo, setupDir, arch)
await setupAptPack("libbinutils")
setupAptPack("libbinutils")
return installationInfo
}
default: {
Expand Down
2 changes: 1 addition & 1 deletion src/llvm/llvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async function _setupLLVM(version: string, setupDir: string, arch: string) {
if (process.platform === "linux") {
// install llvm build dependencies
await setupGcc(getVersion("gcc", undefined), "", arch) // using llvm requires ld, an up to date libstdc++, etc. So, install gcc first
await setupAptPack("libtinfo-dev")
setupAptPack("libtinfo-dev")
}
// eslint-disable-next-line require-atomic-updates
didInit = true
Expand Down
6 changes: 3 additions & 3 deletions src/python/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function setupPython(version: string, setupDir: string, arch: strin
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupPythonViaSystem(version: string, setupDir: string, _arch: string) {
export function setupPythonViaSystem(version: string, setupDir: string, _arch: string) {
switch (process.platform) {
case "win32": {
if (setupDir) {
Expand All @@ -39,8 +39,8 @@ export async function setupPythonViaSystem(version: string, setupDir: string, _a
return setupBrewPack("python3", version)
}
case "linux": {
const installInfo = await setupAptPack("python3", version)
await setupAptPack("python3-pip")
const installInfo = setupAptPack("python3", version)
setupAptPack("python3-pip")
return installInfo
}
default: {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/exec/sudo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { isRoot } from "../env/sudo"

export function execSudo(file: string, args: string[], cwd?: string) {
if (isRoot()) {
return execa.command(`sudo ${[file, ...args].map((arg) => `'${arg}'`).join(" ")}`, {
return execa.commandSync(`sudo ${[file, ...args].map((arg) => `'${arg}'`).join(" ")}`, {
shell: true,
cwd,
stdio: "inherit",
})
} else {
return execa(file, args, { stdio: "inherit" })
return execa.sync(file, args, { stdio: "inherit" })
}
}
24 changes: 12 additions & 12 deletions src/utils/setup/setupAptPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ let didUpdate: boolean = false
let didInit: boolean = false

/** A function that installs a package using apt */
export async function setupAptPack(
export function setupAptPack(
name: string,
version?: string,
repositories: boolean | string[] = true
): Promise<InstallationInfo> {
): InstallationInfo {
info(`Installing ${name} ${version ?? ""} via apt`)

const apt = "apt-get"

process.env.DEBIAN_FRONTEND = "noninteractive"

if (!didUpdate) {
await execSudo(apt, ["update", "-y"])
execSudo(apt, ["update", "-y"])
didUpdate = true
}

Expand All @@ -32,7 +32,7 @@ export async function setupAptPack(
// set time - zone
// TZ = Canada / Pacific
// ln - snf / usr / share / zoneinfo / $TZ / etc / localtime && echo $TZ > /etc/timezone
await execSudo(apt, [
execSudo(apt, [
"install",
"--fix-broken",
"-y",
Expand All @@ -42,9 +42,9 @@ export async function setupAptPack(
"gnupg",
])
try {
await execSudo("apt-key", ["adv", "--keyserver", "keyserver.ubuntu.com", "--recv-keys", "3B4FE6ACC0B21F32"])
await execSudo("apt-key", ["adv", "--keyserver", "keyserver.ubuntu.com", "--recv-keys", "40976EAF437D05B5"])
await execSudo("apt-key", ["adv", "--keyserver", "keyserver.ubuntu.com", "--recv-keys", "1E9377A2BA9EF27F"])
execSudo("apt-key", ["adv", "--keyserver", "keyserver.ubuntu.com", "--recv-keys", "3B4FE6ACC0B21F32"])
execSudo("apt-key", ["adv", "--keyserver", "keyserver.ubuntu.com", "--recv-keys", "40976EAF437D05B5"])
execSudo("apt-key", ["adv", "--keyserver", "keyserver.ubuntu.com", "--recv-keys", "1E9377A2BA9EF27F"])
} catch (err) {
warning(`Failed to add keys: ${err}`)
}
Expand All @@ -54,19 +54,19 @@ export async function setupAptPack(
if (Array.isArray(repositories)) {
for (const repo of repositories) {
// eslint-disable-next-line no-await-in-loop
await execSudo("add-apt-repository", ["--update", "-y", repo])
execSudo("add-apt-repository", ["--update", "-y", repo])
}
await execSudo(apt, ["update", "-y"])
execSudo(apt, ["update", "-y"])
}

if (version !== undefined && version !== "") {
try {
await execSudo(apt, ["install", "--fix-broken", "-y", `${name}=${version}`])
execSudo(apt, ["install", "--fix-broken", "-y", `${name}=${version}`])
} catch {
await execSudo(apt, ["install", "--fix-broken", "-y", `${name}-${version}`])
execSudo(apt, ["install", "--fix-broken", "-y", `${name}-${version}`])
}
} else {
await execSudo(apt, ["install", "--fix-broken", "-y", name])
execSudo(apt, ["install", "--fix-broken", "-y", name])
}

return { binDir: "/usr/bin/" }
Expand Down
6 changes: 3 additions & 3 deletions src/utils/setup/setupBin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export async function setupBin(
if (!didInit) {
if (process.platform === "linux") {
// extraction dependencies
await setupAptPack("unzip")
await setupAptPack("tar")
await setupAptPack("xz-utils")
setupAptPack("unzip")
setupAptPack("tar")
setupAptPack("xz-utils")
}
// eslint-disable-next-line require-atomic-updates
didInit = true
Expand Down
2 changes: 1 addition & 1 deletion src/utils/setup/setupPipPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function setupPipPack(name: string, version?: string): Promise<Inst
execa.sync(python, ["-m", "pip", "install", "-U", "pip==21.3.1"], { stdio: "inherit" })
} else if (process.platform === "linux") {
// ensure that pip is installed on Linux (happens when python is found but pip not installed)
await setupAptPack("python3-pip")
setupAptPack("python3-pip")
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/vcpkg/vcpkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { InstallationInfo } from "../utils/setup/setupBin"
let hasVCPKG = false

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupVcpkg(_version: string, setupDir: string, _arch: string): Promise<InstallationInfo> {
export function setupVcpkg(_version: string, setupDir: string, _arch: string): InstallationInfo {
if (!hasVCPKG || which.sync("vcpkg", { nothrow: true }) === null) {
if (process.platform === "linux") {
// vcpkg download and extraction dependencies
await setupAptPack("curl")
await setupAptPack("zip")
await setupAptPack("unzip")
await setupAptPack("tar")
await setupAptPack("git")
await setupAptPack("pkg-config")
setupAptPack("curl")
setupAptPack("zip")
setupAptPack("unzip")
setupAptPack("tar")
setupAptPack("git")
setupAptPack("pkg-config")
}

if (!existsSync(join(setupDir, addShellExtension("bootstrap-vcpkg")))) {
Expand All @@ -38,7 +38,7 @@ export async function setupVcpkg(_version: string, setupDir: string, _arch: stri
isRoot() &&
process.env.SUDO_USER !== undefined
) {
await execSudo("chown", ["-R", process.env.SUDO_USER, setupDir], setupDir)
execSudo("chown", ["-R", process.env.SUDO_USER, setupDir], setupDir)
}

addPath(setupDir)
Expand Down

0 comments on commit 18f813f

Please sign in to comment.