-
Notifications
You must be signed in to change notification settings - Fork 268
/
container-utils.ts
278 lines (249 loc) · 9.67 KB
/
container-utils.ts
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
/**********************************************************************
* Copyright (C) 2022-2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type { ContainerInfo } from '../../../../main/src/plugin/api/container-info';
import type { ContainerGroupInfoUI, ContainerGroupPartInfoUI, ContainerInfoUI } from './ContainerInfoUI';
import { ContainerGroupInfoTypeUI } from './ContainerInfoUI';
import moment from 'moment';
import humanizeDuration from 'humanize-duration';
import { filesize } from 'filesize';
import type { Port } from '@podman-desktop/api';
import type { ContextUI } from '../context/context';
import type { ViewInfoUI } from '../../../../main/src/plugin/api/view-info';
import { ContextKeyExpr } from '../context/contextKey';
import ContainerIcon from '../images/ContainerIcon.svelte';
export class ContainerUtils {
getName(containerInfo: ContainerInfo) {
// part of a compose ?
const composeService = containerInfo.Labels?.['com.docker.compose.service'];
if (composeService) {
const composeContainerNumber = containerInfo.Labels?.['com.docker.compose.container-number'];
if (composeContainerNumber) {
return `${composeService}-${composeContainerNumber}`;
}
}
return containerInfo.Names[0].replace(/^\//, '');
}
getState(containerInfo: ContainerInfo): string {
return (containerInfo.State || '').toUpperCase();
}
getUptime(containerInfo: ContainerInfo): string {
if (containerInfo.State?.toUpperCase() !== 'RUNNING' || !containerInfo.StartedAt) {
return '';
}
// make it human friendly
return this.humanizeUptime(containerInfo.StartedAt);
}
humanizeUptime(started: string): string {
// get start time in ms
const uptimeInMs = moment().diff(started);
// make it human friendly
return humanizeDuration(uptimeInMs, { round: true, largest: 1 });
}
refreshUptime(containerInfoUI: ContainerInfoUI): string {
if (containerInfoUI.state !== 'RUNNING' || !containerInfoUI.startedAt) {
return '';
}
// make it human friendly
return this.humanizeUptime(containerInfoUI.startedAt);
}
getImage(containerInfo: ContainerInfo): string {
return containerInfo.Image;
}
getShortImage(containerInfo: ContainerInfo): string {
// if image has a digest, keep only first 7 digits
const image = containerInfo.Image;
const shaIndex = image.indexOf('@sha256:');
if (shaIndex > 0) {
return image.substring(0, shaIndex + 15);
}
return image;
}
getPorts(containerInfo: ContainerInfo): Port[] {
return containerInfo.Ports?.filter(port => port.PublicPort).map(port => port) || [];
}
getDisplayPort(containerInfo: ContainerInfo): string {
const ports = this.getPorts(containerInfo).map(port => port.PublicPort);
if (ports.length === 0) {
return '';
}
return `PORT${ports.length > 1 ? 'S' : ''} ${ports}`;
}
hasPublicPort(containerInfo: ContainerInfo): boolean {
const publicPorts = containerInfo.Ports?.filter(port => port.PublicPort).map(port => port.PublicPort);
return publicPorts && publicPorts.length > 0;
}
getOpeningUrl(containerInfo: ContainerInfo): string {
const ports = containerInfo.Ports?.filter(port => port.PublicPort).map(port => port.PublicPort);
if (ports && ports.length > 0) {
return `http://localhost:${ports[0]}`;
} else {
return '';
}
}
getEngineId(containerInfo: ContainerInfo): string {
return containerInfo.engineId;
}
getEngineName(containerInfo: ContainerInfo): string {
return containerInfo.engineName;
}
getContainerInfoUI(
containerInfo: ContainerInfo,
extensionsContext?: ContextUI[],
viewContributions?: ViewInfoUI[],
): ContainerInfoUI {
return {
id: containerInfo.Id,
shortId: containerInfo.Id.substring(0, 8),
name: this.getName(containerInfo),
image: this.getImage(containerInfo),
shortImage: this.getShortImage(containerInfo),
state: this.getState(containerInfo),
startedAt: containerInfo.StartedAt,
uptime: this.getUptime(containerInfo),
engineId: this.getEngineId(containerInfo),
engineName: this.getEngineName(containerInfo),
engineType: containerInfo.engineType,
command: containerInfo.Command,
ports: this.getPorts(containerInfo),
portsAsString: this.getPortsAsString(containerInfo),
displayPort: this.getDisplayPort(containerInfo),
hasPublicPort: this.hasPublicPort(containerInfo),
openingUrl: this.getOpeningUrl(containerInfo),
groupInfo: this.getContainerGroup(containerInfo),
selected: false,
created: containerInfo.Created,
labels: containerInfo.Labels,
icon: this.iconClass(containerInfo, extensionsContext, viewContributions) || ContainerIcon,
};
}
getContainerGroup(containerInfo: ContainerInfo): ContainerGroupPartInfoUI {
// compose metatadata ?
const composeProject = containerInfo.Labels?.['com.docker.compose.project'];
if (composeProject) {
return {
name: composeProject,
type: ContainerGroupInfoTypeUI.COMPOSE,
engineId: containerInfo.engineId,
engineType: containerInfo.engineType,
};
}
// pod ?
const podInfo = containerInfo.pod;
if (podInfo) {
return {
name: podInfo.name,
type: ContainerGroupInfoTypeUI.POD,
id: podInfo.id,
status: (podInfo.status || '').toUpperCase(),
engineId: containerInfo.engineId,
engineType: containerInfo.engineType,
};
}
// else, standalone
return {
name: this.getName(containerInfo),
type: ContainerGroupInfoTypeUI.STANDALONE,
status: (containerInfo.Status || '').toUpperCase(),
engineType: containerInfo.engineType,
};
}
getContainerGroups(containerInfos: ContainerInfoUI[]): ContainerGroupInfoUI[] {
// create a map from containers having the same group field
const groups = new Map<string, ContainerGroupInfoUI>();
containerInfos.forEach(containerInfo => {
const group = containerInfo.groupInfo;
if (group.type === ContainerGroupInfoTypeUI.STANDALONE) {
// standalone group, insert with id as key
groups.set(containerInfo.id, { ...group, containers: [containerInfo], expanded: true, selected: false });
} else {
if (!groups.has(group.name)) {
groups.set(group.name, {
selected: false,
expanded: true,
name: group.name,
type: group.type,
id: group.id,
status: group.status,
engineId: group.engineId,
engineType: group.engineType,
containers: [],
});
}
groups.get(group.name).containers.push(containerInfo);
}
});
Array.from(groups.values())
.filter(group => group.type === ContainerGroupInfoTypeUI.COMPOSE)
.forEach(group => (group.status = group.containers.every(c => c.state === 'RUNNING') ? 'RUNNING' : 'STOPPED'));
return Array.from(groups.values());
}
getMemoryPercentageUsageTitle(memoryUsagePercentage: number, usedMemory: number): string {
return `${memoryUsagePercentage.toFixed(2)}% (${filesize(usedMemory)})`;
}
getMemoryUsageTitle(usedMemory: number): string {
return `${filesize(usedMemory)}`;
}
getPortsAsString(containerInfo: ContainerInfo): string {
const ports = this.getPorts(containerInfo).map(port => port.PublicPort);
if (ports.length > 1) {
return `${ports.join(', ')}`;
} else if (ports.length === 1) {
return `${ports[0]}`;
} else {
return '';
}
}
iconClass(
container: ContainerInfo,
extensionsContext?: ContextUI[],
viewContributions?: ViewInfoUI[],
): string | undefined {
if (!extensionsContext || !viewContributions) {
return undefined;
}
let icon;
// loop over all contribution for this view
for (const contribution of viewContributions) {
// retrieve the extension from the contribution and fetch its context
const extensionContext: ContextUI = extensionsContext.find(ctx => {
return ctx.extension === contribution.extensionId;
});
if (extensionContext) {
// adapt the context to work with containers (e.g save container labels into the context)
this.adaptContextOnContainer(extensionContext, container);
// deserialize the when clause
const whenDeserialized = ContextKeyExpr.deserialize(contribution.when);
// if the when clause has to be applied to this container
if (whenDeserialized?.evaluate(extensionContext)) {
// handle ${} in icon class
// and interpret the value and replace with the class-name
const match = contribution.icon.match(/\$\{(.*)\}/);
if (match && match.length === 2) {
const className = match[1];
icon = contribution.icon.replace(match[0], `podman-desktop-icon-${className}`);
return icon;
}
}
}
}
return icon;
}
adaptContextOnContainer(context: ContextUI, container: ContainerInfo): void {
context.setValue('containerLabelKeys', Object.keys(container.Labels));
}
}