Introduced typing for the buildJob and cleaned up common.ts
This commit is contained in:
parent
3ef093c7e6
commit
e08ec12d26
@ -12,7 +12,8 @@ import { version as currentVersion } from '../../package.json';
|
|||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import Cookie from 'cookie';
|
import Cookie from 'cookie';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import cuid from 'cuid';
|
import type { RequestEvent } from '@sveltejs/kit/types/internal';
|
||||||
|
import type { Job } from 'bullmq';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!dev) {
|
if (!dev) {
|
||||||
@ -45,30 +46,26 @@ const customConfig: Config = {
|
|||||||
|
|
||||||
export const version = currentVersion;
|
export const version = currentVersion;
|
||||||
export const asyncExecShell = util.promisify(child.exec);
|
export const asyncExecShell = util.promisify(child.exec);
|
||||||
export const asyncSleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
|
export const asyncSleep = (delay: number): Promise<unknown> =>
|
||||||
|
new Promise((resolve) => setTimeout(resolve, delay));
|
||||||
export const sentry = Sentry;
|
export const sentry = Sentry;
|
||||||
|
|
||||||
export const uniqueName = () => uniqueNamesGenerator(customConfig);
|
export const uniqueName = (): string => uniqueNamesGenerator(customConfig);
|
||||||
|
|
||||||
export const saveBuildLog = async ({ line, buildId, applicationId }) => {
|
export const saveBuildLog = async ({
|
||||||
|
line,
|
||||||
|
buildId,
|
||||||
|
applicationId
|
||||||
|
}: {
|
||||||
|
line: string;
|
||||||
|
buildId: string;
|
||||||
|
applicationId: string;
|
||||||
|
}): Promise<Job> => {
|
||||||
const addTimestamp = `${generateTimestamp()} ${line}`;
|
const addTimestamp = `${generateTimestamp()} ${line}`;
|
||||||
return await buildLogQueue.add(buildId, { buildId, line: addTimestamp, applicationId });
|
return await buildLogQueue.add(buildId, { buildId, line: addTimestamp, applicationId });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isTeamIdTokenAvailable = (request) => {
|
export const getTeam = (event: RequestEvent): string | null => {
|
||||||
const cookie = request.headers.cookie
|
|
||||||
?.split(';')
|
|
||||||
.map((s) => s.trim())
|
|
||||||
.find((s) => s.startsWith('teamId='))
|
|
||||||
?.split('=')[1];
|
|
||||||
if (!cookie) {
|
|
||||||
return getTeam(request);
|
|
||||||
} else {
|
|
||||||
return cookie;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getTeam = (event) => {
|
|
||||||
const cookies = Cookie.parse(event.request.headers.get('cookie'));
|
const cookies = Cookie.parse(event.request.headers.get('cookie'));
|
||||||
if (cookies?.teamId) {
|
if (cookies?.teamId) {
|
||||||
return cookies.teamId;
|
return cookies.teamId;
|
||||||
@ -78,7 +75,16 @@ export const getTeam = (event) => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getUserDetails = async (event, isAdminRequired = true) => {
|
export const getUserDetails = async (
|
||||||
|
event: RequestEvent,
|
||||||
|
isAdminRequired = true
|
||||||
|
): Promise<{
|
||||||
|
teamId: string;
|
||||||
|
userId: string;
|
||||||
|
permission: string;
|
||||||
|
status: number;
|
||||||
|
body: { message: string };
|
||||||
|
}> => {
|
||||||
const teamId = getTeam(event);
|
const teamId = getTeam(event);
|
||||||
const userId = event?.locals?.session?.data?.userId || null;
|
const userId = event?.locals?.session?.data?.userId || null;
|
||||||
const { permission = 'read' } = await db.prisma.permission.findFirst({
|
const { permission = 'read' } = await db.prisma.permission.findFirst({
|
||||||
@ -104,11 +110,11 @@ export const getUserDetails = async (event, isAdminRequired = true) => {
|
|||||||
return payload;
|
return payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getEngine(engine) {
|
export function getEngine(engine: string): string {
|
||||||
return engine === '/var/run/docker.sock' ? 'unix:///var/run/docker.sock' : engine;
|
return engine === '/var/run/docker.sock' ? 'unix:///var/run/docker.sock' : engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeContainer(id, engine) {
|
export async function removeContainer(id: string, engine: string): Promise<void> {
|
||||||
const host = getEngine(engine);
|
const host = getEngine(engine);
|
||||||
try {
|
try {
|
||||||
const { stdout } = await asyncExecShell(
|
const { stdout } = await asyncExecShell(
|
||||||
@ -124,11 +130,23 @@ export async function removeContainer(id, engine) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const removeDestinationDocker = async ({ id, engine }) => {
|
export const removeDestinationDocker = async ({
|
||||||
|
id,
|
||||||
|
engine
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
engine: string;
|
||||||
|
}): Promise<void> => {
|
||||||
return await removeContainer(id, engine);
|
return await removeContainer(id, engine);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createDirectories = async ({ repository, buildId }) => {
|
export const createDirectories = async ({
|
||||||
|
repository,
|
||||||
|
buildId
|
||||||
|
}: {
|
||||||
|
repository: string;
|
||||||
|
buildId: string;
|
||||||
|
}): Promise<{ workdir: string; repodir: string }> => {
|
||||||
const repodir = `/tmp/build-sources/${repository}/`;
|
const repodir = `/tmp/build-sources/${repository}/`;
|
||||||
const workdir = `/tmp/build-sources/${repository}/${buildId}`;
|
const workdir = `/tmp/build-sources/${repository}/${buildId}`;
|
||||||
|
|
||||||
@ -140,20 +158,10 @@ export const createDirectories = async ({ repository, buildId }) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export function generateTimestamp() {
|
export function generateTimestamp(): string {
|
||||||
return `${dayjs().format('HH:mm:ss.SSS')} `;
|
return `${dayjs().format('HH:mm:ss.SSS')} `;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDomain(domain) {
|
export function getDomain(domain: string): string {
|
||||||
return domain?.replace('https://', '').replace('http://', '');
|
return domain?.replace('https://', '').replace('http://', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dashify(str: string, options?: any): string {
|
|
||||||
if (typeof str !== 'string') return str;
|
|
||||||
return str
|
|
||||||
.trim()
|
|
||||||
.replace(/\W/g, (m) => (/[À-ž]/.test(m) ? m : '-'))
|
|
||||||
.replace(/^-+|-+$/g, '')
|
|
||||||
.replace(/-{2,}/g, (m) => (options && options.condense ? '-' : m))
|
|
||||||
.toLowerCase();
|
|
||||||
}
|
|
||||||
|
@ -20,30 +20,24 @@ import {
|
|||||||
setDefaultConfiguration
|
setDefaultConfiguration
|
||||||
} from '$lib/buildPacks/common';
|
} from '$lib/buildPacks/common';
|
||||||
import yaml from 'js-yaml';
|
import yaml from 'js-yaml';
|
||||||
|
import type { Job } from 'bullmq';
|
||||||
|
import type { BuilderJob } from '$lib/types/builderJob';
|
||||||
|
|
||||||
export default async function (job) {
|
export default async function (job: Job<BuilderJob, void, string>): Promise<void> {
|
||||||
/*
|
/*
|
||||||
Edge cases:
|
Edge cases:
|
||||||
1 - Change build pack and redeploy, what should happen?
|
1 - Change build pack and redeploy, what should happen?
|
||||||
*/
|
*/
|
||||||
let {
|
const {
|
||||||
id: applicationId,
|
id: applicationId,
|
||||||
repository,
|
repository,
|
||||||
branch,
|
|
||||||
buildPack,
|
|
||||||
name,
|
name,
|
||||||
destinationDocker,
|
destinationDocker,
|
||||||
destinationDockerId,
|
destinationDockerId,
|
||||||
gitSource,
|
gitSource,
|
||||||
build_id: buildId,
|
build_id: buildId,
|
||||||
configHash,
|
configHash,
|
||||||
port,
|
|
||||||
installCommand,
|
|
||||||
buildCommand,
|
|
||||||
startCommand,
|
|
||||||
fqdn,
|
fqdn,
|
||||||
baseDirectory,
|
|
||||||
publishDirectory,
|
|
||||||
projectId,
|
projectId,
|
||||||
secrets,
|
secrets,
|
||||||
phpModules,
|
phpModules,
|
||||||
@ -56,6 +50,16 @@ export default async function (job) {
|
|||||||
pythonModule,
|
pythonModule,
|
||||||
pythonVariable
|
pythonVariable
|
||||||
} = job.data;
|
} = job.data;
|
||||||
|
let {
|
||||||
|
branch,
|
||||||
|
buildPack,
|
||||||
|
port,
|
||||||
|
installCommand,
|
||||||
|
buildCommand,
|
||||||
|
startCommand,
|
||||||
|
baseDirectory,
|
||||||
|
publishDirectory
|
||||||
|
} = job.data;
|
||||||
const { debug } = settings;
|
const { debug } = settings;
|
||||||
|
|
||||||
await asyncSleep(500);
|
await asyncSleep(500);
|
||||||
@ -70,7 +74,7 @@ export default async function (job) {
|
|||||||
});
|
});
|
||||||
let imageId = applicationId;
|
let imageId = applicationId;
|
||||||
let domain = getDomain(fqdn);
|
let domain = getDomain(fqdn);
|
||||||
let volumes =
|
const volumes =
|
||||||
persistentStorage?.map((storage) => {
|
persistentStorage?.map((storage) => {
|
||||||
return `${applicationId}${storage.path.replace(/\//gi, '-')}:${
|
return `${applicationId}${storage.path.replace(/\//gi, '-')}:${
|
||||||
buildPack !== 'docker' ? '/app' : ''
|
buildPack !== 'docker' ? '/app' : ''
|
||||||
@ -106,7 +110,7 @@ export default async function (job) {
|
|||||||
publishDirectory = configuration.publishDirectory;
|
publishDirectory = configuration.publishDirectory;
|
||||||
baseDirectory = configuration.baseDirectory;
|
baseDirectory = configuration.baseDirectory;
|
||||||
|
|
||||||
let commit = await importers[gitSource.type]({
|
const commit = await importers[gitSource.type]({
|
||||||
applicationId,
|
applicationId,
|
||||||
debug,
|
debug,
|
||||||
workdir,
|
workdir,
|
||||||
|
51
src/lib/types/builderJob.ts
Normal file
51
src/lib/types/builderJob.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import type { DestinationDocker, GithubApp, GitlabApp, GitSource, Secret } from '@prisma/client';
|
||||||
|
|
||||||
|
export type BuilderJob = {
|
||||||
|
build_id: string;
|
||||||
|
type: BuildType;
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
fqdn: string;
|
||||||
|
repository: string;
|
||||||
|
configHash: unknown;
|
||||||
|
branch: string;
|
||||||
|
buildPack: BuildPackName;
|
||||||
|
projectId: number;
|
||||||
|
port: number;
|
||||||
|
installCommand: string;
|
||||||
|
buildCommand?: string;
|
||||||
|
startCommand?: string;
|
||||||
|
baseDirectory: string;
|
||||||
|
publishDirectory: string;
|
||||||
|
phpModules: unknown; // probably an array of some type;
|
||||||
|
pythonWSGI: unknown; // probably a string?;
|
||||||
|
pythonModule: unknown; // probably a string?;
|
||||||
|
pythonVariable: unknown; // probably a string?;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
destinationDockerId: string;
|
||||||
|
destinationDocker: DestinationDocker;
|
||||||
|
gitSource: GitSource & { githubApp?: GithubApp; gitlabApp?: GitlabApp };
|
||||||
|
settings: BuilderJobSettings;
|
||||||
|
secrets: Secret[];
|
||||||
|
persistentStorage: { path: string }[];
|
||||||
|
pullmergeRequestId?: unknown;
|
||||||
|
sourceBranch?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Add the other build types
|
||||||
|
export type BuildType = 'manual';
|
||||||
|
|
||||||
|
// TODO: Add the other buildpack names
|
||||||
|
export type BuildPackName = 'node' | 'docker';
|
||||||
|
|
||||||
|
export type BuilderJobSettings = {
|
||||||
|
id: string;
|
||||||
|
applicationId: string;
|
||||||
|
dualCerts: boolean;
|
||||||
|
debug: boolean;
|
||||||
|
previews: boolean;
|
||||||
|
autodeploy: boolean;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
};
|
@ -1,4 +1,4 @@
|
|||||||
import { getTeam, getUserDetails } from '$lib/common';
|
import { getUserDetails } from '$lib/common';
|
||||||
import * as db from '$lib/database';
|
import * as db from '$lib/database';
|
||||||
import { ErrorHandler } from '$lib/database';
|
import { ErrorHandler } from '$lib/database';
|
||||||
import type { RequestHandler } from '@sveltejs/kit';
|
import type { RequestHandler } from '@sveltejs/kit';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user