mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: container
This commit is contained in:
1 parent
075e21fb89
commit
64215d3ef1
7 files changed
+128
-24
No files matched your search
@@ -14,9 +14,12 @@ const containerOptions = {
|
||||
HostConfig: {
|
||||
NetworkMode: config.docker.network,
|
||||
},
|
||||
}
|
||||
} as Docker.ContainerCreateOptions
|
||||
|
||||
export function createContainer(name, questId) {
|
||||
export function createContainer(
|
||||
name: string,
|
||||
questId: string
|
||||
): Promise<Docker.Container> {
|
||||
const option = {
|
||||
...containerOptions,
|
||||
name,
|
||||
@@ -25,7 +28,7 @@ export function createContainer(name, questId) {
|
||||
const { hostPwd, mountQuest } = config.docker
|
||||
if (!config.isProduction && hostPwd && mountQuest === questId) {
|
||||
logger.info('Mounting quest folder', mountQuest)
|
||||
option.HostConfig.Binds = [
|
||||
option.HostConfig!.Binds = [
|
||||
`${hostPwd}/quests/${mountQuest}/home:/home/commander`,
|
||||
`${hostPwd}/packages/container:/usr/local/lib/container`,
|
||||
]
|
||||
@@ -33,7 +36,9 @@ export function createContainer(name, questId) {
|
||||
return engine.createContainer(option)
|
||||
}
|
||||
|
||||
export async function getAndStartContainer(id) {
|
||||
export async function getAndStartContainer(
|
||||
id: string
|
||||
): Promise<Docker.Container> {
|
||||
logger.debug(`Getting container: ${id}`)
|
||||
const container = engine.getContainer(id)
|
||||
if (!container) {
|
||||
@@ -45,7 +50,10 @@ export async function getAndStartContainer(id) {
|
||||
return container
|
||||
}
|
||||
|
||||
export async function attachContainer(container, { token }) {
|
||||
export async function attachContainer(
|
||||
container: Docker.Container,
|
||||
{ token }: { token: string }
|
||||
) {
|
||||
const exec = await container.exec({
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
@@ -67,7 +75,7 @@ export async function attachContainer(container, { token }) {
|
||||
return execOutput
|
||||
}
|
||||
|
||||
export async function deleteContainer(id) {
|
||||
export async function deleteContainer(id: string) {
|
||||
const container = engine.getContainer(id)
|
||||
if (!container) {
|
||||
return
|
||||
@@ -80,8 +88,8 @@ export async function deleteContainer(id) {
|
||||
await container.remove()
|
||||
}
|
||||
|
||||
function parseJSONOutput(data) {
|
||||
const { stream, error } = JSON.parse(data.toString())
|
||||
function parseJSONOutput(data: string) {
|
||||
const { stream, error } = JSON.parse(data)
|
||||
if (stream) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(stream)
|
||||
@@ -91,7 +99,7 @@ function parseJSONOutput(data) {
|
||||
}
|
||||
}
|
||||
|
||||
export function buildQuestImage(questPath, questId) {
|
||||
export function buildQuestImage(questPath: string, questId: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
engine.buildImage(
|
||||
{
|
||||
@@ -114,9 +122,9 @@ export function buildQuestImage(questPath, questId) {
|
||||
data
|
||||
.toString()
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line !== '')
|
||||
.forEach((line) => {
|
||||
.map((line: string) => line.trim())
|
||||
.filter((line: string) => line !== '')
|
||||
.forEach((line: string) => {
|
||||
parseJSONOutput(line)
|
||||
})
|
||||
} catch (error) {
|
||||
@@ -1,18 +1,30 @@
|
||||
import schedule from 'node-schedule'
|
||||
import { Session } from '@linux-odyssey/models'
|
||||
import type { ISession } from '@linux-odyssey/models'
|
||||
import type { HydratedDocument } from 'mongoose'
|
||||
import config from '../config.js'
|
||||
import { deleteContainer } from './docker.js'
|
||||
import logger from '../utils/logger.js'
|
||||
|
||||
async function disableSession(session) {
|
||||
async function disableSession(session: HydratedDocument<ISession>) {
|
||||
if (!session.containerId) {
|
||||
logger.warn(`Deleting session ${session._id} has no containerId`)
|
||||
return
|
||||
}
|
||||
try {
|
||||
await deleteContainer(session.containerId)
|
||||
} catch (err) {
|
||||
// If the container doesn't exist, we can ignore the error
|
||||
if (err.statusCode !== 404) {
|
||||
throw new Error(
|
||||
`Failed to delete container ${session.containerId} for session ${session._id}`
|
||||
)
|
||||
// Perform runtime check to safely access statusCode
|
||||
if (typeof err === 'object' && err !== null && 'statusCode' in err) {
|
||||
const error = err as { statusCode: number }
|
||||
if (error.statusCode !== 404) {
|
||||
throw new Error(
|
||||
`Failed to delete container ${session.containerId} for session ${session._id}`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Handle the case where err is not the expected type
|
||||
logger.error(`An unexpected error occurred: ${err}`)
|
||||
}
|
||||
}
|
||||
if (session.status === 'active') {
|
||||
Reference in new issue
Block a user