This commit is contained in:
Andras Bacsai
2021-05-16 21:54:44 +02:00
committed by GitHub
parent 23a4ebb74a
commit adcd68c1ab
68 changed files with 2466 additions and 1194 deletions

View File

@@ -1,42 +0,0 @@
import type { Request } from '@sveltejs/kit';
// export async function api(request: Request, resource: string, data?: {}) {
// const base = 'https://github.com/';
// if (!request.context.isLoggedIn) {
// return { status: 401, body: 'Unauthorized' };
// }
// const res = await fetch(`${base}${resource}`, {
// method: request.method,
// headers: {
// 'content-type': 'application/json'
// },
// body: data && JSON.stringify(data)
// });
// return {
// status: res.status,
// body: await res.json()
// };
// }
export async function githubAPI(
request: Request,
resource: string,
token?: string,
data?: Record<string, unknown>
) {
const base = 'https://api.github.com';
const res = await fetch(`${base}${resource}`, {
method: request.method,
headers: {
'content-type': 'application/json',
accept: 'application/json',
authorization: token ? `token ${token}` : ''
},
body: data && JSON.stringify(data)
});
return {
status: res.status,
body: await res.json()
};
}

View File

@@ -44,7 +44,7 @@ export async function post(request: Request) {
return {
status: 500,
body: {
error
error: error.message || error
}
};
}

View File

@@ -1,9 +1,25 @@
import { docker } from '$lib/api/docker';
import Configuration from '$models/Configuration';
import type { Request } from '@sveltejs/kit';
export async function post(request: Request) {
const { name, organization, branch }: any = request.body || {};
if (name && organization && branch) {
const configurationFound = await Configuration.findOne({
'repository.name': name,
'repository.organization': organization,
'repository.branch': branch,
}).lean()
if (configurationFound) {
return {
status: 200,
body: {
success: true,
...configurationFound
}
};
}
const services = await docker.engine.listServices();
const applications = services.filter(
(r) => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application'
@@ -38,13 +54,12 @@ export async function post(request: Request) {
...JSON.parse(found.Spec.Labels.configuration)
}
};
} else {
return {
status: 500,
body: {
error: 'No configuration found.'
}
};
}
return {
status: 500,
body: {
error: 'No configuration found.'
}
};
}
}

View File

@@ -1,10 +1,11 @@
import type { Request } from '@sveltejs/kit';
import Deployment from '$models/Logs/Deployment';
import Deployment from '$models/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';
import Configuration from '$models/Configuration';
export async function post(request: Request) {
let configuration;
try {
@@ -53,6 +54,27 @@ export async function post(request: Request) {
}
};
}
const { id, organization, name, branch } = configuration.repository;
const { domain } = configuration.publish;
const { deployId, nickname } = configuration.general;
await new Deployment({
repoId: id,
branch,
deployId,
domain,
organization,
name,
nickname
}).save();
await Configuration.findOneAndUpdate({
'repository.id': id,
'repository.organization': organization,
'repository.name': name,
'repository.branch': branch,
},
{ ...configuration },
{ upsert: true, new: true })
queueAndBuild(configuration, imageChanged);
return {
status: 200,
@@ -70,20 +92,21 @@ export async function post(request: Request) {
branch: configuration.repository.branch,
organization: configuration.repository.organization,
name: configuration.repository.name,
domain: configuration.publish.domain,
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'
domain: configuration.publish.domain,
progress: 'failed'
}
);
return {
status: 500,
body: {
error
error: error.message || error
}
};
}

View File

@@ -1,6 +1,6 @@
import type { Request } from '@sveltejs/kit';
import ApplicationLog from '$models/Logs/Application';
import Deployment from '$models/Logs/Deployment';
import ApplicationLog from '$models/ApplicationLog';
import Deployment from '$models/Deployment';
import dayjs from 'dayjs';
export async function get(request: Request) {
@@ -24,11 +24,11 @@ export async function get(request: Request) {
...finalLogs
}
};
} catch (e) {
} catch (error) {
return {
status: 500,
body: {
error: e
error: error.message || error
}
};
}

View File

@@ -2,7 +2,7 @@ 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';
import Deployment from '$models/Deployment';
dayjs.extend(utc);
dayjs.extend(relativeTime);
export async function get(request: Request) {
@@ -10,7 +10,6 @@ export async function get(request: Request) {
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 })
@@ -20,12 +19,9 @@ export async function get(request: Request) {
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 {
@@ -36,11 +32,10 @@ export async function get(request: Request) {
}
};
} catch (error) {
console.log(error);
return {
status: 500,
body: {
error
error: error.message || error
}
};
}

View File

@@ -16,11 +16,12 @@ export async function get(request: Request) {
body: { success: true, logs }
};
} catch (error) {
console.log(error)
await saveServerLog(error);
return {
status: 500,
body: {
error
error: 'No such service. Is it under deployment?'
}
};
}

View File

@@ -1,8 +1,9 @@
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 Deployment from '$models/Deployment';
import ApplicationLog from '$models/ApplicationLog';
import { delay, execShellAsync } from '$lib/api/common';
import Configuration from '$models/Configuration';
async function call(found) {
await delay(10000);
@@ -26,6 +27,11 @@ export async function post(request: Request) {
return null;
});
if (found) {
await Configuration.findOneAndRemove({
'repository.name': name,
'repository.organization': organization,
'repository.branch': branch,
})
const deploys = await Deployment.find({ organization, branch, name });
for (const deploy of deploys) {
await ApplicationLog.deleteMany({ deployId: deploy.deployId });

View File

@@ -1,9 +1,7 @@
import { docker } from '$lib/api/docker';
import LogsServer from '$models/Logs/Server';
import type { Request } from '@sveltejs/kit';
export async function get(request: Request) {
const serverLogs = await LogsServer.find();
const dockerServices = await docker.engine.listServices();
let applications: any = dockerServices.filter(
(r) =>
@@ -61,7 +59,6 @@ export async function get(request: Request) {
status: 200,
body: {
success: true,
serverLogs,
applications: {
deployed: applications
},

View File

@@ -100,6 +100,31 @@ export async function post(request: Request) {
body: fs.readFileSync(`${fullfilename}`)
};
}
} else if (type === 'redis') {
if (databaseService) {
const password = configuration.database.passwords[0];
const databaseName = configuration.database.defaultDatabaseName;
const filename = `${databaseName}_${now.getTime()}.rdb`;
const fullfilename = `${tmpdir}/${filename}`;
await execShellAsync(
`docker exec -i ${containerID} /bin/bash -c "redis-cli --pass ${password} save"`
);
await execShellAsync(
`docker cp ${containerID}:/bitnami/redis/data/dump.rdb ${fullfilename}`
);
await execShellAsync(
`docker exec -i ${containerID} /bin/bash -c "rm -f /bitnami/redis/data/dump.rdb"`
);
return {
status: 200,
headers: {
'Content-Type': 'application/octet-stream',
'Content-Transfer-Encoding': 'binary',
'Content-Disposition': `attachment; filename=${filename}`
},
body: fs.readFileSync(`${fullfilename}`)
};
}
}
return {
status: 501,
@@ -108,12 +133,11 @@ export async function post(request: Request) {
}
};
} catch (error) {
console.log(error);
await saveServerLog(error);
return {
status: 500,
body: {
error
error: error.message || error
}
};
} finally {

View File

@@ -96,6 +96,12 @@ export async function post(request: Request) {
hard: 262144
}
};
} else if (type === 'redis') {
image = 'bitnami/redis';
volume = `${configuration.general.deployId}-${type}-data:/bitnami/redis/data`;
generateEnvs = {
REDIS_PASSWORD: passwords[0]
};
}
const stack = {

View File

@@ -1,10 +1,10 @@
import { githubAPI } from '$api';
import type { Request } from '@sveltejs/kit';
import mongoose from 'mongoose';
import User from '$models/User';
import Settings from '$models/Settings';
import cuid from 'cuid';
import jsonwebtoken from 'jsonwebtoken';
import { githubAPI } from '$lib/api/github';
export async function get(request: Request) {
const code = request.query.get('code');
@@ -17,7 +17,7 @@ export async function get(request: Request) {
{ headers: { accept: 'application/json' } }
)
).json();
const { avatar_url, id } = await (await githubAPI(request, '/user', access_token)).body;
const { avatar_url } = await (await githubAPI(request, '/user', access_token)).body;
const email = (await githubAPI(request, '/user/emails', access_token)).body.filter(
(e) => e.primary
)[0].email;
@@ -41,11 +41,10 @@ export async function get(request: Request) {
try {
await newUser.save();
await defaultSettings.save();
} catch (e) {
console.log(e);
} catch (error) {
return {
status: 500,
body: e
error: error.message || error
};
}
} else {
@@ -73,12 +72,11 @@ export async function get(request: Request) {
});
try {
await newUser.save();
} catch (e) {
console.log(e);
} catch (error) {
return {
status: 500,
body: {
error: e
error: error.message || error
}
};
}
@@ -103,8 +101,6 @@ export async function get(request: Request) {
}
};
} catch (error) {
console.log('error happened');
console.log(error);
return { status: 500, body: { ...error } };
return { status: 500, body: { error: error.message || error } };
}
}

View File

@@ -34,12 +34,11 @@ export async function get(request: Request) {
};
}
} catch (error) {
console.log(error);
return {
status: 500,
body: {
success: false,
error
error: error.message || error
}
};
}

View File

@@ -22,7 +22,7 @@ export async function get(request: Request) {
return {
status: 500,
body: {
error
error: error.message || error
}
};
}
@@ -45,7 +45,7 @@ export async function post(request: Request) {
return {
status: 500,
body: {
error
error: error.message || error
}
};
}

View File

@@ -10,7 +10,6 @@ export async function get(request: Request) {
execShellAsync(
'docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -u root coolify bash -c "$(curl -fsSL https://get.coollabs.io/coolify/upgrade-p2.sh)"'
);
// saveServerLog({ message: upgradeP2, type: 'UPGRADE-P-2' })
return {
status: 200,
body: {

View File

@@ -1,24 +0,0 @@
// import { deleteCookies } from '$lib/api/common';
// import { verifyUserId } from '$lib/api/common';
// import type { Request } from '@sveltejs/kit';
// import * as cookie from 'cookie';
// export async function post(request: Request) {
// const { coolToken } = cookie.parse(request.headers.cookie || '');
// try {
// await verifyUserId(coolToken);
// return {
// status: 200,
// body: { success: true }
// };
// } catch (error) {
// return {
// status: 301,
// headers: {
// location: '/',
// 'set-cookie': [...deleteCookies]
// },
// body: { error: 'Unauthorized' }
// };
// }
// }

View File

@@ -1,6 +1,6 @@
import type { Request } from '@sveltejs/kit';
import crypto from 'crypto';
import Deployment from '$models/Logs/Deployment';
import Deployment from '$models/Deployment';
import { docker } from '$lib/api/docker';
import { precheckDeployment, setDefaultConfiguration } from '$lib/api/applications/configuration';
import cloneRepository from '$lib/api/applications/cloneRepository';
@@ -106,7 +106,7 @@ export async function post(request: Request) {
return {
status: 500,
body: {
error
error: error.message || error
}
};
}