Skip to content

Commit

Permalink
v1.0.7 (#32)
Browse files Browse the repository at this point in the history
New features:
- Automatic error reporting (enabled by default)
- Increase build times by leveraging docker build caches
- 
Fixes:
- Fix error handling
- Fix vue autodetect
- Custom dockerfile is not the default

Others:
- Cleanup `logs-servers` collection, because old errors are not standardized
- New Traefik proxy version
- Standardized directory configurations
  • Loading branch information
andrasbacsai authored Apr 19, 2021
1 parent bad8428 commit 142b83c
Show file tree
Hide file tree
Showing 51 changed files with 1,295 additions and 832 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ dist-ssr
yarn-error.log
api/development/console.log
.pnpm-debug.log
yarn.lock
.pnpm-store
2 changes: 1 addition & 1 deletion api/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = async function (fastify, opts) {
// Private routes
fastify.register(async function (server) {
if (process.env.NODE_ENV === 'production') server.register(require('./plugins/authentication'))
server.register(require('./plugins/authentication'))
server.register(require('./routes/v1/upgrade'), { prefix: '/upgrade' })
server.register(require('./routes/v1/settings'), { prefix: '/settings' })
server.register(require('./routes/v1/dashboard'), { prefix: '/dashboard' })
Expand Down
22 changes: 9 additions & 13 deletions api/buildPacks/custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ const fs = require('fs').promises
const { streamEvents, docker } = require('../../libs/docker')

module.exports = async function (configuration) {
try {
const path = `${configuration.general.workdir}/${configuration.build.directory ? configuration.build.directory : ''}`
if (fs.stat(`${path}/Dockerfile`)) {
const stream = await docker.engine.buildImage(
{ src: ['.'], context: path },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
} else {
throw { error: 'No custom dockerfile found.', type: 'app' }
}
} catch (error) {
throw { error, type: 'server' }
const path = `${configuration.general.workdir}/${configuration.build.directory ? configuration.build.directory : ''}`
if (fs.stat(`${path}/Dockerfile`)) {
const stream = await docker.engine.buildImage(
{ src: ['.'], context: path },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
} else {
throw new Error('No custom dockerfile found.')
}
}
19 changes: 8 additions & 11 deletions api/buildPacks/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ const buildImageNodeDocker = (configuration) => {
return [
'FROM node:lts',
'WORKDIR /usr/src/app',
`COPY ${configuration.build.directory} ./`,
`COPY ${configuration.build.directory}/package*.json .`,
configuration.build.command.installation && `RUN ${configuration.build.command.installation}`,
`COPY ./${configuration.build.directory} .`,
`RUN ${configuration.build.command.build}`
].join('\n')
}
async function buildImage (configuration) {
try {
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, buildImageNodeDocker(configuration))
const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
} catch (error) {
throw { error, type: 'server' }
}
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, buildImageNodeDocker(configuration))
const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
}

module.exports = {
Expand Down
26 changes: 12 additions & 14 deletions api/buildPacks/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ const publishNodejsDocker = (configuration) => {
'FROM node:lts',
'WORKDIR /usr/src/app',
configuration.build.command.build
? `COPY --from=${configuration.build.container.name}:${configuration.build.container.tag} /usr/src/app/${configuration.publish.directory} ./`
: `COPY ${configuration.build.directory} ./`,
configuration.build.command.installation && `RUN ${configuration.build.command.installation}`,
? `COPY --from=${configuration.build.container.name}:${configuration.build.container.tag} /usr/src/app/${configuration.publish.directory} .`
: `
COPY ${configuration.build.directory}/package*.json .
RUN ${configuration.build.command.installation}
COPY ./${configuration.build.directory} .`,
`EXPOSE ${configuration.publish.port}`,
'CMD [ "yarn", "start" ]'
].join('\n')
}

module.exports = async function (configuration) {
try {
if (configuration.build.command.build) await buildImage(configuration)
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishNodejsDocker(configuration))
const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
} catch (error) {
throw { error, type: 'server' }
}
if (configuration.build.command.build) await buildImage(configuration)
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishNodejsDocker(configuration))
const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
}
18 changes: 7 additions & 11 deletions api/buildPacks/php/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ const publishPHPDocker = (configuration) => {
'FROM php:apache',
'RUN a2enmod rewrite',
'WORKDIR /usr/src/app',
`COPY .${configuration.build.directory} /var/www/html`,
`COPY ./${configuration.build.directory} /var/www/html`,
'EXPOSE 80',
' CMD ["apache2-foreground"]'
].join('\n')
}

module.exports = async function (configuration) {
try {
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishPHPDocker(configuration))
const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
} catch (error) {
throw { error, type: 'server' }
}
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishPHPDocker(configuration))
const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
}
38 changes: 17 additions & 21 deletions api/buildPacks/rust/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,24 @@ const cacheRustDocker = (configuration, custom) => {
}

module.exports = async function (configuration) {
try {
const cargoToml = await execShellAsync(`cat ${configuration.general.workdir}/Cargo.toml`)
const parsedToml = TOML.parse(cargoToml)
const custom = {
name: parsedToml.package.name
}
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, cacheRustDocker(configuration, custom))
const cargoToml = await execShellAsync(`cat ${configuration.general.workdir}/Cargo.toml`)
const parsedToml = TOML.parse(cargoToml)
const custom = {
name: parsedToml.package.name
}
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, cacheRustDocker(configuration, custom))

let stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:cache` }
)
await streamEvents(stream, configuration)
let stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:cache` }
)
await streamEvents(stream, configuration)

await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishRustDocker(configuration, custom))
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishRustDocker(configuration, custom))

stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
} catch (error) {
throw { error, type: 'server' }
}
stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
}
22 changes: 9 additions & 13 deletions api/buildPacks/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,20 @@ const publishStaticDocker = (configuration) => {
'COPY nginx.conf /etc/nginx/nginx.conf',
'WORKDIR /usr/share/nginx/html',
configuration.build.command.build
? `COPY --from=${configuration.build.container.name}:${configuration.build.container.tag} /usr/src/app/${configuration.publish.directory} ./`
: `COPY ${configuration.build.directory} ./`,
? `COPY --from=${configuration.build.container.name}:${configuration.build.container.tag} /usr/src/app/${configuration.publish.directory} .`
: `COPY ./${configuration.build.directory} .`,
'EXPOSE 80',
'CMD ["nginx", "-g", "daemon off;"]'
].join('\n')
}

module.exports = async function (configuration) {
try {
if (configuration.build.command.build) await buildImage(configuration)
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishStaticDocker(configuration))
if (configuration.build.command.build) await buildImage(configuration)
await fs.writeFile(`${configuration.general.workdir}/Dockerfile`, publishStaticDocker(configuration))

const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
} catch (error) {
throw { error, type: 'server' }
}
const stream = await docker.engine.buildImage(
{ src: ['.'], context: configuration.general.workdir },
{ t: `${configuration.build.container.name}:${configuration.build.container.tag}` }
)
await streamEvents(stream, configuration)
}
25 changes: 7 additions & 18 deletions api/libs/applications/build/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,12 @@ module.exports = async function (configuration) {

const execute = packs[configuration.build.pack]
if (execute) {
try {
await Deployment.findOneAndUpdate(
{ repoId: id, branch, deployId, organization, name, domain },
{ repoId: id, branch, deployId, organization, name, domain, progress: 'inprogress' })
await saveAppLog('### Building application.', configuration)

await execute(configuration)

await saveAppLog('### Building done.', configuration)
} catch (error) {
await Deployment.findOneAndUpdate(
{ repoId: id, branch, deployId, organization, name, domain },
{ repoId: id, branch, deployId, organization, name, domain, progress: 'failed' })
if (error.stack) throw { error: error.stack, type: 'server' }
throw { error, type: 'app' }
}
await Deployment.findOneAndUpdate(
{ repoId: id, branch, deployId, organization, name, domain },
{ repoId: id, branch, deployId, organization, name, domain, progress: 'inprogress' })
await saveAppLog('### Building application.', configuration)
await execute(configuration)
await saveAppLog('### Building done.', configuration)
} else {
try {
await Deployment.findOneAndUpdate(
Expand All @@ -33,7 +23,6 @@ module.exports = async function (configuration) {
} catch (error) {
// Hmm.
}

throw { error: 'No buildpack found.', type: 'app' }
throw new Error('No buildpack found.')
}
}
42 changes: 14 additions & 28 deletions api/libs/applications/cleanup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,25 @@ const { execShellAsync } = require('../../common')
const Deployment = require('../../../models/Deployment')

async function purgeImagesContainers () {
try {
await execShellAsync('docker container prune -f')
await execShellAsync('docker image prune -f --filter=label!=coolify-reserve=true')
} catch (error) {
throw { error, type: 'server' }
}
await execShellAsync('docker container prune -f')
await execShellAsync('docker image prune -f --filter=label!=coolify-reserve=true')
}

async function cleanupStuckedDeploymentsInDB (configuration) {
const { id } = configuration.repository
const deployId = configuration.general.deployId
try {
// Cleanup stucked deployments.
const deployments = await Deployment.find({ repoId: id, deployId: { $ne: deployId }, progress: { $in: ['queued', 'inprogress'] } })
for (const deployment of deployments) {
await Deployment.findByIdAndUpdate(deployment._id, { $set: { progress: 'failed' } })
}
} catch (error) {
throw { error, type: 'server' }
}
async function cleanupStuckedDeploymentsInDB () {
// Cleanup stucked deployments.
await Deployment.updateMany(
{ progress: { $in: ['queued', 'inprogress'] } },
{ progress: 'failed' }
)
}

async function deleteSameDeployments (configuration) {
try {
await (await docker.engine.listServices()).filter(r => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application').map(async s => {
const running = JSON.parse(s.Spec.Labels.configuration)
if (running.repository.id === configuration.repository.id && running.repository.branch === configuration.repository.branch) {
await execShellAsync(`docker stack rm ${s.Spec.Labels['com.docker.stack.namespace']}`)
}
})
} catch (error) {
throw { error, type: 'server' }
}
await (await docker.engine.listServices()).filter(r => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application').map(async s => {
const running = JSON.parse(s.Spec.Labels.configuration)
if (running.repository.id === configuration.repository.id && running.repository.branch === configuration.repository.branch) {
await execShellAsync(`docker stack rm ${s.Spec.Labels['com.docker.stack.namespace']}`)
}
})
}

module.exports = { cleanupStuckedDeploymentsInDB, deleteSameDeployments, purgeImagesContainers }
Loading

0 comments on commit 142b83c

Please sign in to comment.