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
+173 -84

No files matched your search

+2 -10
View File
@@ -33,8 +33,8 @@ jobs:
- name: Build Test Environment, Stage 1
run: docker-compose -f docker-compose.testing.yml build base backend app-test
- name: Build Test Environment, Stage 2
run: docker-compose -f docker-compose.testing.yml build helloworld
- name: Build Test Quests
run: docker-compose -f docker-compose.testing.yml run --rm backend yarn build:quests
- name: Run Cypress Tests
env:
@@ -96,14 +96,6 @@ jobs:
push: true
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:
needs: build-and-push
runs-on: ubuntu-latest
-1
View File
@@ -9,7 +9,6 @@ lerna-debug.log*
.bash_history
.viminfo
*.swp
home
# Diagnostic reports (https://nodejs.org/api/report.html)
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
### 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
Run Cypress locally, using the current development containers:
-6
View File
@@ -81,12 +81,6 @@ services:
context: .
dockerfile: ./quests/Dockerfile
helloworld:
image: lancatlin/linux-odyssey:helloworld
build:
context: .
dockerfile: quests/helloworld/Dockerfile
networks:
internal:
players:
-6
View File
@@ -76,12 +76,6 @@ services:
context: .
dockerfile: ./quests/Dockerfile
helloworld:
image: lancatlin/linux-odyssey:helloworld
build:
context: .
dockerfile: quests/helloworld/Dockerfile
networks:
internal:
players:
-6
View File
@@ -90,12 +90,6 @@ services:
context: .
dockerfile: ./quests/Dockerfile
helloworld:
image: lancatlin/linux-odyssey:helloworld
build:
context: .
dockerfile: quests/helloworld/Dockerfile
networks:
internal:
players:
+2 -1
View File
@@ -24,7 +24,8 @@
"test:open": "yarn workspace tests test:open",
"analytics": "yarn workspace analytics dev",
"setup": "yarn workspace server setup",
"clean": "yarn workspace server clean"
"clean": "yarn workspace server clean",
"build:quests": "yarn workspace server build:quests"
},
"devDependencies": {
"@vue/eslint-config-prettier": "^7.1.0",
+4
View File
@@ -79,6 +79,10 @@ const Quest = model(
type: String,
required: true,
},
image: {
type: String,
required: true,
},
requirements: {
type: [
{
+1 -3
View File
@@ -1,5 +1,3 @@
FROM lancatlin/linux-odyssey:base
COPY ./quests/helloworld/home ${HOME}
CMD [ "/bin/zsh" ]
COPY home ${HOME}
+1 -1
View File
@@ -1,3 +1,3 @@
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",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"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": {
"@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'),
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}`)
+53 -12
View File
@@ -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,
@@ -79,3 +78,45 @@ export async function deleteContainer(id) {
}
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 socketServer from './api/socket.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 errorHandler from './middleware/error.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(
`quest-${quest.id}-${user.username}-${Date.now()}`
`quest-${quest.id}-${user.username}-${Date.now()}`,
quest.image
)
const sessionHandler = new SessionHandler(
+51 -35
View File
@@ -6,44 +6,60 @@ import { Quest } from '@linux-odyssey/models'
const questDirectory = path.join(process.cwd(), '..', 'quests')
export default async function loadAndUpdateQuests() {
const questNames = await fs.readdir(questDirectory)
async function mapQuests(callback) {
const questNames = await fs.readdir(questDirectory, {
withFileTypes: true,
})
const quests = Promise.all(
return 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
}
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 quest = yaml.parse(body, {
merge: true,
})
return Quest.findByIdAndUpdate(
id,
{
_id: id,
...quest,
},
{
upsert: true,
}
)
} catch (error) {
console.error(`Error parsing quest ${id}:`, error)
throw error
}
.filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith('.'))
.map((dirent) => {
const id = dirent.name
const questPath = path.join(questDirectory, id)
return callback(id, questPath)
})
)
}
return quests
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`)
}
try {
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,
},
{
upsert: true,
}
)
} catch (error) {
console.error(`Error parsing quest ${id}:`, error)
throw error
}
})
}
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)
}