-
Notifications
You must be signed in to change notification settings - Fork 268
/
PodActions.svelte
207 lines (191 loc) · 6.3 KB
/
PodActions.svelte
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
<script lang="ts">
import {
faFileCode,
faPlay,
faRocket,
faStop,
faArrowsRotate,
faTrash,
faExternalLinkSquareAlt,
} from '@fortawesome/free-solid-svg-icons';
import type { PodInfoUI } from './PodInfoUI';
import { router } from 'tinro';
import ListItemButtonIcon from '../ui/ListItemButtonIcon.svelte';
import DropdownMenu from '../ui/DropdownMenu.svelte';
import FlatMenu from '../ui/FlatMenu.svelte';
import type { Menu } from '../../../../main/src/plugin/menu-registry';
import ContributionActions from '/@/lib/actions/ContributionActions.svelte';
import { onMount } from 'svelte';
import { MenuContext } from '../../../../main/src/plugin/menu-registry';
import { ContainerUtils } from '../container/container-utils';
export let pod: PodInfoUI;
export let dropdownMenu = false;
export let detailed = false;
export let inProgressCallback: (inProgress: boolean, state?: string) => void = () => {};
export let errorCallback: (erroMessage: string) => void = () => {};
let contributions: Menu[] = [];
onMount(async () => {
contributions = await window.getContributedMenus(MenuContext.DASHBOARD_POD);
});
let urls: Array<string> = [];
$: openingUrls = urls;
function extractPort(urlString: string) {
const match = urlString.match(/:(\d+)/);
return match ? parseInt(match[1], 10) : undefined;
}
onMount(async () => {
const containerUtils = new ContainerUtils();
const containerIds = pod.containers.map(podContainer => podContainer.Id);
const podContainers = (await window.listContainers()).filter(
container => containerIds.findIndex(containerInfo => containerInfo === container.Id) >= 0,
);
podContainers.forEach(container => {
const openingUrls = containerUtils.getOpeningUrls(container);
urls = [...new Set([...urls, ...openingUrls])];
});
});
async function startPod(podInfoUI: PodInfoUI) {
inProgressCallback(true, 'STARTING');
try {
await window.startPod(podInfoUI.engineId, podInfoUI.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false, 'RUNNING');
}
}
async function restartPod(podInfoUI: PodInfoUI) {
inProgressCallback(true, 'RESTARTING');
try {
await window.restartPod(podInfoUI.engineId, podInfoUI.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false);
}
}
async function stopPod(podInfoUI: PodInfoUI) {
inProgressCallback(true, 'STOPPING');
try {
await window.stopPod(podInfoUI.engineId, podInfoUI.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false, 'STOPPED');
}
}
async function deletePod(podInfoUI: PodInfoUI): Promise<void> {
inProgressCallback(true, 'DELETING');
try {
if (pod.kind === 'podman') {
await window.removePod(podInfoUI.engineId, podInfoUI.id);
} else {
await window.kubernetesDeletePod(podInfoUI.name);
}
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false);
}
}
function openGenerateKube(): void {
router.goto(`/pods/${encodeURI(pod.kind)}/${encodeURI(pod.name)}/${encodeURIComponent(pod.engineId)}/kube`);
}
function deployToKubernetes(): void {
router.goto(`/deploy-to-kube/${pod.id}/${pod.engineId}`);
}
// If dropdownMenu = true, we'll change style to the imported dropdownMenu style
// otherwise, leave blank.
let actionsStyle: typeof DropdownMenu | typeof FlatMenu;
if (dropdownMenu) {
actionsStyle = DropdownMenu;
} else {
actionsStyle = FlatMenu;
}
</script>
{#if pod.kind === 'podman'}
<ListItemButtonIcon
title="Start Pod"
onClick="{() => startPod(pod)}"
hidden="{pod.status === 'RUNNING' || pod.status === 'STOPPING'}"
detailed="{detailed}"
inProgress="{pod.actionInProgress && pod.status === 'STARTING'}"
icon="{faPlay}"
iconOffset="pl-[0.15rem]" />
<ListItemButtonIcon
title="Stop Pod"
onClick="{() => stopPod(pod)}"
hidden="{!(pod.status === 'RUNNING' || pod.status === 'STOPPING')}"
detailed="{detailed}"
inProgress="{pod.actionInProgress && pod.status === 'STOPPING'}"
icon="{faStop}" />
{/if}
<ListItemButtonIcon
title="Delete Pod"
onClick="{() => deletePod(pod)}"
icon="{faTrash}"
detailed="{detailed}"
inProgress="{pod.actionInProgress && pod.status === 'DELETING'}" />
<!-- If dropdownMenu is true, use it, otherwise just show the regular buttons -->
<svelte:component this="{actionsStyle}">
{#if pod.kind === 'podman'}
{#if !detailed}
<ListItemButtonIcon
title="Generate Kube"
onClick="{() => openGenerateKube()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faFileCode}" />
{/if}
<ListItemButtonIcon
title="Deploy to Kubernetes"
onClick="{() => deployToKubernetes()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faRocket}" />
{#if openingUrls.length === 0}
<ListItemButtonIcon
title="Open Exposed Port"
menu="{dropdownMenu}"
enabled="{false}"
hidden="{dropdownMenu}"
detailed="{detailed}"
icon="{faExternalLinkSquareAlt}" />
{:else if openingUrls.length === 1}
<ListItemButtonIcon
title="Open {extractPort(openingUrls[0])}"
onClick="{() => window.openExternal(openingUrls[0])}"
menu="{dropdownMenu}"
enabled="{pod.status === 'RUNNING'}"
hidden="{dropdownMenu}"
detailed="{detailed}"
icon="{faExternalLinkSquareAlt}" />
{:else if openingUrls.length > 1}
<DropdownMenu icon="{faExternalLinkSquareAlt}" hidden="{dropdownMenu}" shownAsMenuActionItem="{true}">
{#each openingUrls as url}
<ListItemButtonIcon
title="Open {extractPort(url)}"
onClick="{() => window.openExternal(url)}"
menu="{!dropdownMenu}"
enabled="{pod.status === 'RUNNING'}"
hidden="{dropdownMenu}"
detailed="{detailed}"
icon="{faExternalLinkSquareAlt}" />
{/each}
</DropdownMenu>
{/if}
<ListItemButtonIcon
title="Restart Pod"
onClick="{() => restartPod(pod)}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faArrowsRotate}" />
{/if}
<ContributionActions
args="{[pod]}"
contextPrefix="podItem"
dropdownMenu="{dropdownMenu}"
contributions="{contributions}"
detailed="{detailed}"
onError="{errorCallback}" />
</svelte:component>