cleanup stuffs

This commit is contained in:
Andras Bacsai
2022-10-27 09:55:32 +02:00
parent 4416646954
commit 2030f714fa
18 changed files with 130 additions and 160 deletions

View File

@@ -10,13 +10,11 @@ import { asyncExecShell, cleanupDockerStorage, createRemoteEngineConfiguration,
import { scheduler } from './lib/scheduler';
import { compareVersions } from 'compare-versions';
import Graceful from '@ladjs/graceful'
import axios from 'axios';
import yaml from 'js-yaml'
import fs from 'fs/promises';
import { verifyRemoteDockerEngineFn } from './routes/api/v1/destinations/handlers';
import { checkContainer } from './lib/docker';
import { migrateServicesToNewTemplate } from './lib';
import { getTemplates } from './lib/services';
import { refreshTags, refreshTemplates } from './routes/api/v1/handlers';
declare module 'fastify' {
interface FastifyInstance {
@@ -129,30 +127,10 @@ const host = '0.0.0.0';
try {
const { default: got } = await import('got')
try {
const tags = await got.get('https://get.coollabs.io/coolify/service-tags.json').text()
if (isDev) {
const templates = await fs.readFile('./devTemplates.yaml', 'utf8')
await fs.writeFile('./template.json', JSON.stringify(yaml.load(templates)))
const tags = await got.get('https://get.coollabs.io/coolify/service-tags.json').text()
await fs.writeFile('./tags.json', tags)
} else {
const response = await got.get('https://get.coollabs.io/coolify/service-templates.yaml').text()
await fs.writeFile('/app/template.json', JSON.stringify(yaml.load(response)))
await fs.writeFile('/app/tags.json', tags)
}
} catch (error) {
console.log("Couldn't get latest templates.")
console.log(error)
}
await migrateServicesToNewTemplate()
await fastify.listen({ port, host })
console.log(`Coolify's API is listening on ${host}:${port}`);
await migrateServicesToNewTemplate()
await initServer();
const graceful = new Graceful({ brees: [scheduler] });
@@ -192,11 +170,12 @@ const host = '0.0.0.0';
}, 10000)
await Promise.all([
getTagsTemplates(),
getArch(),
getIPAddress(),
configureRemoteDockers(),
])
} catch (error) {
console.error(error);
process.exit(1);
@@ -222,6 +201,27 @@ async function getIPAddress() {
} catch (error) { }
}
async function getTagsTemplates() {
const { default: got } = await import('got')
try {
const tags = await got.get('https://get.coollabs.io/coolify/service-tags.json').text()
if (isDev) {
const templates = await fs.readFile('./devTemplates.yaml', 'utf8')
await fs.writeFile('./templates.json', JSON.stringify(yaml.load(templates)))
await fs.writeFile('./tags.json', tags)
console.log('Tags and templates loaded in dev mode...')
} else {
const response = await got.get('https://get.coollabs.io/coolify/service-templates.yaml').text()
await fs.writeFile('/app/templates.json', JSON.stringify(yaml.load(response)))
await fs.writeFile('/app/tags.json', tags)
console.log('Tags and templates loaded...')
}
} catch (error) {
console.log("Couldn't get latest templates.")
console.log(error)
}
}
async function initServer() {
try {
console.log(`Initializing server...`);
@@ -234,6 +234,7 @@ async function initServer() {
}
} catch (error) { }
}
async function getArch() {
try {
const settings = await prisma.setting.findFirst({})
@@ -263,17 +264,15 @@ async function configureRemoteDockers() {
async function autoUpdater() {
try {
const { default: got } = await import('got')
const currentVersion = version;
const { data: versions } = await axios
.get(
`https://get.coollabs.io/versions.json`
, {
params: {
appId: process.env['COOLIFY_APP_ID'] || undefined,
version: currentVersion
}
})
const latestVersion = versions['coolify'].main.version;
const { coolify } = await got.get('https://get.coollabs.io/versions.json', {
searchParams: {
appId: process.env['COOLIFY_APP_ID'] || undefined,
version: currentVersion
}
}).json()
const latestVersion = coolify.main.version;
const isUpdateAvailable = compareVersions(latestVersion, currentVersion);
if (isUpdateAvailable === 1) {
const activeCount = 0
@@ -295,7 +294,9 @@ async function autoUpdater() {
}
}
}
} catch (error) { }
} catch (error) {
console.log(error)
}
}
async function checkFluentBit() {

View File

@@ -3,9 +3,9 @@ import fs from 'fs/promises';
export async function getTemplates() {
let templates: any = [];
if (isDev) {
templates = JSON.parse(await (await fs.readFile('./template.json')).toString())
templates = JSON.parse(await (await fs.readFile('./templates.json')).toString())
} else {
templates = JSON.parse(await (await fs.readFile('/app/template.json')).toString())
templates = JSON.parse(await (await fs.readFile('/app/templates.json')).toString())
}
// if (!isDev) {
// templates.push({
@@ -141,6 +141,19 @@ export async function getTemplates() {
// }
return templates
}
const compareSemanticVersions = (a: string, b: string) => {
const a1 = a.split('.');
const b1 = b.split('.');
const len = Math.min(a1.length, b1.length);
for (let i = 0; i < len; i++) {
const a2 = +a1[ i ] || 0;
const b2 = +b1[ i ] || 0;
if (a2 !== b2) {
return a2 > b2 ? 1 : -1;
}
}
return b1.length - a1.length;
};
export async function getTags(type?: string) {
let tags: any = [];
if (isDev) {
@@ -148,5 +161,7 @@ export async function getTags(type?: string) {
} else {
tags = JSON.parse(await (await fs.readFile('/app/tags.json')).toString())
}
return tags.find((tag: any) => tag.name.includes(type))
tags = tags.find((tag: any) => tag.name.includes(type))
tags.tags = tags.tags.sort(compareSemanticVersions).reverse();
return tags
}

View File

@@ -1,16 +1,15 @@
import cuid from 'cuid';
import crypto from 'node:crypto'
import jsonwebtoken from 'jsonwebtoken';
import axios from 'axios';
import { FastifyReply } from 'fastify';
import fs from 'fs/promises';
import yaml from 'js-yaml';
import csv from 'csvtojson';
import { day } from '../../../../lib/dayjs';
import { makeLabelForStandaloneApplication, setDefaultBaseImage, setDefaultConfiguration } from '../../../../lib/buildPacks/common';
import { checkDomainsIsValidInDNS, checkDoubleBranch, checkExposedPort, createDirectories, decrypt, defaultComposeConfiguration, encrypt, errorHandler, executeDockerCmd, generateSshKeyPair, getContainerUsage, getDomain, isDev, isDomainConfigured, listSettings, prisma, stopBuild, uniqueName } from '../../../../lib/common';
import { checkContainer, formatLabelsOnDocker, isContainerExited, removeContainer } from '../../../../lib/docker';
import { setDefaultBaseImage, setDefaultConfiguration } from '../../../../lib/buildPacks/common';
import { checkDomainsIsValidInDNS, checkExposedPort, createDirectories, decrypt, defaultComposeConfiguration, encrypt, errorHandler, executeDockerCmd, generateSshKeyPair, getContainerUsage, getDomain, isDev, isDomainConfigured, listSettings, prisma, stopBuild, uniqueName } from '../../../../lib/common';
import { checkContainer, formatLabelsOnDocker, removeContainer } from '../../../../lib/docker';
import type { FastifyRequest } from 'fastify';
import type { GetImages, CancelDeployment, CheckDNS, CheckRepository, DeleteApplication, DeleteSecret, DeleteStorage, GetApplicationLogs, GetBuildIdLogs, SaveApplication, SaveApplicationSettings, SaveApplicationSource, SaveDeployKey, SaveDestination, SaveSecret, SaveStorage, DeployApplication, CheckDomain, StopPreviewApplication, RestartPreviewApplication, GetBuilds } from './types';
@@ -124,7 +123,7 @@ export async function getApplicationStatus(request: FastifyRequest<OnlyId>) {
for (const container of containersArray) {
let isRunning = false;
let isExited = false;
let isRestarting = false;
let isRestarting = false;
const containerObj = JSON.parse(container);
const status = containerObj.State
if (status === 'running') {
@@ -771,6 +770,7 @@ export async function saveApplicationSource(request: FastifyRequest<SaveApplicat
export async function getGitHubToken(request: FastifyRequest<OnlyId>, reply: FastifyReply) {
try {
const { default: got } = await import('got')
const { id } = request.params
const { teamId } = request.user
const application: any = await getApplicationFromDB(id, teamId);
@@ -782,13 +782,13 @@ export async function getGitHubToken(request: FastifyRequest<OnlyId>, reply: Fas
const githubToken = jsonwebtoken.sign(payload, application.gitSource.githubApp.privateKey, {
algorithm: 'RS256'
});
const { data } = await axios.post(`${application.gitSource.apiUrl}/app/installations/${application.gitSource.githubApp.installationId}/access_tokens`, {}, {
const { token } = await got.post(`${application.gitSource.apiUrl}/app/installations/${application.gitSource.githubApp.installationId}/access_tokens`, {
headers: {
Authorization: `Bearer ${githubToken}`
'Authorization': `Bearer ${githubToken}`,
}
})
}).json()
return reply.code(201).send({
token: data.token
token
})
} catch ({ status, message }) {
return errorHandler({ status, message })

View File

@@ -1,4 +1,3 @@
import axios from "axios";
import { compareVersions } from "compare-versions";
import cuid from "cuid";
import bcrypt from "bcryptjs";
@@ -66,10 +65,10 @@ export async function refreshTemplates() {
try {
if (isDev) {
const response = await fs.readFile('./devTemplates.yaml', 'utf8')
await fs.writeFile('./template.json', JSON.stringify(yaml.load(response)))
await fs.writeFile('./templates.json', JSON.stringify(yaml.load(response)))
} else {
const response = await got.get('https://get.coollabs.io/coolify/service-templates.yaml').text()
await fs.writeFile('/app/template.json', JSON.stringify(yaml.load(response)))
await fs.writeFile('/app/templates.json', JSON.stringify(yaml.load(response)))
}
} catch (error) {
console.log(error)
@@ -86,14 +85,18 @@ export async function refreshTemplates() {
}
export async function checkUpdate(request: FastifyRequest) {
try {
const { default: got } = await import('got')
const isStaging =
request.hostname === "staging.coolify.io" ||
request.hostname === "arm.coolify.io";
const currentVersion = version;
const { data: versions } = await axios.get(
`https://get.coollabs.io/versions.json?appId=${process.env["COOLIFY_APP_ID"]}&version=${currentVersion}`
);
const latestVersion = versions["coolify"].main.version;
const { coolify } = await got.get('https://get.coollabs.io/versions.json', {
searchParams: {
appId: process.env['COOLIFY_APP_ID'] || undefined,
version: currentVersion
}
}).json()
const latestVersion = coolify.main.version;
const isUpdateAvailable = compareVersions(latestVersion, currentVersion);
if (isStaging) {
return {

View File

@@ -1,4 +1,3 @@
import axios from "axios";
import cuid from "cuid";
import crypto from "crypto";
import { encrypt, errorHandler, getDomain, getUIUrl, isDev, prisma } from "../../../lib/common";
@@ -32,13 +31,14 @@ export async function installGithub(request: FastifyRequest<InstallGithub>, repl
}
export async function configureGitHubApp(request, reply) {
try {
const { default: got } = await import('got')
const { code, state } = request.query;
const { apiUrl } = await prisma.gitSource.findFirst({
where: { id: state },
include: { githubApp: true, gitlabApp: true }
});
const { data }: any = await axios.post(`${apiUrl}/app-manifests/${code}/conversions`);
const data: any = await got.post(`${apiUrl}/app-manifests/${code}/conversions`).json()
const { id, client_id, slug, client_secret, pem, webhook_secret } = data
const encryptedClientSecret = encrypt(client_secret);

View File

@@ -1,4 +1,3 @@
import axios from "axios";
import cuid from "cuid";
import crypto from "crypto";
import type { FastifyReply, FastifyRequest } from "fastify";
@@ -10,6 +9,7 @@ import type { ConfigureGitLabApp, GitLabEvents } from "./types";
export async function configureGitLabApp(request: FastifyRequest<ConfigureGitLabApp>, reply: FastifyReply) {
try {
const { default: got } = await import('got')
const { code, state } = request.query;
const { fqdn } = await listSettings();
const { gitSource: { gitlabApp: { appId, appSecret }, htmlUrl } }: any = await getApplicationFromDB(state, undefined);
@@ -19,19 +19,21 @@ export async function configureGitLabApp(request: FastifyRequest<ConfigureGitLab
if (isDev) {
domain = getAPIUrl();
}
const params = new URLSearchParams({
client_id: appId,
client_secret: appSecret,
code,
state,
grant_type: 'authorization_code',
redirect_uri: `${domain}/webhooks/gitlab`
});
const { data } = await axios.post(`${htmlUrl}/oauth/token`, params)
const { access_token } = await got.post(`${htmlUrl}/oauth/token`, {
searchParams: {
client_id: appId,
client_secret: appSecret,
code,
state,
grant_type: 'authorization_code',
redirect_uri: `${domain}/webhooks/gitlab`
}
}).json()
if (isDev) {
return reply.redirect(`${getUIUrl()}/webhooks/success?token=${data.access_token}`)
return reply.redirect(`${getUIUrl()}/webhooks/success?token=${access_token}`)
}
return reply.redirect(`/webhooks/success?token=${data.access_token}`)
return reply.redirect(`/webhooks/success?token=${access_token}`)
} catch ({ status, message, ...other }) {
return errorHandler({ status, message })
}