Create service dockerising stuff
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user