2017-09-06 17:30:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-16 09:18:19 +00:00
|
|
|
"os"
|
|
|
|
|
2017-09-06 17:30:34 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libkpod"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultTimeout int64 = 10
|
|
|
|
stopFlags = []cli.Flag{
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "timeout, t",
|
|
|
|
Usage: "Seconds to wait for stop before killing the container",
|
|
|
|
Value: defaultTimeout,
|
|
|
|
},
|
|
|
|
}
|
2017-09-14 13:56:33 +00:00
|
|
|
stopDescription = `
|
|
|
|
kpod stop
|
|
|
|
|
|
|
|
Stops one or more running containers. The container name or ID can be used.
|
|
|
|
A timeout to forcibly stop the container can also be set but defaults to 10
|
|
|
|
seconds otherwise.
|
|
|
|
`
|
|
|
|
|
|
|
|
stopCommand = cli.Command{
|
|
|
|
Name: "stop",
|
|
|
|
Usage: "Stop one or more containers",
|
2017-09-06 17:30:34 +00:00
|
|
|
Description: stopDescription,
|
|
|
|
Flags: stopFlags,
|
|
|
|
Action: stopCmd,
|
2017-09-14 13:56:33 +00:00
|
|
|
ArgsUsage: "CONTAINER-NAME [CONTAINER-NAME ...]",
|
2017-09-06 17:30:34 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func stopCmd(c *cli.Context) error {
|
|
|
|
args := c.Args()
|
|
|
|
stopTimeout := c.Int64("timeout")
|
|
|
|
if len(args) < 1 {
|
|
|
|
return errors.Errorf("you must provide at least one container name or id")
|
|
|
|
}
|
2017-09-28 18:44:48 +00:00
|
|
|
if err := validateFlags(c, stopFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-06 17:30:34 +00:00
|
|
|
|
|
|
|
config, err := getConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get config")
|
|
|
|
}
|
|
|
|
server, err := libkpod.New(config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get container server")
|
|
|
|
}
|
|
|
|
defer server.Shutdown()
|
|
|
|
err = server.Update()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not update list of containers")
|
|
|
|
}
|
2017-09-14 13:56:33 +00:00
|
|
|
var lastError error
|
2017-09-06 17:30:34 +00:00
|
|
|
for _, container := range c.Args() {
|
|
|
|
cid, err := server.ContainerStop(container, stopTimeout)
|
|
|
|
if err != nil {
|
2017-09-14 13:56:33 +00:00
|
|
|
if lastError != nil {
|
2017-09-16 09:18:19 +00:00
|
|
|
fmt.Fprintln(os.Stderr, lastError)
|
2017-09-14 13:56:33 +00:00
|
|
|
}
|
2017-09-16 09:18:19 +00:00
|
|
|
lastError = errors.Wrapf(err, "failed to stop container %v", container)
|
2017-09-06 17:30:34 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(cid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:56:33 +00:00
|
|
|
return lastError
|
2017-09-06 17:30:34 +00:00
|
|
|
}
|