mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +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
@@ -87,12 +87,6 @@ services:
|
|||||||
context: .
|
context: .
|
||||||
dockerfile: ./quests/Dockerfile
|
dockerfile: ./quests/Dockerfile
|
||||||
|
|
||||||
helloworld:
|
|
||||||
image: lancatlin/linux-odyssey:helloworld
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: quests/helloworld/Dockerfile
|
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
internal:
|
internal:
|
||||||
players:
|
players:
|
||||||
|
|||||||
@@ -79,6 +79,10 @@ const Quest = model(
|
|||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
image: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
requirements: {
|
requirements: {
|
||||||
type: [
|
type: [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
FROM lancatlin/linux-odyssey:base
|
FROM lancatlin/linux-odyssey:base
|
||||||
|
|
||||||
COPY ./quests/helloworld/home ${HOME}
|
COPY ./home ${HOME}
|
||||||
|
|
||||||
CMD [ "/bin/zsh" ]
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
FROM lancatlin/linux-odyssey:base
|
FROM lancatlin/linux-odyssey:base
|
||||||
|
|
||||||
CMD [ "/bin/zsh" ]
|
COPY ./home ${HOME}
|
||||||
@@ -40,6 +40,9 @@ const config = {
|
|||||||
trustedProxies: getTrustProxies('TRUSTED_PROXIES'),
|
trustedProxies: getTrustProxies('TRUSTED_PROXIES'),
|
||||||
surveyUrl: getUrl('SURVEY_URL', ''),
|
surveyUrl: getUrl('SURVEY_URL', ''),
|
||||||
bugReportUrl: getUrl('BUG_REPORT_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}`)
|
config.baseUrl = get('BASE_URL', `http://${config.host}:${config.port}`)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import config from '../config.js'
|
|||||||
const engine = new Docker()
|
const engine = new Docker()
|
||||||
|
|
||||||
const containerOptions = {
|
const containerOptions = {
|
||||||
Image: 'lancatlin/linux-odyssey:helloworld',
|
|
||||||
AttachStdin: true,
|
AttachStdin: true,
|
||||||
AttachStdout: true,
|
AttachStdout: true,
|
||||||
AttachStderr: true,
|
AttachStderr: true,
|
||||||
@@ -13,23 +12,24 @@ const containerOptions = {
|
|||||||
StdinOnce: false,
|
StdinOnce: false,
|
||||||
HostConfig: {
|
HostConfig: {
|
||||||
NetworkMode: config.dockerNetwork,
|
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)
|
// const network = engine.getNetwork(config.dockerNetwork)
|
||||||
|
|
||||||
export function createContainer(name) {
|
export function createContainer(name, tag) {
|
||||||
return engine.createContainer({
|
const option = {
|
||||||
...containerOptions,
|
...containerOptions,
|
||||||
name,
|
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) {
|
export async function getAndStartContainer(id) {
|
||||||
@@ -45,7 +45,6 @@ export async function getAndStartContainer(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function attachContainer(container, { token }) {
|
export async function attachContainer(container, { token }) {
|
||||||
// Create an exec instance with bash shell
|
|
||||||
const exec = await container.exec({
|
const exec = await container.exec({
|
||||||
AttachStdin: true,
|
AttachStdin: true,
|
||||||
AttachStdout: true,
|
AttachStdout: true,
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ export async function createNewSession(user, questId) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const container = await createContainer(
|
const container = await createContainer(
|
||||||
`quest-${quest.id}-${user.username}-${Date.now()}`
|
`quest-${quest.id}-${user.username}-${Date.now()}`,
|
||||||
|
quest.image
|
||||||
)
|
)
|
||||||
|
|
||||||
const sessionHandler = new SessionHandler(
|
const sessionHandler = new SessionHandler(
|
||||||
|
|||||||
+17
-10
@@ -7,31 +7,38 @@ import { Quest } from '@linux-odyssey/models'
|
|||||||
const questDirectory = path.join(process.cwd(), '..', 'quests')
|
const questDirectory = path.join(process.cwd(), '..', 'quests')
|
||||||
|
|
||||||
export default async function loadAndUpdateQuests() {
|
export default async function loadAndUpdateQuests() {
|
||||||
const questNames = await fs.readdir(questDirectory)
|
const questNames = await fs.readdir(questDirectory, {
|
||||||
|
withFileTypes: true,
|
||||||
|
})
|
||||||
|
|
||||||
const quests = Promise.all(
|
const quests = Promise.all(
|
||||||
questNames
|
questNames
|
||||||
.filter((name) => !name.startsWith('.'))
|
.filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith('.'))
|
||||||
.map(async (id) => {
|
.map(async (dirent) => {
|
||||||
// Check if id is directory
|
const id = dirent.name
|
||||||
const stat = await fs.stat(path.join(questDirectory, id))
|
const questPath = path.join(questDirectory, id)
|
||||||
if (!stat.isDirectory()) {
|
const files = await fs.readdir(questPath)
|
||||||
return null
|
if (!files.includes('game.yml')) {
|
||||||
|
throw new Error(`Quest ${id} is missing game.yml`)
|
||||||
}
|
}
|
||||||
const fullPath = path.join(questDirectory, id, 'game.yml')
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Read and parse the README.md file.
|
const body = await fs.readFile(
|
||||||
const body = await fs.readFile(fullPath, 'utf-8')
|
path.join(questPath, 'game.yml'),
|
||||||
|
'utf-8'
|
||||||
|
)
|
||||||
|
|
||||||
const quest = yaml.parse(body, {
|
const quest = yaml.parse(body, {
|
||||||
merge: true,
|
merge: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const image = files.includes('Dockerfile') ? id : 'base'
|
||||||
|
|
||||||
return Quest.findByIdAndUpdate(
|
return Quest.findByIdAndUpdate(
|
||||||
id,
|
id,
|
||||||
{
|
{
|
||||||
_id: id,
|
_id: id,
|
||||||
|
image,
|
||||||
...quest,
|
...quest,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user