Skip to content

Commit 2d27334

Browse files
committed
refactor: refactor and add docs for setup-apt functions
1 parent 1865b24 commit 2d27334

15 files changed

+643
-490
lines changed

dist/actions/setup-cpp.js

+28-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

+28-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

+28-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/setup-apt/README.md

+86-32
Original file line numberDiff line numberDiff line change
@@ -19,76 +19,130 @@ npm install --save setup-apt
1919

2020
<!-- INSERT GENERATED DOCS START -->
2121

22+
### `updateAptAlternatives` (function)
23+
24+
Update the alternatives for a package
25+
26+
**Parameters:**
27+
28+
- name (`string`) - The name of the package
29+
- path (`string`) - The path to the binary
30+
- priority (`number`) - The priority of the alternative (Defaults to `40`)
31+
32+
**returns:** Promise<void>
33+
34+
### `addUpdateAlternativesToRc` (function)
35+
36+
Add the update-alternatives command to the rc file
37+
38+
**Parameters:**
39+
40+
- name (`string`) - The name of the package
41+
- path (`string`) - The path to the binary
42+
- rcOptions (`RcOptions`) - The options for the rc file to add the update-alternatives command to
43+
- priority (`number`) - The priority of the alternative (Defaults to `40`)
44+
45+
**returns:** Promise<void>
46+
47+
### `isAptPackInstalled` (function)
48+
49+
Check if a package is installed
50+
51+
**Parameters:**
52+
53+
- pack (`string`) - The package to check
54+
55+
**returns:** Promise<boolean>
56+
57+
### `isAptPackRegexInstalled` (function)
58+
59+
Check if a package matching a regexp is installed
60+
61+
**Parameters:**
62+
63+
- regexp (`string`) - The regexp to check
64+
65+
**returns:** Promise<boolean>
66+
67+
### `updateRepos` (function)
68+
69+
Update the apt repositories
70+
71+
**Parameters:**
72+
73+
- apt (`string`) - The apt command to use (optional)
74+
75+
**returns:** void
76+
2277
### `InstallationInfo` (type)
2378

79+
The information about an installation result
80+
2481
### `aptTimeout` (variable)
2582

83+
The timeout to use for apt commands
84+
Wait up to 300 seconds if the apt-get lock is held
85+
2686
### `AptPackage` (type)
2787

88+
The information about an apt package
89+
2890
### `installAptPack` (function)
2991

30-
A function that installs a package using apt
92+
Install a package using apt
3193

3294
**Parameters:**
3395

34-
- packages (`AptPackage[]`)
35-
- update (`boolean`)
96+
- packages (`AptPackage[]`) - The packages to install (name, and optional info like version and repositories)
97+
- update (`boolean`) - Whether to update the package list before installing (Defaults to `false`)
3698

3799
**returns:** Promise<InstallationInfo>
38100

39101
### `hasNala` (function)
40102

103+
Check if nala is installed
104+
41105
**returns:** boolean
42106

43107
### `getApt` (function)
44108

45-
**returns:** string
46-
47-
### `addAptKeyViaServer` (function)
48-
49-
**Parameters:**
109+
Get the apt command to use
110+
If nala is installed, use that, otherwise use apt-get
50111

51-
- keys (`string[]`)
52-
- name (`string`)
53-
- server (`string`)
112+
**returns:** string
54113

55-
**returns:** Promise<string>
114+
### `getEnv` (function)
56115

57-
### `addAptKeyViaDownload` (function)
116+
Get the environment variables to use for the apt command
58117

59118
**Parameters:**
60119

61-
- name (`string`)
62-
- url (`string`)
120+
- apt (`string`) - The apt command to use
63121

64-
**returns:** Promise<string>
65-
66-
### `updateAptAlternatives` (function)
67-
68-
**Parameters:**
69-
70-
- name (`string`)
71-
- path (`string`)
72-
- rcOptions (`RcOptions`)
73-
- priority (`number`)
122+
**returns:** ProcessEnv
74123

75-
**returns:** Promise<void>
124+
### `addAptKeyViaServer` (function)
76125

77-
### `isAptPackInstalled` (function)
126+
Add an apt key via a keyserver
78127

79128
**Parameters:**
80129

81-
- pack (`string`)
130+
- keys (`string[]`) - The keys to add
131+
- name (`string`) - The name of the key
132+
- server (`string`) - The keyserver to use (Defaults to `keyserver.ubuntu.com`)
82133

83-
**returns:** Promise<boolean>
134+
**returns:** Promise<string>
84135

85-
### `isAptPackRegexInstalled` (function)
136+
### `addAptKeyViaDownload` (function)
137+
138+
Add an apt key via a download
86139

87140
**Parameters:**
88141

89-
- regexp (`string`)
142+
- name (`string`) - The name of the key
143+
- url (`string`) - The URL of the key
90144

91-
**returns:** Promise<boolean>
145+
**returns:** Promise<string>
92146

93147
<!-- INSERT GENERATED DOCS END -->
94148

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { promises } from "fs"
2+
import { execRoot } from "admina"
3+
import { GITHUB_ACTIONS } from "ci-info"
4+
import { sourceRC } from "os-env"
5+
import type { RcOptions } from "os-env/dist/rc-file.js"
6+
const { appendFile } = promises
7+
8+
/**
9+
* Update the alternatives for a package
10+
* @param name The name of the package
11+
* @param path The path to the binary
12+
* @param priority The priority of the alternative (Defaults to `40`)
13+
*/
14+
export async function updateAptAlternatives(name: string, path: string, priority: number = 40) {
15+
await execRoot("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, priority.toString()])
16+
}
17+
18+
/**
19+
* Add the update-alternatives command to the rc file
20+
* @param name The name of the package
21+
* @param path The path to the binary
22+
* @param rcOptions The options for the rc file to add the update-alternatives command to
23+
* @param priority The priority of the alternative (Defaults to `40`)
24+
*/
25+
export async function addUpdateAlternativesToRc(
26+
name: string,
27+
path: string,
28+
rcOptions: RcOptions,
29+
priority: number = 40,
30+
) {
31+
if (GITHUB_ACTIONS) {
32+
await updateAptAlternatives(name, path, priority)
33+
} else {
34+
await sourceRC(rcOptions)
35+
await appendFile(
36+
rcOptions.rcPath,
37+
`\nif [ $UID -eq 0 ]; then update-alternatives --install /usr/bin/${name} ${name} ${path} ${priority}; fi\n`,
38+
)
39+
}
40+
}

packages/setup-apt/src/apt-key.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { execRoot, execRootSync } from "admina"
2+
import { warning } from "ci-log"
3+
import { execa } from "execa"
4+
import { pathExists } from "path-exists"
5+
import { installAptPack } from "./install.js"
6+
7+
function initGpg() {
8+
execRootSync("gpg", ["-k"])
9+
}
10+
11+
/**
12+
* Add an apt key via a keyserver
13+
* @param keys The keys to add
14+
* @param name The name of the key
15+
* @param server The keyserver to use (Defaults to `keyserver.ubuntu.com`)
16+
* @returns The file name of the key that was added or `undefined` if it failed
17+
*/
18+
export async function addAptKeyViaServer(keys: string[], name: string, server = "keyserver.ubuntu.com") {
19+
try {
20+
const fileName = `/etc/apt/trusted.gpg.d/${name}`
21+
if (!(await pathExists(fileName))) {
22+
initGpg()
23+
24+
await Promise.all(
25+
keys.map(async (key) => {
26+
await execRoot("gpg", [
27+
"--no-default-keyring",
28+
"--keyring",
29+
`gnupg-ring:${fileName}`,
30+
"--keyserver",
31+
server,
32+
"--recv-keys",
33+
key,
34+
])
35+
await execRoot("chmod", ["644", fileName])
36+
}),
37+
)
38+
}
39+
return fileName
40+
} catch (err) {
41+
warning(`Failed to add apt key via server ${server}: ${err}`)
42+
return undefined
43+
}
44+
}
45+
46+
/**
47+
* Add an apt key via a download
48+
* @param name The name of the key
49+
* @param url The URL of the key
50+
* @returns The file name of the key that was added
51+
*/
52+
export async function addAptKeyViaDownload(name: string, url: string) {
53+
const fileName = `/etc/apt/trusted.gpg.d/${name}`
54+
if (!(await pathExists(fileName))) {
55+
initGpg()
56+
await installAptPack([{ name: "curl" }, { name: "ca-certificates" }], undefined)
57+
await execa("curl", ["-s", url, "-o", `/tmp/${name}`])
58+
execRootSync("gpg", ["--no-default-keyring", "--keyring", `gnupg-ring:${fileName}`, "--import", `/tmp/${name}`])
59+
execRootSync("chmod", ["644", fileName])
60+
}
61+
return fileName
62+
}

0 commit comments

Comments
 (0)