Debian Codegraph @

doc/modules/guide/pages/packages.adoc

raw
1 [[packages]]
2 = Cockpit Packages
3
4 Cockpit is separated into various packages, each of which brings
5 specific features and/or code.
6
7 [WARNING]
8 ====
9 In addition, any APIs or behavior not explicitly documented here is an
10 internal API and can be changed at any time.
11 ====
12
13 [[package-layout]]
14 == Layout of Package Files
15
16 A package consists of one or more files placed in a directory or its
17 subdirectories. It must have a `+manifest.json+` file and follow certain
18 naming conventions.
19
20 The name of a package is the name of the directory.
21
22 The name of the package must be ASCII alphanumeric, and may contain an
23 underscore. Names of directories and files in the package must consist
24 of ASCII alphanumeric along with dash, underscore, dot, and comma. No
25 spaces are allowed.
26
27 Cockpit uses the data directories from the
28 https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html[XDG
29 Base Directory Specification] to locate packages. The `+$XDG_DATA_DIRS+`
30 represents a colon separate list of system data directories, and
31 `+$XDG_DATA_HOME+` is a user specific data directory. If the environment
32 variables are not set, defaults are used, according to the spec. If
33 cockpit has been built with an alternate `+--prefix=/path+` then the
34 `+$prefix/share/cockpit+` is used by default.
35
36 A `+cockpit/+` subdirectories in any of these data directories is the
37 location where packages are loaded by Cockpit. If Cockpit finds a
38 package with the same name, in multiple data directories, then the first
39 one wins. According to the spec the first data directory is
40 `+$XDG_DATA_HOME+` and then `+$XDG_DATA_DIRS+` in order.
41
42 This means that, by default the following directories are searched for
43 cockpit packages, and in this order:
44
45 * `+~/.local/share/cockpit/+`
46 * `+/usr/local/share/cockpit/+`
47 * `+/usr/share/cockpit/+`
48
49 Packages placed in `+$XDG_DATA_HOME+` are not cached by Cockpit or the
50 web browser. Other packages are cached aggressively, and are accessed
51 using a checksum of the files in the packages and their names.
52
53 You can use the following command to list the packages installed on a
54 server. You'll note that it's output may change when you run the command
55 as different users, if there are packages installed in the user's home
56 directory.
57
58 ....
59 $ cockpit-bridge --packages
60 ...
61 ....
62
63 To further clarify things, here is an example package called
64 "my-package" and its file layout:
65
66 ....
67 /usr/share/cockpit/
68 my-package/
69 manifest.json
70 file.html
71 some.js
72 ....
73
74 Place or symlink packages in your `+~/.local/share/cockpit+` directory
75 (or appropriate `+$XDG_DATA_HOME+` location) that you would like to
76 modify and develop. System installed packages should not change while
77 Cockpit is running.
78
79 == Package Manifest
80
81 Each package has a `+manifest.json+` file. It is a JSON object. The
82 following fields may be present in the manifest:
83
84 content-security-policy::
85 By default Cockpit serves packages using a strict
86 https://en.wikipedia.org/wiki/Content_Security_Policy[Content Security
87 Policy], which among other things does not allow inline styles or
88 scripts. This can be overridden on a per-package basis, with this
89 setting.
90 +
91 If the overridden content security policy does not contain a
92 `+default-src+`, `+connect-src+`, `+base-uri+`, `+form-action+`,
93 `+object-src+`, or `+block-all-mixed-content+` then these will be
94 added to the policy from the manifest.
95 name::
96 An optional string that changes the name of the package. Normally
97 packages derive their name from the directory that they are located
98 in. This field overrides that name.
99 priority::
100 An optional integer that specifies which package is preferred in cases
101 where there are conflicts. For example given two packages with the
102 same `+name+` a package is chosen based on its priority. A higher integer
103 means higher priority.
104 conditions::
105 An optional list of `+{"predicate": "value"}+` objects. Cockpit will
106 only consider the package if _all_ conditions are met. This is
107 preferable to using `+priority+`. +
108 +
109 Currently supported predicates are `+path-exists+` and `+path-not-exists+`
110 (since Cockpit 286) and `+any+` (since Cockpit 348). `+any+` accepts a
111 list of conditions and evaluates to true if _at least one_ of the nested
112 conditions is met. Unknown predicates are ignored.
113
114 requires::
115 An optional JSON object that contains a `+"cockpit"+` string version
116 number. The package will only be usable if the Cockpit bridge and
117 javascript base are equal or newer than the given version number.
118 version::
119 An informational version number for the package.
120 preload::
121 A list of identifiers of the components that should be preloaded.
122 Normally, the files of a component are loaded when the user navigates
123 to it for the first time. The files of a preloaded component are
124 loaded immediately after the user logs in, and the initialization code
125 of the component is invoked.
126 +
127 The value of this field is an array of strings, where each string is
128 one of the keys used in the `+dashboard+`, `+menu+`, or `+tools+`
129 fields that are explained below.
130 parent::
131 This option is used when module does not have its own menu item but is
132 a part of a different module. This is described by JSON object with
133 properties `+component+` which takes name of the superordinate
134 component and `+docs+` with list of documentation URLs for the given
135 page. See below for structure of `+docs+` property.
136
137 In addition, the following keys contain information about where
138 components of the package should appear in Cockpit's user interface.
139 Each of these keys is optional and contains an object mapping unique
140 identifiers to menu items, which are described below. (The naming of
141 these fields doesn't perfectly match the current user interface for
142 historical reasons.)
143
144 dashboard::
145 Dashboard items appear in the menu under the section _Apps_.
146 menu::
147 These items appear in the menu under the section _System_. This
148 section is roughly ordered into these categories (with their _order_
149 in parentheses):
150 +
151 * System Information (10)
152 * Logs (20)
153 * Configuring major subsystems (30-40)
154 * Things running on the machine (VMs, Containers - 50-60)
155 * Implementation Details (Accounts, Services - 70-100)
156 tools::
157 These items appear in the menu under the section _Tools_.
158
159 Menu items and tools are registered using JSON objects that have the
160 following properties:
161
162 label::
163 The label for the menu item or tool.
164 order::
165 An optional order integer to place this menu item or tool. Lower
166 integers are listed first.
167 path::
168 The relative path to the HTML file within the package that implements
169 the menu item or tool.
170 docs::
171 List of documentation URLs for the given page. Each item is an object
172 containing `+label+` and `+url+`.
173 keywords::
174 Keywords that describe the page and which are used for searching.
175 These keywords should be lowercase. Keywords is a list containing
176 keyword items as described below. Page label is prepended as first
177 keyword in the first keyword item.
178
179 Keyword items are registered using JSON objects that have the following
180 properties:
181
182 matches::
183 List of keywords to be matched.
184 goto::
185 Optional path that is used for all keywords in this item. When this
186 argument starts with slash, then it is used as pathname, otherwise it
187 is used as hash. Defining `+goto:"page_hash"+` in page with
188 `+path:"/page_path"+` would redirect to `+/page_path#page_hash+`,
189 while `+goto:"/page_path"+` would redirect to `+/page_path+` ignoring
190 default page path.
191 weight::
192 How much keywords are prioritized over others. Default is 3.
193 translate::
194 `+false+` when keywords should not be localized. Default is `+true+`.
195
196 An example manifest.json with some optional properties set:
197
198 ....
199 {
200 "version": 0,
201 "require": {
202 "cockpit": "120"
203 },
204 "tools": {
205 "mytool": {
206 "label": "My Tool",
207 "path": "tool.html"
208 }
209 }
210 }
211 ....
212
213 ....
214 {
215 "version": 0,
216 "require": {
217 "cockpit": "120"
218 },
219 "conditions": [
220 {"path-exists": "/usr/bin/mytool"},
221 {"path-exists": "/etc/mytool.conf"},
222 {"path-not-exist": "/etc/incompatible-tool"}
223 ],
224 "tools": {
225 "mytool": {
226 "label": "My Tool",
227 "path": "tool.html"
228 }
229 }
230 }
231 ....
232
233 The following example shows how to use disjunctive conditions with `+any+`
234 to specify alternative requirements (available since Cockpit 348). This
235 package will be available if either `+/usr/bin/alt1+` or `+/usr/bin/alt2+`
236 exists. In any case, `+/etc/incompatible-tool+` must not exist.
237
238 ....
239 "conditions": [
240 {"any": [{"path-exists": "/usr/bin/alt1"}, {"path-exists": "/usr/bin/alt2"}]},
241 {"path-not-exists": "/etc/incompatible-tool"}
242 ]
243 ....
244
245 [[package-manifest-override]]
246 == Manifest overrides
247
248 To change a manifest system-wide, a file
249 `+<package-directory-name>.override.json+` may be placed into
250 `+/etc/cockpit/+`, or below `+$XDG_CONFIG_DIRS+` if set (see
251 link:./cockpit.conf.5.html[cockpit.conf]). To change it for a particular
252 user only, put the override into `+~/.config/cockpit+`.
253
254 These override the information in the manifest in the simple
255 https://tools.ietf.org/html/rfc7386[JSON Merge Patch] format.
256
257 This can be used to hide or modify menu items of an existing package.
258 For example `+/etc/cockpit/systemd.override.json+` or
259 `+~/.config/cockpit/systemd.override.json+` could hide the _Logs_ menu
260 item and move the _Services_ menu item to the top of the menu.
261
262 ....
263 {
264 "menu": {
265 "logs": null,
266 "services": {
267 "order": -1
268 }
269 }
270 }
271 ....
272
273 [[package-links]]
274 == Package Links and Paths
275
276 When referring to files in your package, such as in a hyperlink or a
277 `+<style>+` tag or `+<script>+` tag, simply use a relative path, and
278 refer to the files in the same directory. When you need to refer to
279 files in another package use a relative link.
280
281 For example here's how to include the base `+cockpit.js+` script in your
282 HTML from the `+latest+` package:
283
284 ....
285 <script src="../base1/cockpit.js"></script>
286 ....
287
288 Do not assume you can link to any file in any other package. Refer to
289 the xref:development.adoc[list of API packages] for those that are available
290 for use.
291
292 [[package-minified]]
293 == Content Negotiation
294
295 In order to support gzipped and/or minified data, the files in a package
296 are loaded using content negotiation logic. A HTTP request for the file
297 `+test.js+` in the package named `+mypackage+` will return
298 `+mypackage/test.js+` or `+mypackage/test.js.gz+` (in undefined
299 preference). If neither exists, then it returns
300 `+mypackage/test.js.min+` or `+mypackage/test.js.min.gz+` (again in
301 undefined preference).
302
303 When packages are loaded from a system directory, Cockpit optimizes the
304 file system lookups above, by pre-listing the files. This is one of the
305 reasons that you should never change packages installed to a system
306 directory while Cockpit is running.
307
308 [[package-api]]
309 == Using Cockpit API
310
311 Cockpit has API available for writing packages. There is no API
312 available for external callers to invoke via HTTP, REST or otherwise.
313
314 API from various packages can be used to implement Cockpit packages.
315 Each package listed here has some API available for use. Only the API
316 explicitly documented should be used.
317
318 * xref:development.adoc[API Listing]
319
320 To include javascript from the API, simply load it into your HTML using
321 a script tag. Alternatively you can use an javascript loader.
322
323 [[package-bridges]]
324 == Bridges for specific tasks
325
326 On the server side the man:cockpit-bridge[1]
327 connects to various system APIs that the front end UI requests it to.
328 There are additional bridges for specific tasks that the main
329 `+cockpit-bridge+` cannot handle, such as using the PCP C library API.
330
331 These additional bridges can be registered in a `+"bridges"+` section of
332 a package's `+manifest.json+` file. Building such a bridge is a complex
333 tasks, and we will skip over that here. However it is useful to adjust
334 how these additional bridges are called, and so we'll look at how they
335 are registered.
336
337 An example `+manifest.json+` with a bridges section:
338
339 ....
340 {
341 "bridges": [
342 {
343 "match": { "payload": "metrics1" },
344 "spawn": [ "/usr/libexec/cockpit-pcp" ]
345 }
346 ]
347 }
348 ....
349
350 The bridges are considered in the order they are listed in the array.
351 Use the `+manifest.json+``+"priority"+` field to control order between
352 packages. The bridges are registered using JSON objects that have the
353 following properties:
354
355 environ::
356 Optional, additional environment variables to pass to the bridge
357 command.
358 match::
359 The `+"match"+` object describes which channel open command options
360 need to match for a given channel to be handed over to this bridge.
361 privileged::
362 If set to `+true+`, this marks the bridge as a superuser bridge.
363 Cockpit will start one of these explicitly when trying to escalate the
364 privileges of a session. A privileged bridge can not have a
365 `+"match"+` property.
366 label::
367 Setting this enables selection of privileged bridges in the UI. When
368 no privileged bridge has a `+label+`, then Cockpit will start the
369 bridge that runs `+sudo+`. This is the case in a default Cockpit
370 installation. When at least one privileged bridge has a `+label+` then
371 the user can select one of them when escalating privileges. As a
372 special case, if only one bridge has a `+label+`, then the step of
373 selecting a bridge is omitted in the UI and that one bridge is always
374 started.
375 +
376 Thus, if you add a privileged bridge with a `+label+` in a new
377 manifest, Cockpit will use that bridge the next time a user opens the
378 "Administrative access" dialog. If you want the user to choose between
379 the `+sudo+` method and your new one, you need to duplicate the
380 `+sudo+` bridge definition in your manifest and give it a label.
381 problem::
382 If a problem is specified, and this bridge fails to start up then
383 channels will be closed with this problem code. Otherwise later
384 bridges or internal handlers for the channel will be invoked.
385 spawn::
386 The command and arguments to invoke.
387
388 The `+spawn+` and `+environ+` values can be dynamically taken from a
389 matching open command values. When a value in either the `+spawn+` or
390 `+environ+` array contains a named variable wrapped in `+${}+`, the
391 variable will be replaced with the value contained in the matching open
392 command. Only named variables are supported and name can only contain
393 letters, numbers and the following symbols: `+._-+`
394
395 For example a bridges section like:
396
397 ....
398 {
399 "bridges": [
400 {
401 "match": { "payload": "example" },
402 "environ": [ "TAG=${tag}" ],
403 "spawn: [ "/example-bridge", "--tag", "${tag}" ],
404 "problem": "access-denied"
405 }
406 ]
407 }
408 ....
409
410 when a open command is received with a payload of `+example+` with
411 `+tag+` value of `+tag1+`. The following command will be spawned
412
413 ....
414 TAG=tag1 /example-bridge --tag tag1
415 ....
416
417 Processes that are reused so if another open command with a "tag" of
418 `+tag1+` is received. The open command will be passed to existing
419 process, rather than spawning a new one. However a open command with an
420 tag of `+tag2+` will spawn a new command:
421
422 ....
423 TAG=tag2 /example-bridge --tag tag2
424 ....
425
426 If you need to include `+${}+`, as an actual value in your arguments you
427 can escape it by prefixing it with a `+\+`
428
429 [[package-replace]]
430 == Replacing an existing package
431
432 If the functionality in a package replaces that of another package then
433 it can replace that package by claiming the same `+name+` and a higher
434 `+priority+`.
435
436 For example, a package in the `+/usr/share/cockpit/disks+` directory
437 could replace Cockpit's _storage_ package with a `+manifest.json+` like
438 this:
439
440 ....
441 {
442 "version": 0,
443 "name": "storage",
444 "priority": 10,
445 "menu": {
446 "index": {
447 "label": "Disk Storage",
448 "order": 15
449 }
450 }
451 }
452 ....