@xrystal/core 3.55.94 → 3.55.95
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -16,17 +16,12 @@ import {
|
|
|
16
16
|
import {
|
|
17
17
|
ensureServiceLaptopEnvs,
|
|
18
18
|
ensureServiceTelepresenceEnv,
|
|
19
|
+
resolveTelepresenceContext,
|
|
19
20
|
resolveTelepresenceDeployFromCwd,
|
|
20
|
-
resolveTelepresenceServiceDir,
|
|
21
21
|
TELEPRESENCE_DEPLOYMENTS,
|
|
22
22
|
} from '../helpers/telepresence-layout.mjs'
|
|
23
|
-
import { findWorkspaceRoot } from '../helpers/workspace-resolve.mjs'
|
|
24
23
|
|
|
25
|
-
function
|
|
26
|
-
return value && /^\d+$/.test(String(value)) ? String(value) : '3000'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function resolveInterceptTarget(deploy, port, command) {
|
|
24
|
+
function resolveInterceptTarget(deploy, port) {
|
|
30
25
|
let deployName = String(deploy ?? '').trim()
|
|
31
26
|
let portArg = '3000'
|
|
32
27
|
|
|
@@ -50,7 +45,7 @@ function resolveInterceptTarget(deploy, port, command) {
|
|
|
50
45
|
}
|
|
51
46
|
|
|
52
47
|
if (!TELEPRESENCE_DEPLOYMENTS.includes(deployName)) {
|
|
53
|
-
console.error(chalk.red(`\n✗ Unknown
|
|
48
|
+
console.error(chalk.red(`\n✗ Unknown service "${deployName}"\n`))
|
|
54
49
|
console.error(chalk.gray(` Use: ${TELEPRESENCE_DEPLOYMENTS.join(', ')}\n`))
|
|
55
50
|
process.exit(1)
|
|
56
51
|
}
|
|
@@ -58,7 +53,11 @@ function resolveInterceptTarget(deploy, port, command) {
|
|
|
58
53
|
return { deployName, portArg, serviceRootHint }
|
|
59
54
|
}
|
|
60
55
|
|
|
61
|
-
function resolveLocalCmd(command) {
|
|
56
|
+
function resolveLocalCmd(command, options) {
|
|
57
|
+
const execOpt = String(options?.exec ?? '').trim()
|
|
58
|
+
if (execOpt) {
|
|
59
|
+
return execOpt.split(/\s+/).filter(Boolean)
|
|
60
|
+
}
|
|
62
61
|
const raw = command?.args ?? []
|
|
63
62
|
let offset = 0
|
|
64
63
|
const first = raw[0] != null ? String(raw[0]).trim() : ''
|
|
@@ -94,34 +93,33 @@ function runQuit({ cliName, commandLabel }) {
|
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
function resolveLeaveTarget(deploy) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
resolveTelepresenceServiceDir(workspaceRoot ?? process.cwd(), deployName) ??
|
|
107
|
-
(workspaceRoot
|
|
108
|
-
? resolveTelepresenceServiceDir(path.dirname(workspaceRoot), deployName)
|
|
109
|
-
: null)
|
|
110
|
-
if (svcDir) serviceRoot = svcDir
|
|
111
|
-
} else {
|
|
112
|
-
console.error(chalk.red(`\n✗ Unknown deploy "${deployName}"\n`))
|
|
113
|
-
console.error(chalk.gray(` Use: ${TELEPRESENCE_DEPLOYMENTS.join(', ')}\n`))
|
|
96
|
+
try {
|
|
97
|
+
const ctx = resolveTelepresenceContext({
|
|
98
|
+
cwd: process.cwd(),
|
|
99
|
+
deployName: String(deploy ?? '').trim(),
|
|
100
|
+
})
|
|
101
|
+
return { serviceName: ctx.deploy, serviceRoot: ctx.serviceRoot, ctx }
|
|
102
|
+
} catch (err) {
|
|
103
|
+
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
104
|
+
console.error(chalk.gray(` Services: ${TELEPRESENCE_DEPLOYMENTS.join(', ')}\n`))
|
|
114
105
|
process.exit(1)
|
|
115
106
|
}
|
|
116
|
-
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function printTelepresenceBrief(serviceName, { ns, portArg } = {}) {
|
|
110
|
+
console.log(chalk.gray(` service: ${serviceName}`))
|
|
111
|
+
if (ns != null) console.log(chalk.gray(` ns: ${ns}`))
|
|
112
|
+
if (portArg) console.log(chalk.gray(` local port: ${portArg}`))
|
|
117
113
|
}
|
|
118
114
|
|
|
119
115
|
function runLeaveIntercept(deploy, ns, { cliName, commandLabel } = {}) {
|
|
120
116
|
const version = readCliPackageJson().version
|
|
121
|
-
const {
|
|
122
|
-
printCommandHeader({ cliName, command: commandLabel ?? `tp leave ${
|
|
117
|
+
const { serviceName, serviceRoot } = resolveLeaveTarget(deploy)
|
|
118
|
+
printCommandHeader({ cliName, command: commandLabel ?? `tp leave · ${serviceName}`, version })
|
|
119
|
+
printTelepresenceBrief(serviceName, { ns })
|
|
120
|
+
console.log('')
|
|
123
121
|
try {
|
|
124
|
-
leaveTelepresenceIntercept({ deploy:
|
|
122
|
+
leaveTelepresenceIntercept({ deploy: serviceName, ns, cwd: serviceRoot })
|
|
125
123
|
} catch (err) {
|
|
126
124
|
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
127
125
|
process.exit(1)
|
|
@@ -129,6 +127,90 @@ function runLeaveIntercept(deploy, ns, { cliName, commandLabel } = {}) {
|
|
|
129
127
|
printCommandFooter({ version })
|
|
130
128
|
}
|
|
131
129
|
|
|
130
|
+
function executeIntercept(deploy, port, options, command, { cliName }) {
|
|
131
|
+
const version = readCliPackageJson().version
|
|
132
|
+
const ns = String(options?.ns ?? 'dev').trim() || 'dev'
|
|
133
|
+
|
|
134
|
+
const deployToken = String(deploy ?? '').trim().toLowerCase()
|
|
135
|
+
if (deployToken === 'leave') {
|
|
136
|
+
runLeaveIntercept('', ns, { cliName, commandLabel: 'tp leave' })
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
const portToken = String(port ?? '').trim().toLowerCase()
|
|
140
|
+
if (portToken === 'leave' && deployToken && TELEPRESENCE_DEPLOYMENTS.includes(deployToken)) {
|
|
141
|
+
runLeaveIntercept(deployToken, ns, { cliName, commandLabel: `tp leave · ${deployToken}` })
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const { deployName, portArg, serviceRootHint } = resolveInterceptTarget(deploy, port)
|
|
146
|
+
const localCmd = resolveLocalCmd(command, options)
|
|
147
|
+
|
|
148
|
+
let ctx
|
|
149
|
+
try {
|
|
150
|
+
ctx = resolveTelepresenceContext({
|
|
151
|
+
cwd: process.cwd(),
|
|
152
|
+
deployName,
|
|
153
|
+
serviceRootHint,
|
|
154
|
+
})
|
|
155
|
+
} catch (err) {
|
|
156
|
+
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
157
|
+
process.exit(1)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
printCommandHeader({ cliName, command: `tp · ${ctx.deploy}`, version: version })
|
|
161
|
+
|
|
162
|
+
const env = ensureServiceLaptopEnvs(ctx.serviceRoot).telepresence
|
|
163
|
+
process.env.NS = ns
|
|
164
|
+
|
|
165
|
+
console.log('')
|
|
166
|
+
printTelepresenceBrief(ctx.deploy, { ns, portArg })
|
|
167
|
+
console.log('')
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
runTelepresenceIntercept({
|
|
171
|
+
deploy: ctx.deploy,
|
|
172
|
+
ns,
|
|
173
|
+
port: portArg,
|
|
174
|
+
envFile: env.envPath,
|
|
175
|
+
localCmd,
|
|
176
|
+
cwd: ctx.serviceRoot,
|
|
177
|
+
cliName,
|
|
178
|
+
})
|
|
179
|
+
} catch (err) {
|
|
180
|
+
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
181
|
+
process.exit(1)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function registerInterceptCommand(tp, { cliName }) {
|
|
186
|
+
const interceptCmd = tp
|
|
187
|
+
.command('intercept', { isDefault: true })
|
|
188
|
+
.alias('dev')
|
|
189
|
+
.alias('open')
|
|
190
|
+
.description('Intercept cluster traffic → local process (default when you run tp)')
|
|
191
|
+
.argument('[service]', `Service name — ${TELEPRESENCE_DEPLOYMENTS.join(', ')} (optional in service repo)`)
|
|
192
|
+
.argument('[port]', 'Local listen port', '3000')
|
|
193
|
+
.option('--ns <namespace>', 'Kubernetes namespace', 'dev')
|
|
194
|
+
.option('--exec <command>', 'Local command (default: xry dev)')
|
|
195
|
+
.allowUnknownOption()
|
|
196
|
+
.allowExcessArguments()
|
|
197
|
+
.action((deploy, port, options, command) => {
|
|
198
|
+
executeIntercept(deploy, port, options, command, { cliName })
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
interceptCmd
|
|
202
|
+
.command('leave')
|
|
203
|
+
.description('Alias of tp leave')
|
|
204
|
+
.argument('[service]', 'Service (default from x/tmp.yml in this repo)')
|
|
205
|
+
.option('--ns <namespace>', 'Kubernetes namespace', 'dev')
|
|
206
|
+
.action((deploy, options) => {
|
|
207
|
+
const ns = String(options.ns ?? 'dev').trim() || 'dev'
|
|
208
|
+
runLeaveIntercept(deploy, ns, { cliName, commandLabel: 'tp leave' })
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
return interceptCmd
|
|
212
|
+
}
|
|
213
|
+
|
|
132
214
|
export function registerTelepresenceCommand(cli, { cliName = CLI_PRIMARY_NAME } = {}) {
|
|
133
215
|
const tp = cli
|
|
134
216
|
.command('telepresence')
|
|
@@ -138,20 +220,18 @@ export function registerTelepresenceCommand(cli, { cliName = CLI_PRIMARY_NAME }
|
|
|
138
220
|
'after',
|
|
139
221
|
`
|
|
140
222
|
${chalk.gray('Examples:')}
|
|
223
|
+
${cliName} tp
|
|
224
|
+
${cliName} tp iam --ns dev
|
|
225
|
+
${cliName} tp gateway 3000 --exec "bun run dev"
|
|
226
|
+
${cliName} tp leave
|
|
227
|
+
${cliName} tp open-leave gateway --ns dev
|
|
141
228
|
${cliName} tp start --ns dev
|
|
142
|
-
${cliName} tp intercept
|
|
143
|
-
${cliName} tp intercept gateway 3000 --ns dev
|
|
144
|
-
${cliName} tp intercept leave
|
|
145
229
|
${cliName} tp stop
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
${chalk.gray('
|
|
151
|
-
${chalk.gray('intercept')} = omit deploy in service repo → configs.service.name from x/tmp.yml
|
|
152
|
-
${chalk.gray('--ns')} = Kubernetes namespace (default: dev)
|
|
153
|
-
${chalk.gray('<deploy>')} = optional — ${TELEPRESENCE_DEPLOYMENTS.join(', ')}
|
|
154
|
-
${chalk.gray('kubeconfig:')} KUBECONFIG, workspace tmp.yml, xry config kubeconfig
|
|
230
|
+
|
|
231
|
+
${chalk.gray('tp')} (no subcommand) = intercept (same as tp open / tp intercept)
|
|
232
|
+
${chalk.gray('leave / open-leave')} = end intercept; works inside service repo or with service name
|
|
233
|
+
${chalk.gray('--exec')} = override local process (default: xry dev)
|
|
234
|
+
${chalk.gray('--ns')} = Kubernetes namespace (default: dev)
|
|
155
235
|
`,
|
|
156
236
|
)
|
|
157
237
|
|
|
@@ -168,8 +248,9 @@ export function registerTelepresenceCommand(cli, { cliName = CLI_PRIMARY_NAME }
|
|
|
168
248
|
.action(() => runQuit({ cliName, commandLabel: 'tp stop' }))
|
|
169
249
|
|
|
170
250
|
tp.command('leave')
|
|
171
|
-
.description('End intercept only — detach workload + env
|
|
172
|
-
.
|
|
251
|
+
.description('End intercept only — detach workload + env cleanup; stay connected')
|
|
252
|
+
.alias('open-leave')
|
|
253
|
+
.argument('[service]', 'Service (default from x/tmp.yml when cwd is a service repo)')
|
|
173
254
|
.option('--ns <namespace>', 'Kubernetes namespace', 'dev')
|
|
174
255
|
.action((deploy, options) => {
|
|
175
256
|
const ns = String(options.ns ?? 'dev').trim() || 'dev'
|
|
@@ -208,81 +289,5 @@ export function registerTelepresenceCommand(cli, { cliName = CLI_PRIMARY_NAME }
|
|
|
208
289
|
printCommandFooter({ version })
|
|
209
290
|
})
|
|
210
291
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
.alias('dev')
|
|
214
|
-
.description('Intercept deployment → local process (deploy optional in service repo)')
|
|
215
|
-
|
|
216
|
-
interceptCmd
|
|
217
|
-
.command('leave')
|
|
218
|
-
.description('End intercept for this service (default deploy from x/tmp.yml when omitted)')
|
|
219
|
-
.argument('[deploy]', `Deployment (default: configs.service.name in x/tmp.yml)`)
|
|
220
|
-
.option('--ns <namespace>', 'Kubernetes namespace', 'dev')
|
|
221
|
-
.action((deploy, options) => {
|
|
222
|
-
const ns = String(options.ns ?? 'dev').trim() || 'dev'
|
|
223
|
-
runLeaveIntercept(deploy, ns, { cliName, commandLabel: 'tp intercept leave' })
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
interceptCmd
|
|
227
|
-
.argument('[deploy]', `Deployment name (default: configs.service.name in x/tmp.yml) — ${TELEPRESENCE_DEPLOYMENTS.join(', ')}`)
|
|
228
|
-
.argument('[port]', 'Local listen port', '3000')
|
|
229
|
-
.option('--ns <namespace>', 'Kubernetes namespace', 'dev')
|
|
230
|
-
.allowUnknownOption()
|
|
231
|
-
.allowExcessArguments()
|
|
232
|
-
.action((deploy, port, options, command) => {
|
|
233
|
-
const version = readCliPackageJson().version
|
|
234
|
-
const ns = String(options.ns ?? 'dev').trim() || 'dev'
|
|
235
|
-
|
|
236
|
-
const deployToken = String(deploy ?? '').trim().toLowerCase()
|
|
237
|
-
if (deployToken === 'leave') {
|
|
238
|
-
runLeaveIntercept('', ns, { cliName, commandLabel: 'tp intercept leave' })
|
|
239
|
-
return
|
|
240
|
-
}
|
|
241
|
-
const portToken = String(port ?? '').trim().toLowerCase()
|
|
242
|
-
if (portToken === 'leave' && deployToken && TELEPRESENCE_DEPLOYMENTS.includes(deployToken)) {
|
|
243
|
-
runLeaveIntercept(deployToken, ns, { cliName, commandLabel: `tp intercept ${deployToken} leave` })
|
|
244
|
-
return
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const { deployName, portArg, serviceRootHint } = resolveInterceptTarget(deploy, port, command)
|
|
248
|
-
const localCmd = resolveLocalCmd(command)
|
|
249
|
-
|
|
250
|
-
printCommandHeader({ cliName, command: `tp intercept ${deployName}`, version })
|
|
251
|
-
|
|
252
|
-
const workspaceRoot = findWorkspaceRoot(process.cwd())
|
|
253
|
-
let svcDir =
|
|
254
|
-
serviceRootHint ??
|
|
255
|
-
resolveTelepresenceServiceDir(workspaceRoot ?? process.cwd(), deployName)
|
|
256
|
-
if (!svcDir && workspaceRoot) {
|
|
257
|
-
svcDir = resolveTelepresenceServiceDir(path.dirname(workspaceRoot), deployName)
|
|
258
|
-
}
|
|
259
|
-
if (!svcDir) {
|
|
260
|
-
svcDir = process.cwd()
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
const env = ensureServiceLaptopEnvs(svcDir).telepresence
|
|
264
|
-
process.env.NS = ns
|
|
265
|
-
if (workspaceRoot) process.env.XRYSTAL_WORKSPACE = workspaceRoot
|
|
266
|
-
|
|
267
|
-
console.log(chalk.gray(` deploy: ${deployName}`))
|
|
268
|
-
console.log(chalk.gray(` ns: ${ns}`))
|
|
269
|
-
console.log(chalk.gray(` service: ${svcDir}`))
|
|
270
|
-
console.log(chalk.gray(` env: ${env.envRel}${env.created ? chalk.green(' (created)') : ''}\n`))
|
|
271
|
-
console.log(chalk.gray(` local port: ${portArg} (pod container :3000)`))
|
|
272
|
-
|
|
273
|
-
try {
|
|
274
|
-
runTelepresenceIntercept({
|
|
275
|
-
deploy: deployName,
|
|
276
|
-
ns,
|
|
277
|
-
port: portArg,
|
|
278
|
-
envFile: env.envPath,
|
|
279
|
-
localCmd,
|
|
280
|
-
cwd: svcDir,
|
|
281
|
-
cliName,
|
|
282
|
-
})
|
|
283
|
-
} catch (err) {
|
|
284
|
-
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
285
|
-
process.exit(1)
|
|
286
|
-
}
|
|
287
|
-
})
|
|
288
|
-
}
|
|
292
|
+
registerInterceptCommand(tp, { cliName })
|
|
293
|
+
}
|
|
@@ -19,7 +19,7 @@ function quickCommands() {
|
|
|
19
19
|
{ cmd: 'create <name> --workspace', desc: t('quick.createWs') },
|
|
20
20
|
{ cmd: 'workspaces', desc: t('quick.workspaces') },
|
|
21
21
|
{ cmd: 'watch status', desc: t('quick.watch') },
|
|
22
|
-
{ cmd: 'tp
|
|
22
|
+
{ cmd: 'tp / tp <service> --ns dev', desc: t('quick.tp') },
|
|
23
23
|
{ cmd: 'dev / build / doctor', desc: t('quick.dev') },
|
|
24
24
|
]
|
|
25
25
|
}
|
|
@@ -54,10 +54,14 @@ function commandGroups() {
|
|
|
54
54
|
{ cmd: 'tp start --ns dev', aliases: 'tp connect', desc: 'Connect Telepresence to cluster namespace' },
|
|
55
55
|
{ cmd: 'tp stop', aliases: 'tp quit', desc: 'Disconnect Telepresence' },
|
|
56
56
|
{
|
|
57
|
-
cmd: 'tp leave [
|
|
57
|
+
cmd: 'tp leave | open-leave [service] --ns dev',
|
|
58
58
|
desc: 'End intercept only (detach + env cleanup; stay connected)',
|
|
59
59
|
},
|
|
60
|
-
{
|
|
60
|
+
{
|
|
61
|
+
cmd: 'tp [service] --ns dev',
|
|
62
|
+
aliases: 'tp open · tp intercept',
|
|
63
|
+
desc: 'Intercept (default) — omit service in repo; tp leave ends it',
|
|
64
|
+
},
|
|
61
65
|
{ cmd: 'telepresence install', aliases: 'tp install', desc: 'Install Telepresence OSS client (~2.31.x)' },
|
|
62
66
|
],
|
|
63
67
|
},
|
|
@@ -143,7 +147,7 @@ export function printQuickStartGuide({ cliName = CLI_PRIMARY_NAME } = {}) {
|
|
|
143
147
|
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} guide`)} ${chalk.gray(`· ${t('tips.guide')}`)}`)
|
|
144
148
|
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} -h`)} ${chalk.gray(`· ${t('tips.help')}`)}`)
|
|
145
149
|
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} doctor`)} ${chalk.gray(`· ${t('tips.doctor')}`)}`)
|
|
146
|
-
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} tp
|
|
150
|
+
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} tp --ns dev`)} ${chalk.gray(`· ${t('tips.tp')}`)}`)
|
|
147
151
|
console.log(` ${chalk.dim('tip')} ${chalk.gray(t('langTip'))}`)
|
|
148
152
|
console.log('')
|
|
149
153
|
}
|
|
@@ -4,6 +4,7 @@ import path from 'node:path'
|
|
|
4
4
|
import yaml from 'yaml'
|
|
5
5
|
|
|
6
6
|
import { resolveTmpDeploymentName } from './tmp-schema.mjs'
|
|
7
|
+
import { findWorkspaceRoot, workspaceProjectName } from './workspace-resolve.mjs'
|
|
7
8
|
|
|
8
9
|
const TELEPRESENCE_ENV_EXAMPLE = `# Telepresence intercept env — copied to x/environments/.env.telepresence per service
|
|
9
10
|
# Used by: telepresence intercept --env-file .env.telepresence
|
|
@@ -117,7 +118,7 @@ export function readDeployNameFromServiceRoot(serviceRoot) {
|
|
|
117
118
|
if (!name) return null
|
|
118
119
|
if (!TELEPRESENCE_DEPLOYMENTS.includes(name)) {
|
|
119
120
|
throw new Error(
|
|
120
|
-
`tmp
|
|
121
|
+
`tmp service name "${name}" is not known (${TELEPRESENCE_DEPLOYMENTS.join(', ')}).`,
|
|
121
122
|
)
|
|
122
123
|
}
|
|
123
124
|
return name
|
|
@@ -127,7 +128,7 @@ export function resolveTelepresenceDeployFromCwd(cwd = process.cwd()) {
|
|
|
127
128
|
const root = findServiceRootFromCwd(cwd)
|
|
128
129
|
if (!root) {
|
|
129
130
|
throw new Error(
|
|
130
|
-
'No x/tmp.yml in this tree — cd into a service repo or
|
|
131
|
+
'No x/tmp.yml in this tree — cd into a service repo or: xry tp <gateway|iam|…>',
|
|
131
132
|
)
|
|
132
133
|
}
|
|
133
134
|
return { deploy: readDeployNameFromServiceRoot(root), serviceRoot: root }
|
|
@@ -146,3 +147,67 @@ export function resolveTelepresenceServiceDir(workspaceRoot, deployName) {
|
|
|
146
147
|
|
|
147
148
|
return null
|
|
148
149
|
}
|
|
150
|
+
|
|
151
|
+
function resolveServiceRootForDeploy({ cwd, deploy, serviceRootHint }) {
|
|
152
|
+
const cwdResolved = path.resolve(cwd)
|
|
153
|
+
let serviceRoot = serviceRootHint ? path.resolve(serviceRootHint) : findServiceRootFromCwd(cwdResolved)
|
|
154
|
+
|
|
155
|
+
const workspaceRoot = findWorkspaceRoot(serviceRoot ?? cwdResolved)
|
|
156
|
+
if (workspaceRoot && deploy) {
|
|
157
|
+
const byWorkspace =
|
|
158
|
+
resolveTelepresenceServiceDir(workspaceRoot, deploy) ??
|
|
159
|
+
resolveTelepresenceServiceDir(path.dirname(workspaceRoot), deploy)
|
|
160
|
+
if (byWorkspace) return byWorkspace
|
|
161
|
+
}
|
|
162
|
+
return serviceRoot ?? cwdResolved
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Workspace name + deploy for CLI (e.g. xrystal/iam).
|
|
167
|
+
* Reads outer tmp project.name when cwd is inside a workspace; deploy from arg or inner x/tmp.yml.
|
|
168
|
+
*/
|
|
169
|
+
export function resolveTelepresenceContext({
|
|
170
|
+
cwd = process.cwd(),
|
|
171
|
+
deployName = '',
|
|
172
|
+
serviceRootHint = null,
|
|
173
|
+
} = {}) {
|
|
174
|
+
const cwdResolved = path.resolve(cwd)
|
|
175
|
+
let deploy = String(deployName ?? '').trim()
|
|
176
|
+
let serviceRoot = serviceRootHint ? path.resolve(serviceRootHint) : null
|
|
177
|
+
|
|
178
|
+
if (!deploy) {
|
|
179
|
+
const fromCwd = resolveTelepresenceDeployFromCwd(cwdResolved)
|
|
180
|
+
deploy = fromCwd.deploy
|
|
181
|
+
serviceRoot = serviceRoot || fromCwd.serviceRoot
|
|
182
|
+
} else if (!serviceRoot) {
|
|
183
|
+
serviceRoot = findServiceRootFromCwd(cwdResolved)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (!deploy) {
|
|
187
|
+
throw new Error(
|
|
188
|
+
'Service name missing — cd into a service repo, or pass: xry tp <gateway|iam|…>',
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (!TELEPRESENCE_DEPLOYMENTS.includes(deploy)) {
|
|
193
|
+
throw new Error(`Unknown service "${deploy}" — use: ${TELEPRESENCE_DEPLOYMENTS.join(', ')}`)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
serviceRoot = resolveServiceRootForDeploy({
|
|
197
|
+
cwd: serviceRoot ?? cwdResolved,
|
|
198
|
+
deploy,
|
|
199
|
+
serviceRootHint: serviceRoot,
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
const workspaceRoot = findWorkspaceRoot(serviceRoot)
|
|
203
|
+
const workspaceName = workspaceRoot ? workspaceProjectName(workspaceRoot) : null
|
|
204
|
+
const targetLabel = workspaceName ? `${workspaceName}/${deploy}` : deploy
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
workspaceRoot,
|
|
208
|
+
workspaceName,
|
|
209
|
+
deploy,
|
|
210
|
+
serviceRoot,
|
|
211
|
+
targetLabel,
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -234,16 +234,97 @@ export function quitTelepresence({ cwd = process.cwd() } = {}) {
|
|
|
234
234
|
}
|
|
235
235
|
|
|
236
236
|
/** End intercept session: detach workload, remove cluster env snapshot, restore scaffold. */
|
|
237
|
-
export function leaveTelepresenceIntercept({
|
|
237
|
+
export function leaveTelepresenceIntercept({
|
|
238
|
+
deploy,
|
|
239
|
+
ns = 'dev',
|
|
240
|
+
cwd = process.cwd(),
|
|
241
|
+
port = process.env.TELEPRESENCE_LOCAL_PORT || '3000',
|
|
242
|
+
} = {}) {
|
|
238
243
|
assertNamespaceAllowed(ns)
|
|
239
244
|
const serviceRoot = path.resolve(cwd)
|
|
240
245
|
const envDir = path.join(serviceRoot, 'x', 'environments')
|
|
241
|
-
finishInterceptEnvSession({ envDir, serviceRoot, deploy, ns })
|
|
242
|
-
console.log(
|
|
246
|
+
finishInterceptEnvSession({ envDir, serviceRoot, deploy, ns, port })
|
|
247
|
+
console.log(
|
|
248
|
+
`Intercept ended (${deploy}, ns=${ns}). Cluster connect may still be active — use xry tp stop to quit.`,
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function listenerPidsOnPort(port) {
|
|
253
|
+
const raw = String(port ?? '3000').trim()
|
|
254
|
+
if (!/^\d+$/.test(raw)) return []
|
|
255
|
+
try {
|
|
256
|
+
const out = execSync(`lsof -nP -iTCP:${raw} -sTCP:LISTEN -t 2>/dev/null`, {
|
|
257
|
+
encoding: 'utf8',
|
|
258
|
+
shell: true,
|
|
259
|
+
}).trim()
|
|
260
|
+
if (!out) return []
|
|
261
|
+
return [...new Set(out.split(/\s+/).map((n) => Number(n)).filter((n) => Number.isFinite(n) && n > 0))]
|
|
262
|
+
} catch {
|
|
263
|
+
return []
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function processCwd(pid) {
|
|
268
|
+
try {
|
|
269
|
+
return execSync(`lsof -a -p ${pid} -d cwd -Fn 2>/dev/null | sed -n 's/^n//p'`, {
|
|
270
|
+
encoding: 'utf8',
|
|
271
|
+
shell: true,
|
|
272
|
+
}).trim()
|
|
273
|
+
} catch {
|
|
274
|
+
return ''
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function bunWatchPidsInServiceRoot(serviceRoot) {
|
|
279
|
+
const root = path.resolve(serviceRoot)
|
|
280
|
+
const pids = []
|
|
281
|
+
try {
|
|
282
|
+
const listing = execSync(`pgrep -fl ${JSON.stringify('bun --watch')}`, {
|
|
283
|
+
encoding: 'utf8',
|
|
284
|
+
shell: true,
|
|
285
|
+
}).trim()
|
|
286
|
+
if (!listing) return pids
|
|
287
|
+
for (const line of listing.split('\n')) {
|
|
288
|
+
const match = line.match(/^(\d+)\s/)
|
|
289
|
+
if (!match) continue
|
|
290
|
+
const pid = Number(match[1])
|
|
291
|
+
if (!Number.isFinite(pid) || pid <= 0) continue
|
|
292
|
+
const cwd = processCwd(pid)
|
|
293
|
+
if (cwd && path.resolve(cwd) === root) pids.push(pid)
|
|
294
|
+
}
|
|
295
|
+
} catch {
|
|
296
|
+
/* none */
|
|
297
|
+
}
|
|
298
|
+
return pids
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Stop local `xry dev` / `bun --watch` left after intercept ends, crashes, or `tp leave` from another terminal. */
|
|
302
|
+
export function stopLocalInterceptDev({ serviceRoot, port = '3000' } = {}) {
|
|
303
|
+
if (!serviceRoot) return []
|
|
304
|
+
const root = path.resolve(serviceRoot)
|
|
305
|
+
const candidates = new Set([...listenerPidsOnPort(port), ...bunWatchPidsInServiceRoot(root)])
|
|
306
|
+
candidates.delete(process.pid)
|
|
307
|
+
|
|
308
|
+
const stopped = []
|
|
309
|
+
for (const pid of candidates) {
|
|
310
|
+
try {
|
|
311
|
+
process.kill(pid, 'SIGTERM')
|
|
312
|
+
stopped.push(pid)
|
|
313
|
+
} catch {
|
|
314
|
+
/* already exited */
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (stopped.length) {
|
|
318
|
+
console.log(`Stopped local intercept dev (${root}, port ${port}): PIDs ${stopped.join(', ')}`)
|
|
319
|
+
}
|
|
320
|
+
return stopped
|
|
243
321
|
}
|
|
244
322
|
|
|
245
323
|
/** Detach intercept + delete cluster env snapshot (.env.telepresence). `.env.local` is kept. */
|
|
246
|
-
export function finishInterceptEnvSession({ envDir, serviceRoot, deploy, ns }) {
|
|
324
|
+
export function finishInterceptEnvSession({ envDir, serviceRoot, deploy, ns, port = '3000' }) {
|
|
325
|
+
if (serviceRoot) {
|
|
326
|
+
stopLocalInterceptDev({ serviceRoot, port })
|
|
327
|
+
}
|
|
247
328
|
if (deploy && ns) {
|
|
248
329
|
detachInterceptWorkload({ deploy, ns })
|
|
249
330
|
}
|
|
@@ -263,12 +344,12 @@ export function finishInterceptEnvSession({ envDir, serviceRoot, deploy, ns }) {
|
|
|
263
344
|
}
|
|
264
345
|
}
|
|
265
346
|
|
|
266
|
-
function runInterceptSession({ envDir, serviceRoot, deploy, ns, run }) {
|
|
347
|
+
function runInterceptSession({ envDir, serviceRoot, deploy, ns, port, run }) {
|
|
267
348
|
let sessionEnded = false
|
|
268
349
|
const endSession = () => {
|
|
269
350
|
if (sessionEnded) return
|
|
270
351
|
sessionEnded = true
|
|
271
|
-
finishInterceptEnvSession({ envDir, serviceRoot, deploy, ns })
|
|
352
|
+
finishInterceptEnvSession({ envDir, serviceRoot, deploy, ns, port })
|
|
272
353
|
}
|
|
273
354
|
|
|
274
355
|
const onSignal = () => {
|
|
@@ -423,6 +504,7 @@ export function runTelepresenceIntercept({
|
|
|
423
504
|
serviceRoot,
|
|
424
505
|
deploy,
|
|
425
506
|
ns,
|
|
507
|
+
port,
|
|
426
508
|
run: () => {
|
|
427
509
|
if (localCmd?.length) {
|
|
428
510
|
const wrap = buildInterceptLocalInvocation(cliName, absEnvFile, localCmd)
|