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

fix(cloud): properly handle dev delegation #4675

Merged
merged 2 commits into from
Jun 21, 2023
Merged
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
3 changes: 3 additions & 0 deletions core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ export class CloudApi {
localServerPort,
environment,
namespace,
isDevCommand,
}: {
parentSessionId: string | undefined
sessionId: string
Expand All @@ -594,6 +595,7 @@ export class CloudApi {
localServerPort?: number
environment: string
namespace: string
isDevCommand: boolean
}): Promise<CloudSession | undefined> {
let session = this.registeredSessions.get(sessionId)

Expand All @@ -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", {
Expand Down
36 changes: 25 additions & 11 deletions core/src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -258,17 +259,18 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =
*
* @returns The result from the command action
*/
async run({
garden: parentGarden,
args,
opts,
cli,
commandLine,
sessionId,
parentCommand,
parentSessionId,
overrideLogLevel,
}: RunCommandParams<A, O>): Promise<CommandResult<R>> {
async run(params: RunCommandParams<A, O>): Promise<CommandResult<R>> {
const {
garden: parentGarden,
args,
opts,
cli,
commandLine,
sessionId,
parentCommand,
parentSessionId,
overrideLogLevel,
} = params
const commandStartTime = new Date()
const server = this.server

Expand All @@ -284,6 +286,17 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =

let cloudSession: CloudSession | undefined

// It's not ideal that we have to update the command info here after init, but we don't know whether we're
// going into a subcommand in the `GardenCli` class where the `commandInfo` object is first created.
if (!["dev", "serve"].includes(this.name) && this.maybePersistent(params) && !params.parentCommand) {
// Then this command will be starting a `dev` command and then running itself from there, so we update
// the `commandInfo` accordingly.
// Example: `garden deploy --sync`.
const outerName = this.name
garden.commandInfo.name = "dev"
garden.commandInfo.opts.cmd = getCmdOptionForDev(outerName, params)
}

vvagaytsev marked this conversation as resolved.
Show resolved Hide resolved
if (garden.cloudApi && garden.projectId && this.streamEvents) {
cloudSession = await garden.cloudApi.registerSession({
parentSessionId: parentSessionId || undefined,
Expand All @@ -293,6 +306,7 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =
localServerPort: server?.port,
environment: garden.environmentName,
namespace: garden.namespace,
isDevCommand: garden.commandInfo.name === "dev"
})
}

Expand Down
5 changes: 2 additions & 3 deletions core/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
emptyActionResults,
} from "./base"
import { printEmoji, printHeader } from "../logger/util"
import { watchParameter, watchRemovedWarning } from "./helpers"
import { getCmdOptionForDev, watchParameter, watchRemovedWarning } from "./helpers"
import { DeployTask } from "../tasks/deploy"
import { naturalList } from "../util/string"
import { StringsParameter, BooleanParameter } from "../cli/params"
Expand Down Expand Up @@ -178,8 +178,7 @@ export class DeployCommand extends Command<Args, Opts> {
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)
Expand Down
5 changes: 5 additions & 0 deletions core/src/commands/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`

Expand Down
1 change: 1 addition & 0 deletions core/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export class ServeCommand<
localServerPort: this.server.port,
environment: defaultGarden.environmentName,
namespace: defaultGarden.namespace,
isDevCommand: false,
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/commands/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -51,7 +52,7 @@ export class UpCommand extends Command<UpArgs, UpOpts> {
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)
Expand Down