Add 'podman restart' command

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #503
Approved by: rhatdan
This commit is contained in:
Matthew Heon 2018-03-15 15:00:18 -04:00 committed by Atomic Bot
parent 8840b92da6
commit 2724434369
5 changed files with 197 additions and 31 deletions

View File

@ -56,6 +56,7 @@ func main() {
portCommand,
pullCommand,
pushCommand,
restartCommand,
rmCommand,
rmiCommand,
runCommand,

110
cmd/podman/restart.go Normal file
View File

@ -0,0 +1,110 @@
package main
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod"
"github.com/urfave/cli"
)
var (
restartFlags = []cli.Flag{
cli.UintFlag{
Name: "timeout, time, t",
Usage: "Seconds to wait for stop before killing the container",
Value: libpod.CtrRemoveTimeout,
},
LatestFlag,
}
restartDescription = `Restarts one or more running containers. The container ID or name can be used. A timeout before forcibly stopping can be set, but defaults to 10 seconds`
restartCommand = cli.Command{
Name: "restart",
Usage: "Restart one or more containers",
Description: restartDescription,
Flags: restartFlags,
Action: restartCmd,
ArgsUsage: "CONTAINER [CONTAINER ...]",
UseShortOptionHandling: true,
}
)
func restartCmd(c *cli.Context) error {
args := c.Args()
if len(args) < 1 && !c.Bool("latest") {
return errors.Wrapf(libpod.ErrInvalidArg, "you must provide at least one container name or ID")
}
if err := validateFlags(c, restartFlags); err != nil {
return err
}
runtime, err := getRuntime(c)
if err != nil {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.Shutdown(false)
var lastError error
timeout := c.Uint("timeout")
useTimeout := c.IsSet("timeout")
// Handle --latest
if c.Bool("latest") {
lastCtr, err := runtime.GetLatestContainer()
if err != nil {
lastError = errors.Wrapf(err, "unable to get latest container")
} else {
lastError = restartCtr(timeout, useTimeout, lastCtr)
}
}
for _, id := range args {
ctr, err := runtime.LookupContainer(id)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "unable to find container %s", id)
continue
}
if err := restartCtr(timeout, useTimeout, ctr); err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "error restarting container %s", ctr.ID())
}
}
return lastError
}
// Restart a single container
func restartCtr(cliTimeout uint, useTimeout bool, ctr *libpod.Container) error {
timeout := ctr.StopTimeout()
if useTimeout {
timeout = cliTimeout
}
state, err := ctr.State()
if err != nil {
return err
}
if state == libpod.ContainerStateRunning {
if err := ctr.StopWithTimeout(timeout); err != nil {
return err
}
}
if err := ctr.Start(); err != nil {
return err
}
fmt.Printf("%s\n", ctr.ID())
return nil
}

View File

@ -1312,6 +1312,16 @@ _podman_run() {
_podman_container_run
}
_podman_restart() {
local options_with_args="
--timeout -t
"
local boolean_options="
--latest
-l"
_complete_ "$options_with_args" "$boolean_options"
}
_podman_rm() {
local boolean_options="
--all

45
docs/podman-restart.1.md Normal file
View File

@ -0,0 +1,45 @@
% podman(1) podman-restart - Restart a container
% Matt Heon
# podman-restart "1" "March 2017" "podman"
## NAME
podman restart - Restart a container
## SYNOPSIS
**podman attach [OPTIONS] CONTAINER [CONTAINER...]**
## DESCRIPTION
The restart command allows containers to be restarted using their ID or name.
Containers will be stopped if they are running and then restarted.
## OPTIONS
**--timeout**
Timeout to wait before forcibly stopping the container
**--latest, -l**
Instead of providing the container name or ID, use the last created container. If you use methods other than Podman
to run containers such as CRI-O, the last started container could be from either of those methods.
## EXAMPLES ##
```
podman restart -l
ec588fc80b05e19d3006bf2e8aa325f0a2e2ff1f609b7afb39176ca8e3e13467
```
```
podman restart ff6cf1
ff6cf1e5e77e6dba1efc7f3fcdb20e8b89ad8947bc0518be1fcb2c78681f226f
```
```
podman restart --timeout 4 test1 test2
c3bb026838c30e5097f079fa365c9a4769d52e1017588278fa00d5c68ebc1502
17e13a63081a995136f907024bcfe50ff532917988a152da229db9d894c5a9ec
```
## SEE ALSO
podman(1), podman-run(1), podman-start(1), podman-create(1)
## HISTORY
March 2018, Originally compiled by Matt Heon <mheon@redhat.com>

View File

@ -56,6 +56,7 @@ There are other equivalents for these tools
| `docker ps` | [`podman ps`](./docs/podman-ps.1.md) |
| `docker pull` | [`podman pull`](./docs/podman-pull.1.md) |
| `docker push` | [`podman push`](./docs/podman-push.1.md) |
| `docker restart` | [`podman restart`](./docs/podman-restart.1.md)] |
| `docker rm` | [`podman rm`](./docs/podman-rm.1.md) |
| `docker rmi` | [`podman rmi`](./docs/podman-rmi.1.md) |
| `docker run` | [`podman run`](./docs/podman-run.1.md) |
@ -85,7 +86,6 @@ Those Docker commands currently do not have equivalents in `podman`:
| `docker plugin` |podman does not support plugins. We recommend you use alternative OCI Runtimes or OCI Runtime Hooks to alter behavior of podman.|
| `docker port` ||
| `docker rename` | podman does not support rename, you need to use `podman rm` and `podman create` to rename a container.|
| `docker restart` | podman does not support restart. We recommend that you put your podman containers into a systemd unit file and use it for restarting applications.|
| `docker secret` ||
| `docker service` ||
| `docker stack` ||