files with local repo volumes

main
Antonio De Lucreziis 2 years ago
parent a2cd57c679
commit 8d951b0e80

@ -67,7 +67,7 @@ export async function createContainerPty(
const id = await createContainer(tag) const id = await createContainer(tag)
const container = pty.spawn('docker', ['exec', '-it', id, ...shellCommand]) const container = pty.spawn('docker', ['exec', '-it', '-w', '/project', id, ...shellCommand])
container.onExit(async e => { container.onExit(async e => {
onExit?.(e) onExit?.(e)

@ -1,7 +1,10 @@
import express from 'express' import express from 'express'
import { WebSocketServer } from 'ws' import { WebSocketServer } from 'ws'
import { createContainerPty } from './docker.js' import { createContainerPty } from './docker.js'
import { runCommand } from './utils.js' import { getContainerPath, runCommand } from './utils.js'
import path from 'path'
import fs from 'fs/promises'
const app = express() const app = express()
@ -9,11 +12,12 @@ app.use(express.json())
app.get('/api/status', c => c.json(42)) app.get('/api/status', c => c.json(42))
app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls', async (req, res) => { app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls/*', async (req, res) => {
const uuid = req.params.uuid const uuid = req.params.uuid
const p = path.normalize(req.params[0])
try { try {
const files = await runCommand('/bin/ls', ['exec', uuid, '/bin/ls']) const files = await runCommand('/bin/ls', [path.join(getContainerPath('phc', uuid), p)])
res.json(files.trim().split('\n')) res.json(files.trim().split('\n'))
} catch (e) { } catch (e) {
@ -23,13 +27,12 @@ app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls', async (req, res) => {
app.get('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => { app.get('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => {
const uuid = req.params.uuid const uuid = req.params.uuid
const path = req.params[0] const p = path.normalize(req.params[0])
console.log('Getting', path) console.log('Getting', p)
try { try {
const content = await runCommand('docker', ['exec', uuid, '/bin/cat', `/project/${path}`]) const content = await fs.readFile(path.join(getContainerPath('phc', uuid), p), 'utf8')
res.json(content) res.json(content)
} catch (e) { } catch (e) {
res.json({ error: e.toString() }) res.json({ error: e.toString() })
@ -37,15 +40,13 @@ app.get('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => {
}) })
app.put('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => { app.put('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => {
const path = req.params[0] const p = path.normalize(req.params[0])
const content = req.body const content = req.body
console.log(content) console.log(content)
try { try {
await runCommand('docker', ['exec', uuid, '/bin/sh', '-c', `cat - > "/project/${path}"`], { await fs.writeFile(path.join(getContainerPath('phc', uuid), p), content)
stdin: content,
})
res.json('ok') res.json('ok')
} catch (e) { } catch (e) {

@ -26,13 +26,12 @@ let id
ws.addEventListener('message', e => { ws.addEventListener('message', e => {
const event = JSON.parse(e.data) const event = JSON.parse(e.data)
console.log(event)
if (event.type === 'pty') { if (event.type === 'pty') {
term.write(event.data) term.write(event.data)
} else if (event.type === 'created') { } else if (event.type === 'created') {
id = event.id id = event.id
} else {
console.log(event)
} }
}) })
@ -45,7 +44,7 @@ ws.addEventListener('close', () => {
}) })
setInterval(async () => { setInterval(async () => {
const req = await fetch(`/api/container/${id}/ls`) const req = await fetch(`/api/container/${id}/exec/ls/`)
const files = await req.json() const files = await req.json()
console.log(files) console.log(files)