lots of changes

This commit is contained in:
Andras Bacsai
2022-10-18 11:32:38 +02:00
parent 9afb713df1
commit a6f457749b
13 changed files with 256 additions and 113 deletions

View File

@@ -8,7 +8,7 @@ import { defaultServiceConfigurations } from '../services';
import { OnlyId } from '../../types';
import templates from '../templates'
import { parseAndFindServiceTemplates } from '../../routes/api/v1/services/handlers';
import path from 'path';
// export async function startService(request: FastifyRequest<ServiceStartStop>) {
// try {
// const { type } = request.params
@@ -692,7 +692,7 @@ export async function startService(request: FastifyRequest<ServiceStartStop>) {
const teamId = request.user.teamId;
const service = await getServiceFromDB({ id, teamId });
const { type, version, destinationDockerId, destinationDocker, serviceSecret,serviceSetting, exposePort, persistentStorage } =
const { type, version, destinationDockerId, destinationDocker, serviceSecret, serviceSetting, exposePort, persistentStorage } =
service;
const { workdir } = await createDirectories({ repository: type, buildId: id });
@@ -701,9 +701,10 @@ export async function startService(request: FastifyRequest<ServiceStartStop>) {
const config = {};
for (const service in template.services) {
console.log(template.services[service])
config[service] = {
container_name: service,
build: template.services[service].build || undefined,
command: template.services[service].command,
image: template.services[service].image,
expose: template.services[service].ports,
// ...(exposePort ? { ports: [`${exposePort}:${port}`] } : {}),
@@ -714,16 +715,23 @@ export async function startService(request: FastifyRequest<ServiceStartStop>) {
labels: makeLabelForServices(type),
...defaultComposeConfiguration(network),
}
// Generate files for builds
if (template.services[service].build) {
if (template.services[service]?.extras?.files?.length > 0) {
console.log(template.services[service]?.extras?.files)
let Dockerfile = `
FROM ${template.services[service].image}`
for (const file of template.services[service].extras.files) {
const { source, destination, content } = file;
await fs.writeFile(source, content);
Dockerfile += `
COPY ./${path.basename(source)} ${destination}`
}
await fs.writeFile(`${workdir}/Dockerfile.${service}`, Dockerfile);
}
}
}
const { volumeMounts } = persistentVolumes(id, persistentStorage, config)
const composeFile: ComposeFile = {
version: '3.8',
services: config,

View File

@@ -43,7 +43,7 @@ export default [
"$$id": {
"name": "Plausible Analytics",
"documentation": "Taken from https://plausible.io/",
"command": ['sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh db init-admin && /entrypoint.sh run"'],
"command": 'sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh db init-admin && /entrypoint.sh run"',
"depends_on": [
"$$id-postgresql",
"$$id-clickhouse"
@@ -52,7 +52,7 @@ export default [
"environment": [
"ADMIN_USER_EMAIL=$$config_admin_user_email",
"ADMIN_USER_NAME=$$config_admin_user_name",
"ADMIN_USER_PASSWORD=$$secret_admin_user_password",
"ADMIN_USER_PWD=$$secret_admin_user_pwd",
"BASE_URL=$$config_base_url",
"SECRET_KEY_BASE=$$secret_secret_key_base",
"DISABLE_AUTH=$$config_disable_auth",
@@ -68,6 +68,9 @@ export default [
"name": "PostgreSQL",
"documentation": "Taken from https://plausible.io/",
"image": "bitnami/postgresql:13.2.0",
"volumes": [
'$$id-postgresql-data:/bitnami/postgresql/',
],
"environment": [
"POSTGRESQL_PASSWORD=$$secret_postgresql_password",
"POSTGRESQL_USERNAME=$$config_postgresql_username",
@@ -78,7 +81,13 @@ export default [
"$$id-clickhouse": {
"name": "Clickhouse",
"documentation": "Taken from https://plausible.io/",
"build": "$$workdir",
"build": {
context: "$$workdir",
dockerfile: "Dockerfile.$$id-clickhouse"
},
"volumes": [
'$$id-clickhouse-data:/var/lib/clickhouse',
],
"image": "yandex/clickhouse-server:21.3.2.5",
"ulimits": {
"nofile": {
@@ -87,21 +96,25 @@ export default [
}
},
"extras": {
"files:": [
"files": [
{
location: '$$workdir/clickhouse-config.xml',
source: "$$workdir/clickhouse-config.xml",
destination: '/etc/clickhouse-server/users.d/logging.xml',
content: '<yandex><logger><level>warning</level><console>true</console></logger><query_thread_log remove="remove"/><query_log remove="remove"/><text_log remove="remove"/><trace_log remove="remove"/><metric_log remove="remove"/><asynchronous_metric_log remove="remove"/><session_log remove="remove"/><part_log remove="remove"/></yandex>'
},
{
location: '$$workdir/clickhouse-user-config.xml',
source: "$$workdir/clickhouse-user-config.xml",
destination: '/etc/clickhouse-server/config.d/logging.xml',
content: '<yandex><profiles><default><log_queries>0</log_queries><log_query_threads>0</log_query_threads></default></profiles></yandex>'
},
{
location: '$$workdir/init.query',
source: "$$workdir/init.query",
destination: '/docker-entrypoint-initdb.d/init.query',
content: 'CREATE DATABASE IF NOT EXISTS plausible;'
},
{
location: '$$workdir/init-db.sh',
source: "$$workdir/init-db.sh",
destination: '/docker-entrypoint-initdb.d/init-db.sh',
content: 'clickhouse client --queries-file /docker-entrypoint-initdb.d/init.query'
}
]
@@ -146,12 +159,14 @@ export default [
"description": "This is the admin username. Please change it.",
},
{
"id": "$$secret_admin_user_password",
"name": "ADMIN_USER_PASSWORD",
"showAsConfiguration": true,
"id": "$$secret_admin_user_pwd",
"name": "ADMIN_USER_PWD",
"label": "Admin User Password",
"defaultValue": "$$generate_password",
"description": "This is the admin password. Please change it.",
"extras": {
"isVisibleOnUI": true
}
},
{
"id": "$$secret_secret_key_base",
@@ -159,8 +174,8 @@ export default [
"label": "Secret Key Base",
"defaultValue": "$$generate_passphrase",
"description": "",
"details": {
"length":64
"extras": {
"length": 64
}
},
{

View File

@@ -132,8 +132,8 @@ export async function parseAndFindServiceTemplates(service: any, workdir?: strin
const label = foundTemplate.variables.find(v => v.name === envKey)?.label
const description = foundTemplate.variables.find(v => v.name === envKey)?.description
const defaultValue = foundTemplate.variables.find(v => v.name === envKey)?.defaultValue
const showAsConfiguration = foundTemplate.variables.find(v => v.name === envKey)?.showAsConfiguration
if (envValue.startsWith('$$config') || showAsConfiguration) {
const isVisibleOnUI = foundTemplate.variables.find(v => v.name === envKey)?.extras?.isVisibleOnUI
if (envValue.startsWith('$$config') || isVisibleOnUI) {
parsedTemplate[realKey].environment.push(
{ name: envKey, value: envValue, label, description, defaultValue }
)
@@ -220,12 +220,11 @@ export async function saveServiceType(request: FastifyRequest<SaveServiceType>,
foundTemplate.variables = foundTemplate.variables.map(variable => {
let { id: variableId } = variable;
if (variableId.startsWith('$$secret_')) {
const length = variable?.extras && variable.extras['length']
if (variable.defaultValue === '$$generate_password') {
const length = variable?.details['length'] || null
variable.value = generatePassword({length});
variable.value = generatePassword({ length });
} else if (variable.defaultValue === '$$generate_passphrase') {
const length = variable?.details['length'] || null
variable.value = generatePassword({length});
variable.value = generatePassword({ length });
}
}
if (variableId.startsWith('$$config_')) {
@@ -267,6 +266,18 @@ export async function saveServiceType(request: FastifyRequest<SaveServiceType>,
}
}
}
for (const service of Object.keys(foundTemplate.services)) {
if (foundTemplate.services[service].volumes) {
for (const volume of foundTemplate.services[service].volumes) {
const [volumeName, path] = volume.split(':')
if (!volumeName.startsWith('/')) {
await prisma.servicePersistentStorage.create({
data: { volumeName, path, containerId: service, predefined: true, service: { connect: { id } } }
});
}
}
}
}
await prisma.service.update({ where: { id }, data: { type, version: foundTemplate.serviceDefaultVersion } })
return reply.code(201).send()
} else {

View File

@@ -329,10 +329,9 @@ export async function traefikConfiguration(request, reply) {
fqdn,
id,
type,
destinationDocker,
destinationDockerId,
dualCerts,
plausibleAnalytics
serviceSetting
} = service;
if (destinationDockerId) {
const found = supportedServiceTypesAndVersions.find((a) => a.name === type);
@@ -348,8 +347,11 @@ export async function traefikConfiguration(request, reply) {
if (isRunning) {
// Plausible Analytics custom script
let scriptName = false;
if (type === 'plausibleanalytics' && plausibleAnalytics.scriptName !== 'plausible.js') {
scriptName = plausibleAnalytics.scriptName;
if (type === 'plausibleanalytics') {
const foundScriptName = serviceSetting.find((a) => a.name === 'SCRIPT_NAME')?.value;
if (foundScriptName) {
scriptName = foundScriptName;
}
}
let container = id;