From 4cb6acd813978bf34679ec8b1cfeb3255a6a8205 Mon Sep 17 00:00:00 2001 From: Yshay Yaacobi Date: Mon, 2 Oct 2023 17:28:51 +0300 Subject: [PATCH 1/4] support global scripts --- packages/cli/src/commands/proxy/connect.ts | 9 +++++++-- packages/compose-tunnel-agent/src/docker/services.ts | 5 ++++- packages/compose-tunnel-agent/src/machine-status.ts | 2 +- packages/core/src/commands/proxy.ts | 11 +++++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/commands/proxy/connect.ts b/packages/cli/src/commands/proxy/connect.ts index bf2ca5b9..55514237 100644 --- a/packages/cli/src/commands/proxy/connect.ts +++ b/packages/cli/src/commands/proxy/connect.ts @@ -4,6 +4,7 @@ import { tunnelServerFlags, urlFlags } from '@preevy/cli-common' import { inspect } from 'util' import { formatPublicKey } from '@preevy/common' import { spawn } from 'child_process' +import { COMPOSE_SERVICE_LABEL } from '@preevy/compose-tunnel-agent/src/docker/labels' import { connectToTunnelServerSsh } from '../../tunnel-server-client' import ProfileCommand from '../../profile-command' import { filterUrls, printUrls } from '../urls' @@ -24,6 +25,7 @@ export default class Connect extends ProfileCommand { 'disable-widget': Flags.boolean({ default: true, hidden: true, + allowNo: true, }), 'livecycle-widget-url': Flags.string({ required: true, @@ -91,7 +93,9 @@ export default class Connect extends ProfileCommand { const inspector = commands.proxy.inspectRunningComposeApp(composeProject) const networks = await inspector.getComposeNetworks() + const serviceNames = (await inspector.listAllContainers()).map(x => x.labels[COMPOSE_SERVICE_LABEL]) const projectDirectory = await inspector.getWorkingDirectory() + const thumbprint = await jwkThumbprint(tunnelingKey) const model = await commands.proxy.initProxyComposeModel({ version: this.config.version, envId, @@ -99,9 +103,10 @@ export default class Connect extends ProfileCommand { projectName: composeProject, tunnelOpts, networks, + serviceNames, privateMode: flags['private-env'], - injectLivecycleScript: flags['disable-widget'] ? undefined : flags['livecycle-widget-url'], - tunnelingKeyThumbprint: await jwkThumbprint(tunnelingKey), + injectLivecycleScript: flags['disable-widget'] ? undefined : `${flags['livecycle-widget-url']}?profile=${thumbprint}&env=${envId}`, + tunnelingKeyThumbprint: thumbprint, projectDirectory, }) diff --git a/packages/compose-tunnel-agent/src/docker/services.ts b/packages/compose-tunnel-agent/src/docker/services.ts index a9f6fe66..ea50cafb 100644 --- a/packages/compose-tunnel-agent/src/docker/services.ts +++ b/packages/compose-tunnel-agent/src/docker/services.ts @@ -12,6 +12,9 @@ export type RunningService = { inject: ScriptInjection[] } +const GLOBAL_INJECT_SCRIPTS = process.env.GLOBAL_INJECT_SCRIPTS + ? JSON.parse(process.env.GLOBAL_INJECT_SCRIPTS) as ScriptInjection[] : [] + export const containerToService = ({ container, defaultAccess, @@ -22,5 +25,5 @@ export const containerToService = ({ networks: Object.keys(container.NetworkSettings.Networks), // ports may have both IPv6 and IPv4 addresses, ignoring ports: [...new Set(container.Ports.filter(p => p.Type === 'tcp').filter(portFilter(container)).map(p => p.PrivatePort))], - inject: scriptInjectionFromLabels(container.Labels), + inject: [...scriptInjectionFromLabels(container.Labels), ...GLOBAL_INJECT_SCRIPTS], }) diff --git a/packages/compose-tunnel-agent/src/machine-status.ts b/packages/compose-tunnel-agent/src/machine-status.ts index 22295f96..44a4d9f2 100644 --- a/packages/compose-tunnel-agent/src/machine-status.ts +++ b/packages/compose-tunnel-agent/src/machine-status.ts @@ -79,4 +79,4 @@ export const runMachineStatusCommand = ( } return { contentType, data: oo.stdout() } } -} +} \ No newline at end of file diff --git a/packages/core/src/commands/proxy.ts b/packages/core/src/commands/proxy.ts index 528636c0..955080fe 100644 --- a/packages/core/src/commands/proxy.ts +++ b/packages/core/src/commands/proxy.ts @@ -2,10 +2,10 @@ import { COMPOSE_TUNNEL_AGENT_PORT, COMPOSE_TUNNEL_AGENT_SERVICE_NAME, tunnelNam import { mkdtemp, writeFile } from 'node:fs/promises' import path from 'node:path' import { tmpdir } from 'node:os' +import { set } from 'lodash' import { Connection } from '../tunneling' import { execPromiseStdout } from '../child-process' import { addComposeTunnelAgentService } from '../compose-tunnel-agent-client' -import { widgetScriptInjector } from '../compose/script-injection' import { ComposeModel } from '../compose' import { TunnelOpts } from '../ssh' import { EnvId } from '../env-id' @@ -63,6 +63,7 @@ export function inspectRunningComposeApp(projectName: string) { getPreevyAgentContainer, getEnvId, getWorkingDirectory, + listAllContainers, } } @@ -73,6 +74,7 @@ export async function initProxyComposeModel(opts: { tunnelingKeyThumbprint: string debug?: boolean privateMode?: boolean + serviceNames: string[] networks: ComposeModel['networks'] version: string injectLivecycleScript?: string @@ -93,7 +95,7 @@ export async function initProxyComposeModel(opts: { git: opts.projectDirectory ? await detectGitMetadata(opts.projectDirectory) : undefined, } - let newComposeModel = addComposeTunnelAgentService({ + const newComposeModel = addComposeTunnelAgentService({ tunnelOpts: opts.tunnelOpts, envId: opts.envId, debug: !!opts.debug, @@ -108,8 +110,9 @@ export async function initProxyComposeModel(opts: { }, compose) if (opts.injectLivecycleScript) { - const { inject } = widgetScriptInjector(opts.injectLivecycleScript) - newComposeModel = inject(newComposeModel) + set(newComposeModel, ['services', COMPOSE_TUNNEL_AGENT_SERVICE_NAME, 'environment', 'GLOBAL_INJECT_SCRIPTS'], JSON.stringify([{ + src: opts.injectLivecycleScript, + }])) } return { From 8da2637fb4414a82ac97ce7b106c616f6ce23340 Mon Sep 17 00:00:00 2001 From: Yshay Yaacobi Date: Mon, 2 Oct 2023 18:14:48 +0300 Subject: [PATCH 2/4] lint --- packages/compose-tunnel-agent/src/machine-status.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compose-tunnel-agent/src/machine-status.ts b/packages/compose-tunnel-agent/src/machine-status.ts index 44a4d9f2..22295f96 100644 --- a/packages/compose-tunnel-agent/src/machine-status.ts +++ b/packages/compose-tunnel-agent/src/machine-status.ts @@ -79,4 +79,4 @@ export const runMachineStatusCommand = ( } return { contentType, data: oo.stdout() } } -} \ No newline at end of file +} From 0ed4b5767327c73e73cb1a1f185e0a17a3cb0e7e Mon Sep 17 00:00:00 2001 From: Yshay Yaacobi Date: Mon, 2 Oct 2023 18:32:04 +0300 Subject: [PATCH 3/4] fix --- packages/cli/src/commands/proxy/connect.ts | 3 --- packages/core/src/commands/proxy.ts | 1 - 2 files changed, 4 deletions(-) diff --git a/packages/cli/src/commands/proxy/connect.ts b/packages/cli/src/commands/proxy/connect.ts index 55514237..26415fdb 100644 --- a/packages/cli/src/commands/proxy/connect.ts +++ b/packages/cli/src/commands/proxy/connect.ts @@ -4,7 +4,6 @@ import { tunnelServerFlags, urlFlags } from '@preevy/cli-common' import { inspect } from 'util' import { formatPublicKey } from '@preevy/common' import { spawn } from 'child_process' -import { COMPOSE_SERVICE_LABEL } from '@preevy/compose-tunnel-agent/src/docker/labels' import { connectToTunnelServerSsh } from '../../tunnel-server-client' import ProfileCommand from '../../profile-command' import { filterUrls, printUrls } from '../urls' @@ -93,7 +92,6 @@ export default class Connect extends ProfileCommand { const inspector = commands.proxy.inspectRunningComposeApp(composeProject) const networks = await inspector.getComposeNetworks() - const serviceNames = (await inspector.listAllContainers()).map(x => x.labels[COMPOSE_SERVICE_LABEL]) const projectDirectory = await inspector.getWorkingDirectory() const thumbprint = await jwkThumbprint(tunnelingKey) const model = await commands.proxy.initProxyComposeModel({ @@ -103,7 +101,6 @@ export default class Connect extends ProfileCommand { projectName: composeProject, tunnelOpts, networks, - serviceNames, privateMode: flags['private-env'], injectLivecycleScript: flags['disable-widget'] ? undefined : `${flags['livecycle-widget-url']}?profile=${thumbprint}&env=${envId}`, tunnelingKeyThumbprint: thumbprint, diff --git a/packages/core/src/commands/proxy.ts b/packages/core/src/commands/proxy.ts index 955080fe..bca659a6 100644 --- a/packages/core/src/commands/proxy.ts +++ b/packages/core/src/commands/proxy.ts @@ -74,7 +74,6 @@ export async function initProxyComposeModel(opts: { tunnelingKeyThumbprint: string debug?: boolean privateMode?: boolean - serviceNames: string[] networks: ComposeModel['networks'] version: string injectLivecycleScript?: string From bb5cf5d7f213a55d9edbd0908732690aea844e29 Mon Sep 17 00:00:00 2001 From: Yshay Yaacobi Date: Mon, 2 Oct 2023 18:58:58 +0300 Subject: [PATCH 4/4] cosmetic --- packages/cli/src/commands/proxy/connect.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/commands/proxy/connect.ts b/packages/cli/src/commands/proxy/connect.ts index 26415fdb..2d3779b5 100644 --- a/packages/cli/src/commands/proxy/connect.ts +++ b/packages/cli/src/commands/proxy/connect.ts @@ -21,10 +21,9 @@ export default class Connect extends ProfileCommand { description: 'specify the environment ID for this app', required: false, }), - 'disable-widget': Flags.boolean({ - default: true, + 'enable-widget': Flags.boolean({ + default: false, hidden: true, - allowNo: true, }), 'livecycle-widget-url': Flags.string({ required: true, @@ -102,7 +101,7 @@ export default class Connect extends ProfileCommand { tunnelOpts, networks, privateMode: flags['private-env'], - injectLivecycleScript: flags['disable-widget'] ? undefined : `${flags['livecycle-widget-url']}?profile=${thumbprint}&env=${envId}`, + injectLivecycleScript: flags['enable-widget'] ? `${flags['livecycle-widget-url']}?profile=${thumbprint}&env=${envId}` : undefined, tunnelingKeyThumbprint: thumbprint, projectDirectory, })