-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor and add docs for setup-apt functions
- Loading branch information
Showing
17 changed files
with
644 additions
and
491 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { promises } from "fs" | ||
import { execRoot } from "admina" | ||
import { GITHUB_ACTIONS } from "ci-info" | ||
import { sourceRC } from "os-env" | ||
import type { RcOptions } from "os-env/dist/rc-file.js" | ||
const { appendFile } = promises | ||
|
||
/** | ||
* Update the alternatives for a package | ||
* @param name The name of the package | ||
* @param path The path to the binary | ||
* @param priority The priority of the alternative (Defaults to `40`) | ||
*/ | ||
export async function updateAptAlternatives(name: string, path: string, priority: number = 40) { | ||
await execRoot("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, priority.toString()]) | ||
} | ||
|
||
/** | ||
* Add the update-alternatives command to the rc file | ||
* @param name The name of the package | ||
* @param path The path to the binary | ||
* @param rcOptions The options for the rc file to add the update-alternatives command to | ||
* @param priority The priority of the alternative (Defaults to `40`) | ||
*/ | ||
export async function addUpdateAlternativesToRc( | ||
name: string, | ||
path: string, | ||
rcOptions: RcOptions, | ||
priority: number = 40, | ||
) { | ||
if (GITHUB_ACTIONS) { | ||
await updateAptAlternatives(name, path, priority) | ||
} else { | ||
await sourceRC(rcOptions) | ||
await appendFile( | ||
rcOptions.rcPath, | ||
`\nif [ $UID -eq 0 ]; then update-alternatives --install /usr/bin/${name} ${name} ${path} ${priority}; fi\n`, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { execRoot, execRootSync } from "admina" | ||
import { warning } from "ci-log" | ||
import { execa } from "execa" | ||
import { pathExists } from "path-exists" | ||
import { installAptPack } from "./install.js" | ||
|
||
function initGpg() { | ||
execRootSync("gpg", ["-k"]) | ||
} | ||
|
||
/** | ||
* Add an apt key via a keyserver | ||
* @param keys The keys to add | ||
* @param name The name of the key | ||
* @param server The keyserver to use (Defaults to `keyserver.ubuntu.com`) | ||
* @returns The file name of the key that was added or `undefined` if it failed | ||
*/ | ||
export async function addAptKeyViaServer(keys: string[], name: string, server = "keyserver.ubuntu.com") { | ||
try { | ||
const fileName = `/etc/apt/trusted.gpg.d/${name}` | ||
if (!(await pathExists(fileName))) { | ||
initGpg() | ||
|
||
await Promise.all( | ||
keys.map(async (key) => { | ||
await execRoot("gpg", [ | ||
"--no-default-keyring", | ||
"--keyring", | ||
`gnupg-ring:${fileName}`, | ||
"--keyserver", | ||
server, | ||
"--recv-keys", | ||
key, | ||
]) | ||
await execRoot("chmod", ["644", fileName]) | ||
}), | ||
) | ||
} | ||
return fileName | ||
} catch (err) { | ||
warning(`Failed to add apt key via server ${server}: ${err}`) | ||
return undefined | ||
} | ||
} | ||
|
||
/** | ||
* Add an apt key via a download | ||
* @param name The name of the key | ||
* @param url The URL of the key | ||
* @returns The file name of the key that was added | ||
*/ | ||
export async function addAptKeyViaDownload(name: string, url: string) { | ||
const fileName = `/etc/apt/trusted.gpg.d/${name}` | ||
if (!(await pathExists(fileName))) { | ||
initGpg() | ||
await installAptPack([{ name: "curl" }, { name: "ca-certificates" }], undefined) | ||
await execa("curl", ["-s", url, "-o", `/tmp/${name}`]) | ||
execRootSync("gpg", ["--no-default-keyring", "--keyring", `gnupg-ring:${fileName}`, "--import", `/tmp/${name}`]) | ||
execRootSync("chmod", ["644", fileName]) | ||
} | ||
return fileName | ||
} |
Oops, something went wrong.