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

feat: add exec command, to run commands in running service containers #221

Merged
merged 1 commit into from
Jul 31, 2018
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
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