2017-02-01 00:45:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-06 20:16:36 +00:00
|
|
|
"fmt"
|
2017-02-01 00:45:59 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2017-02-06 20:16:36 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-02-01 00:45:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var startCommand = cli.Command{
|
|
|
|
Name: "start",
|
2017-02-06 20:16:36 +00:00
|
|
|
Usage: "executes the user defined process in a created container",
|
|
|
|
ArgsUsage: `<container-id> [container-id...]
|
2017-02-01 00:45:59 +00:00
|
|
|
|
|
|
|
Where "<container-id>" is your name for the instance of the container that you
|
|
|
|
are starting. The name you provide for the container instance must be unique on
|
|
|
|
your host.`,
|
2017-02-06 20:16:36 +00:00
|
|
|
Description: `The start command executes the user defined process in a created container .`,
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
if err := checkArgs(context, 1, minArgs); err != nil {
|
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
hasError := false
|
|
|
|
factory, err := loadFactory(context)
|
2017-02-01 00:45:59 +00:00
|
|
|
if err != nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 20:16:36 +00:00
|
|
|
for _, id := range context.Args() {
|
|
|
|
container, err := factory.Load(id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "container %s does not exist\n", id)
|
|
|
|
hasError = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "status for %s: %v\n", id, err)
|
|
|
|
hasError = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch status {
|
|
|
|
case libcontainer.Created:
|
|
|
|
if err := container.Exec(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "start for %s failed: %v\n", id, err)
|
|
|
|
hasError = true
|
|
|
|
}
|
|
|
|
case libcontainer.Stopped:
|
|
|
|
fmt.Fprintln(os.Stderr, "cannot start a container that has run and stopped")
|
|
|
|
hasError = true
|
|
|
|
case libcontainer.Running:
|
|
|
|
fmt.Fprintln(os.Stderr, "cannot start an already running container")
|
|
|
|
hasError = true
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "cannot start a container in the %s state\n", status)
|
|
|
|
hasError = true
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 20:16:36 +00:00
|
|
|
if hasError {
|
|
|
|
return fmt.Errorf("one or more of container start failed")
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
return nil
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
}
|