From 384fd242b11a6532dfbeb1d35ae52b510af895ad Mon Sep 17 00:00:00 2001 From: Thorarinn Sigurdsson Date: Tue, 20 Jun 2023 21:55:30 +0200 Subject: [PATCH 1/2] fix(cloud): properly handle dev delegation When a command is run with an option that results in it being run inside the `dev` command instead (e.g. `garden deploy --sync`), we now register the initial command session with Cloud as a `dev` command with the `--cmd` option set appropriately. This is useful for Cloud when listing command sessions created this way. For good measure, we include an explicit `isDevCommand` parameter with the session registration request to Cloud to make this setup more robust against future changes (instead of inferring whether the command is a `dev` command from the `commandInfo` map). --- core/src/cloud/api.ts | 3 +++ core/src/commands/base.ts | 36 +++++++++++++++++++++++++----------- core/src/commands/deploy.ts | 5 ++--- core/src/commands/helpers.ts | 5 +++++ core/src/commands/up.ts | 3 ++- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/core/src/cloud/api.ts b/core/src/cloud/api.ts index e3cb1a9ec5..be133a474e 100644 --- a/core/src/cloud/api.ts +++ b/core/src/cloud/api.ts @@ -586,6 +586,7 @@ export class CloudApi { localServerPort, environment, namespace, + isDevCommand, }: { parentSessionId: string | undefined sessionId: string @@ -594,6 +595,7 @@ export class CloudApi { localServerPort?: number environment: string namespace: string + isDevCommand?: boolean }): Promise { let session = this.registeredSessions.get(sessionId) @@ -610,6 +612,7 @@ export class CloudApi { projectUid: projectId, environment, namespace, + isDevCommand, } this.log.debug(`Registering session with ${this.distroName} for ${projectId} in ${environment}/${namespace}.`) const res: CloudSessionResponse = await this.post("sessions", { diff --git a/core/src/commands/base.ts b/core/src/commands/base.ts index 70d1bec4e6..a5c3c56c34 100644 --- a/core/src/commands/base.ts +++ b/core/src/commands/base.ts @@ -55,6 +55,7 @@ import { GraphResultMapWithoutTask, GraphResultWithoutTask, GraphResults } from import { splitFirst } from "../util/string" import { ActionMode } from "../actions/types" import { AnalyticsHandler } from "../analytics/analytics" +import { getCmdOptionForDev } from "./helpers" export interface CommandConstructor { new (parent?: CommandGroup): Command @@ -258,17 +259,18 @@ export abstract class Command): Promise> { + async run(params: RunCommandParams): Promise> { + const { + garden: parentGarden, + args, + opts, + cli, + commandLine, + sessionId, + parentCommand, + parentSessionId, + overrideLogLevel, + } = params const commandStartTime = new Date() const server = this.server @@ -284,6 +286,17 @@ export abstract class Command { const monitor = this.maybePersistent(params) if (monitor && !params.parentCommand) { // Then we're not in the dev command yet, so we call that instead with the appropriate initial command. - // TODO: Abstract this delegation process into a helper if we write more commands that do this sort of thing. - params.opts.cmd = ["deploy " + params.args.$all!.join(" ")] + params.opts.cmd = getCmdOptionForDev("deploy", params) const devCmd = new DevCommand() devCmd.printHeader(params) await devCmd.prepare(params) diff --git a/core/src/commands/helpers.ts b/core/src/commands/helpers.ts index 28febc8071..d60f9d9901 100644 --- a/core/src/commands/helpers.ts +++ b/core/src/commands/helpers.ts @@ -19,11 +19,16 @@ import { ActionKind } from "../actions/types" import isGlob from "is-glob" import { ParameterError } from "../exceptions" import { naturalList } from "../util/string" +import { CommandParams } from "./base" export function makeGetTestOrTaskLog(actions: (TestAction | RunAction)[]) { return actions.map((t) => prettyPrintTestOrTask(t)).join("\n") } +export function getCmdOptionForDev(commandName: string, params: CommandParams) { + return [commandName + " " + params.args.$all?.join(" ")] +} + export function prettyPrintWorkflow(workflow: WorkflowConfig): string { let out = `${chalk.cyan.bold(workflow.name)}` diff --git a/core/src/commands/up.ts b/core/src/commands/up.ts index 38da5c8eb1..cdc6ce8e20 100644 --- a/core/src/commands/up.ts +++ b/core/src/commands/up.ts @@ -13,6 +13,7 @@ import { deployArgs, DeployCommand, deployOpts } from "./deploy" import { serveOpts } from "./serve" import { DevCommand } from "./dev" import type { LoggerType } from "../logger/logger" +import { getCmdOptionForDev } from "./helpers" const upArgs = { ...deployArgs, @@ -51,7 +52,7 @@ export class UpCommand extends Command { cmd = new DeployCommand() params.opts.logs = true } else { - params.opts.cmd = ["deploy --logs " + params.args.$all!.join(" ")] + params.opts.cmd = getCmdOptionForDev("deploy --logs", params) } cmd.printHeader(params) From 4638f7bc2533ec47b892af957e8388248a5582f1 Mon Sep 17 00:00:00 2001 From: Thorarinn Sigurdsson Date: Wed, 21 Jun 2023 12:58:29 +0200 Subject: [PATCH 2/2] chore: address PR comment --- core/src/cloud/api.ts | 2 +- core/src/commands/serve.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/cloud/api.ts b/core/src/cloud/api.ts index be133a474e..4ad84292cb 100644 --- a/core/src/cloud/api.ts +++ b/core/src/cloud/api.ts @@ -595,7 +595,7 @@ export class CloudApi { localServerPort?: number environment: string namespace: string - isDevCommand?: boolean + isDevCommand: boolean }): Promise { let session = this.registeredSessions.get(sessionId) diff --git a/core/src/commands/serve.ts b/core/src/commands/serve.ts index 6263ed8e99..54688c785a 100644 --- a/core/src/commands/serve.ts +++ b/core/src/commands/serve.ts @@ -166,6 +166,7 @@ export class ServeCommand< localServerPort: this.server.port, environment: defaultGarden.environmentName, namespace: defaultGarden.namespace, + isDevCommand: false, }) } }