interface with db

This commit is contained in:
Cory Sanin 2025-01-12 01:14:50 -05:00
parent f02676ba2e
commit 07ab1fb40d
13 changed files with 366 additions and 29 deletions

2
.gitignore vendored
View File

@ -109,3 +109,5 @@ assets/css/
assets/js/ assets/js/
assets/webp/ assets/webp/
config/config.json config/config.json
config/postgres/
.env

View File

@ -6,6 +6,7 @@ COPY ./package*json ./
RUN npm ci RUN npm ci
COPY . . COPY . .
RUN node --experimental-strip-types build.ts && \ RUN node --experimental-strip-types build.ts && \
npm exec tsc && \
npm ci --only=production --omit=dev npm ci --only=production --omit=dev
FROM base as deploy FROM base as deploy

View File

@ -12,3 +12,16 @@ services:
restart: "no" restart: "no"
ports: ports:
- 8080:8080 - 8080:8080
depends_on:
- postgres
postgres:
container_name: archy-postgres
image: postgres:17-alpine
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: archery
POSTGRES_DB: archery
volumes:
- ./config/postgres:/var/lib/postgresql/data
restart: "no"

1
package-lock.json generated
View File

@ -9,6 +9,7 @@
"version": "0.0.1", "version": "0.0.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"body-parser": "^1.20.3",
"ejs": "3.1.10", "ejs": "3.1.10",
"express": "^4.21.2", "express": "^4.21.2",
"pg": "^8.13.1", "pg": "^8.13.1",

View File

@ -20,6 +20,7 @@
"type": "module", "type": "module",
"main": "index.ts", "main": "index.ts",
"dependencies": { "dependencies": {
"body-parser": "^1.20.3",
"ejs": "3.1.10", "ejs": "3.1.10",
"express": "^4.21.2", "express": "^4.21.2",
"pg": "^8.13.1", "pg": "^8.13.1",

196
src/DB.ts Normal file
View File

@ -0,0 +1,196 @@
import { Sequelize, DataTypes, Op, } from 'sequelize';
import type { ModelStatic, Filterable } from 'sequelize';
interface DBConfig {
db?: string;
user?: string;
password?: string;
host?: string;
port?: number;
}
const MONTH = 1000 * 60 * 60 * 24 * 24;
const FRESH = {
[Op.or]: [
{ startTime: { [Op.gt]: new Date(Date.now() - MONTH) } },
{ startTime: { [Op.is]: null } }
]
}
const SELECT = ['id', 'repo', 'commit', 'distro', 'startTime', 'endTime', 'status'];
class DB {
private build: ModelStatic<any>;
private sequelize: Sequelize;
constructor(config: DBConfig = {}) {
this.sequelize = new Sequelize(config.db || 'archery', config.user || 'archery', config.password || '', {
host: config.host || 'localhost',
port: config.port || 5432,
dialect: 'postgres'
});
this.build = this.sequelize.define('builds', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
repo: {
type: DataTypes.STRING,
allowNull: false
},
commit: {
type: DataTypes.STRING,
allowNull: true
},
patch: {
type: DataTypes.TEXT,
allowNull: true
},
distro: {
type: DataTypes.STRING,
allowNull: false
},
startTime: {
type: DataTypes.DATE,
allowNull: true
},
endTime: {
type: DataTypes.DATE,
allowNull: true
},
status: {
type: DataTypes.ENUM('queued', 'running', 'cancelled', 'success', 'error'),
allowNull: false,
defaultValue: 'queued'
},
pid: {
type: DataTypes.INTEGER,
allowNull: true
},
log: {
type: DataTypes.TEXT,
allowNull: true
}
});
this.build.sync();
}
public async createBuild(repo: string, commit: string, patch: string, distro: string): Promise<number> {
const buildRec = await this.build.create({
repo,
commit: commit || null,
patch: patch || null,
distro
});
return buildRec.id;
}
public async startBuild(id: number, pid: number): Promise<void> {
await this.build.update({
startTime: new Date(),
status: 'running',
pid,
log: ''
}, {
where: {
id
}
});
}
public async finishBuild(id: number, status: string): Promise<void> {
await this.build.update({
endTime: new Date(),
status
}, {
where: {
id
}
});
}
public async appendLog(id: number, log: string): Promise<void> {
await this.build.update({
log: Sequelize.literal(`log || '${log}'`)
}, {
where: {
id
}
});
}
public async getBuild(id: number): Promise<any> {
return await this.build.findByPk(id);
}
public async getBuilds(): Promise<any> {
return await this.build.findAll({
attributes: SELECT,
order: [['id', 'DESC']],
where: FRESH,
});
}
public async getBuildsByStatus(status: string): Promise<any> {
return await this.build.findAll({
attributes: SELECT,
order: [['id', 'DESC']],
where: {
...FRESH,
status
}
});
}
public async getBuildsByDistro(distro: string): Promise<any> {
return await this.build.findAll({
attributes: SELECT,
order: [['id', 'DESC']],
where: {
...FRESH,
distro
}
});
}
public async getBuildsBy(filterable: Filterable): Promise<any> {
return await this.build.findAll({
attributes: SELECT,
order: [['id', 'DESC']],
where: {
...FRESH,
...filterable
}
});
}
public async searchBuilds(query: string): Promise<any> {
return await this.build.findAll({
attributes: SELECT,
order: [['id', 'DESC']],
where: {
[Op.or]: [
{ repo: { [Op.iLike]: `%${query}%` } }
]
},
limit: 100
});
}
public async cleanup(): Promise<void> {
await this.build.destroy({
where: {
startTime: { [Op.lt]: new Date(Date.now() - MONTH * 6) }
}
});
}
public async close(): Promise<void> {
await this.sequelize.close();
}
}
export default DB;
export { DB };
export type { DBConfig };

View File

@ -2,6 +2,8 @@ import * as http from "http";
import crypto from 'crypto'; import crypto from 'crypto';
import type { Express } from "express"; import type { Express } from "express";
import express, { application } from 'express'; import express, { application } from 'express';
import bodyParser from "body-parser";
import type { DB } from "./DB.ts";
interface WebConfig { interface WebConfig {
port?: number; port?: number;
@ -16,6 +18,7 @@ function notStupidParseInt(v: string | undefined): number {
class Web { class Web {
private _webserver: http.Server | null = null; private _webserver: http.Server | null = null;
private db: DB;
constructor(options: WebConfig = {}) { constructor(options: WebConfig = {}) {
const app: Express = express(); const app: Express = express();
@ -25,6 +28,8 @@ class Web {
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
app.set('view options', { outputFunctionName: 'echo' }); app.set('view options', { outputFunctionName: 'echo' });
app.use('/assets', express.static('assets', { maxAge: '30 days' })); app.use('/assets', express.static('assets', { maxAge: '30 days' }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((_req, res, next) => { app.use((_req, res, next) => {
crypto.randomBytes(32, (err, randomBytes) => { crypto.randomBytes(32, (err, randomBytes) => {
if (err) { if (err) {
@ -37,18 +42,26 @@ class Web {
}); });
}); });
app.get('/', (_, res) => { app.get('/', async (req, res) => {
try {
const builds = await this.db.getBuildsBy(req.query);
res.render('index', { res.render('index', {
page: { page: {
title: 'Archery', title: 'Archery',
titlesuffix: 'Dashboard', titlesuffix: 'Dashboard',
description: 'PKGBUILD central' description: 'PKGBUILD central'
} },
builds
}); });
}
catch (err) {
console.error(err);
res.sendStatus(400);
}
}); });
app.get('/build/?', (_, res) => { app.get('/build/?', (_, res) => {
res.render('build', { res.render('build-new', {
page: { page: {
title: 'Archery', title: 'Archery',
titlesuffix: 'New Build', titlesuffix: 'New Build',
@ -57,6 +70,36 @@ class Web {
}); });
}); });
app.post('/build/?', async (req, res) => {
const build = await this.db.createBuild(req.body.repo, req.body.commit || null, req.body.patch || null, req.body.distro);
res.redirect(`/build/${build}`);
});
app.get('/build/:num/?', async (req, res) => {
const build = await this.db.getBuild(parseInt(req.params.num));
if (!build) {
res.sendStatus(404);
return;
}
res.render('build', {
page: {
title: 'Archery',
titlesuffix: `Build #${req.params.num}`,
description: `Building ${build.repo} on ${build.distro}`
},
build
});
});
app.get('/build/:num/logs/?', async (req, res) => {
const build = await this.db.getBuild(parseInt(req.params.num));
if (!build) {
res.sendStatus(404);
return;
}
res.set('Content-Type', 'text/plain').send(build.log);
});
app.get('/healthcheck', (_, res) => { app.get('/healthcheck', (_, res) => {
res.send('Healthy'); res.send('Healthy');
}); });
@ -69,6 +112,12 @@ class Web {
this._webserver.close(); this._webserver.close();
} }
} }
setDB = (db: DB) => {
this.db = db;
}
} }
export default Web; export default Web;
export { Web };
export type { WebConfig };

View File

@ -1,9 +1,23 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import Web from './Web.ts'; import { Web } from './Web.ts';
import { DB } from './DB.ts';
import type { WebConfig } from './Web.ts';
import type { DBConfig } from './DB.ts';
const config = JSON.parse(await fs.promises.readFile(process.env.config || path.join('config', 'config.json'), 'utf-8')); interface compositeConfig {
web?: WebConfig,
db?: DBConfig
}
const web = new Web(config); const config: compositeConfig = JSON.parse(await fs.promises.readFile(process.env.config || path.join('config', 'config.json'), 'utf-8'));
process.on('SIGTERM', web.close); const web = new Web(config.web);
await new Promise((resolve) => setTimeout(resolve, 1500));
const db = new DB(config.db);
web.setDB(db);
process.on('SIGTERM', () => {
web.close();
db.close();
});

View File

@ -35,6 +35,21 @@ h1 {
padding: 0; padding: 0;
} }
h2 {
font-size: 1.5em;
margin: 0;
padding: 0;
}
a {
color: var(--primary);
text-decoration: underline;
&:hover {
color: var(--primary-light);
}
}
table { table {
background-color: #2a2a2a; background-color: #2a2a2a;
border-collapse: collapse; border-collapse: collapse;
@ -69,6 +84,16 @@ th a {
form { form {
width: 40em; width: 40em;
max-width: 100%; max-width: 100%;
textarea {
width: 100%;
max-width: 100%;
min-width: 100%;
height: 10em;
max-height: 30em;
min-height: 5em;
resize: vertical;
}
} }
button, button,
@ -146,7 +171,7 @@ input[type="submit"] {
.grid-2col { .grid-2col {
display: grid; display: grid;
grid-template-columns: auto 1fr; grid-template-columns: auto 1fr;
gap: 1em; gap: .65em;
.span-2 { .span-2 {
grid-column: span 2; grid-column: span 2;

25
views/build-new.ejs Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<%- include("head", locals) %>
</head>
<body class="preload">
<%- include("navigation", locals) %>
<div class="content">
<h1>Start a build</h1>
<form action="/build" method="post" class="grid-2col">
<label for="repoTxt">Repo</label> <input type="text" name="repo" id="repoTxt" required />
<label for="commitTxt">Commit</label> <input type="text" name="commit" id="commitTxt" />
<label for="patchTxt">Patch</label> <textarea name="patch" id="patchTxt"></textarea>
<label for="distroTxt">Distro</label>
<select name="distro" id="distroTxt" required>
<option value="arch">Arch</option>
<option value="artix">Artix</option>
</select>
<div class="span-2"><button type="submit">Build</button></div>
</form>
</div>
</body>
</html>

View File

@ -8,17 +8,18 @@
<body class="preload"> <body class="preload">
<%- include("navigation", locals) %> <%- include("navigation", locals) %>
<div class="content"> <div class="content">
<h1>Start a build</h1> <h1>Build #<%= build.id %></h1>
<form action="/build" method="post" class="grid-2col"> <h2><%= build.status %></h2>
<label for="repoTxt">Repo</label> <input type="text" name="repo" id="repoTxt" required /> <div class="grid-2col">
<label for="commitTxt">Commit</label> <input type="text" name="commit" id="commitTxt" /> <label>Repo</label> <span><%= build.repo %></span>
<label for="distroTxt">Distro</label> <label>Commit</label> <span><% if (build.commit) { %><%= build.commit %><% } else { %>latest<% } %></span>
<select name="distro" id="distroTxt" required> <label>Patch</label> <span><% if (build.patch) { %><a href="/build/<%= build.id %>/patch">patch file</a><% } else { %>none<% } %></span>
<option value="arch">Arch</option> <label>Distro</label> <span><%= build.distro %></span>
<option value="artix">Artix</option> <label>Start time</label> <span><%= build.startTime %></span>
</select> </div>
<div class="span-2"><button type="submit">Build</button></div> <p><a href="/build/<%= build.id %>/logs">Full logs</a></p>
</form> <div class="logs">
</div>
</div> </div>
</body> </body>
</html> </html>

View File

@ -17,6 +17,15 @@
<th>Build Duration</th> <th>Build Duration</th>
<th>Build Status</th> <th>Build Status</th>
</tr> </tr>
<% builds.forEach(build => { %>
<tr>
<td><a href="/build/<%= build.id %>"><%= build.repo %></a></td>
<td><%= build.distro %></td>
<td><%= build.startTime %></td>
<td>TODO</td>
<td><%= build.status %></td>
</tr>
<% }) %>
</table> </table>
</div> </div>
</body> </body>

View File

@ -11,7 +11,7 @@
<ul class="sidebar_links"> <ul class="sidebar_links">
<li><a href="/build">New Build</a></li> <li><a href="/build">New Build</a></li>
<li><a href="/">All Builds</a></li> <li><a href="/">All Builds</a></li>
<li><a href="/?status=failed">Failed Builds</a></li> <li><a href="/?status=error">Failed Builds</a></li>
<li><a href="/?distro=arch">Arch Builds</a></li> <li><a href="/?distro=arch">Arch Builds</a></li>
<li><a href="/?distro=artix">Artix Builds</a></li> <li><a href="/?distro=artix">Artix Builds</a></li>
</ul> </ul>