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