From cbd474982987a0ae27ee876a844a3bd2312966e3 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Wed, 18 Sep 2024 13:22:33 +0700 Subject: [PATCH] Finally docker works --- src/executor/docker.js | 21 ++++++++++++++++----- src/executor/runner.js | 22 ++++++++++------------ src/executor/runnersub.js | 3 ++- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/executor/docker.js b/src/executor/docker.js index 7b536f9..4424885 100644 --- a/src/executor/docker.js +++ b/src/executor/docker.js @@ -106,6 +106,9 @@ class DockerExecutor { } else { throw new Error("Unknown ports format: " + name); } + } else if (typeof port === 'object' && port.target && port.published) { + conf.published = port.published; + conf.target = port.target; } exposedPorts.push(conf.published); service.ports[i] = conf; @@ -121,26 +124,30 @@ class DockerExecutor { nginxChanged = true; } let proxyPass = matchedConf.proxy_pass + ""; - if (!proxyPass || !proxyPass.startsWith('docker:') || exposedPorts.includes(proxyPass.replace(/^docker:/, ''))) { + if (!proxyPass || !proxyPass.startsWith('docker:') || !exposedPorts.includes(proxyPass.replace(/^docker:/, ''))) { if (exposedPorts.length == 0) { throw new Error("There are no exposed ports! Need atleast one to forward it into NGINX"); } matchedConf.proxy_pass = "docker:" + exposedPorts[exposedPorts.length - 1]; nginxChanged = true; } + let nginxStatus = ''; if (nginxChanged) { - await nginxExec.setDirect(domain, nginx); + nginxStatus = await nginxExec.setDirect(domain, nginx); + } else { + nginxStatus = "Done unchanged"; } - return services; + return [services, nginxStatus]; } /** * * @param {any} services * @param {string} home * @param {string} domain + * @param {(arg0: string) => Promise} logWriter * @return {Promise} */ - async executeServices(services, home, domain) { + async executeServices(services, home, domain, logWriter) { let filename = path.join(home, 'docker-compose.yml'); let composeObject = {}; if (typeof services === 'string') { @@ -156,7 +163,11 @@ class DockerExecutor { } else { composeObject.services = services; } - composeObject.services = await this.rewriteServices(composeObject.services, domain); + let nginxStatus = ''; + [composeObject.services, nginxStatus] = await this.rewriteServices(composeObject.services, domain); + if (logWriter) { + await logWriter(nginxStatus) + } let composeFile = yaml.stringify(composeObject); await executeLock('compose', () => { return new Promise((resolve, reject) => { diff --git a/src/executor/runner.js b/src/executor/runner.js index 03ccb1f..b8f959b 100644 --- a/src/executor/runner.js +++ b/src/executor/runner.js @@ -154,21 +154,21 @@ export default async function runConfig(config, domain, writer, sandbox = false) let match = chunk.match(/\[.+?\@.+? .+?\]\$/); if (match) { cb = null; - chunk = chunk.replace(/\x1b.+?$/, '').trimEnd(); - if (write && chunk) { - writer(chunk + "\n"); + if (write) { + writer("\n"); } resolve(); return true; - } else if (first && chunk.startsWith("<")) { - // stdin line skips - if (chunk.includes("\n")) { - first = false; - } - return false; } else { if (write && chunk) { - writer(chunk); + if (first) { + let pos = chunk.indexOf('\n'); + if (pos >= 0) { + writer(chunk.substring(pos + 1)); + } + } else { + writer(chunk); + } } first = false; return false; @@ -312,5 +312,3 @@ export default async function runConfig(config, domain, writer, sandbox = false) } } } - - diff --git a/src/executor/runnersub.js b/src/executor/runnersub.js index 26a0512..3d7e8dd 100644 --- a/src/executor/runnersub.js +++ b/src/executor/runnersub.js @@ -476,8 +476,9 @@ export async function runConfigSubdomain(config, domaindata, subdomain, sshExec, } else { await sshExec(`docker compose --progress-plain quiet --remove-orphans --rmi all || true`); } + await writeLog("$> Configuring NGINX forwarding for docker"); + let d = await dockerExec.executeServices(config.services, subdomaindata['Home directory'] + '/public_html', subdomain, writeLog); await writeLog("$> Writing docker compose services"); - let d = await dockerExec.executeServices(config.services, subdomaindata['Home directory'] + '/public_html', subdomain); await writeLog(d.split('\n').map(x => ` ${x}`).join('\n')); await writeLog("$> Applying compose services"); if (typeof config.services == 'string') {