lasthourcloud/apps/api/prisma/seed.js

118 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-07-06 09:02:36 +00:00
const dotEnvExtended = require('dotenv-extended');
dotEnvExtended.load();
const crypto = require('crypto');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const algorithm = 'aes-256-ctr';
async function main() {
// Enable registration for the first user
const settingsFound = await prisma.setting.findFirst({});
if (!settingsFound) {
await prisma.setting.create({
data: {
2022-11-28 10:48:38 +00:00
id: '0',
2022-07-06 09:02:36 +00:00
isRegistrationEnabled: true,
2022-09-07 07:06:30 +00:00
arch: process.arch,
DNSServers: '1.1.1.1,8.8.8.8'
2022-07-06 09:02:36 +00:00
}
});
} else {
await prisma.setting.update({
where: {
id: settingsFound.id
},
data: {
2022-11-28 10:48:38 +00:00
id: '0',
2022-07-06 09:02:36 +00:00
isTraefikUsed: true,
}
});
}
const localDocker = await prisma.destinationDocker.findFirst({
where: { engine: '/var/run/docker.sock' }
});
if (!localDocker) {
await prisma.destinationDocker.create({
data: {
engine: '/var/run/docker.sock',
name: 'Local Docker',
isCoolifyProxyUsed: true,
network: 'coolify'
}
});
}
// Set auto-update based on env variable
const isAutoUpdateEnabled = process.env['COOLIFY_AUTO_UPDATE'] === 'true';
2022-11-28 11:53:44 +00:00
await prisma.setting.update({
where: {
id: '0'
},
data: {
isAutoUpdateEnabled
}
});
2022-08-18 09:53:42 +00:00
const github = await prisma.gitSource.findFirst({
where: { htmlUrl: 'https://github.com', forPublic: true }
});
const gitlab = await prisma.gitSource.findFirst({
where: { htmlUrl: 'https://gitlab.com', forPublic: true }
});
if (!github) {
await prisma.gitSource.create({
data: {
apiUrl: 'https://api.github.com',
htmlUrl: 'https://github.com',
forPublic: true,
name: 'Github Public',
type: 'github'
}
});
}
if (!gitlab) {
await prisma.gitSource.create({
data: {
apiUrl: 'https://gitlab.com/api/v4',
htmlUrl: 'https://gitlab.com',
forPublic: true,
name: 'Gitlab Public',
type: 'gitlab'
}
});
}
2022-09-26 11:36:15 +00:00
// Set new preview secrets
const secrets = await prisma.secret.findMany({ where: { isPRMRSecret: false } })
if (secrets.length > 0) {
for (const secret of secrets) {
2022-09-26 13:42:19 +00:00
const previewSecrets = await prisma.secret.findMany({ where: { applicationId: secret.applicationId, name: secret.name, isPRMRSecret: true } })
2022-09-26 11:36:15 +00:00
if (previewSecrets.length === 0) {
await prisma.secret.create({ data: { ...secret, id: undefined, isPRMRSecret: true } })
}
}
}
2022-11-23 13:39:30 +00:00
// Add default docker registry (dockerhub)
const registries = await prisma.dockerRegistry.findMany()
if (registries.length === 0) {
await prisma.dockerRegistry.create({ data: { id: "0", name: 'Docker Hub', url: 'https://index.docker.io/v1/', isSystemWide: true } })
}
2022-07-06 09:02:36 +00:00
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
2022-07-06 09:49:07 +00:00
});
const encrypt = (text) => {
if (text) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, process.env['COOLIFY_SECRET_KEY'], iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return JSON.stringify({
iv: iv.toString('hex'),
content: encrypted.toString('hex')
});
}
};