Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urls: wait for all expected services #479

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/cli/src/commands/up.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Args, Flags } from '@oclif/core'
import { buildFlags, parseBuildFlags, parseTunnelServerFlags, tableFlags, text, tunnelServerFlags } from '@preevy/cli-common'
import { editUrl, tunnelNameResolver } from '@preevy/common'
import {
ComposeModel,
Logger,
Expand All @@ -11,13 +13,11 @@ import {
telemetryEmitter,
withSpinner,
} from '@preevy/core'
import { buildFlags, parseBuildFlags, parseTunnelServerFlags, tableFlags, text, tunnelServerFlags } from '@preevy/cli-common'
import { inspect } from 'util'
import { editUrl, tunnelNameResolver } from '@preevy/common'
import MachineCreationDriverCommand from '../machine-creation-driver-command.js'
import { envIdFlags, urlFlags } from '../common-flags.js'
import { filterUrls, printUrls, writeUrlsToFile } from './urls.js'
import MachineCreationDriverCommand from '../machine-creation-driver-command.js'
import { connectToTunnelServerSsh } from '../tunnel-server-client.js'
import { filterUrls, printUrls, writeUrlsToFile } from './urls.js'

const fetchTunnelServerDetails = async ({
log,
Expand Down Expand Up @@ -175,7 +175,7 @@ export default class Up extends MachineCreationDriverCommand<typeof Up> {

const buildSpec = parseBuildFlags(flags)

await commands.up({
const { composeModel } = await commands.up({
connection,
machineStatusCommand,
dockerPlatform,
Expand All @@ -201,6 +201,7 @@ export default class Up extends MachineCreationDriverCommand<typeof Up> {

this.log(`Preview environment ${text.code(envId)} provisioned at: ${text.code(machine.locationDescription)}`)

const expectedServiceNames = Object.keys(composeModel.services ?? {})
const composeTunnelServiceUrl = findComposeTunnelAgentUrl(expectedServiceUrls)
const flatTunnels = await withSpinner(() => commands.urls({
composeTunnelServiceUrl,
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/commands/urls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import retry from 'p-retry'
import { COMPOSE_TUNNEL_AGENT_SERVICE_NAME } from '@preevy/common'
import { generateBasicAuthCredentials, jwtGenerator } from '../credentials/index.js'
import retry from 'p-retry'
import { queryTunnels } from '../compose-tunnel-agent-client.js'
import { generateBasicAuthCredentials, jwtGenerator } from '../credentials/index.js'
import { FlatTunnel, flattenTunnels } from '../tunneling/index.js'

const tunnelFilter = ({ serviceAndPort, showPreevyService }: {
Expand All @@ -26,6 +26,7 @@ export const urls = async ({
showPreevyService,
composeTunnelServiceUrl,
fetchTimeout,
expectedServiceNames,
}: {
serviceAndPort?: { service: string; port?: number }
tunnelingKey: string | Buffer
Expand All @@ -34,6 +35,7 @@ export const urls = async ({
showPreevyService: boolean
composeTunnelServiceUrl: string
fetchTimeout: number
expectedServiceNames?: string[]
}) => {
const credentials = await generateBasicAuthCredentials(jwtGenerator(tunnelingKey))

Expand All @@ -43,6 +45,7 @@ export const urls = async ({
credentials,
includeAccessCredentials,
fetchTimeout,
expectedServiceNames,
})

return flattenTunnels(tunnels).filter(tunnelFilter({ serviceAndPort, showPreevyService }))
Expand Down
36 changes: 26 additions & 10 deletions packages/core/src/compose-tunnel-agent-client.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import path from 'path'
import { COMPOSE_TUNNEL_AGENT_PORT, COMPOSE_TUNNEL_AGENT_SERVICE_LABELS, COMPOSE_TUNNEL_AGENT_SERVICE_NAME, MachineStatusCommand, ScriptInjection, dateReplacer } from '@preevy/common'
import { mapValues, merge } from 'lodash-es'
import { createRequire } from 'module'
import retry from 'p-retry'
import path from 'path'
import util from 'util'
import { createRequire } from 'module'
import { mapValues, merge } from 'lodash-es'
import { COMPOSE_TUNNEL_AGENT_PORT, COMPOSE_TUNNEL_AGENT_SERVICE_LABELS, COMPOSE_TUNNEL_AGENT_SERVICE_NAME, MachineStatusCommand, ScriptInjection, dateReplacer } from '@preevy/common'
import { ComposeModel, ComposeService, composeModelFilename } from './compose/model.js'
import { TunnelOpts } from './ssh/url.js'
import { Tunnel } from './tunneling/index.js'
import { addScriptInjectionsToServices } from './compose/script-injection.js'
import { withBasicAuthCredentials } from './credentials/index.js'
import { EnvId } from './env-id.js'
import { EnvMetadata, driverMetadataFilename } from './env-metadata.js'
import { REMOTE_DIR_BASE } from './remote-files.js'
import { EnvId } from './env-id.js'
import { addScriptInjectionsToServices } from './compose/script-injection.js'
import { TunnelOpts } from './ssh/url.js'
import { Tunnel } from './tunneling/index.js'

const require = createRequire(import.meta.url)
const COMPOSE_TUNNEL_AGENT_DIR = path.join(path.dirname(require.resolve('@preevy/compose-tunnel-agent')), '..')
Expand Down Expand Up @@ -156,6 +156,16 @@ export const findComposeTunnelAgentUrl = (
return serviceUrl
}

const ensureExpectedServices = (
{ tunnels }: { tunnels: Tunnel[] },
expectedServiceNames: string[]
) => {
const actualServiceNames = new Set(tunnels.map(tunnel => tunnel.service))
const missingServiceNames = expectedServiceNames.filter(name => !actualServiceNames.has(name))
if (missingServiceNames.length) {
throw new Error(`Expected service names ${missingServiceNames.join(', ')} not found in tunnels: ${util.inspect(tunnels)}`)
}
}
export type ComposeTunnelAgentFetchOpts = {
composeTunnelServiceUrl: string
credentials: { user: string; password: string }
Expand Down Expand Up @@ -184,14 +194,20 @@ const fetchFromComposeTunnelAgent = async ({

export const queryTunnels = async ({
includeAccessCredentials,
expectedServiceNames,
...fetchOpts
}: ComposeTunnelAgentFetchOpts & {
includeAccessCredentials: false | 'browser' | 'api'
expectedServiceNames?: string[]
}) => {
const r = await fetchFromComposeTunnelAgent({ ...fetchOpts, pathAndQuery: 'tunnels' })
const { tunnels } = await (r.json() as Promise<{ tunnels: Tunnel[] }>)
const tunnelsObj = await (r.json() as Promise<{ tunnels: Tunnel[] }>)

if (expectedServiceNames) {
ensureExpectedServices(tunnelsObj, expectedServiceNames)
}

return tunnels
return tunnelsObj.tunnels
.map(tunnel => ({
...tunnel,
ports: mapValues(
Expand Down
Loading