2022-10-20 14:06:33 +00:00
|
|
|
import fs from 'fs/promises';
|
|
|
|
import yaml from 'js-yaml';
|
|
|
|
const templateYml = await fs.readFile('./caprover.yml', 'utf8')
|
|
|
|
const template = yaml.load(templateYml)
|
|
|
|
|
|
|
|
const newTemplate = {
|
|
|
|
"templateVersion": "1.0.0",
|
2022-10-21 13:51:32 +00:00
|
|
|
"defaultVersion": "latest",
|
2022-10-20 14:06:33 +00:00
|
|
|
"name": "",
|
|
|
|
"description": "",
|
|
|
|
"services": {
|
|
|
|
|
|
|
|
},
|
|
|
|
"variables": []
|
|
|
|
}
|
|
|
|
const version = template.caproverOneClickApp.variables.find(v => v.id === '$$cap_APP_VERSION').defaultValue || 'latest'
|
|
|
|
|
2022-10-21 13:51:32 +00:00
|
|
|
newTemplate.name = template.caproverOneClickApp.displayName
|
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) {
|
|
|
|
newService.image = serviceTemplate.image.replaceAll('cap_APP_VERSION', 'core_version')
|
|
|
|
} else if (FROM) {
|
|
|
|
newService.image = FROM.split(' ')[1].replaceAll('cap_APP_VERSION', 'core_version')
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
defaultValue = serviceTemplate.environment[env].replaceAll('srv-captain--$$cap_appname', '$$$id').replace('$$cap', '').replaceAll('captain-overlay-network', `$$$config_${env}`).toLowerCase()
|
|
|
|
} else {
|
|
|
|
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))
|