wip: trpc

This commit is contained in:
Andras Bacsai
2022-12-13 12:47:14 +01:00
parent 1639d1725a
commit 1180d3fdde
28 changed files with 1341 additions and 175 deletions

View File

@@ -7,6 +7,7 @@ import { uniqueNamesGenerator, adjectives, colors, animals } from 'unique-names-
import type { Config } from 'unique-names-generator';
import { env } from '../env';
import { day } from './dayjs';
import { executeCommand } from './executeCommand';
const customConfig: Config = {
dictionaries: [adjectives, colors, animals],
@@ -132,3 +133,51 @@ export async function removeService({ id }: { id: string }): Promise<void> {
await prisma.service.delete({ where: { id } });
}
export const createDirectories = async ({
repository,
buildId
}: {
repository: string;
buildId: string;
}): Promise<{ workdir: string; repodir: string }> => {
if (repository) repository = repository.replaceAll(' ', '');
const repodir = `/tmp/build-sources/${repository}/`;
const workdir = `/tmp/build-sources/${repository}/${buildId}`;
let workdirFound = false;
try {
workdirFound = !!(await fs.stat(workdir));
} catch (error) {}
if (workdirFound) {
await executeCommand({ command: `rm -fr ${workdir}` });
}
await executeCommand({ command: `mkdir -p ${workdir}` });
return {
workdir,
repodir
};
};
export async function saveDockerRegistryCredentials({ url, username, password, workdir }) {
if (!username || !password) {
return null;
}
let decryptedPassword = decrypt(password);
const location = `${workdir}/.docker`;
try {
await fs.mkdir(`${workdir}/.docker`);
} catch (error) {
console.log(error);
}
const payload = JSON.stringify({
auths: {
[url]: {
auth: Buffer.from(`${username}:${decryptedPassword}`).toString('base64')
}
}
});
await fs.writeFile(`${location}/config.json`, payload);
return location;
}

View File

@@ -122,3 +122,36 @@ export async function stopTcpHttpProxy(
return error;
}
}
export function formatLabelsOnDocker(data: any) {
return data
.trim()
.split('\n')
.map((a) => JSON.parse(a))
.map((container) => {
const labels = container.Labels.split(',');
let jsonLabels = {};
labels.forEach((l) => {
const name = l.split('=')[0];
const value = l.split('=')[1];
jsonLabels = { ...jsonLabels, ...{ [name]: value } };
});
container.Labels = jsonLabels;
return container;
});
}
export function defaultComposeConfiguration(network: string): any {
return {
networks: [network],
restart: 'on-failure',
deploy: {
restart_policy: {
condition: 'on-failure',
delay: '5s',
max_attempts: 10,
window: '120s'
}
}
};
}

View File

@@ -23,7 +23,7 @@ export async function executeCommand({
sshCommand?: boolean;
shell?: boolean;
stream?: boolean;
dockerId?: string;
dockerId?: string | null;
buildId?: string;
applicationId?: string;
debug?: boolean;
@@ -31,8 +31,8 @@ export async function executeCommand({
const { execa, execaCommand } = await import('execa');
const { parse } = await import('shell-quote');
const parsedCommand = parse(command);
const dockerCommand = parsedCommand[0]?.toString();
const dockerArgs = parsedCommand.slice(1).toString();
const dockerCommand = parsedCommand[0];
const dockerArgs = parsedCommand.slice(1);
if (dockerId && dockerCommand && dockerArgs) {
const destinationDocker = await prisma.destinationDocker.findUnique({
@@ -41,14 +41,12 @@ export async function executeCommand({
if (!destinationDocker) {
throw new Error('Destination docker not found');
}
let {
remoteEngine,
remoteIpAddress,
engine = 'unix:///var/run/docker.sock'
} = destinationDocker;
let { remoteEngine, remoteIpAddress, engine } = destinationDocker;
if (remoteEngine) {
await createRemoteEngineConfiguration(dockerId);
engine = `ssh://${remoteIpAddress}-remote`;
} else {
engine = 'unix:///var/run/docker.sock';
}
if (env.CODESANDBOX_HOST) {
@@ -60,16 +58,19 @@ export async function executeCommand({
if (shell) {
return execaCommand(`ssh ${remoteIpAddress}-remote ${command}`);
}
//@ts-ignore
return await execa('ssh', [`${remoteIpAddress}-remote`, dockerCommand, ...dockerArgs]);
}
if (stream) {
return await new Promise(async (resolve, reject) => {
let subprocess = null;
if (shell) {
//@ts-ignore
subprocess = execaCommand(command, {
env: { DOCKER_BUILDKIT: '1', DOCKER_HOST: engine }
});
} else {
//@ts-ignore
subprocess = execa(dockerCommand, dockerArgs, {
env: { DOCKER_BUILDKIT: '1', DOCKER_HOST: engine }
});
@@ -112,7 +113,8 @@ export async function executeCommand({
});
subprocess.on('exit', async (code: number) => {
if (code === 0) {
resolve('success');
//@ts-ignore
resolve(code);
} else {
if (!debug) {
for (const log of logs) {
@@ -127,9 +129,11 @@ export async function executeCommand({
} else {
if (shell) {
return await execaCommand(command, {
//@ts-ignore
env: { DOCKER_BUILDKIT: '1', DOCKER_HOST: engine }
});
} else {
//@ts-ignore
return await execa(dockerCommand, dockerArgs, {
env: { DOCKER_BUILDKIT: '1', DOCKER_HOST: engine }
});
@@ -139,6 +143,7 @@ export async function executeCommand({
if (shell) {
return execaCommand(command, { shell: true });
}
//@ts-ignore
return await execa(dockerCommand, dockerArgs);
}
}