Create service dockerising stuff

This commit is contained in:
Emma 2023-05-04 20:04:06 -06:00
parent a910783882
commit cd3f653f3f
20 changed files with 194 additions and 30 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
secrets.json
data/

36
.vscode/settings.json vendored
View File

@ -1,24 +1,24 @@
{
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#a210bb",
"activityBar.background": "#a210bb",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#3b3305",
"activityBarBadge.foreground": "#e7e7e7",
"commandCenter.border": "#e7e7e799",
"sash.hoverBorder": "#a210bb",
"statusBar.background": "#790c8c",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#a210bb",
"statusBarItem.remoteBackground": "#790c8c",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#790c8c",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#790c8c99",
"titleBar.inactiveForeground": "#e7e7e799"
"activityBar.activeBackground": "#d2d1f5",
"activityBar.background": "#d2d1f5",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#dd6562",
"activityBarBadge.foreground": "#15202b",
"commandCenter.border": "#15202b99",
"sash.hoverBorder": "#d2d1f5",
"statusBar.background": "#a9a7ec",
"statusBar.foreground": "#15202b",
"statusBarItem.hoverBackground": "#807de3",
"statusBarItem.remoteBackground": "#a9a7ec",
"statusBarItem.remoteForeground": "#15202b",
"titleBar.activeBackground": "#a9a7ec",
"titleBar.activeForeground": "#15202b",
"titleBar.inactiveBackground": "#a9a7ec99",
"titleBar.inactiveForeground": "#15202b99"
},
"peacock.remoteColor": "#790c8c",
"peacock.remoteColor": "#a9a7ec",
"deno.enable": true,
"deno.unstable": true,
"deno.config": "./deno.jsonc"

View File

@ -0,0 +1,24 @@
for await (const service of Deno.readDir('.')) {
if (service.isFile || !service.name.includes('-service')) continue;
const serviceFile = './' + service.name + '/';
const serviceName = service.name.replace('-service', '');
const port = await Deno.readTextFile(serviceFile + 'port');
const perms = (await Deno.readTextFile(serviceFile + 'perms')).split('\n');
await Deno.writeTextFile(serviceFile + 'Dockerfile', `
FROM denoland/deno:1.33.2
${port ? 'EXPOSE ' + port : ''}
WORKDIR /${serviceName}
ADD ./user-service .
COPY ./deno.jsonc .
COPY ./secrets.json .
COPY ./key.txt .
ADD ./common ./common
ADD ./lib ./lib
ADD ./middleware ./middleware
CMD ["run", "${perms.join('", "')}", main.ts${port ? `, "${port}"` : ''}]
`);
}

View File

@ -1,3 +1,5 @@
import {parse, stringify} from 'yaml';
(async () => {
const serviceName = prompt('Service name? (This is used by the router to determine the route prefix)')?.replace('-service', '');
if (!serviceName) return;
@ -69,10 +71,15 @@
if (!confirm('Service does not have permissions to "net", is this correct?'))
perms.push(getPermissionByNameOrShort('net'));
}
const serviceFile = `./${serviceName}-service/`;
let port;
await Deno.mkdir(serviceFile);
await Deno.writeTextFile(serviceFile + 'index.ts', `
port = await getAvailablePort();
if (perms.find(p => p.name === 'net')) {
await Deno.writeTextFile(serviceFile + 'port', port)
}
await Deno.writeTextFile(serviceFile + 'main.ts', `
import { CGGService } from 'cgg/Application.ts';
import { Router } from 'oak';
@ -85,6 +92,25 @@ console.log('User service running on ' + Deno.args.at(0));
`);
await Deno.writeTextFile(serviceFile + 'perms', perms.map(p => p.denoPerm).join('\n'));
await Deno.writeTextFile(serviceFile + 'prefix', serviceName);
if (confirm('Containerize this service?')) {
await Deno.writeTextFile(serviceFile + 'Dockerfile', `
FROM denoland/deno:1.33.2
${port ? 'EXPOSE ' + port : ''}
WORKDIR /${serviceName}
ADD ./user-service .
COPY ./deno.jsonc .
COPY ./secrets.json .
COPY ./key.txt .
ADD ./common ./common
ADD ./lib ./lib
ADD ./middleware ./middleware
CMD ["run", "${perms.join('", "')}", main.ts${port ? `, "${port}"` : ''}]
`);
}
if (confirm('Does this service need DB access?'))
await Deno.writeTextFile(serviceFile + 'data.ts', `
@ -93,11 +119,36 @@ import { configDatabase } from 'lib/data.ts';
configDatabase(mongoose);
`)
})();
Deno.run({
cmd: [
'mplayer',
'./chimes.wav'
]
})
})();
async function getAvailablePort() {
let start = 6900;
const missingPorts = [];
const takenPorts = [];
for await (const dirEntry of Deno.readDir('.')) {
if (dirEntry.isFile || !dirEntry.name.includes('-service')) continue;
const dir = './' + dirEntry.name + '/';
if (!Array.from(Deno.readDirSync(dir)).find(e => e.name === 'port')) {
missingPorts.push(dir);
continue;
}
takenPorts.push(Number(await Deno.readTextFile(dir + 'port')));
}
takenPorts.sort();
for (const port of takenPorts) {
if (start === port) start++;
}
for (const missing of missingPorts) {
Deno.writeTextFile(missing + 'port', start.toString());
takenPorts.push(start);
while(takenPorts.includes(start)) start++;
}
return start.toString();
}

View File

@ -2,6 +2,7 @@
"imports": {
"oak": "https://deno.land/x/oak@v12.2.0/mod.ts",
"mongoose": "npm:mongoose",
"yaml": "npm:yaml",
"/": "./",
"./": "./",
"common/": "./common/",

7
deno.lock generated
View File

@ -529,7 +529,8 @@
"specifiers": {
"bcryptjs": "bcryptjs@2.4.3",
"mongoose": "mongoose@7.0.4",
"play-sound": "play-sound@1.1.5"
"play-sound": "play-sound@1.1.5",
"yaml": "yaml@2.2.2"
},
"packages": {
"@types/node@18.11.18": {
@ -676,6 +677,10 @@
"tr46": "tr46@3.0.0",
"webidl-conversions": "webidl-conversions@7.0.0"
}
},
"yaml@2.2.2": {
"integrity": "sha512-CBKFWExMn46Foo4cldiChEzn7S7SRV+wqiluAb6xmueD/fGyRHIhX8m14vVGgeFWjN540nKCNVj6P21eQjgTuA==",
"dependencies": {}
}
}
}

17
docker-compose.yml Normal file
View File

@ -0,0 +1,17 @@
version: '3.9'
services:
nginx:
image: 'jc21/nginx-proxy-manager:latest'
restart: unless-stopped
ports:
- '80:80'
- '81:81'
- '443:443'
volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt
user:
build:
context: ./
dockerfile: ./user-service/Dockerfile

View File

@ -0,0 +1,15 @@
FROM denoland/deno:1.33.2
EXPOSE 6903
WORKDIR /game-systems
ADD ./user-service .
COPY ./deno.jsonc .
COPY ./secrets.json .
COPY ./key.txt .
ADD ./common ./common
ADD ./lib ./lib
ADD ./middleware ./middleware
CMD ["run", "--allow-net", "--allow-read", "--allow-write", main.ts, "6903"]

View File

@ -0,0 +1 @@
6903

15
honors-service/Dockerfile Normal file
View File

@ -0,0 +1,15 @@
FROM denoland/deno:1.33.2
EXPOSE 6902
WORKDIR /honors
ADD ./user-service .
COPY ./deno.jsonc .
COPY ./secrets.json .
COPY ./key.txt .
ADD ./common ./common
ADD ./lib ./lib
ADD ./middleware ./middleware
CMD ["run", "--allow-net", main.ts, "6902"]

1
honors-service/port Normal file
View File

@ -0,0 +1 @@
6902

View File

@ -1,5 +1,5 @@
import { Context } from "oak";
import { USER_TOKEN } from "../constsAndEnums.ts";
import { USER_TOKEN } from "common/constsAndEnums.ts";
import { verifyUserSessionToken } from "../lib/jwt.ts";
export const authenticateUser = async (ctx: Context, next: () => any) => {

15
rules-service/Dockerfile Normal file
View File

@ -0,0 +1,15 @@
FROM denoland/deno:1.33.2
EXPOSE 6901
WORKDIR /rules
ADD ./user-service .
COPY ./deno.jsonc .
COPY ./secrets.json .
COPY ./key.txt .
ADD ./common ./common
ADD ./lib ./lib
ADD ./middleware ./middleware
CMD ["run", "--allow-net", main.ts, "6901"]

1
rules-service/port Normal file
View File

@ -0,0 +1 @@
6901

15
user-service/Dockerfile Normal file
View File

@ -0,0 +1,15 @@
FROM denoland/deno:1.33.2
EXPOSE 6900
WORKDIR /user
ADD ./user-service .
COPY ./deno.jsonc .
COPY ./secrets.json .
COPY ./key.txt .
ADD ./common ./common
ADD ./lib ./lib
ADD ./middleware ./middleware
CMD ["run", "--allow-read", "--allow-write", "--allow-net", "--allow-sys", "--allow-env", main.ts, "6900"]

View File

@ -1,7 +1,7 @@
import { Router } from 'oak';
import { CGGService } from 'cgg/Application.ts';
import { createUserSessionToken } from 'lib/jwt.ts';
import { USER_TOKEN } from '../constsAndEnums.ts';
import { USER_TOKEN } from 'common/constsAndEnums.ts';
import { User } from './data.ts';
import { bodyExists } from '../middleware/bodyExists.ts';

1
user-service/port Normal file
View File

@ -0,0 +1 @@
6900