Debian Codegraph @

pkg/systemd/service.jsx

raw
1 /*
2 * Copyright (C) 2020 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React, { useRef, useState } from "react";
7 import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js";
8 import { PageBreadcrumb, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
9 import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js";
10 import { ExclamationCircleIcon } from '@patternfly/react-icons';
11
12 import { EmptyStatePanel } from "cockpit-components-empty-state.jsx";
13 import { ServiceDetails } from "./service-details.jsx";
14 import { LogsPanel } from "cockpit-components-logs-panel.jsx";
15 import { superuser } from 'superuser';
16 import { WithDialogs } from "dialogs.jsx";
17
18 import cockpit from "cockpit";
19 import { useObject } from "hooks";
20
21 import s_bus from "./busnames.js";
22
23 const _ = cockpit.gettext;
24
25 function debug() {
26 if (window.debugging == "all" || window.debugging?.includes("service-details")) // not-covered: debugging
27 console.debug.apply(console, arguments); // not-covered: debugging
28 }
29
30 export const Service = ({ dbusClient, owner, unitId, unitIsValid, addTimerProperties, pinnedUnits }) => {
31 const _path = useRef(null);
32 const [error, setError] = useState(null);
33 const [reloading, setReloading] = useState(false);
34 const [unitProps, setUnitProps] = useState(null);
35
36 const updateProperties = () => {
37 const path = _path.current?.path;
38 const promises = [dbusClient.call(path, s_bus.I_PROPS, "GetAll", [s_bus.I_UNIT])];
39
40 if (unitId.endsWith(".timer"))
41 promises.push(dbusClient.call(path, s_bus.I_PROPS, "GetAll", [s_bus.I_TIMER]));
42 else if (unitId.endsWith(".socket"))
43 promises.push(dbusClient.call(path, s_bus.I_PROPS, "GetAll", [s_bus.I_SOCKET]));
44
45 Promise.all(promises)
46 .then(replies => {
47 const unit_props = replies[0][0];
48
49 // unwrap variants
50 for (const key in unit_props)
51 unit_props[key] = unit_props[key].v;
52
53 if (unitId.endsWith(".timer"))
54 addTimerProperties(replies[1][0], unit_props);
55 else if (unitId.endsWith(".socket"))
56 unit_props.Listen = replies[1][0].Listen.v;
57
58 unit_props.path = path;
59
60 debug("Service detail", unitId, "updated properties:", JSON.stringify(unit_props));
61 setUnitProps(unit_props);
62 setError(null);
63 })
64 .catch(ex => setError(ex.toString())); // not-covered: unexpected OS error
65 };
66
67 // load object path and set up PropertiesChanged subscription whenever unitId changes
68 useObject(
69 () => {
70 dbusClient.call(s_bus.O_MANAGER, s_bus.I_MANAGER, "LoadUnit", [unitId])
71 .then(([path]) => {
72 debug("Service detail", unitId, "loaded path", path);
73
74 _path.current?.propSub.remove();
75 const propSub = dbusClient.subscribe(
76 { path, interface: s_bus.I_PROPS, member: "PropertiesChanged" }, () => {
77 debug("Service detail", unitId, "PropertiesChanged; path", path);
78 updateProperties();
79 });
80
81 _path.current = { unitId, path, propSub };
82
83 // initial load
84 updateProperties();
85 })
86 .catch(ex => setError(ex.toString())); // not-covered: unexpected OS error
87 },
88 null, [unitId]);
89
90 useObject(
91 () => dbusClient.subscribe(
92 { interface: s_bus.I_MANAGER, member: "Reloading" },
93 (_, _i, _s, [is_reloading]) => {
94 debug("Service detail", unitId, "Reloading", is_reloading, "current unit", _path.current?.path);
95 setReloading(is_reloading);
96 if (!is_reloading && _path.current)
97 updateProperties();
98 }),
99 sub => sub.remove(), []);
100
101 /* We need this *only* to pick up failed Conditions after attempting to start a service, as the unit immediately gets
102 * unloaded again and we don't get a PropertiesChanged signal. */
103 useObject(
104 () => dbusClient.subscribe(
105 { interface: s_bus.I_MANAGER, member: "JobRemoved" }, (_p, _i, _s, [_job_id, _job_path, unit, result]) => {
106 if (result === "done" && unitId === _path.current?.unitId) {
107 debug("Service detail", unitId, "JobRemoved", _path.current?.path);
108 updateProperties();
109 }
110 }),
111 sub => sub.remove(), []);
112
113 // render
114 if (error)
115 return <EmptyStatePanel title={_("Loading unit failed")} icon={ExclamationCircleIcon} paragraph={error} />; // not-covered: unexpected OS error
116
117 if (unitProps === null)
118 return <EmptyStatePanel loading title={_("Loading...")} paragraph={unitId} />;
119
120 // resolve Alias name to primary ID
121 const cur_unit_id = unitProps.Id;
122
123 const unit_type = owner == "system" ? "UNIT" : "USER_UNIT";
124 const match = [
125 "_SYSTEMD_" + unit_type + "=" + cur_unit_id, "+",
126 "COREDUMP_" + unit_type + "=" + cur_unit_id, "+",
127 unit_type + "=" + cur_unit_id,
128 ];
129 const service_type = owner == "system" ? "service" : "user-service";
130 const url = "/system/logs/#/?prio=debug&" + service_type + "=" + cur_unit_id;
131 const load_state = unitProps.LoadState;
132
133 return (
134 <WithDialogs>
135 <PageBreadcrumb hasBodyWrapper={false} stickyOnBreakpoint={{ default: "top" }}>
136 <Breadcrumb>
137 <BreadcrumbItem to={"#" + cockpit.location.href.replace(/\/[^?]*/, '')}>{_("Services")}</BreadcrumbItem>
138 <BreadcrumbItem isActive>
139 {cur_unit_id}
140 </BreadcrumbItem>
141 </Breadcrumb>
142 </PageBreadcrumb>
143 <PageSection hasBodyWrapper={false} id="service-details">
144 <Stack hasGutter>
145 <ServiceDetails unit={unitProps}
146 owner={owner}
147 permitted={superuser.allowed}
148 loadingUnits={reloading}
149 isValid={unitIsValid}
150 pinnedUnits={pinnedUnits} />
151 {(load_state === "loaded" || load_state === "masked") &&
152 <LogsPanel title={_("Service logs")} match={match} emptyMessage={_("No log entries")} max={10} goto_url={url} search_options={{ prio: "debug", [service_type]: cur_unit_id }} />}
153 </Stack>
154 </PageSection>
155 </WithDialogs>
156 );
157 };