forked from chabad360/cockpit-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.jsx
630 lines (574 loc) · 23.9 KB
/
app.jsx
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
* This file is part of Cockpit.
*
* Copyright (C) 2017 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import { Page, PageSection, PageSectionVariants } from "@patternfly/react-core/dist/esm/components/Page";
import { Alert, AlertActionCloseButton, AlertActionLink, AlertGroup } from "@patternfly/react-core/dist/esm/components/Alert";
import { Button } from "@patternfly/react-core/dist/esm/components/Button";
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox";
import { EmptyState, EmptyStateHeader, EmptyStateFooter, EmptyStateIcon, EmptyStateActions, EmptyStateVariant } from "@patternfly/react-core/dist/esm/components/EmptyState";
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack";
import { ExclamationCircleIcon } from '@patternfly/react-icons';
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner";
import { WithDialogs } from "dialogs.jsx";
import cockpit from 'cockpit';
import { superuser } from "superuser";
import ContainerHeader from './ContainerHeader.jsx';
import Containers from './Containers.jsx';
import Images from './Images.jsx';
import * as client from './client.js';
import { WithDockerInfo } from './util.js';
const _ = cockpit.gettext;
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {
serviceAvailable: null,
enableService: true,
images: null,
imagesLoaded: false,
containers: null,
containersFilter: "all",
containersStats: {},
containersLoaded: null,
textFilter: "",
ownerFilter: "all",
dropDownValue: 'Everything',
notifications: [],
showStartService: true,
version: '1.3.0',
selinuxAvailable: false,
dockerRestartAvailable: false,
currentUser: _("User"),
privileged: false,
hasDockerGroup: false,
location: {},
};
this.onAddNotification = this.onAddNotification.bind(this);
this.onDismissNotification = this.onDismissNotification.bind(this);
this.onFilterChanged = this.onFilterChanged.bind(this);
this.onContainerFilterChanged = this.onContainerFilterChanged.bind(this);
this.updateContainer = this.updateContainer.bind(this);
this.startService = this.startService.bind(this);
this.goToServicePage = this.goToServicePage.bind(this);
this.onNavigate = this.onNavigate.bind(this);
this.pendingUpdateContainer = {}; // id → promise
}
onAddNotification(notification) {
notification.index = this.state.notifications.length;
this.setState(prevState => ({
notifications: [
...prevState.notifications,
notification
]
}));
}
onDismissNotification(notificationIndex) {
const notificationsArray = this.state.notifications.concat();
const index = notificationsArray.findIndex(current => current.index == notificationIndex);
if (index !== -1) {
notificationsArray.splice(index, 1);
this.setState({ notifications: notificationsArray });
}
}
updateUrl(options) {
cockpit.location.go([], options);
}
onFilterChanged(value) {
this.setState({
textFilter: value
});
const options = this.state.location;
if (value === "") {
delete options.name;
this.updateUrl(Object.assign(options));
} else {
this.updateUrl(Object.assign(this.state.location, { name: value }));
}
}
onContainerFilterChanged(value) {
this.setState({
containersFilter: value
});
const options = this.state.location;
if (value == "running") {
delete options.container;
this.updateUrl(Object.assign(options));
} else {
this.updateUrl(Object.assign(options, { container: value }));
}
}
updateState(state, id, newValue) {
this.setState(prevState => {
return {
[state]: { ...prevState[state], [id]: newValue }
};
});
}
updateContainerStats(id) {
client.streamContainerStats(id, reply => {
if (reply.Error != null) // executed when container stop
console.warn("Failed to update container stats:", JSON.stringify(reply.message));
else {
this.updateState("containersStats", id, reply);
}
}).catch(ex => {
if (ex.cause == "no support for CGroups V1 in rootless environments" || ex.cause == "Container stats resource only available for cgroup v2") {
console.log("This OS does not support CgroupsV2. Some information may be missing.");
} else
console.warn("Failed to update container stats:", JSON.stringify(ex.message));
});
}
initContainers() {
return client.getContainers()
.then(containerList => Promise.all(
containerList.map(container => client.inspectContainer(container.Id))
))
.then(containerDetails => {
this.setState(prevState => {
const copyContainers = prevState.containers || {};
for (const detail of containerDetails) {
copyContainers[detail.Id] = detail;
this.updateContainerStats(detail.Id);
}
return {
containers: copyContainers,
containersLoaded: true,
};
});
})
.catch(console.log);
}
updateImages() {
client.getImages()
.then(reply => {
this.setState(prevState => {
// Copy only images that could not be deleted with this event
const copyImages = prevState.images || {};
Object.entries(reply).forEach(([Id, image]) => {
copyImages[Id] = image;
});
return {
images: copyImages,
imagesLoaded: true
};
});
})
.catch(ex => {
console.warn("Failed to do Update Images:", JSON.stringify(ex));
});
}
updateContainer(id, event) {
/* when firing off multiple calls in parallel, docker can return them in a random order.
* This messes up the state. So we need to serialize them for a particular container. */
const idx = id;
const wait = this.pendingUpdateContainer[idx] ?? Promise.resolve();
const new_wait = wait.then(() => client.inspectContainer(id))
.then(details => {
// HACK: during restart State never changes from "running"
// override it to reconnect console after restart
if (event?.Action === "restart")
details.State.Status = "restarting";
this.updateState("containers", idx, details);
})
.catch(console.log);
this.pendingUpdateContainer[idx] = new_wait;
new_wait.finally(() => { delete this.pendingUpdateContainer[idx] });
return new_wait;
}
updateImage(id) {
client.getImages(id)
.then(reply => {
const image = reply[id];
this.updateState("images", id, image);
})
.catch(ex => {
console.warn("Failed to do Update Image:", JSON.stringify(ex));
});
}
// see https://docs.podman.io/en/latest/markdown/podman-events.1.html
handleImageEvent(event) {
switch (event.Action) {
case 'push':
case 'save':
case 'tag':
this.updateImage(event.Actor.ID);
break;
case 'pull': // Pull event has not event.id
case 'untag':
case 'delete':
case 'remove':
case 'prune':
case 'build':
this.updateImages();
break;
default:
console.warn('Unhandled event type ', event.Type, event.Action);
}
}
handleContainerEvent(event) {
if (event.Action.includes(':'))
event.Action = event.Action.split(':')[0];
const id = event.Actor.ID;
switch (event.Action) {
/* The following events do not need to trigger any state updates */
case 'attach':
case 'exec':
case 'export':
case 'import':
case 'resize':
case 'init':
case 'kill':
case 'mount':
case 'prune':
case 'restart':
case 'sync':
case 'unmount':
case 'wait':
break;
/* The following events need only to update the Container list
* We do get the container affected in the event object but for
* now we 'll do a batch update
*/
case 'exec_start':
case 'start':
this.updateContainer(id, event);
break;
case 'checkpoint':
case 'cleanup':
case 'exec_create':
case 'create':
case 'died':
case 'die':
case 'exec_die':
case 'exec_died': // HACK: pick up health check runs with older podman versions, see https://github.com/containers/podman/issues/19237
case 'health_status':
case 'pause':
case 'restore':
case 'stop':
case 'unpause':
case 'rename': // rename event is available starting podman v4.1; until then the container does not get refreshed after renaming
this.updateContainer(id, event);
break;
case 'remove':
this.setState(prevState => {
const containers = { ...prevState.containers };
delete containers[id];
let pods;
return { containers, pods };
});
break;
// only needs to update the Image list, this ought to be an image event
case 'commit':
this.updateImages();
break;
default:
console.warn('Unhandled event type ', event.Type, event.Action);
}
}
handleEvent(event) {
switch (event.Type) {
case 'container':
this.handleContainerEvent(event);
break;
case 'image':
this.handleImageEvent(event);
break;
default:
console.warn('Unhandled event type ', event.Type);
}
}
cleanupAfterService(key) {
["images", "containers"].forEach(t => {
if (this.state[t])
this.setState(prevState => {
const copy = {};
Object.entries(prevState[t] || {}).forEach(([id, v]) => {
copy[id] = v;
});
return { [t]: copy };
});
});
}
init() {
client.getInfo()
.then(reply => {
this.setState({
serviceAvailable: true,
version: reply.ServerVersion,
registries: reply.RegistryConfig.IndexConfigs,
cgroupVersion: reply.CgroupVersion,
});
this.updateImages();
this.initContainers();
client.streamEvents(message => this.handleEvent(message))
.then(() => {
this.setState({ serviceAvailable: false });
this.cleanupAfterService();
})
.catch(e => {
console.log(e);
this.setState({ serviceAvailable: false });
this.cleanupAfterService();
});
// Listen if docker is still running
const ch = cockpit.channel({ payload: "stream", unix: client.getAddress() });
ch.addEventListener("close", () => {
this.setState({ serviceAvailable: false });
this.cleanupAfterService();
});
ch.send("GET " + client.VERSION + "/events HTTP/1.0\r\nContent-Length: 0\r\n\r\n");
})
.catch((r) => {
console.log("Failed to get info from docker", r);
this.setState({
serviceAvailable: false,
containersLoaded: true,
imagesLoaded: true
});
});
}
componentDidMount() {
cockpit.script("[ `id -u` -eq 0 ] || [ `id -nG | grep -qw docker; echo $?` -eq 0 ]; echo $?")
.done(result => {
const hasDockerGroup = result.trim() === "0";
this.setState({ hasDockerGroup });
if (hasDockerGroup) {
this.init();
}
})
.fail(e => console.log("Could not read $XDG_RUNTIME_DIR: ", e.message));
cockpit.spawn("selinuxenabled", { error: "ignore" })
.then(() => this.setState({ selinuxAvailable: true }))
.catch(() => this.setState({ selinuxAvailable: false }));
cockpit.spawn(["systemctl", "show", "--value", "-p", "LoadState", "docker"], { environ: ["LC_ALL=C"], error: "ignore" })
.then(out => this.setState({ dockerRestartAvailable: out.trim() === "loaded" }));
superuser.addEventListener("changed", () => this.setState({ privileged: !!superuser.allowed }));
this.setState({ privileged: superuser.allowed });
cockpit.addEventListener("locationchanged", this.onNavigate);
this.onNavigate();
}
componentWillUnmount() {
cockpit.removeEventListener("locationchanged", this.onNavigate);
}
onNavigate() {
// HACK: Use usePageLocation when this is rewritten into a functional component
const { options, path } = cockpit.location;
this.setState({ location: options }, () => {
// only use the root path
if (path.length === 0) {
if (options.name) {
this.onFilterChanged(options.name);
}
if (options.container) {
this.onContainerFilterChanged(options.container);
}
}
});
}
startService(e) {
if (!e || e.button !== 0)
return;
let argv;
if (this.state.enableService)
argv = ["systemctl", "enable", "--now", "docker.socket"];
else
argv = ["systemctl", "start", "docker.socket"];
cockpit.spawn(argv, { superuser: "require", err: "message" })
.then(() => this.init())
.catch(err => {
this.setState({
serviceAvailable: false,
containersLoaded: true,
imagesLoaded: true
});
console.warn("Failed to start docker.socket:", JSON.stringify(err));
});
}
goToServicePage(e) {
if (!e || e.button !== 0)
return;
cockpit.jump("/system/services#/docker.socket");
}
render() {
if (!this.state.hasDockerGroup) {
return (
<Page>
<PageSection variant={PageSectionVariants.light}>
<EmptyState variant={EmptyStateVariant.full}>
<EmptyStateHeader titleText={_("You are not a member of the docker group")} icon={<EmptyStateIcon icon={ExclamationCircleIcon} />} headingLevel="h2" />
<EmptyStateFooter>
<Button onClick={() => cockpit.jump("/users")}>
{_("Manage users")}
</Button>
</EmptyStateFooter>
</EmptyState>
</PageSection>
</Page>
);
}
if (this.state.serviceAvailable === null) // not detected yet
return (
<Page>
<PageSection variant={PageSectionVariants.light}>
<EmptyState variant={EmptyStateVariant.full}>
{/* loading spinner */}
<Spinner size="xl" />
<EmptyStateHeader titleText={_("Loading...")} />
</EmptyState>
</PageSection>
</Page>
);
if (!this.state.serviceAvailable) {
return (
<Page>
<PageSection variant={PageSectionVariants.light}>
<EmptyState variant={EmptyStateVariant.full}>
<EmptyStateHeader titleText={_("Docker service is not active")} icon={<EmptyStateIcon icon={ExclamationCircleIcon} />} headingLevel="h2" />
<EmptyStateFooter>
<Checkbox isChecked={this.state.enableService}
id="enable"
label={_("Automatically start docker on boot")}
onChange={ (_event, checked) => this.setState({ enableService: checked }) } />
<Button onClick={this.startService}>
{_("Start docker")}
</Button>
{ cockpit.manifests.system &&
<EmptyStateActions>
<Button variant="link" onClick={this.goToServicePage}>
{_("Troubleshoot")}
</Button>
</EmptyStateActions>
}
</EmptyStateFooter>
</EmptyState>
</PageSection>
</Page>
);
}
let imageContainerList = {};
if (this.state.containers !== null) {
Object.keys(this.state.containers).forEach(c => {
const container = this.state.containers[c];
const image = container.Image;
if (imageContainerList[image]) {
imageContainerList[image].push({
container,
stats: this.state.containersStats[container.Id],
});
} else {
imageContainerList[image] = [{
container,
stats: this.state.containersStats[container.Id]
}];
}
});
} else
imageContainerList = null;
let startService = "";
const action = (
<>
<AlertActionLink variant='secondary' onClick={this.startService}>{_("Start")}</AlertActionLink>
<AlertActionCloseButton onClose={() => this.setState({ showStartService: false })} />
</>
);
if (!this.state.serviceAvailable && this.state.privileged) {
startService = (
<Alert
title={_("Docker service is available")}
actionClose={action} />
);
}
const imageList = (
<Images
key="imageList"
images={this.state.imagesLoaded ? this.state.images : null}
imageContainerList={imageContainerList}
onAddNotification={this.onAddNotification}
textFilter={this.state.textFilter}
ownerFilter={this.state.ownerFilter}
showAll={ () => this.setState({ containersFilter: "all" }) }
user={this.state.currentUser}
serviceAvailable={this.state.serviceAvailable}
/>
);
const containerList = (
<Containers
key="containerList"
version={this.state.version}
images={this.state.imagesLoaded ? this.state.images : null}
containers={this.state.containersLoaded ? this.state.containers : null}
containersStats={this.state.containersStats}
filter={this.state.containersFilter}
handleFilterChange={this.onContainerFilterChanged}
textFilter={this.state.textFilter}
ownerFilter={this.state.ownerFilter}
user={this.state.currentUser}
onAddNotification={this.onAddNotification}
serviceAvailable={this.state.serviceAvailable}
cgroupVersion={this.state.cgroupVersion}
updateContainer={this.updateContainer}
/>
);
const notificationList = (
<AlertGroup isToast>
{this.state.notifications.map((notification, index) => {
return (
<Alert key={index} title={notification.error} variant={notification.type}
isLiveRegion
actionClose={<AlertActionCloseButton onClose={() => this.onDismissNotification(notification.index)} />}>
{notification.errorDetail}
</Alert>
);
})}
</AlertGroup>
);
const contextInfo = {
cgroupVersion: this.state.cgroupVersion,
registries: this.state.registries,
selinuxAvailable: this.state.selinuxAvailable,
dockerRestartAvailable: this.state.dockerRestartAvailable,
version: this.state.version,
};
return (
<WithDockerInfo value={contextInfo}>
<WithDialogs>
<Page id="overview" key="overview">
{notificationList}
<PageSection className="content-filter" padding={{ default: 'noPadding' }}
variant={PageSectionVariants.light}>
<ContainerHeader
handleFilterChanged={this.onFilterChanged}
ownerFilter={this.state.ownerFilter}
textFilter={this.state.textFilter}
/>
</PageSection>
<PageSection className='ct-pagesection-mobile'>
<Stack hasGutter>
{ this.state.showStartService ? startService : null }
{imageList}
{containerList}
</Stack>
</PageSection>
</Page>
</WithDialogs>
</WithDockerInfo>
);
}
}
export default Application;