lasthourcloud/apps/api/scripts/convertCaproverTemplates.mjs

111 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-10-24 12:23:34 +00:00
// Convert caprover format to coolify format
2022-10-20 14:06:33 +00:00
import fs from 'fs/promises';
import yaml from 'js-yaml';
2022-10-26 07:27:43 +00:00
const templateYml = await fs.readFile('./convert.yaml', 'utf8')
2022-10-20 14:06:33 +00:00
const template = yaml.load(templateYml)
const newTemplate = {
"templateVersion": "1.0.0",
2022-10-21 13:51:32 +00:00
"defaultVersion": "latest",
2022-10-28 09:54:03 +00:00
"type": "",
2022-10-20 14:06:33 +00:00
"name": "",
"description": "",
"services": {
},
"variables": []
}
2022-10-26 07:27:43 +00:00
const version = template.caproverOneClickApp.variables.find(v => v.id === '$$cap_APP_VERSION' || v.id === '$$cap_version').defaultValue || 'latest'
2022-10-20 14:06:33 +00:00
2022-10-21 13:51:32 +00:00
newTemplate.name = template.caproverOneClickApp.displayName
2022-10-28 09:54:03 +00:00
newTemplate.type = template.caproverOneClickApp.displayName.replaceAll(' ', '').toLowerCase()
2022-10-20 14:06:33 +00:00
newTemplate.documentation = template.caproverOneClickApp.documentation
newTemplate.description = template.caproverOneClickApp.description
2022-10-21 13:51:32 +00:00
newTemplate.defaultVersion = version
2022-10-20 14:06:33 +00:00
const varSet = new Set()
2022-10-21 13:51:32 +00:00
2022-10-20 14:06:33 +00:00
const caproverVariables = template.caproverOneClickApp.variables
for (const service of Object.keys(template.services)) {
const serviceTemplate = template.services[service]
const newServiceName = service.replaceAll('cap_appname', 'id')
const newService = {
image: '',
command: '',
environment: [],
volumes: []
}
const FROM = serviceTemplate.caproverExtra?.dockerfileLines?.find((line) => line.startsWith('FROM'))
if (serviceTemplate.image) {
2022-10-26 07:27:43 +00:00
newService.image = serviceTemplate.image.replaceAll('cap_APP_VERSION', 'core_version').replaceAll('cap_version', 'core_version')
2022-10-20 14:06:33 +00:00
} else if (FROM) {
2022-10-26 07:27:43 +00:00
newService.image = FROM.split(' ')[1].replaceAll('cap_APP_VERSION', 'core_version').replaceAll('cap_version', 'core_version')
2022-10-20 14:06:33 +00:00
}
const CMD = serviceTemplate.caproverExtra?.dockerfileLines?.find((line) => line.startsWith('CMD'))
if (serviceTemplate.command) {
newService.command = serviceTemplate.command
} else if (CMD) {
newService.command = CMD.replace('CMD ', '').replaceAll('"', '').replaceAll('[', '').replaceAll(']', '').replaceAll(',', ' ').replace(/\s+/g, ' ')
} else {
delete newService.command
}
const ENTRYPOINT = serviceTemplate.caproverExtra?.dockerfileLines?.find((line) => line.startsWith('ENTRYPOINT'))
if (serviceTemplate.entrypoint) {
newService.command = serviceTemplate.entrypoint
} else if (ENTRYPOINT) {
newService.entrypoint = ENTRYPOINT.replace('ENTRYPOINT ', '').replaceAll('"', '').replaceAll('[', '').replaceAll(']', '').replaceAll(',', ' ').replace(/\s+/g, ' ')
} else {
delete newService.entrypoint
}
if (serviceTemplate.environment && Object.keys(serviceTemplate.environment).length > 0) {
for (const env of Object.keys(serviceTemplate.environment)) {
2022-10-21 13:51:32 +00:00
const foundCaproverVariable = caproverVariables.find((item) => item.id === serviceTemplate.environment[env])
let value = null;
let defaultValue = foundCaproverVariable?.defaultValue ? foundCaproverVariable?.defaultValue.toString()?.replace('$$cap_gen_random_hex', '$$$generate_hex') : ''
2022-10-20 14:06:33 +00:00
if (serviceTemplate.environment[env].startsWith('srv-captain--$$cap_appname')) {
2022-10-21 13:51:32 +00:00
value = `$$config_${env}`.toLowerCase()
2022-10-26 07:27:43 +00:00
defaultValue = serviceTemplate.environment[env].replaceAll('srv-captain--$$cap_appname', '$$$id').replace('$$cap_', '').replaceAll('captain-overlay-network', `$$$config_${env}`).toLowerCase()
2022-10-21 13:51:32 +00:00
} else {
2022-10-26 07:27:43 +00:00
value = '$$config_' + serviceTemplate.environment[env].replaceAll('srv-captain--$$cap_appname', '$$$id').replace('$$cap_', '').replaceAll('captain-overlay-network', `$$$config_${env}`).toLowerCase()
2022-10-20 14:06:33 +00:00
}
newService.environment.push(`${env}=${value}`)
const foundVariable = varSet.has(env)
if (!foundVariable) {
newTemplate.variables.push({
"id": value,
"name": env,
"label": foundCaproverVariable?.label || '',
"defaultValue": defaultValue,
"description": foundCaproverVariable?.description || '',
})
}
varSet.add(env)
}
}
2022-10-21 13:51:32 +00:00
2022-10-20 14:06:33 +00:00
if (serviceTemplate.volumes && serviceTemplate.volumes.length > 0) {
for (const volume of serviceTemplate.volumes) {
const [source, target] = volume.split(':')
2022-10-21 13:51:32 +00:00
if (source === '/var/run/docker.sock' || source === '/tmp') {
continue;
}
2022-10-20 14:06:33 +00:00
newService.volumes.push(`${source.replaceAll('$$cap_appname-', '$$$id-')}:${target}`)
}
}
2022-10-21 13:51:32 +00:00
2022-10-20 14:06:33 +00:00
newTemplate.services[newServiceName] = newService
2022-10-21 13:51:32 +00:00
const services = { ...newTemplate.services }
newTemplate.services = {}
for (const key of Object.keys(services).sort()) {
newTemplate.services[key] = services[key]
}
2022-10-20 14:06:33 +00:00
}
2022-10-21 13:51:32 +00:00
await fs.writeFile('./caprover_new.yml', yaml.dump([{ ...newTemplate }]))
await fs.writeFile('./caprover_new.json', JSON.stringify([{ ...newTemplate }], null, 2))