Skip to content

Commit

Permalink
fix(cloud): fix session registration flow for dev
Browse files Browse the repository at this point in the history
This fixes some problems/limitations with the changes introduced in
`4cdad7f502d88dd2b08d9bec663684b5359d87ce` (see
#4675).

Tests for the total flow will follow in a separate PR.
  • Loading branch information
thsig committed Jun 22, 2023
1 parent c4f6659 commit ea3bba2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
14 changes: 7 additions & 7 deletions core/src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ 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 @@ -286,18 +285,19 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =

let cloudSession: CloudSession | undefined

const skipRegistration = !["dev", "serve"].includes(this.name) && this.maybePersistent(params) && !params.parentCommand
// 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) {
// 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)
}
// const outerName = this.name
// garden.commandInfo.name = "dev"
// garden.commandInfo.opts.cmd = getCmdOptionForDev(outerName, params)
// }

if (garden.cloudApi && garden.projectId && this.streamEvents) {
if (!skipRegistration && garden.cloudApi && garden.projectId && this.streamEvents) {
cloudSession = await garden.cloudApi.registerSession({
parentSessionId: parentSessionId || undefined,
sessionId: garden.sessionId,
Expand Down
19 changes: 11 additions & 8 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 { getCmdOptionForDev, watchParameter, watchRemovedWarning } from "./helpers"
import { runAsDevCommand, watchParameter, watchRemovedWarning } from "./helpers"
import { DeployTask } from "../tasks/deploy"
import { naturalList } from "../util/string"
import { StringsParameter, BooleanParameter } from "../cli/params"
Expand All @@ -35,7 +35,6 @@ import { PortForwardMonitor } from "../monitors/port-forward"
import { LogMonitor } from "../monitors/logs"
import { LoggerType, parseLogLevel } from "../logger/logger"
import { serveOpts } from "./serve"
import { DevCommand } from "./dev"
import { gardenEnv } from "../constants"

export const deployArgs = {
Expand Down Expand Up @@ -182,12 +181,16 @@ 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.
params.opts.cmd = getCmdOptionForDev("deploy", params)
const devCmd = new DevCommand()
devCmd.printHeader(params)
await devCmd.prepare(params)

return devCmd.action(params)
return runAsDevCommand("deploy", params)
// params.opts.cmd = getCmdOptionForDev("deploy", params)
// this.garden.commandInfo.name = "dev"
// this.garden.commandInfo.args["$all"] = []
// this.garden.commandInfo.opts.cmd = params.opts.cmd
// const devCmd = new DevCommand()
// devCmd.printHeader(params)
// await devCmd.prepare(params)

// return devCmd.action(params)
}

const disablePortForwards = gardenEnv.GARDEN_DISABLE_PORT_FORWARDS || opts["disable-port-forwards"] || false
Expand Down
24 changes: 24 additions & 0 deletions core/src/commands/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,35 @@ import isGlob from "is-glob"
import { ParameterError } from "../exceptions"
import { naturalList } from "../util/string"
import { CommandParams } from "./base"
import { ServeCommandOpts } from "./serve"
import { DevCommand } from "./dev"

export function makeGetTestOrTaskLog(actions: (TestAction | RunAction)[]) {
return actions.map((t) => prettyPrintTestOrTask(t)).join("\n")
}

/**
* Runs a `dev` command and runs `commandName` with the args & opts provided in `params` as the first
* interactive command.
*
* Also updates the `commandInfo` accordinly so that the session registration parameters sent to Cloud are correct.
*/
export async function runAsDevCommand(
commandName: string, // The calling command's opts need to extend `ServeCommandOpts`.
params: CommandParams<{}, ServeCommandOpts>
) {
const commandInfo = params.garden.commandInfo
params.opts.cmd = getCmdOptionForDev(commandName, params)
commandInfo.name = "dev"
commandInfo.args["$all"] = []
commandInfo.opts.cmd = params.opts.cmd
const devCmd = new DevCommand()
devCmd.printHeader(params)
await devCmd.prepare(params)

return devCmd.action(params)
}

export function getCmdOptionForDev(commandName: string, params: CommandParams) {
return [commandName + " " + params.args.$all?.join(" ")]
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class ServeCommand<
localServerPort: this.server.port,
environment: defaultGarden.environmentName,
namespace: defaultGarden.namespace,
isDevCommand: false,
isDevCommand: true,
})
if (session?.shortId) {
const distroName = getCloudDistributionName(cloudDomain)
Expand Down
18 changes: 8 additions & 10 deletions core/src/commands/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import { Command, CommandParams, CommandResult } from "./base"
import { dedent } from "../util/string"
import { deployArgs, DeployCommand, deployOpts } from "./deploy"
import { serveOpts } from "./serve"
import { DevCommand } from "./dev"
import type { LoggerType } from "../logger/logger"
import { getCmdOptionForDev } from "./helpers"
import { runAsDevCommand } from "./helpers"

const upArgs = {
...deployArgs,
Expand Down Expand Up @@ -45,16 +44,15 @@ export class UpCommand extends Command<UpArgs, UpOpts> {
}

async action(params: CommandParams<UpArgs, UpOpts>): Promise<CommandResult> {
let cmd: Command = new DevCommand()

if (params.commandLine) {
// We're already in the dev command
cmd = new DeployCommand()
params.opts.logs = true
} else {
params.opts.cmd = getCmdOptionForDev("deploy --logs", params)
if (!params.commandLine) {
// Then we start a dev command and run `deploy --logs` as the first interactive command.
return runAsDevCommand("deploy --logs", params)
}

params.opts.logs = true
// Else, we're already in the dev command.
const cmd = new DeployCommand()

cmd.printHeader(params)
await cmd.prepare(params)

Expand Down

0 comments on commit ea3bba2

Please sign in to comment.