2022-02-10 15:47:44 +01:00
|
|
|
import { base64Encode } from '$lib/crypto';
|
|
|
|
import { getDomain, saveBuildLog, version } from '$lib/common';
|
|
|
|
import * as db from '$lib/database';
|
|
|
|
import { scanningTemplates } from '$lib/components/templates';
|
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import { staticDeployments } from '$lib/components/common';
|
|
|
|
|
|
|
|
export function makeLabelForStandaloneApplication({
|
|
|
|
applicationId,
|
|
|
|
fqdn,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
pullmergeRequestId = null,
|
|
|
|
buildPack,
|
|
|
|
repository,
|
|
|
|
branch,
|
|
|
|
projectId,
|
|
|
|
port,
|
|
|
|
commit,
|
|
|
|
installCommand,
|
|
|
|
buildCommand,
|
|
|
|
startCommand,
|
|
|
|
baseDirectory,
|
|
|
|
publishDirectory
|
|
|
|
}) {
|
|
|
|
if (pullmergeRequestId) {
|
|
|
|
const protocol = fqdn.startsWith('https://') ? 'https' : 'http';
|
|
|
|
const domain = getDomain(fqdn);
|
|
|
|
fqdn = `${protocol}://${pullmergeRequestId}.${domain}`;
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'--label coolify.managed=true',
|
|
|
|
`--label coolify.version=${version}`,
|
|
|
|
`--label coolify.type=standalone-application`,
|
|
|
|
`--label coolify.configuration=${base64Encode(
|
|
|
|
JSON.stringify({
|
|
|
|
applicationId,
|
|
|
|
fqdn,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
pullmergeRequestId,
|
|
|
|
buildPack,
|
|
|
|
repository,
|
|
|
|
branch,
|
|
|
|
projectId,
|
|
|
|
port,
|
|
|
|
commit,
|
|
|
|
installCommand,
|
|
|
|
buildCommand,
|
|
|
|
startCommand,
|
|
|
|
baseDirectory,
|
|
|
|
publishDirectory
|
|
|
|
})
|
|
|
|
)}`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
export async function makeLabelForStandaloneDatabase({ id, image, volume }) {
|
|
|
|
const database = await db.prisma.database.findFirst({ where: { id } });
|
|
|
|
delete database.destinationDockerId;
|
|
|
|
delete database.createdAt;
|
|
|
|
delete database.updatedAt;
|
|
|
|
return [
|
|
|
|
'coolify.managed=true',
|
|
|
|
`coolify.version=${version}`,
|
|
|
|
`coolify.type=standalone-database`,
|
|
|
|
`coolify.configuration=${base64Encode(
|
|
|
|
JSON.stringify({
|
|
|
|
version,
|
|
|
|
image,
|
|
|
|
volume,
|
|
|
|
...database
|
|
|
|
})
|
|
|
|
)}`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:44:36 +01:00
|
|
|
export function makeLabelForServices(type) {
|
2022-02-10 15:47:44 +01:00
|
|
|
return [
|
|
|
|
'coolify.managed=true',
|
|
|
|
`coolify.version=${version}`,
|
2022-02-15 21:44:36 +01:00
|
|
|
`coolify.type=service`,
|
|
|
|
`coolify.service.type=${type}`
|
2022-02-10 15:47:44 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const setDefaultConfiguration = async (data) => {
|
2022-03-19 15:04:52 +01:00
|
|
|
let {
|
|
|
|
buildPack,
|
|
|
|
port,
|
|
|
|
installCommand,
|
|
|
|
startCommand,
|
|
|
|
buildCommand,
|
|
|
|
publishDirectory,
|
|
|
|
baseDirectory
|
|
|
|
} = data;
|
2022-02-10 15:47:44 +01:00
|
|
|
const template = scanningTemplates[buildPack];
|
|
|
|
if (!port) {
|
|
|
|
port = template?.port || 3000;
|
|
|
|
|
|
|
|
if (buildPack === 'static') port = 80;
|
|
|
|
else if (buildPack === 'node') port = 3000;
|
|
|
|
else if (buildPack === 'php') port = 80;
|
|
|
|
}
|
|
|
|
if (!installCommand) installCommand = template?.installCommand || 'yarn install';
|
|
|
|
if (!startCommand) startCommand = template?.startCommand || 'yarn start';
|
|
|
|
if (!buildCommand) buildCommand = template?.buildCommand || null;
|
|
|
|
if (!publishDirectory) publishDirectory = template?.publishDirectory || null;
|
2022-03-19 15:04:52 +01:00
|
|
|
if (baseDirectory) {
|
|
|
|
if (!baseDirectory.startsWith('/')) baseDirectory = `/${baseDirectory}`;
|
|
|
|
if (!baseDirectory.endsWith('/')) baseDirectory = `${baseDirectory}/`;
|
|
|
|
}
|
2022-02-10 15:47:44 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
buildPack,
|
|
|
|
port,
|
|
|
|
installCommand,
|
|
|
|
startCommand,
|
|
|
|
buildCommand,
|
2022-03-19 15:04:52 +01:00
|
|
|
publishDirectory,
|
|
|
|
baseDirectory
|
2022-02-10 15:47:44 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function copyBaseConfigurationFiles(buildPack, workdir, buildId, applicationId) {
|
|
|
|
try {
|
|
|
|
// TODO: Write full .dockerignore for all deployments!!
|
|
|
|
if (buildPack === 'php') {
|
|
|
|
await fs.writeFile(
|
|
|
|
`${workdir}/.htaccess`,
|
|
|
|
`
|
|
|
|
RewriteEngine On
|
|
|
|
RewriteBase /
|
|
|
|
RewriteCond %{REQUEST_FILENAME} !-d
|
|
|
|
RewriteCond %{REQUEST_FILENAME} !-f
|
|
|
|
RewriteRule ^(.+)$ index.php [QSA,L]
|
|
|
|
`
|
|
|
|
);
|
2022-03-21 21:46:49 +01:00
|
|
|
await fs.writeFile(`${workdir}/entrypoint.sh`, `chown -R 1000 /app`);
|
2022-02-10 15:47:44 +01:00
|
|
|
saveBuildLog({ line: 'Copied default configuration file for PHP.', buildId, applicationId });
|
|
|
|
} else if (staticDeployments.includes(buildPack)) {
|
|
|
|
await fs.writeFile(
|
|
|
|
`${workdir}/nginx.conf`,
|
|
|
|
`user nginx;
|
|
|
|
worker_processes auto;
|
|
|
|
|
2022-03-21 21:25:01 +01:00
|
|
|
error_log /docker.stdout;
|
|
|
|
pid /run/nginx.pid;
|
2022-02-10 15:47:44 +01:00
|
|
|
|
|
|
|
events {
|
|
|
|
worker_connections 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
http {
|
2022-03-21 21:25:01 +01:00
|
|
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
|
|
|
'$status $body_bytes_sent "$http_referer" '
|
|
|
|
'"$http_user_agent" "$http_x_forwarded_for"';
|
|
|
|
|
|
|
|
access_log /docker.stdout main;
|
|
|
|
|
|
|
|
sendfile on;
|
|
|
|
tcp_nopush on;
|
|
|
|
tcp_nodelay on;
|
|
|
|
keepalive_timeout 65;
|
|
|
|
types_hash_max_size 2048;
|
|
|
|
|
|
|
|
include /etc/nginx/mime.types;
|
|
|
|
default_type application/octet-stream;
|
2022-02-10 15:47:44 +01:00
|
|
|
|
|
|
|
server {
|
|
|
|
listen 80;
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
location / {
|
2022-03-21 21:25:01 +01:00
|
|
|
root /app;
|
2022-02-10 15:47:44 +01:00
|
|
|
index index.html;
|
|
|
|
try_files $uri $uri/index.html $uri/ /index.html =404;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_page 404 /50x.html;
|
|
|
|
|
|
|
|
# redirect server error pages to the static page /50x.html
|
|
|
|
#
|
|
|
|
error_page 500 502 503 504 /50x.html;
|
|
|
|
location = /50x.html {
|
2022-03-21 21:25:01 +01:00
|
|
|
root /app;
|
2022-02-10 15:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
`
|
|
|
|
);
|
|
|
|
saveBuildLog({ line: 'Copied default configuration file.', buildId, applicationId });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 15:04:52 +01:00
|
|
|
|
|
|
|
export function checkPnpm(installCommand = null, buildCommand = null, startCommand = null) {
|
|
|
|
return (
|
|
|
|
installCommand?.includes('pnpm') ||
|
|
|
|
buildCommand?.includes('pnpm') ||
|
|
|
|
startCommand?.includes('pnpm')
|
|
|
|
);
|
|
|
|
}
|