feat: auto create config folder and player network

This commit is contained in:
Justin Lin committed 2024-10-16 14:13:41 +08:00
1 parent 91ebaec22c
commit b449d84c54
5 files changed
+25 -2

No files matched your search

+1
View File
@@ -67,6 +67,7 @@ This will start the db. it will automatically restarts by Docker.
Building quest images:
docker compose build base
yarn build:quests
### Build Quest Images in Details
+2 -2
View File
@@ -12,8 +12,8 @@ services:
base:
image: linuxodyssey/quest-base
# networks:
# - players
networks:
- players
build:
context: .
dockerfile: ./quests/Dockerfile
+4
View File
@@ -2,10 +2,14 @@
USERNAME=commander
echo "Initializing container for $USERNAME"
if ! id -u ${USERNAME} >/dev/null 2>&1; then
echo "Adding user $USERNAME"
adduser --disabled-password --gecos '' --shell /bin/zsh ${USERNAME} && \
find / -name ".gitkeep" -type f -exec rm -f {} +
echo "Adding public key for $USERNAME"
PUB_KEY=/etc/ssh/authorized_keys/${USERNAME}
cp /ssh_key.pub ${PUB_KEY}
chmod 644 ${PUB_KEY}
+13
View File
@@ -47,9 +47,22 @@ export async function createContainer(
]
}
const option = newContainerOptions(name, imageId, { binds })
await createNetworkIfNotExists(config.docker.network)
return engine.createContainer(option)
}
async function createNetworkIfNotExists(network: string) {
const dockerNetworks = await engine.listNetworks({
filters: {
name: [network],
},
})
if (dockerNetworks.length === 0) {
console.log('Creating player network', network)
await engine.createNetwork({ Name: network })
}
}
async function questHomeExists(imageId: string) {
try {
const stat = await fs.stat(`${config.projectRoot}/quests/${imageId}/home`)
+5
View File
@@ -1,4 +1,5 @@
import fs from 'fs'
import path from 'path'
import ssh2 from 'ssh2'
const {
@@ -17,6 +18,10 @@ export function loadOrCreateKeyPair(privateKeyPath: string): KeyPair {
const publicKey = fs.readFileSync(publicKeyPath, 'utf8')
return { privateKey, publicKey }
}
const dir = path.dirname(privateKeyPath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
const keyPair = generateKeyPair()
fs.writeFileSync(privateKeyPath, keyPair.privateKey, {
mode: 0o600,