-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* narrow down docker API * convert to fastify
- Loading branch information
Roy Razon
authored
Sep 6, 2023
1 parent
367a3e7
commit fcd6601
Showing
40 changed files
with
911 additions
and
733 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const COMPOSE_TUNNEL_AGENT_SERVICE_LABELS = { | ||
PROFILE_THUMBPRINT: 'preevy.profile_thumbprint', | ||
PRIVATE_MODE: 'preevy.private_mode', | ||
ENV_ID: 'preevy.env_id', | ||
ACCESS: 'preevy.access', | ||
EXPOSE: 'preevy.expose', | ||
} | ||
|
||
export const COMPOSE_TUNNEL_AGENT_SERVICE_NAME = 'preevy_proxy' | ||
export const COMPOSE_TUNNEL_AGENT_PORT = 3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/compose-tunnel-agent/src/api-server/containers/errors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NotFoundError } from '../http-errors' | ||
|
||
export class ContainerNotFoundError extends NotFoundError { | ||
constructor(containerId: string) { | ||
super(`Container "${containerId}" does not exist or is not managed by this agent`) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/compose-tunnel-agent/src/api-server/containers/exec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { inspect } from 'util' | ||
import { createWebSocketStream } from 'ws' | ||
import z from 'zod' | ||
import { FastifyPluginAsync } from 'fastify' | ||
import Dockerode from 'dockerode' | ||
import { DockerFilterClient } from '../../docker' | ||
import { containerIdSchema, execQueryString } from './schema' | ||
import { inspectFilteredContainer } from './filter' | ||
|
||
const handler: FastifyPluginAsync<{ | ||
docker: Dockerode | ||
dockerFilter: DockerFilterClient | ||
}> = async (app, { docker, dockerFilter }) => { | ||
app.get<{ | ||
Params: z.infer<typeof containerIdSchema> | ||
Querystring: z.infer<typeof execQueryString> | ||
}>('/:containerId/exec', { | ||
schema: { | ||
params: containerIdSchema, | ||
querystring: execQueryString, | ||
}, | ||
websocket: true, | ||
}, async (connection, { params: { containerId }, query: { tty, cmd }, log }) => { | ||
await inspectFilteredContainer(dockerFilter, containerId) | ||
const abort = new AbortController() | ||
const exec = await docker.getContainer(containerId).exec({ | ||
AttachStdin: true, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Cmd: cmd, | ||
Tty: tty, | ||
abortSignal: abort.signal, | ||
}) | ||
|
||
const execStream = await exec.start({ | ||
hijack: true, | ||
stdin: true, | ||
Tty: tty, | ||
}) | ||
|
||
execStream.on('close', () => { connection.socket.close() }) | ||
execStream.on('error', err => { log.warn('execStream error %j', inspect(err)) }) | ||
connection.socket.on('close', () => { | ||
abort.abort() | ||
execStream.destroy() | ||
}) | ||
|
||
const inspectResults = await exec.inspect() | ||
log.debug('exec %s: %j', containerId, inspect(inspectResults)) | ||
|
||
const wsStream = createWebSocketStream(connection.socket) | ||
wsStream.on('error', err => { | ||
const level = err.message === 'aborted' || err.message.includes('WebSocket is not open') ? 'debug' : 'warn' | ||
log[level]('wsStream error %j', inspect(err)) | ||
}) | ||
|
||
if (tty) { | ||
execStream.pipe(wsStream, { end: false }).pipe(execStream) | ||
} else { | ||
docker.modem.demuxStream(execStream, wsStream, wsStream) | ||
wsStream.pipe(execStream) | ||
} | ||
|
||
return undefined | ||
}) | ||
} | ||
|
||
export default handler |
10 changes: 10 additions & 0 deletions
10
packages/compose-tunnel-agent/src/api-server/containers/filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { DockerFilterClient } from '../../docker' | ||
import { ContainerNotFoundError } from './errors' | ||
|
||
export const inspectFilteredContainer = async (dockerFilter: DockerFilterClient, containerId: string) => { | ||
const container = await dockerFilter.inspectContainer(containerId) | ||
if (!container) { | ||
throw new ContainerNotFoundError(containerId) | ||
} | ||
return container | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/compose-tunnel-agent/src/api-server/containers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Dockerode from 'dockerode' | ||
import { FastifyPluginAsync } from 'fastify' | ||
import z from 'zod' | ||
import fastifyWebsocket from '@fastify/websocket' | ||
import { DockerFilterClient } from '../../docker' | ||
import { containerIdSchema } from './schema' | ||
import exec from './exec' | ||
import logs from './logs' | ||
import { inspectFilteredContainer } from './filter' | ||
|
||
export const containers: FastifyPluginAsync<{ | ||
docker: Dockerode | ||
dockerFilter: DockerFilterClient | ||
}> = async (app, { docker, dockerFilter }) => { | ||
app.get('/', async () => await dockerFilter.listContainers()) | ||
|
||
app.get<{ | ||
Params: z.infer<typeof containerIdSchema> | ||
}>('/:containerId', { | ||
schema: { | ||
params: containerIdSchema, | ||
}, | ||
}, async ({ params: { containerId } }, res) => { | ||
const container = await inspectFilteredContainer(dockerFilter, containerId) | ||
void res.send(container) | ||
}) | ||
|
||
await app.register(fastifyWebsocket) | ||
await app.register(exec, { docker, dockerFilter }) | ||
await app.register(logs, { docker, dockerFilter }) | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/compose-tunnel-agent/src/api-server/containers/logs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { inspect } from 'util' | ||
import { createWebSocketStream } from 'ws' | ||
import z from 'zod' | ||
import { FastifyPluginAsync } from 'fastify' | ||
import Dockerode from 'dockerode' | ||
import { DockerFilterClient } from '../../docker' | ||
import { containerIdSchema, logsQueryString } from './schema' | ||
import { inspectFilteredContainer } from './filter' | ||
|
||
const handler: FastifyPluginAsync<{ | ||
docker: Dockerode | ||
dockerFilter: DockerFilterClient | ||
}> = async (app, { docker, dockerFilter }) => { | ||
app.get<{ | ||
Params: z.infer<typeof containerIdSchema> | ||
Querystring: z.infer<typeof logsQueryString> | ||
}>('/:containerId/logs', { | ||
schema: { | ||
params: containerIdSchema, | ||
querystring: logsQueryString, | ||
}, | ||
websocket: true, | ||
}, async ( | ||
connection, | ||
{ params: { containerId }, query: { stdout, stderr, since, until, timestamps, tail }, log } | ||
) => { | ||
await inspectFilteredContainer(dockerFilter, containerId) | ||
const abort = new AbortController() | ||
const logStream = await docker.getContainer(containerId).logs({ | ||
stdout, | ||
stderr, | ||
since, | ||
until, | ||
timestamps, | ||
tail, | ||
follow: true, | ||
abortSignal: abort.signal, | ||
}) | ||
|
||
logStream.on('close', async () => { connection.socket.close() }) | ||
logStream.on('error', err => { | ||
if (err.message !== 'aborted') { | ||
log.error('logs stream error %j', inspect(err)) | ||
} | ||
}) | ||
connection.socket.on('close', () => { abort.abort() }) | ||
|
||
const wsStream = createWebSocketStream(connection.socket) | ||
wsStream.on('error', err => { log.error('wsStream error %j', inspect(err)) }) | ||
docker.modem.demuxStream(logStream, wsStream, wsStream) | ||
|
||
return undefined | ||
}) | ||
} | ||
|
||
export default handler |
19 changes: 19 additions & 0 deletions
19
packages/compose-tunnel-agent/src/api-server/containers/schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import z from 'zod' | ||
|
||
export const containerIdSchema = z.object({ | ||
containerId: z.string(), | ||
}) | ||
|
||
export const execQueryString = z.object({ | ||
cmd: z.array(z.string()).optional().default(['sh']), | ||
tty: z.coerce.boolean().optional().default(true), | ||
}) | ||
|
||
export const logsQueryString = z.object({ | ||
stdout: z.coerce.boolean().optional(), | ||
stderr: z.coerce.boolean().optional(), | ||
since: z.string().optional(), | ||
until: z.string().optional(), | ||
timestamps: z.coerce.boolean().optional(), | ||
tail: z.coerce.number().optional(), | ||
}) |
Oops, something went wrong.