Skip to content

Commit

Permalink
feat: add exec command, to run commands in running service containers
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jul 30, 2018
1 parent d6ea994 commit 7f74edc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
24 changes: 24 additions & 0 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,30 @@ Starts the garden development console.

garden dev

### garden exec

Executes a command (such as an interactive shell) in a running service.

Finds an active container for a deployed service and executes the given command within the container.
Supports interactive shells.

_NOTE: This command may not be supported for all module types._

Examples:

garden exec my-service /bin/sh # runs a shell in the my-service container

##### Usage

garden exec <service> <command>

##### Arguments

| Argument | Required | Description |
| -------- | -------- | ----------- |
| `service` | Yes | The service to exec the command in.
| `command` | Yes | The command to run.

### garden get config

Get a configuration variable from the environment.
Expand Down
2 changes: 2 additions & 0 deletions garden-cli/src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ScanCommand } from "./scan"
import { SetCommand } from "./set"
import { TestCommand } from "./test"
import { ValidateCommand } from "./validate"
import { ExecCommand } from "./exec"

export const coreCommands: Command[] = [
new BuildCommand(),
Expand All @@ -32,6 +33,7 @@ export const coreCommands: Command[] = [
new DeleteCommand(),
new DeployCommand(),
new DevCommand(),
new ExecCommand(),
new GetCommand(),
new InitCommand(),
new LoginCommand(),
Expand Down
10 changes: 4 additions & 6 deletions garden-cli/src/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

import chalk from "chalk"
import { PluginContext } from "../plugin-context"
import {
ExecInServiceResult,
} from "../types/plugin/outputs"
import { LoggerType } from "../logger/types"
import { ExecInServiceResult } from "../types/plugin/outputs"
import {
Command,
CommandResult,
Expand Down Expand Up @@ -49,7 +48,7 @@ export class ExecCommand extends Command<typeof runArgs, typeof runOpts> {
Finds an active container for a deployed service and executes the given command within the container.
Supports interactive shells.
_NOTE: This command may not be supported for all module types.
_NOTE: This command may not be supported for all module types._
Examples:
Expand All @@ -58,6 +57,7 @@ export class ExecCommand extends Command<typeof runArgs, typeof runOpts> {

arguments = runArgs
options = runOpts
loggerType = LoggerType.basic

async action(ctx: PluginContext, args: Args): Promise<CommandResult<ExecInServiceResult>> {
const serviceName = args.service
Expand All @@ -68,8 +68,6 @@ export class ExecCommand extends Command<typeof runArgs, typeof runOpts> {
command: `Running command ${chalk.cyan(args.command)} in service ${chalk.cyan(serviceName)}`,
})

await ctx.configureEnvironment({})

const result = await ctx.execInService({ serviceName, command })

return { result }
Expand Down
8 changes: 7 additions & 1 deletion garden-cli/src/plugins/kubernetes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ export async function execInService(
}

// exec in the pod via kubectl
const res = await kubectl(context, namespace).tty(["exec", "-it", pod.metadata.name, "--", ...command])
const kubecmd = ["exec", "-it", pod.metadata.name, "--", ...command]
const res = await kubectl(context, namespace).tty(kubecmd, {
ignoreError: true,
silent: false,
timeout: 999999,
tty: true,
})

return { code: res.code, output: res.output }
}
Expand Down

0 comments on commit 7f74edc

Please sign in to comment.