2016-05-14 07:54:16 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-08 10:02:39 +08:00
|
|
|
"errors"
|
2016-05-26 02:24:26 +08:00
|
|
|
"fmt"
|
2018-05-26 00:04:06 +08:00
|
|
|
"os"
|
2016-05-26 02:24:26 +08:00
|
|
|
|
2016-05-18 01:25:28 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2016-05-14 07:54:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var startCommand = cli.Command{
|
|
|
|
Name: "start",
|
2016-06-12 20:38:45 +08:00
|
|
|
Usage: "executes the user defined process in a created container",
|
2017-03-08 10:02:39 +08:00
|
|
|
ArgsUsage: `<container-id>
|
2016-05-14 07:54:16 +08: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-03-08 10:02:39 +08:00
|
|
|
Description: `The start command executes the user defined process in a created container.`,
|
2016-05-26 02:24:26 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2017-03-08 10:02:39 +08:00
|
|
|
if err := checkArgs(context, 1, exactArgs); err != nil {
|
2016-10-28 23:43:10 +08:00
|
|
|
return err
|
2016-05-14 07:54:16 +08:00
|
|
|
}
|
2017-03-08 10:02:39 +08:00
|
|
|
container, err := getContainer(context)
|
2016-05-18 01:25:28 +08:00
|
|
|
if err != nil {
|
2016-05-26 02:24:26 +08:00
|
|
|
return err
|
2016-05-14 07:54:16 +08:00
|
|
|
}
|
2017-03-08 10:02:39 +08:00
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-23 17:02:53 +08:00
|
|
|
}
|
2017-03-08 10:02:39 +08:00
|
|
|
switch status {
|
|
|
|
case libcontainer.Created:
|
2018-05-26 00:04:06 +08:00
|
|
|
notifySocket, err := notifySocketStart(context, os.Getenv("NOTIFY_SOCKET"), container.ID())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := container.Exec(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if notifySocket != nil {
|
|
|
|
return notifySocket.waitForContainer(container)
|
|
|
|
}
|
|
|
|
return nil
|
2017-03-08 10:02:39 +08:00
|
|
|
case libcontainer.Stopped:
|
|
|
|
return errors.New("cannot start a container that has stopped")
|
|
|
|
case libcontainer.Running:
|
|
|
|
return errors.New("cannot start an already running container")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("cannot start a container in the %s state\n", status)
|
2016-05-18 01:25:28 +08:00
|
|
|
}
|
2016-05-14 07:54:16 +08:00
|
|
|
},
|
|
|
|
}
|