fix: wait for apt lock if it is held

This commit is contained in:
Amin Yahyaabadi 2024-08-12 02:42:51 -07:00
parent 7e559c044f
commit a93caaa4ac
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
7 changed files with 25 additions and 22 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -13,6 +13,9 @@ import type { InstallationInfo } from "./setupBin"
let didUpdate: boolean = false let didUpdate: boolean = false
let didInit: boolean = false let didInit: boolean = false
// wait up to 300 seconds if the apt-get lock is held
const aptTimeout = "DPkg::Lock::Timeout=300"
export type AptPackage = { export type AptPackage = {
name: string name: string
version?: string version?: string
@ -65,7 +68,7 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
const stderr = (err as ExecaError).stderr const stderr = (err as ExecaError).stderr
if (retryErrors.some((error) => stderr.includes(error))) { if (retryErrors.some((error) => stderr.includes(error))) {
warning(`Failed to install packages ${needToInstall}. Retrying...`) warning(`Failed to install packages ${needToInstall}. Retrying...`)
execRootSync(apt, ["install", "--fix-broken", "-y", ...needToInstall]) execRootSync(apt, ["install", "--fix-broken", "-y", "-o", aptTimeout, ...needToInstall])
} }
} else { } else {
throw err throw err
@ -104,10 +107,10 @@ async function addRepositories(apt: string, packages: AptPackage[]) {
await initApt(apt) await initApt(apt)
didInit = true didInit = true
} }
await installAddAptRepo() await installAddAptRepo(apt)
for (const repo of allRepositories) { for (const repo of allRepositories) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
execRootSync("add-apt-repository", ["-y", repo]) execRootSync("add-apt-repository", ["-y", "--no-update", repo])
} }
updateRepos(apt) updateRepos(apt)
didUpdate = true didUpdate = true
@ -185,14 +188,14 @@ function getApt() {
} }
function updateRepos(apt: string) { function updateRepos(apt: string) {
execRootSync(apt, apt !== "nala" ? ["update", "-y"] : ["update"]) execRootSync(apt, apt !== "nala" ? ["update", "-y", "-o", aptTimeout] : ["update", "-o", aptTimeout])
} }
async function installAddAptRepo() { async function installAddAptRepo(apt: string) {
if (await isPackageInstalled("software-properties-common")) { if (await isPackageInstalled("software-properties-common")) {
return return
} }
execRootSync("apt-get", ["install", "-y", "--fix-broken", "software-properties-common"]) execRootSync(apt, ["install", "-y", "--fix-broken", "-o", aptTimeout, "software-properties-common"])
} }
/** Install gnupg and certificates (usually missing from docker containers) */ /** Install gnupg and certificates (usually missing from docker containers) */
@ -210,7 +213,7 @@ async function initApt(apt: string) {
]) ])
if (toInstall.length !== 0) { if (toInstall.length !== 0) {
execRootSync(apt, ["install", "-y", "--fix-broken", ...toInstall]) execRootSync(apt, ["install", "-y", "--fix-broken", "-o", aptTimeout, ...toInstall])
} }
const promises: Promise<string | void>[] = [ const promises: Promise<string | void>[] = [