-
Notifications
You must be signed in to change notification settings - Fork 268
/
BuildImageFromContainerfile.svelte
226 lines (202 loc) · 8.26 KB
/
BuildImageFromContainerfile.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">
/* eslint-disable import/no-duplicates */
// https://github.com/import-js/eslint-plugin-import/issues/1479
import { get } from 'svelte/store';
import { onDestroy, onMount } from 'svelte';
/* eslint-enable import/no-duplicates */
import type { ProviderContainerConnectionInfo, ProviderInfo } from '../../../../main/src/plugin/api/provider-info';
import { providerInfos } from '../../stores/providers';
import FormPage from '../ui/FormPage.svelte';
import NoContainerEngineEmptyScreen from './NoContainerEngineEmptyScreen.svelte';
import {
type BuildImageCallback,
clearBuildTask,
disconnectUI,
eventCollect,
reconnectUI,
startBuild,
} from './build-image-task';
import { type BuildImageInfo, buildImagesInfo } from '/@/stores/build-images';
import TerminalWindow from '../ui/TerminalWindow.svelte';
import type { Terminal } from 'xterm';
import Button from '../ui/Button.svelte';
import { faCube } from '@fortawesome/free-solid-svg-icons';
let buildFinished = false;
let containerImageName = 'my-custom-image';
let containerFilePath: string;
let containerBuildContextDirectory: string;
let buildImageInfo: BuildImageInfo | undefined = undefined;
let providers: ProviderInfo[] = [];
let providerConnections: ProviderContainerConnectionInfo[] = [];
let selectedProvider: ProviderContainerConnectionInfo | undefined = undefined;
let selectedProviderConnection: ProviderContainerConnectionInfo | undefined = undefined;
let logsTerminal: Terminal;
$: hasInvalidFields = !containerFilePath || !containerBuildContextDirectory;
function getTerminalCallback(): BuildImageCallback {
return {
onStream: function (data: string): void {
logsTerminal.write(`${data}\r`);
},
onError: function (error: string): void {
logsTerminal.write(`Error:${error}\r`);
},
onEnd: function (): void {
buildFinished = true;
window.dispatchEvent(new CustomEvent('image-build', { detail: { name: containerImageName } }));
},
};
}
async function buildContainerImage(): Promise<void> {
buildFinished = false;
if (containerFilePath && selectedProvider) {
// extract the relative path from the containerFilePath and containerBuildContextDirectory
const relativeContainerfilePath = containerFilePath.substring(containerBuildContextDirectory.length + 1);
buildImageInfo = startBuild(containerImageName, getTerminalCallback());
// store the key
buildImagesInfo.set(buildImageInfo);
try {
await window.buildImage(
containerBuildContextDirectory,
relativeContainerfilePath,
containerImageName,
selectedProvider,
buildImageInfo.buildImageKey,
eventCollect,
);
} catch (error) {
logsTerminal.write('Error:' + error + '\r');
} finally {
buildImageInfo.buildRunning = false;
buildFinished = true;
}
}
}
function cleanupBuild(): void {
// clear
if (buildImageInfo) {
clearBuildTask(buildImageInfo);
buildImageInfo = undefined;
}
// redirect to the imlage list
window.location.href = '#/images';
}
onMount(async () => {
providers = get(providerInfos);
providerConnections = providers
.map(provider => provider.containerConnections)
.flat()
.filter(providerContainerConnection => providerContainerConnection.status === 'started');
const selectedProviderConnection = providerConnections.length > 0 ? providerConnections[0] : undefined;
selectedProvider = !selectedProvider && selectedProviderConnection ? selectedProviderConnection : selectedProvider;
// check if we have an existing build info
buildImageInfo = get(buildImagesInfo);
if (buildImageInfo) {
reconnectUI(buildImageInfo.buildImageKey, getTerminalCallback());
}
});
onDestroy(() => {
if (buildImageInfo) {
disconnectUI(buildImageInfo.buildImageKey);
}
});
async function getContainerfileLocation() {
const result = await window.openFileDialog('Select Containerfile to build');
if (!result.canceled && result.filePaths.length === 1) {
containerFilePath = result.filePaths[0];
if (!containerBuildContextDirectory) {
// select the parent directory of the file as default
// eslint-disable-next-line no-useless-escape
containerBuildContextDirectory = containerFilePath.replace(/\\/g, '/').replace(/\/[^\/]*$/, '');
}
}
}
async function getContainerBuildContextDirectory() {
const result = await window.openFolderDialog('Select Root Context');
if (!result.canceled && result.filePaths.length === 1) {
containerBuildContextDirectory = result.filePaths[0];
}
}
</script>
<FormPage title="Build Image from Containerfile">
<svelte:fragment slot="icon">
<i class="fas fa-cube fa-2x" aria-hidden="true"></i>
</svelte:fragment>
<div slot="content" class="p-5 min-w-full h-fit">
{#if providerConnections.length === 0}
<NoContainerEngineEmptyScreen />
{:else}
<div class="bg-charcoal-900 pt-5 space-y-6 px-8 sm:pb-6 xl:pb-8 rounded-lg">
<div hidden="{buildImageInfo?.buildRunning}">
<label for="containerFilePath" class="block mb-2 text-sm font-bold text-gray-400">Containerfile Path</label>
<div class="flex flex-row">
<input
name="containerFilePath"
id="containerFilePath"
bind:value="{containerFilePath}"
placeholder="Containerfile to build"
class="w-full p-2 mr-3 outline-none text-sm bg-charcoal-600 rounded-sm text-gray-700 placeholder-gray-700"
required />
<Button on:click="{() => getContainerfileLocation()}">Browse...</Button>
</div>
</div>
<div hidden="{buildImageInfo?.buildRunning}">
<label for="containerBuildContextDirectory" class="block mb-2 text-sm font-bold text-gray-400"
>Build Context Directory</label>
<div class="flex flex-row">
<input
name="containerBuildContextDirectory"
id="containerBuildContextDirectory"
bind:value="{containerBuildContextDirectory}"
placeholder="Folder to build in"
class="w-full p-2 mr-3 outline-none text-sm bg-charcoal-600 rounded-sm text-gray-700 placeholder-gray-700"
required />
<Button on:click="{() => getContainerBuildContextDirectory()}">Browse...</Button>
</div>
</div>
<div hidden="{buildImageInfo?.buildRunning}">
<label for="containerImageName" class="block mb-2 text-sm font-bold text-gray-400">Image Name</label>
<input
type="text"
bind:value="{containerImageName}"
name="containerImageName"
id="containerImageName"
placeholder="image name (e.g. quay.io/namespace/my-custom-image)"
class="w-full p-2 outline-none text-sm bg-charcoal-600 rounded-sm text-gray-700 placeholder-gray-700"
required />
{#if providerConnections.length > 1}
<label for="providerChoice" class="py-6 block mb-2 text-sm font-bold text-gray-400 dark:text-gray-400"
>Container Engine
<select
class="w-full p-2 outline-none text-sm bg-charcoal-600 rounded-sm text-gray-700 placeholder-gray-700"
name="providerChoice"
id="providerChoice"
bind:value="{selectedProvider}">
{#each providerConnections as providerConnection}
<option value="{providerConnection}">{providerConnection.name}</option>
{/each}
</select>
</label>
{/if}
{#if providerConnections.length === 1 && selectedProviderConnection}
<input type="hidden" name="providerChoice" readonly bind:value="{selectedProviderConnection.name}" />
{/if}
</div>
<div class="w-full flex flex-row space-x-4">
{#if !buildImageInfo?.buildRunning}
<Button
on:click="{() => buildContainerImage()}"
disabled="{hasInvalidFields}"
class="w-full"
icon="{faCube}">
Build
</Button>
{/if}
{#if buildFinished}
<Button on:click="{() => cleanupBuild()}" class="w-full">Done</Button>
{/if}
</div>
<TerminalWindow bind:terminal="{logsTerminal}" />
</div>
{/if}
</div>
</FormPage>