80 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-04-02 16:22:51 +02:00
import { promises as fs } from 'fs';
2022-07-06 11:02:36 +02:00
import { buildImage } from './common';
2022-04-02 16:22:51 +02:00
const createDockerfile = async (data, image): Promise<void> => {
const {
workdir,
port,
baseDirectory,
secrets,
pullmergeRequestId,
pythonWSGI,
pythonModule,
2022-05-02 14:15:50 +02:00
pythonVariable,
buildId
2022-04-02 16:22:51 +02:00
} = data;
const Dockerfile: Array<string> = [];
Dockerfile.push(`FROM ${image}`);
Dockerfile.push('WORKDIR /app');
2022-05-02 14:15:50 +02:00
Dockerfile.push(`LABEL coolify.buildId=${buildId}`);
2022-04-02 16:22:51 +02:00
if (secrets.length > 0) {
secrets.forEach((secret) => {
if (secret.isBuildSecret) {
if (pullmergeRequestId) {
2022-09-09 15:46:47 +02:00
const isSecretFound = secrets.filter(s => s.name === secret.name && s.isPRMRSecret)
if (isSecretFound.length > 0) {
Dockerfile.push(`ARG ${secret.name}=${isSecretFound[0].value}`);
} else {
2022-04-02 16:22:51 +02:00
Dockerfile.push(`ARG ${secret.name}=${secret.value}`);
}
} else {
if (!secret.isPRMRSecret) {
Dockerfile.push(`ARG ${secret.name}=${secret.value}`);
}
}
}
});
}
if (pythonWSGI?.toLowerCase() === 'gunicorn') {
Dockerfile.push(`RUN pip install gunicorn`);
2022-06-10 10:42:58 +02:00
} else if (pythonWSGI?.toLowerCase() === 'uvicorn') {
2022-05-11 22:45:50 +05:30
Dockerfile.push(`RUN pip install uvicorn`);
2022-05-11 22:54:13 +05:30
} else if (pythonWSGI?.toLowerCase() === 'uwsgi') {
2022-04-02 16:22:51 +02:00
Dockerfile.push(`RUN apk add --no-cache uwsgi-python3`);
// Dockerfile.push(`RUN pip install --no-cache-dir uwsgi`)
}
try {
await fs.stat(`${workdir}${baseDirectory || ''}/requirements.txt`);
Dockerfile.push(`COPY .${baseDirectory || ''}/requirements.txt ./`);
Dockerfile.push(`RUN pip install --no-cache-dir -r .${baseDirectory || ''}/requirements.txt`);
} catch (e) {
//
}
Dockerfile.push(`COPY .${baseDirectory || ''} ./`);
Dockerfile.push(`EXPOSE ${port}`);
if (pythonWSGI?.toLowerCase() === 'gunicorn') {
2022-05-11 22:54:13 +05:30
Dockerfile.push(`CMD gunicorn -w=4 -b=0.0.0.0:8000 ${pythonModule}:${pythonVariable}`);
2022-06-10 10:42:58 +02:00
} else if (pythonWSGI?.toLowerCase() === 'uvicorn') {
Dockerfile.push(`CMD uvicorn ${pythonModule}:${pythonVariable} --port ${port} --host 0.0.0.0`);
2022-05-11 22:54:13 +05:30
} else if (pythonWSGI?.toLowerCase() === 'uwsgi') {
2022-04-02 16:22:51 +02:00
Dockerfile.push(
`CMD uwsgi --master -p 4 --http-socket 0.0.0.0:8000 --uid uwsgi --plugins python3 --protocol uwsgi --wsgi ${pythonModule}:${pythonVariable}`
);
} else {
Dockerfile.push(`CMD python ${pythonModule}`);
}
await fs.writeFile(`${workdir}/Dockerfile`, Dockerfile.join('\n'));
};
export default async function (data) {
try {
2022-04-26 14:51:08 +02:00
const { baseImage, baseBuildImage } = data;
await createDockerfile(data, baseImage);
2022-04-02 16:22:51 +02:00
await buildImage(data);
} catch (error) {
throw error;
}
2022-06-10 10:42:58 +02:00
}