Skip to content

Commit

Permalink
support global scripts in CTA (#239)
Browse files Browse the repository at this point in the history
* support global scripts
* cosmetic
  • Loading branch information
Yshayy authored Oct 2, 2023
1 parent 1db2036 commit 28caec4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
9 changes: 5 additions & 4 deletions packages/cli/src/commands/proxy/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default class Connect extends ProfileCommand<typeof Connect> {
description: 'specify the environment ID for this app',
required: false,
}),
'disable-widget': Flags.boolean({
default: true,
'enable-widget': Flags.boolean({
default: false,
hidden: true,
}),
'livecycle-widget-url': Flags.string({
Expand Down Expand Up @@ -92,6 +92,7 @@ export default class Connect extends ProfileCommand<typeof Connect> {
const inspector = commands.proxy.inspectRunningComposeApp(composeProject)
const networks = await inspector.getComposeNetworks()
const projectDirectory = await inspector.getWorkingDirectory()
const thumbprint = await jwkThumbprint(tunnelingKey)
const model = await commands.proxy.initProxyComposeModel({
version: this.config.version,
envId,
Expand All @@ -100,8 +101,8 @@ export default class Connect extends ProfileCommand<typeof Connect> {
tunnelOpts,
networks,
privateMode: flags['private-env'],
injectLivecycleScript: flags['disable-widget'] ? undefined : flags['livecycle-widget-url'],
tunnelingKeyThumbprint: await jwkThumbprint(tunnelingKey),
injectLivecycleScript: flags['enable-widget'] ? `${flags['livecycle-widget-url']}?profile=${thumbprint}&env=${envId}` : undefined,
tunnelingKeyThumbprint: thumbprint,
projectDirectory,
})

Expand Down
5 changes: 4 additions & 1 deletion packages/compose-tunnel-agent/src/docker/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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],
})
10 changes: 6 additions & 4 deletions packages/core/src/commands/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -63,6 +63,7 @@ export function inspectRunningComposeApp(projectName: string) {
getPreevyAgentContainer,
getEnvId,
getWorkingDirectory,
listAllContainers,
}
}

Expand Down Expand Up @@ -93,7 +94,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,
Expand All @@ -108,8 +109,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 {
Expand Down

0 comments on commit 28caec4

Please sign in to comment.