-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.js
More file actions
572 lines (487 loc) · 18.3 KB
/
docker.js
File metadata and controls
572 lines (487 loc) · 18.3 KB
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
import { connect } from 'cloudflare:sockets'
let containers
let containerCacheTime = 0
let myPort = 5196
export default {
async fetch(request, env) {
const url = new URL(request.url)
if (url.host === "api.moby.localhost") {
return new Response("Comment out this line in the docker.js file to use the docker API, disable for security reasons...", { status: 403 })
// Check if this is a TCP upgrade request (Docker socket connection)
const connection = request.headers.get('connection')
const upgrade = request.headers.get('upgrade')
if (connection?.toLowerCase().includes('upgrade') && upgrade?.toLowerCase() === 'tcp') {
console.log('Handling Docker socket TCP upgrade for:', url.pathname)
try {
// First, try to establish the Docker API session via HTTP
const upgradeResponse = await env.docker.fetch(url, {
method: request.method,
headers: request.headers,
body: request.body
})
// If the Docker daemon responds with 101 Switching Protocols,
// we need to establish a raw TCP connection
if (upgradeResponse.status === 101) {
console.log('Docker daemon confirmed protocol switch, establishing TCP connection')
// Create TCP connection to Docker daemon
// Note: The actual host/port should match your Docker daemon configuration
const dockerSocket = connect({
hostname: 'localhost', // or docker daemon host
port: 2375 // or 2376 for TLS, or Unix socket port
})
// Send the HTTP upgrade request over the TCP connection
const writer = dockerSocket.writable.getWriter()
const encoder = new TextEncoder()
// Reconstruct the HTTP request
const httpRequest = `${request.method} ${url.pathname}${url.search} HTTP/1.1\r\n` +
`Host: ${url.host}\r\n` +
`Connection: Upgrade\r\n` +
`Upgrade: tcp\r\n` +
Array.from(request.headers.entries())
.filter(([key]) => !['host', 'connection', 'upgrade'].includes(key.toLowerCase()))
.map(([key, value]) => `${key}: ${value}`)
.join('\r\n') +
'\r\n\r\n'
await writer.write(encoder.encode(httpRequest))
// If there's a request body, send it
if (request.body) {
const reader = request.body.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
await writer.write(value)
}
} catch (e) {
console.error('Error streaming request body:', e)
}
}
// Return the TCP socket stream as the response
return new Response(dockerSocket.readable, {
status: 101,
statusText: 'Switching Protocols',
headers: {
'Connection': 'Upgrade',
'Upgrade': 'tcp'
}
})
}
// If not a protocol switch, return the regular response
return upgradeResponse
} catch (error) {
console.error('Docker TCP upgrade failed:', error)
return new Response(`Docker socket connection failed: ${error}`, {
status: 500,
headers: { 'Content-Type': 'text/plain' }
})
}
}
// Regular HTTP request to Docker API
const response = await env.docker.fetch(url, {
method: request.method,
headers: request.headers,
body: request.body
})
console.log(request.url)
console.log(request.method)
console.log(request.body)
console.log(request.headers)
return response
}
// Handle deploy endpoint
if (url.pathname === '/deploy' && request.method === 'POST') {
return new Response("Comment out this line in the docker.js file to use the docker API, disable for security reasons...", { status: 403 })
try {
const { gitUrl, dockerfilePath } = await request.json();
if (!gitUrl) {
return new Response(JSON.stringify({ error: 'Git URL is required' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}
// Basic validation of git URL
if (!gitUrl.match(/^https?:\/\/(github\.com|gitlab\.com|bitbucket\.org)/)) {
return new Response(JSON.stringify({
error: 'Only GitHub, GitLab, and Bitbucket URLs are supported'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}
console.log(`Deploying Docker container from ${gitUrl} using ${dockerfilePath}`)
// Ensure we have a proper Git clone URL for Docker's remote parameter
let normalizedGitUrl = gitUrl
// Convert GitHub/GitLab web URLs to proper Git clone URLs
if (gitUrl.match(/^https?:\/\/(github\.com|gitlab\.com)/)) {
// Remove trailing slash if present
normalizedGitUrl = gitUrl.replace(/\/$/, '')
// Add .git if not already present
if (!normalizedGitUrl.endsWith('.git')) {
normalizedGitUrl += '.git'
}
}
console.log(`Normalized Git URL for Docker remote: ${normalizedGitUrl}`)
// Generate deployment ID and service name
const deploymentId = `deploy-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
const serviceName = gitUrl.split('/').pop().replace(/\.git$/, '').toLowerCase()
const projectName = 'darc'
// Build the image using Docker API with Git remote URL instead of tar stream
const dockerfileName = dockerfilePath || 'Dockerfile'
const imageTag = `${projectName}_${serviceName}:latest`
console.log('Building Docker image from Git remote URL...')
const buildParams = new URLSearchParams()
buildParams.append('dockerfile', dockerfileName)
buildParams.append('t', imageTag)
buildParams.append('remote', normalizedGitUrl)
buildParams.append('version', '1')
console.log('Docker build URL:', `http://api.moby.localhost/1.47/build?${buildParams.toString()}`)
console.log('Build parameters:', Object.fromEntries(buildParams.entries()))
const buildResponse = await env.docker.fetch(`http://api.moby.localhost/1.47/build?${buildParams.toString()}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
console.log('Docker build response status:', buildResponse.status)
console.log('Docker build response headers:', Object.fromEntries(buildResponse.headers.entries()))
// Read Docker build output once (can't read response body twice!)
const buildOutput = await buildResponse.text()
console.log('Docker build output length:', buildOutput.length)
if (!buildResponse.ok) {
console.error('Docker build failed with status:', buildResponse.status)
console.error('Docker build error response:', buildOutput)
return new Response(JSON.stringify({
error: 'Docker build failed',
status: buildResponse.status,
details: buildOutput
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
})
}
// Parse Docker API v2 build stream output (newline-separated JSON objects)
let hasError = false
if (buildOutput) {
const lines = buildOutput.split('\n').filter(line => line.trim())
console.log('Docker build lines count:', lines.length)
for (const [index, line] of lines.entries()) {
if (!line.trim()) continue
try {
const json = JSON.parse(line)
console.log(`Build line ${index}:`, json)
// Handle Docker API v2 message types
if (json.error) {
console.error('Docker daemon error:', json.error)
hasError = true
} else if (json.errorDetail) {
console.error('Docker daemon error detail:', json.errorDetail)
hasError = true
} else if (json.aux) {
// Base64 decode the aux field for buildkit output
try {
const decodedAux = atob(json.aux)
console.log('Docker build aux (decoded):', decodedAux)
} catch (decodeError) {
console.log('Docker build aux (raw):', json.aux)
}
} else if (json.stream) {
console.log('Docker build stream:', json.stream.trim())
} else if (json.status) {
console.log('Docker build status:', json.status)
} else if (json.progress) {
console.log('Docker build progress:', json.progress)
} else {
console.log('Docker build message:', json)
}
} catch (parseError) {
console.log('Non-JSON build output:', line)
}
}
}
// If we found errors in the build stream, fail the deployment
if (hasError) {
return new Response(JSON.stringify({
error: 'Docker build failed with errors',
details: buildOutput
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
})
}
// Verify the image was created
console.log(`Verifying image exists: ${imageTag}`)
const imageResponse = await env.docker.fetch(`http://api.moby.localhost/1.47/images/${imageTag}/json`)
if (!imageResponse.ok) {
console.error('Image verification failed:', imageResponse.status, await imageResponse.text())
return new Response(JSON.stringify({
error: 'Docker build completed but image was not created',
details: `Image ${imageTag} not found after build. Build output: ${buildOutput}`
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
})
}
const imageInfo = await imageResponse.json()
console.log('Docker image verified successfully:', imageInfo.Id)
// Create container with compose-style configuration
const containerConfig = {
Image: imageTag,
name: `${projectName}_${serviceName}_1`,
Labels: {
'com.docker.compose.project': projectName,
'com.docker.compose.service': serviceName,
'com.docker.compose.container-number': '1',
'darc.deployment.id': deploymentId,
'darc.deployment.source': gitUrl
},
HostConfig: {
PublishAllPorts: true,
RestartPolicy: {
Name: 'unless-stopped'
}
},
NetworkingConfig: {
EndpointsConfig: {
[`${projectName}_default`]: {}
}
}
}
console.log('Creating container...', containerConfig)
// Create the container using proper API endpoint
const createResponse = await env.docker.fetch('http://api.moby.localhost/1.47/containers/create', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(containerConfig)
})
if (!createResponse.ok) {
const createError = await createResponse.text()
console.error('Container creation failed:', createError)
return new Response(JSON.stringify({
error: 'Container creation failed',
details: createError
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
})
}
const createResult = await createResponse.json()
const containerId = createResult.Id
console.log('Container created:', containerId)
// Start the container using proper API endpoint
const startResponse = await env.docker.fetch(`http://api.moby.localhost/1.47/containers/${containerId}/start`, {
method: 'POST'
})
if (!startResponse.ok) {
const startError = await startResponse.text()
console.error('Container start failed:', startError)
return new Response(JSON.stringify({
error: 'Container start failed',
details: startError
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
})
}
console.log('Container started successfully')
// Clear container cache to refresh the listing
containers = null
return new Response(JSON.stringify({
success: true,
deploymentId,
containerId,
serviceName,
projectName,
gitUrl: normalizedGitUrl,
dockerfilePath,
status: 'deployed',
message: `Docker container deployed successfully from ${normalizedGitUrl}`,
serviceUrl: `http://${serviceName}.localhost:${myPort}`
}), {
status: 200,
headers: { 'Content-Type': 'application/json' }
})
} catch (error) {
console.error('Docker deployment error:', error)
return new Response(JSON.stringify({
error: 'Failed to process deployment request',
details: error.message
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
})
}
}
if (Date.now() - containerCacheTime > 20000) {
containers = null
}
if (!containers) {
containerCacheTime = Date.now()
console.log("fetching containers")
const containersRes = await (
await env.docker.fetch("http://api.moby.localhost/containers/json")
).json()
containers = { all: {} }
// console.log({containersRes})
containersRes.forEach((container) => {
container.ports = []
for (const port of container.Ports) {
if (port.PublicPort && port.Type === "tcp" && port.IP !== '::') {
container.ports.push({
public: port.PublicPort,
private: port.PrivatePort
})
}
}
container.ports = container.ports.sort(({ private: a }, { private:b }) => a - b)
container.project = container.Labels["com.docker.compose.project"]
container.service = container.Labels["com.docker.compose.service"]
container.number = container.Labels["com.docker.compose.container-number"]
// container.cors = container.Labels["cors"]
if (containers.all[container.service]) {
container.service = container.service + "_" + container.project
}
if (container.number > 1) {
container.service = container.service + "_" + container.number
}
container.state = container.State
if (!containers[container.project]) {
containers[container.project] = []
}
containers[container.project].push(container)
containers.all[container.service] = container
})
// console.log(JSON.stringify(containers, null, 2))
}
if (url.hostname !== "localhost") {
const urlParts = url.hostname.split(".")
let portIndex
if (urlParts.length === 3) {
portIndex = Number(urlParts[1])
} else {
portIndex = 0
}
const serviceName = urlParts[0]
const newRequest = new Request(request)
newRequest.headers.append('x-forwarded-host', url.host)
url.hostname = containers.all[serviceName]?.Names[0]?.replace('/', '')
url.port = containers.all[serviceName]?.ports[portIndex]?.private
const newReq = new Request(url.href, newRequest)
// if (serviceName === "..." && newReq.method === "OPTIONS") {
// const headers = new Headers()
// headers.set("Access-Control-Allow-Origin", "http://localhost")
// headers.set("Access-Control-Allow-Credentials", "true")
// headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, HEAD")
// headers.set(
// "Access-Control-Allow-Headers",
// "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-newResuested-With,If-Modified-Since,Cache-Control,Content-Type,git-protocol,token"
// )
// headers.set("Access-Control-Max-Age", 1728000)
// headers.set("Content-Type", "text/plain charset=UTF-8")
// headers.set("Content-Length", 0)
// return new Response("", { headers, status: 204 })
// }
return fetch(newReq)
.catch(async (err) => {
// const log = await env.docker.fetch(`http://localhost/containers/${containers.all[serviceName].Id}/logs?stdout=true&stderr=true`).catch(() => {})
// if (log) {
// const reader = log.body.getReader()
// const { readable, writable } = new TransformStream()
// const writer = writable.getWriter()
// const decoder = new TextDecoder()
// async function processFeed () {
// while (true) {
// const { done, value } = await reader.read()
// console.log('deco:', decoder.decode(value))
// await writer.write(value) // await ?
// if (done) {
// if (!readable.locked) {
// await readable.cancel()
// }
// await reader.cancel()
// break
// }
// }
// }
// processFeed()
// }
containers = null
const html = `
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://matcha.mizu.sh/matcha.css">
<script>
setTimeout(() => {
location.reload()
}, 10000)
</script>
</head>
<html>
<body>
<section>
<article>
Error: ${JSON.stringify(err) + ' ' + url.href}
</article>
</section>
</body>
</html>
`
return new Response(html, { headers: { "content-type": "text/html" }, status: 500 })
})
.then((res) => {
// if (serviceName === "...") {
// return addCorsHeaders(res)
// } else {
return res
})
}
const html = `
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://matcha.mizu.sh/matcha.css">
</head>
<html>
<body>
<section>
${Object.keys(containers)
.map((project) =>
project === "all" ? "" : `<h2>${project}</h2>${renderContainers(containers[project])}`
)
.join("")}
</section>
</body>
</html>
`
return new Response(html, { headers: { "content-type": "text/html" } })
},
}
function renderContainers(containers) {
return `${containers
.sort((a, b) => b.state - a.state || b.ports.length - a.ports.length)
.map(
(container) =>
`<article><header><h3>${container.service} ${
container.state === "running" ? "" : "(" + container.state + ")"
}</h3></header><p>${renderPorts(container)}</p><p>${
container.cors ? "cors: " + container.cors : ""
}</p></article>`
)
.join("")}`
}
function renderPorts(container) {
return container.ports.length
? container.ports.map((port, i) => `<a href="http://${container.service}${i ? '.' + i : ''}.localhost${myPort === 80 ? "" : (":" + myPort)}">${container.service}${i ? '.' + i : ''}.localhost${myPort === 80 ? "" : ":" + myPort}</a> > <a href="http://localhost:${port.public}">localhost:${port.public}</a> > internal:${port.private}`).join('<br>\n')
: ""
}
// function addCorsHeaders(res) {
// const headers = new Headers(res.headers)
// headers.set("Access-Control-Allow-Origin", "http://exp.localhost")
// headers.set("Access-Control-Allow-Credentials", "true")
// headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, HEAD")
// headers.set(
// "Access-Control-Allow-Headers",
// "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,git-protocol,token"
// )
// return new Response(res.body, { headers, status: res.status, statusText: res.statusText })
// }