Skip to content

Commit

Permalink
Add -task flag to alloc signal, restart
Browse files Browse the repository at this point in the history
Alloc exec only works when task is passed as a flag and not an arg.
Alloc logs currently accepts either, but alloc signal and restart only
accept task as an arg. This adds -task as a flag to the other alloc
commands to make the cli UX consistent. If task is passed as a flag and
an arg, it ignores the arg.
  • Loading branch information
isabeldepapel committed Jul 6, 2021
1 parent 18d359f commit 8e02feb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
20 changes: 14 additions & 6 deletions command/alloc_restart.go
Expand Up @@ -31,6 +31,9 @@ General Options:
Restart Specific Options:
-task <task-name>
Specify the individual task to restart.
-verbose
Show full information.
`
Expand All @@ -41,10 +44,12 @@ func (c *AllocRestartCommand) Name() string { return "alloc restart" }

func (c *AllocRestartCommand) Run(args []string) int {
var verbose bool
var task string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.StringVar(&task, "task", "", "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -107,18 +112,21 @@ func (c *AllocRestartCommand) Run(args []string) int {
return 1
}

var taskName string
if len(args) == 2 {
// Validate Task
taskName = args[1]
err := validateTaskExistsInAllocation(taskName, alloc)
// If -task isn't provided fallback to reading the task name
// from args.
if task == "" && len(args) >= 2 {
task = args[1]
}

if task != "" {
err := validateTaskExistsInAllocation(task, alloc)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
}

err = client.Allocations().Restart(alloc, taskName, nil)
err = client.Allocations().Restart(alloc, task, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to restart allocation:\n\n%s", err.Error()))
return 1
Expand Down
21 changes: 14 additions & 7 deletions command/alloc_signal.go
Expand Up @@ -34,6 +34,9 @@ Signal Specific Options:
-s
Specify the signal that the selected tasks should receive.
-task <task-name>
Specify the individual task that will receive the signal
-verbose
Show full information.
`
Expand All @@ -44,12 +47,13 @@ func (c *AllocSignalCommand) Name() string { return "alloc signal" }

func (c *AllocSignalCommand) Run(args []string) int {
var verbose bool
var signal string
var signal, task string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.StringVar(&signal, "s", "SIGKILL", "")
flags.StringVar(&task, "task", "", "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -112,18 +116,21 @@ func (c *AllocSignalCommand) Run(args []string) int {
return 1
}

var taskName string
if len(args) == 2 {
// Validate Task
taskName = args[1]
err := validateTaskExistsInAllocation(taskName, alloc)
// If -task isn't provided fallback to reading the task name
// from args.
if task == "" && len(args) >= 2 {
task = args[1]
}

if task != "" {
err := validateTaskExistsInAllocation(task, alloc)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
}

err = client.Allocations().Signal(alloc, nil, taskName, signal)
err = client.Allocations().Signal(alloc, nil, task, signal)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error signalling allocation: %s", err))
return 1
Expand Down
19 changes: 19 additions & 0 deletions website/content/docs/commands/alloc/logs.mdx
Expand Up @@ -22,6 +22,10 @@ allocation is only running a single task, the task name can be omitted.
Optionally, the `-job` option may be used in which case a random allocation from
the given job will be chosen.

Task name may also be specified using the `-task` option rather than a command
argument. If task name is given with both an argument and the `-task` option,
preference is given to the latter.

When ACLs are enabled, this command requires a token with the `read-logs`,
`read-job`, and `list-jobs` capabilities for the allocation's namespace.

Expand All @@ -38,6 +42,8 @@ When ACLs are enabled, this command requires a token with the `read-logs`,
- `-job`: Use a random allocation from the specified job, preferring a running
allocation.

- `-task`: Specify the task to view the logs.

- `-f`: Causes the output to not stop when the end of the logs are reached, but
rather to wait for additional output.

Expand Down Expand Up @@ -81,6 +87,19 @@ bam
<blocking>
```

Specifying task name with the `-task` option:

```shell-session
$ nomad alloc logs -task redis eb17e557
```

If task name is specified using both options, the command argument is ignored.
The following will output the logs from the redis task only, not the api task:

```shell-session
$ nomad alloc logs -task redis eb17e557 api
```

## Using Job ID instead of Allocation ID

Setting the `-job` flag causes a random allocation of the specified job to be
Expand Down
19 changes: 19 additions & 0 deletions website/content/docs/commands/alloc/restart.mdx
Expand Up @@ -20,6 +20,10 @@ This command accepts a single allocation ID and a task name. The task name must
be part of the allocation and the task must be currently running. The task name
is optional and if omitted every task in the allocation will be restarted.

Task name may also be specified using the `-task` option rather than a command
argument. If task name is given with both an argument and the `-task` option,
preference is given to the latter.

When ACLs are enabled, this command requires a token with the
`alloc-lifecycle`, `read-job`, and `list-jobs` capabilities for the
allocation's namespace.
Expand All @@ -30,6 +34,8 @@ allocation's namespace.

## Restart Options

- `-task`: Specify the individual task to restart.

- `-verbose`: Display verbose output.

## Examples
Expand All @@ -42,3 +48,16 @@ Could not find task named: foo, found:
* test
<blocking>
```

Specifying task name with the `-task` option:

```shell-session
$ nomad alloc restart -task redis eb17e557
```

If task name is specified using both options, the command argument is ignored.
The following will restart the redis task only, not the api task:

```shell-session
$ nomad alloc restart -task redis eb17e557 api
```
24 changes: 22 additions & 2 deletions website/content/docs/commands/alloc/signal.mdx
Expand Up @@ -20,6 +20,10 @@ This command accepts a single allocation ID and a task name. The task name must
be part of the allocation and the task must be currently running. The task name
is optional and if omitted every task in the allocation will be signaled.

Task name may also be specified using the `-task` option rather than a command
argument. If task name is given with both an argument and the `-task` option,
preference is given to the latter.

When ACLs are enabled, this command requires a token with the
`alloc-lifecycle`, `read-job`, and `list-jobs` capabilities for the
allocation's namespace.
Expand All @@ -31,15 +35,31 @@ allocation's namespace.
## Signal Options

- `-s`: Signal to send to the tasks. Valid options depend on the driver.

- `-task`: Specify the individual task that will receive the signal.

- `-verbose`: Display verbose output.

## Examples

```shell-session
$ nomad alloc signal eb17e557
$ nomad alloc signal eb17e557 foo
Could not find task named: foo, found:
$ nomad alloc signal eb17e557 redis
Could not find task named: redis, found:
* test
<blocking>
```

Specifying task name with the `-task` option:

```shell-session
$ nomad alloc signal -task redis eb17e557
```

If task name is specified using both options, the command argument is ignored.
The following will signal the redis task only, not the api task:

```shell-session
$ nomad alloc signal -task redis eb17e557 api
```

0 comments on commit 8e02feb

Please sign in to comment.