-
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.
- Loading branch information
Roy Razon
committed
Sep 6, 2023
1 parent
7cf16f2
commit e7a2fb1
Showing
19 changed files
with
311 additions
and
414 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
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`) | ||
} | ||
} |
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
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(), | ||
}) |
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,32 @@ | ||
import fs from 'node:fs' | ||
import { FastifyPluginAsync } from 'fastify' | ||
import { SshState } from '../ssh' | ||
|
||
export const env: FastifyPluginAsync<{ | ||
currentSshState: () => Promise<SshState> | ||
machineStatus?: () => Promise<{ data: Buffer; contentType: string }> | ||
envMetadata?: Record<string, unknown> | ||
composeModelPath: string | ||
}> = async (app, { currentSshState, machineStatus, envMetadata, composeModelPath }) => { | ||
app.get('/tunnels', async () => await currentSshState()) | ||
|
||
if (machineStatus) { | ||
app.get('/machine-status', async (_req, res) => { | ||
const { data, contentType } = await machineStatus() | ||
void res | ||
.header('Content-Type', contentType) | ||
.send(data) | ||
}) | ||
} | ||
|
||
if (envMetadata) { | ||
app.get('/env-metadata', async () => envMetadata) | ||
} | ||
|
||
app.get('/compose-model', async ({ log }, res) => { | ||
log.debug('compose-model handler') | ||
void res | ||
.header('Content-Type', 'application/x-yaml') | ||
.send(await fs.promises.readFile(composeModelPath, { encoding: 'utf-8' })) | ||
}) | ||
Check failure Code scanning / CodeQL Missing rate limiting High
This route handler performs
a file system access Error loading related location Loading |
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.