mirror of
https://github.com/ershisan99/coolify.git
synced 2025-12-27 12:33:54 +00:00
v1.0.12 - Sveltekit migration (#44)
Changed the whole tech stack to SvelteKit which means: - Typescript - SSR - No fastify :( - Beta, but it's fine! Other changes: - Tailwind -> Tailwind JIT - A lot more
This commit is contained in:
51
src/routes/api/v1/application/check.ts
Normal file
51
src/routes/api/v1/application/check.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { setDefaultConfiguration } from '$lib/api/applications/configuration';
|
||||
import { saveServerLog } from '$lib/api/applications/logging';
|
||||
import { docker } from '$lib/api/docker';
|
||||
import type { Request } from '@sveltejs/kit';
|
||||
|
||||
export async function post(request: Request) {
|
||||
try {
|
||||
const { DOMAIN } = process.env;
|
||||
const configuration = setDefaultConfiguration(request.body);
|
||||
|
||||
const services = (await docker.engine.listServices()).filter(
|
||||
(r) => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application'
|
||||
);
|
||||
let foundDomain = false;
|
||||
|
||||
for (const service of services) {
|
||||
const running = JSON.parse(service.Spec.Labels.configuration);
|
||||
if (running) {
|
||||
if (
|
||||
running.publish.domain === configuration.publish.domain &&
|
||||
running.repository.id !== configuration.repository.id &&
|
||||
running.publish.path === configuration.publish.path
|
||||
) {
|
||||
foundDomain = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (DOMAIN === configuration.publish.domain) foundDomain = true;
|
||||
if (foundDomain) {
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
success: false,
|
||||
message: 'Domain already in use.'
|
||||
}
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: 200,
|
||||
body: { success: true, message: 'OK' }
|
||||
};
|
||||
} catch (error) {
|
||||
await saveServerLog(error);
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
50
src/routes/api/v1/application/config.ts
Normal file
50
src/routes/api/v1/application/config.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { docker } from '$lib/api/docker';
|
||||
import type { Request } from '@sveltejs/kit';
|
||||
|
||||
export async function post(request: Request) {
|
||||
const { name, organization, branch }: any = request.body || {};
|
||||
if (name && organization && branch) {
|
||||
const services = await docker.engine.listServices();
|
||||
const applications = services.filter(
|
||||
(r) => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application'
|
||||
);
|
||||
const found = applications.find((r) => {
|
||||
const configuration = r.Spec.Labels.configuration
|
||||
? JSON.parse(r.Spec.Labels.configuration)
|
||||
: null;
|
||||
if (branch) {
|
||||
if (
|
||||
configuration.repository.name === name &&
|
||||
configuration.repository.organization === organization &&
|
||||
configuration.repository.branch === branch
|
||||
) {
|
||||
return r;
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
configuration.repository.name === name &&
|
||||
configuration.repository.organization === organization
|
||||
) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
if (found) {
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
success: true,
|
||||
...JSON.parse(found.Spec.Labels.configuration)
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error: 'No configuration found.'
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
90
src/routes/api/v1/application/deploy/index.ts
Normal file
90
src/routes/api/v1/application/deploy/index.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import type { Request } from '@sveltejs/kit';
|
||||
import Deployment from '$models/Logs/Deployment';
|
||||
import { docker } from '$lib/api/docker';
|
||||
import { precheckDeployment, setDefaultConfiguration } from '$lib/api/applications/configuration';
|
||||
import cloneRepository from '$lib/api/applications/cloneRepository';
|
||||
import { cleanupTmp } from '$lib/api/common';
|
||||
import queueAndBuild from '$lib/api/applications/queueAndBuild';
|
||||
export async function post(request: Request) {
|
||||
let configuration;
|
||||
try {
|
||||
const services = (await docker.engine.listServices()).filter(
|
||||
(r) => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application'
|
||||
);
|
||||
configuration = setDefaultConfiguration(request.body);
|
||||
|
||||
if (!configuration) {
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error: 'Whaaat?'
|
||||
}
|
||||
};
|
||||
}
|
||||
await cloneRepository(configuration);
|
||||
const { foundService, imageChanged, configChanged, forceUpdate } = await precheckDeployment({
|
||||
services,
|
||||
configuration
|
||||
});
|
||||
if (foundService && !forceUpdate && !imageChanged && !configChanged) {
|
||||
cleanupTmp(configuration.general.workdir);
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
success: false,
|
||||
message: 'Nothing changed, no need to redeploy.'
|
||||
}
|
||||
};
|
||||
}
|
||||
const alreadyQueued = await Deployment.find({
|
||||
repoId: configuration.repository.id,
|
||||
branch: configuration.repository.branch,
|
||||
organization: configuration.repository.organization,
|
||||
name: configuration.repository.name,
|
||||
domain: configuration.publish.domain,
|
||||
progress: { $in: ['queued', 'inprogress'] }
|
||||
});
|
||||
if (alreadyQueued.length > 0) {
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
success: false,
|
||||
message: 'Already in the queue.'
|
||||
}
|
||||
};
|
||||
}
|
||||
queueAndBuild(configuration, imageChanged);
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
message: 'Deployment queued.',
|
||||
nickname: configuration.general.nickname,
|
||||
name: configuration.build.container.name,
|
||||
deployId: configuration.general.deployId
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
await Deployment.findOneAndUpdate(
|
||||
{
|
||||
repoId: configuration.repository.id,
|
||||
branch: configuration.repository.branch,
|
||||
organization: configuration.repository.organization,
|
||||
name: configuration.repository.name,
|
||||
domain: configuration.publish.domain,
|
||||
},
|
||||
{
|
||||
repoId: configuration.repository.id,
|
||||
branch: configuration.repository.branch,
|
||||
organization: configuration.repository.organization,
|
||||
name: configuration.repository.name,
|
||||
domain: configuration.publish.domain, progress: 'failed'
|
||||
}
|
||||
);
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
35
src/routes/api/v1/application/deploy/logs/[deployId].ts
Normal file
35
src/routes/api/v1/application/deploy/logs/[deployId].ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type { Request } from '@sveltejs/kit';
|
||||
import ApplicationLog from '$models/Logs/Application';
|
||||
import Deployment from '$models/Logs/Deployment';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export async function get(request: Request) {
|
||||
const { deployId } = request.params;
|
||||
try {
|
||||
const logs: any = await ApplicationLog.find({ deployId })
|
||||
.select('-_id -__v')
|
||||
.sort({ createdAt: 'asc' });
|
||||
|
||||
const deploy: any = await Deployment.findOne({ deployId })
|
||||
.select('-_id -__v')
|
||||
.sort({ createdAt: 'desc' });
|
||||
|
||||
const finalLogs: any = {};
|
||||
finalLogs.progress = deploy.progress;
|
||||
finalLogs.events = logs.map((log) => log.event);
|
||||
finalLogs.human = dayjs(deploy.updatedAt).from(dayjs(deploy.updatedAt));
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
...finalLogs
|
||||
}
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error: e
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
47
src/routes/api/v1/application/deploy/logs/index.ts
Normal file
47
src/routes/api/v1/application/deploy/logs/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { Request } from '@sveltejs/kit';
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc.js';
|
||||
import relativeTime from 'dayjs/plugin/relativeTime.js';
|
||||
import Deployment from '$models/Logs/Deployment';
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(relativeTime);
|
||||
export async function get(request: Request) {
|
||||
try {
|
||||
const repoId = request.query.get('repoId');
|
||||
const branch = request.query.get('branch');
|
||||
const page = request.query.get('page');
|
||||
|
||||
const onePage = 5;
|
||||
const show = Number(page) * onePage || 5;
|
||||
const deploy: any = await Deployment.find({ repoId, branch })
|
||||
.select('-_id -__v -repoId')
|
||||
.sort({ createdAt: 'desc' })
|
||||
.limit(show);
|
||||
|
||||
const finalLogs = deploy.map((d) => {
|
||||
const finalLogs = { ...d._doc };
|
||||
|
||||
const updatedAt = dayjs(d.updatedAt).utc();
|
||||
|
||||
finalLogs.took = updatedAt.diff(dayjs(d.createdAt)) / 1000;
|
||||
finalLogs.since = updatedAt.fromNow();
|
||||
|
||||
return finalLogs;
|
||||
});
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
success: true,
|
||||
logs: finalLogs
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
27
src/routes/api/v1/application/logs.ts
Normal file
27
src/routes/api/v1/application/logs.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { saveServerLog } from '$lib/api/applications/logging';
|
||||
import { docker } from '$lib/api/docker';
|
||||
import type { Request } from '@sveltejs/kit';
|
||||
|
||||
export async function get(request: Request) {
|
||||
try {
|
||||
const name = request.query.get('name');
|
||||
const service = await docker.engine.getService(`${name}_${name}`);
|
||||
const logs = (await service.logs({ stdout: true, stderr: true, timestamps: true }))
|
||||
.toString()
|
||||
.split('\n')
|
||||
.map((l) => l.slice(8))
|
||||
.filter((a) => a);
|
||||
return {
|
||||
status: 200,
|
||||
body: { success: true, logs }
|
||||
};
|
||||
} catch (error) {
|
||||
await saveServerLog(error);
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
60
src/routes/api/v1/application/remove.ts
Normal file
60
src/routes/api/v1/application/remove.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { purgeImagesContainers } from '$lib/api/applications/cleanup';
|
||||
import { docker } from '$lib/api/docker';
|
||||
import Deployment from '$models/Logs/Deployment';
|
||||
import ApplicationLog from '$models/Logs/Application';
|
||||
import { delay, execShellAsync } from '$lib/api/common';
|
||||
|
||||
async function call(found) {
|
||||
await delay(10000);
|
||||
await purgeImagesContainers(found, true);
|
||||
}
|
||||
export async function post(request: Request) {
|
||||
const { organization, name, branch } = request.body;
|
||||
let found = false;
|
||||
try {
|
||||
(await docker.engine.listServices())
|
||||
.filter((r) => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application')
|
||||
.map((s) => {
|
||||
const running = JSON.parse(s.Spec.Labels.configuration);
|
||||
if (
|
||||
running.repository.organization === organization &&
|
||||
running.repository.name === name &&
|
||||
running.repository.branch === branch
|
||||
) {
|
||||
found = running;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
if (found) {
|
||||
const deploys = await Deployment.find({ organization, branch, name });
|
||||
for (const deploy of deploys) {
|
||||
await ApplicationLog.deleteMany({ deployId: deploy.deployId });
|
||||
await Deployment.deleteMany({ deployId: deploy.deployId });
|
||||
}
|
||||
await execShellAsync(`docker stack rm ${found.build.container.name}`);
|
||||
call(found);
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
organization,
|
||||
name,
|
||||
branch
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: 500,
|
||||
error: {
|
||||
message: 'Nothing to do.'
|
||||
}
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 500,
|
||||
error: {
|
||||
message: 'Nothing to do.'
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user