Skip to content

Commit

Permalink
improvement(core): add --forward flag to deploy command
Browse files Browse the repository at this point in the history
This allows users to run the deploy command as a long lived, persistent
process with port forwards enabled without having Garden watch for
changes.
  • Loading branch information
eysi09 committed Sep 10, 2021
1 parent 6382d3d commit 8f817d5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
7 changes: 6 additions & 1 deletion core/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export const deployOpts = {
"skip": new StringsParameter({
help: "The name(s) of services you'd like to skip when deploying.",
}),
"forward": new BooleanParameter({
help: deline`Create port forwards and leave process running without watching
for changes. Ignored if --watch/-w flag is set or when in dev or hot-reload mode.`,
}),
}

type Args = typeof deployArgs
Expand Down Expand Up @@ -105,7 +109,8 @@ export class DeployCommand extends Command<Args, Opts> {

outputsSchema = () => processCommandResultSchema()

private isPersistent = (opts: ParameterValues<Opts>) => !!opts.watch || !!opts["hot-reload"] || !!opts["dev-mode"]
private isPersistent = (opts: ParameterValues<Opts>) =>
!!opts.watch || !!opts["hot-reload"] || !!opts["dev-mode"] || !!opts.forward

printHeader({ headerLog }) {
printHeader(headerLog, "Deploy", "rocket")
Expand Down
27 changes: 25 additions & 2 deletions core/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ interface ProcessParams {
footerLog?: LogEntry
watch: boolean
initialTasks: BaseTask[]
// use this if the behavior should be different on watcher changes than on initial processing
/**
* Use this if the behavior should be different on watcher changes than on initial processing
*/
changeHandler: ProcessHandler
}

Expand Down Expand Up @@ -81,7 +83,28 @@ export async function processModules({

const results = await garden.processTasks(initialTasks)

if (!watch) {
if (!watch && !garden.persistent) {
return {
taskResults: results,
restartRequired: false,
}
}

if (!watch && garden.persistent) {
// Garden process is persistent but not in watch mode. E.g. used to
// keep port forwards alive without enabling watch or dev mode.
await new Promise((resolve) => {
garden.events.on("_restart", () => {
log.debug({ symbol: "info", msg: `Manual restart triggered` })
resolve({})
})

garden.events.on("_exit", () => {
log.debug({ symbol: "info", msg: `Manual exit triggered` })
restartRequired = false
resolve({})
})
})
return {
taskResults: results,
restartRequired: false,
Expand Down
36 changes: 33 additions & 3 deletions core/test/unit/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": undefined,
"forward": false,
}),
})

Expand Down Expand Up @@ -470,6 +471,7 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": undefined,
"forward": false,
}),
})

Expand Down Expand Up @@ -523,6 +525,7 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": undefined,
"forward": false,
}),
})

Expand Down Expand Up @@ -573,6 +576,7 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": undefined,
"forward": false,
}),
})

Expand Down Expand Up @@ -616,6 +620,7 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": ["service-b"],
"forward": false,
}),
})

Expand All @@ -627,7 +632,7 @@ describe("DeployCommand", () => {
})

describe("prepare", () => {
it("return persistent=true if --watch is set", async () => {
it("should return persistent=true if --watch is set", async () => {
const cmd = new DeployCommand()
const log = getLogger().placeholder()
const { persistent } = await cmd.prepare({
Expand All @@ -644,12 +649,13 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": ["service-b"],
"forward": false,
}),
})
expect(persistent).to.be.true
})

it("return persistent=true if --dev is set", async () => {
it("should return persistent=true if --dev is set", async () => {
const cmd = new DeployCommand()
const log = getLogger().placeholder()
const { persistent } = await cmd.prepare({
Expand All @@ -666,12 +672,13 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": ["service-b"],
"forward": false,
}),
})
expect(persistent).to.be.true
})

it("return persistent=true if --hot-reload is set", async () => {
it("should return persistent=true if --hot-reload is set", async () => {
const cmd = new DeployCommand()
const log = getLogger().placeholder()
const { persistent } = await cmd.prepare({
Expand All @@ -688,6 +695,29 @@ describe("DeployCommand", () => {
"force": false,
"force-build": true,
"skip": ["service-b"],
"forward": false,
}),
})
expect(persistent).to.be.true
})
it("should return persistent=true if --follow is set", async () => {
const cmd = new DeployCommand()
const log = getLogger().placeholder()
const { persistent } = await cmd.prepare({
log,
headerLog: log,
footerLog: log,
args: {
services: undefined,
},
opts: withDefaultGlobalOpts({
"dev-mode": undefined,
"hot-reload": undefined,
"watch": false,
"force": false,
"force-build": true,
"skip": ["service-b"],
"forward": true,
}),
})
expect(persistent).to.be.true
Expand Down
1 change: 1 addition & 0 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ Examples:
| `--dev-mode` | `-dev` | array:string | [EXPERIMENTAL] The name(s) of the service(s) to deploy with dev mode enabled. Use comma as a separator to specify multiple services. Use * to deploy all services with dev mode enabled. When this option is used, the command is run in watch mode (i.e. implicitly sets the --watch/-w flag).
| `--hot-reload` | `-hot` | array:string | The name(s) of the service(s) to deploy with hot reloading enabled. Use comma as a separator to specify multiple services. Use * to deploy all services with hot reloading enabled (ignores services belonging to modules that don&#x27;t support or haven&#x27;t configured hot reloading). When this option is used, the command is run in watch mode (i.e. implicitly sets the --watch/-w flag).
| `--skip` | | array:string | The name(s) of services you&#x27;d like to skip when deploying.
| `--forward` | | boolean | Create port forwards and leave process running without watching for changes. Ignored if --watch/-w flag is set or when in dev or hot-reload mode.

#### Outputs

Expand Down

0 comments on commit 8f817d5

Please sign in to comment.