mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: load create session by quest image
This commit is contained in:
1 parent
4d408333b7
commit
8bbd5da513
8 files changed
+39
-33
No files matched your search
@@ -40,6 +40,9 @@ const config = {
|
||||
trustedProxies: getTrustProxies('TRUSTED_PROXIES'),
|
||||
surveyUrl: getUrl('SURVEY_URL', ''),
|
||||
bugReportUrl: getUrl('BUG_REPORT_URL', ''),
|
||||
dockerImage: get('DOCKER_IMAGE', 'lancatlin/linux-odyssey'),
|
||||
|
||||
mountQuest: get('MOUNT_QUEST', ''),
|
||||
}
|
||||
|
||||
config.baseUrl = get('BASE_URL', `http://${config.host}:${config.port}`)
|
||||
|
||||
@@ -4,7 +4,6 @@ import config from '../config.js'
|
||||
const engine = new Docker()
|
||||
|
||||
const containerOptions = {
|
||||
Image: 'lancatlin/linux-odyssey:helloworld',
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
@@ -13,23 +12,24 @@ const containerOptions = {
|
||||
StdinOnce: false,
|
||||
HostConfig: {
|
||||
NetworkMode: config.dockerNetwork,
|
||||
Binds:
|
||||
!config.isProduction && config.hostPwd
|
||||
? [
|
||||
`${config.hostPwd}/quests/helloworld/home:/home/commander`,
|
||||
`${config.hostPwd}/packages/container:/usr/local/lib/container`,
|
||||
]
|
||||
: [],
|
||||
},
|
||||
}
|
||||
|
||||
// const network = engine.getNetwork(config.dockerNetwork)
|
||||
|
||||
export function createContainer(name) {
|
||||
return engine.createContainer({
|
||||
export function createContainer(name, tag) {
|
||||
const option = {
|
||||
...containerOptions,
|
||||
name,
|
||||
})
|
||||
Image: `${config.dockerImage}:${tag}`,
|
||||
}
|
||||
if (!config.isProduction && config.hostPwd && config.mountQuest) {
|
||||
option.HostConfig.Bind = [
|
||||
`${config.hostPwd}/quests/${config.mountQuest}/home:/home/commander`,
|
||||
`${config.hostPwd}/packages/container:/usr/local/lib/container`,
|
||||
]
|
||||
}
|
||||
return engine.createContainer(option)
|
||||
}
|
||||
|
||||
export async function getAndStartContainer(id) {
|
||||
@@ -45,7 +45,6 @@ export async function getAndStartContainer(id) {
|
||||
}
|
||||
|
||||
export async function attachContainer(container, { token }) {
|
||||
// Create an exec instance with bash shell
|
||||
const exec = await container.exec({
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
|
||||
@@ -33,7 +33,8 @@ export async function createNewSession(user, questId) {
|
||||
})
|
||||
|
||||
const container = await createContainer(
|
||||
`quest-${quest.id}-${user.username}-${Date.now()}`
|
||||
`quest-${quest.id}-${user.username}-${Date.now()}`,
|
||||
quest.image
|
||||
)
|
||||
|
||||
const sessionHandler = new SessionHandler(
|
||||
|
||||
+17
-10
@@ -7,31 +7,38 @@ import { Quest } from '@linux-odyssey/models'
|
||||
const questDirectory = path.join(process.cwd(), '..', 'quests')
|
||||
|
||||
export default async function loadAndUpdateQuests() {
|
||||
const questNames = await fs.readdir(questDirectory)
|
||||
const questNames = await fs.readdir(questDirectory, {
|
||||
withFileTypes: true,
|
||||
})
|
||||
|
||||
const quests = Promise.all(
|
||||
questNames
|
||||
.filter((name) => !name.startsWith('.'))
|
||||
.map(async (id) => {
|
||||
// Check if id is directory
|
||||
const stat = await fs.stat(path.join(questDirectory, id))
|
||||
if (!stat.isDirectory()) {
|
||||
return null
|
||||
.filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith('.'))
|
||||
.map(async (dirent) => {
|
||||
const id = dirent.name
|
||||
const questPath = path.join(questDirectory, id)
|
||||
const files = await fs.readdir(questPath)
|
||||
if (!files.includes('game.yml')) {
|
||||
throw new Error(`Quest ${id} is missing game.yml`)
|
||||
}
|
||||
const fullPath = path.join(questDirectory, id, 'game.yml')
|
||||
|
||||
try {
|
||||
// Read and parse the README.md file.
|
||||
const body = await fs.readFile(fullPath, 'utf-8')
|
||||
const body = await fs.readFile(
|
||||
path.join(questPath, 'game.yml'),
|
||||
'utf-8'
|
||||
)
|
||||
|
||||
const quest = yaml.parse(body, {
|
||||
merge: true,
|
||||
})
|
||||
|
||||
const image = files.includes('Dockerfile') ? id : 'base'
|
||||
|
||||
return Quest.findByIdAndUpdate(
|
||||
id,
|
||||
{
|
||||
_id: id,
|
||||
image,
|
||||
...quest,
|
||||
},
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user