Feat build quests (#65)

Feat build quests
This commit is contained in:
Lin, Hung-Shin authored and GitHub committed 2023-11-17 15:16:34 +08:00
commit 9e935b320a
18 files changed
+152 -63

No files matched your search

+2 -10
View File
@@ -33,8 +33,8 @@ jobs:
- name: Build Test Environment, Stage 1 - name: Build Test Environment, Stage 1
run: docker-compose -f docker-compose.testing.yml build base backend app-test run: docker-compose -f docker-compose.testing.yml build base backend app-test
- name: Build Test Environment, Stage 2 - name: Build Test Quests
run: docker-compose -f docker-compose.testing.yml build helloworld run: docker-compose -f docker-compose.testing.yml run --rm backend yarn build:quests
- name: Run Cypress Tests - name: Run Cypress Tests
env: env:
@@ -96,14 +96,6 @@ jobs:
push: true push: true
tags: docker.io/${{ secrets.DOCKERHUB_USERNAME }}/linux-odyssey:base tags: docker.io/${{ secrets.DOCKERHUB_USERNAME }}/linux-odyssey:base
- name: Build quest helloworld
uses: docker/build-push-action@v2
with:
context: .
file: ./quests/helloworld/Dockerfile
push: true
tags: docker.io/${{ secrets.DOCKERHUB_USERNAME }}/linux-odyssey:helloworld
deploy: deploy:
needs: build-and-push needs: build-and-push
runs-on: ubuntu-latest runs-on: ubuntu-latest
-1
View File
@@ -9,7 +9,6 @@ lerna-debug.log*
.bash_history .bash_history
.viminfo .viminfo
*.swp *.swp
home
# Diagnostic reports (https://nodejs.org/api/report.html) # Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
+27
View File
@@ -23,6 +23,33 @@ If you want to enable social login, you should have OAuth client token in a `.en
docker compose --env-file .env up -d docker compose --env-file .env up -d
### Build Quest Images
In each quest folder, there is a `game.yml` file, which is the quest definition. And a optional `Dockerfile` to overwrite the base image. If you want to include additional files, put them in a `home` folder. The files in `home` will **not** be copied to the image automatically, you have to copy them in the `Dockerfile`.
quests/
├── Dockerfile # Base image
├── discover
│ └── game.yml
├── helloworld
│ ├── Dockerfile # Overwrite the image
│ ├── game.yml
│ └── home # Include additional files
│ └── forgotten_scroll.txt
├── read
│ └── game.yml
└── spell
├── Dockerfile
├── game.yml
└── home
└── ancient_scroll.txt
To build all quests:
yarn build:quests
The base image is built with docker-compose, so you can use `docker compose build base` to rebuild it. Once you rebuild the base image, you have to rebuild all quests.
## Testing ## Testing
Run Cypress locally, using the current development containers: Run Cypress locally, using the current development containers:
-6
View File
@@ -81,12 +81,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:
-6
View File
@@ -76,12 +76,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:
-6
View File
@@ -90,12 +90,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:
+2 -1
View File
@@ -24,7 +24,8 @@
"test:open": "yarn workspace tests test:open", "test:open": "yarn workspace tests test:open",
"analytics": "yarn workspace analytics dev", "analytics": "yarn workspace analytics dev",
"setup": "yarn workspace server setup", "setup": "yarn workspace server setup",
"clean": "yarn workspace server clean" "clean": "yarn workspace server clean",
"build:quests": "yarn workspace server build:quests"
}, },
"devDependencies": { "devDependencies": {
"@vue/eslint-config-prettier": "^7.1.0", "@vue/eslint-config-prettier": "^7.1.0",
+4
View File
@@ -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 -3
View File
@@ -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 -1
View File
@@ -1,3 +1,3 @@
FROM lancatlin/linux-odyssey:base FROM lancatlin/linux-odyssey:base
CMD [ "/bin/zsh" ] COPY home ${HOME}
Whitespace-only changes.
+2 -1
View File
@@ -8,7 +8,8 @@
"dev": "nodemon src/index.js", "dev": "nodemon src/index.js",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)", "test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"setup": "node src/cli/setup.js", "setup": "node src/cli/setup.js",
"clean": "node src/cli/clean.js" "clean": "node src/cli/clean.js",
"build:quests": "node src/cli/build.js"
}, },
"dependencies": { "dependencies": {
"@linux-odyssey/constants": "*", "@linux-odyssey/constants": "*",
+24
View File
@@ -0,0 +1,24 @@
import { getQuestDockerfiles } from '../utils/quest.js'
import { buildQuestImage } from '../containers/docker.js'
async function buildImages() {
const quests = await getQuestDockerfiles()
console.log(`Building ${quests.length} images`)
return Promise.all(
quests.map(({ id, questPath }) => {
console.log(`Building image for ${id}`)
return buildQuestImage(questPath, id)
})
)
}
buildImages()
.then((result) => {
console.log(result)
console.log('Done!')
process.exit(0)
})
.catch((err) => {
console.error(err)
process.exit(1)
})
+3
View File
@@ -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}`)
+53 -12
View File
@@ -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,
@@ -79,3 +78,45 @@ export async function deleteContainer(id) {
} }
await container.remove() await container.remove()
} }
export function buildQuestImage(questPath, questId) {
return new Promise((resolve, reject) => {
engine.buildImage(
{
context: questPath,
src: ['Dockerfile', 'home'],
},
{
t: `${config.dockerImage}:${questId}`,
networkmode: 'none',
memory: 10 * 1e6,
},
(err, response) => {
if (err) {
reject(err)
}
response.on('data', (data) => {
// Process the data (this could be Docker build output)
const { stream, error } = JSON.parse(data.toString())
if (stream) {
console.log(stream)
}
if (error) {
console.error(error)
reject(error)
}
})
response.on('end', () => {
console.log(`Build completed for ${questId}`)
resolve(questId)
})
response.on('error', (error) => {
console.error(`Build failed for ${questId}:`, error)
reject(error)
})
}
)
})
}
+1 -1
View File
@@ -9,7 +9,7 @@ import connectDB from '@linux-odyssey/models'
import './auth/passport.js' import './auth/passport.js'
import socketServer from './api/socket.js' import socketServer from './api/socket.js'
import apiRouter from './api/routes/index.js' import apiRouter from './api/routes/index.js'
import loadAndUpdateQuests from './utils/quest.js' import { loadAndUpdateQuests } from './utils/quest.js'
import config from './config.js' import config from './config.js'
import errorHandler from './middleware/error.js' import errorHandler from './middleware/error.js'
import { globalRateLimit } from './middleware/rateLimiter.js' import { globalRateLimit } from './middleware/rateLimiter.js'
+2 -1
View File
@@ -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(
+30 -14
View File
@@ -6,32 +6,43 @@ 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() { async function mapQuests(callback) {
const questNames = await fs.readdir(questDirectory) const questNames = await fs.readdir(questDirectory, {
withFileTypes: true,
})
const quests = Promise.all( return Promise.all(
questNames questNames
.filter((name) => !name.startsWith('.')) .filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith('.'))
.map(async (id) => { .map((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()) { return callback(id, questPath)
return null })
)
}
export function loadAndUpdateQuests() {
return mapQuests(async (id, questPath) => {
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 { try {
// Read and parse the README.md file. const body = await fs.readFile(path.join(questPath, 'game.yml'), 'utf-8')
const body = await fs.readFile(fullPath, '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,
}, },
{ {
@@ -43,7 +54,12 @@ export default async function loadAndUpdateQuests() {
throw error throw error
} }
}) })
) }
return quests export async function getQuestDockerfiles() {
const quests = await mapQuests(async (id, questPath) => {
const files = await fs.readdir(questPath)
return files.includes('Dockerfile') ? { id, questPath } : null
})
return quests.filter((quest) => quest !== null)
} }