-
Notifications
You must be signed in to change notification settings - Fork 284
/
ContainerActions.svelte
226 lines (204 loc) · 6.32 KB
/
ContainerActions.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<script lang="ts">
import {
faAlignLeft,
faArrowsRotate,
faDownload,
faExternalLinkSquareAlt,
faFileCode,
faPlay,
faRocket,
faStop,
faTerminal,
faTrash,
} from '@fortawesome/free-solid-svg-icons';
import { createEventDispatcher, onMount } from 'svelte';
import { router } from 'tinro';
import ContributionActions from '/@/lib/actions/ContributionActions.svelte';
import { exportContainerInfo } from '/@/stores/export-container-store';
import type { Menu } from '../../../../main/src/plugin/menu-registry';
import { MenuContext } from '../../../../main/src/plugin/menu-registry';
import DropdownMenu from '../ui/DropdownMenu.svelte';
import FlatMenu from '../ui/FlatMenu.svelte';
import ListItemButtonIcon from '../ui/ListItemButtonIcon.svelte';
import { ContainerGroupInfoTypeUI, type ContainerInfoUI } from './ContainerInfoUI';
export let container: ContainerInfoUI;
export let dropdownMenu = false;
export let detailed = false;
const dispatch = createEventDispatcher<{ update: ContainerInfoUI }>();
let contributions: Menu[] = [];
onMount(async () => {
contributions = await window.getContributedMenus(MenuContext.DASHBOARD_CONTAINER);
});
function inProgress(inProgress: boolean, state?: string): void {
container.actionInProgress = inProgress;
// reset error when starting task
if (inProgress) {
container.actionError = '';
}
if (state) {
container.state = state;
}
dispatch('update', container);
}
function handleError(errorMessage: string): void {
container.actionError = errorMessage;
container.state = 'ERROR';
dispatch('update', container);
}
async function startContainer() {
inProgress(true, 'STARTING');
try {
await window.startContainer(container.engineId, container.id);
} catch (error) {
handleError(String(error));
} finally {
inProgress(false);
}
}
async function restartContainer() {
inProgress(true, 'RESTARTING');
try {
await window.restartContainer(container.engineId, container.id);
} catch (error) {
handleError(String(error));
} finally {
inProgress(false);
}
}
async function stopContainer() {
inProgress(true, 'STOPPING');
try {
await window.stopContainer(container.engineId, container.id);
} catch (error) {
handleError(String(error));
} finally {
inProgress(false);
}
}
function openBrowser(): void {
if (!container.openingUrl) {
return;
}
window.openExternal(container.openingUrl);
}
function openLogs(): void {
router.goto(`/containers/${container.id}/logs`);
}
async function deleteContainer(): Promise<void> {
inProgress(true, 'DELETING');
try {
await window.deleteContainer(container.engineId, container.id);
} catch (error) {
handleError(String(error));
} finally {
inProgress(false);
}
}
async function exportContainer() {
exportContainerInfo.set(container);
router.goto('/containers/export');
}
function openTerminalContainer(): void {
router.goto(`/containers/${container.id}/terminal`);
}
function openGenerateKube(): void {
router.goto(`/containers/${container.id}/kube`);
}
function deployToKubernetes(): void {
router.goto(`/deploy-to-kube/${container.id}/${container.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>
<ListItemButtonIcon
title="Start Container"
onClick="{() => startContainer()}"
hidden="{container.state === 'RUNNING' || container.state === 'STOPPING'}"
detailed="{detailed}"
inProgress="{container.actionInProgress && container.state === 'STARTING'}"
icon="{faPlay}"
iconOffset="pl-[0.15rem]" />
<ListItemButtonIcon
title="Stop Container"
onClick="{() => stopContainer()}"
hidden="{!(container.state === 'RUNNING' || container.state === 'STOPPING')}"
detailed="{detailed}"
inProgress="{container.actionInProgress && container.state === 'STOPPING'}"
icon="{faStop}" />
<ListItemButtonIcon
title="Delete Container"
confirm="{true}"
onClick="{() => deleteContainer()}"
icon="{faTrash}"
detailed="{detailed}"
inProgress="{container.actionInProgress && container.state === 'DELETING'}" />
<!-- If dropdownMenu is true, use it, otherwise just show the regular buttons -->
<svelte:component this="{actionsStyle}">
{#if !detailed}
<ListItemButtonIcon
title="Open Logs"
onClick="{() => openLogs()}"
menu="{dropdownMenu}"
detailed="{false}"
icon="{faAlignLeft}" />
<ListItemButtonIcon
title="Generate Kube"
onClick="{() => openGenerateKube()}"
menu="{dropdownMenu}"
hidden="{!(
container.engineType === 'podman' && container.groupInfo.type === ContainerGroupInfoTypeUI.STANDALONE
)}"
detailed="{detailed}"
icon="{faFileCode}" />
{/if}
<ListItemButtonIcon
title="Deploy to Kubernetes"
onClick="{() => deployToKubernetes()}"
menu="{dropdownMenu}"
hidden="{!(container.engineType === 'podman' && container.groupInfo.type === ContainerGroupInfoTypeUI.STANDALONE)}"
detailed="{detailed}"
icon="{faRocket}" />
<ListItemButtonIcon
title="Open Browser"
onClick="{() => openBrowser()}"
menu="{dropdownMenu}"
enabled="{container.state === 'RUNNING' && container.hasPublicPort}"
hidden="{dropdownMenu && container.state !== 'RUNNING'}"
detailed="{detailed}"
icon="{faExternalLinkSquareAlt}" />
{#if !detailed}
<ListItemButtonIcon
title="Open Terminal"
onClick="{() => openTerminalContainer()}"
menu="{dropdownMenu}"
hidden="{!(container.state === 'RUNNING')}"
detailed="{false}"
icon="{faTerminal}" />
{/if}
<ListItemButtonIcon
title="Restart Container"
onClick="{() => restartContainer()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faArrowsRotate}" />
<ListItemButtonIcon
title="Export Container"
tooltip="Exports container's filesystem contents as a tar archive and saves it on the local machine"
onClick="{() => exportContainer()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faDownload}" />
<ContributionActions
args="{[container]}"
contextPrefix="containerItem"
dropdownMenu="{dropdownMenu}"
contributions="{contributions}"
detailed="{detailed}"
onError="{handleError}" />
</svelte:component>