Require containerd id as arg 1

Closes #532

This requires the container id to always be passed to all runc commands
as arg one on the cli.  This was the result of the last OCI meeting and
how operations work with the spec.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-02-08 14:25:03 -08:00
parent 8e8d01d38d
commit a7278cad98
8 changed files with 36 additions and 40 deletions

View File

@ -65,9 +65,11 @@ You can also run specific test cases by:
### Using:
To run a container, execute `runc start` in the bundle's root directory:
To run a container with the id "test", execute `runc start` with the containers id as arg one
in the bundle's root directory:
```bash
runc start
runc start test
/ $ ps
PID USER COMMAND
1 daemon sh
@ -98,7 +100,7 @@ tar -C rootfs -xf busybox.tar
* Create `config.json` by using `runc spec`.
* Execute `runc start` and you should be placed into a shell where you can run `ps`:
```
$ runc start
$ runc start test
/ # ps
PID USER COMMAND
1 root sh
@ -120,7 +122,7 @@ After=network.target
[Service]
CPUQuota=200%
MemoryLimit=1536M
ExecStart=/usr/local/bin/runc start
ExecStart=/usr/local/bin/runc start minecraft
Restart=on-failure
WorkingDirectory=/containers/minecraftbuild

View File

@ -128,7 +128,7 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
return nil, err
}
p := spec.Process
p.Args = context.Args()
p.Args = context.Args()[1:]
// override the cwd, if passed
if context.String("cwd") != "" {
p.Cwd = context.String("cwd")

View File

@ -58,7 +58,7 @@ var killCommand = cli.Command{
fatal(err)
}
sigstr := context.Args().First()
sigstr := context.Args().Get(1)
if sigstr == "" {
sigstr = "SIGTERM"
}

View File

@ -27,7 +27,7 @@ After creating config files for your root filesystem with runc, you can execute
a container in your shell by running:
# cd /mycontainer
# runc start [ -b bundle ]
# runc start [ -b bundle ] <container-id>
If not specified, the default value for the 'bundle' is the current directory.
'Bundle' is the directory where '` + specConfig + `' must be located.`
@ -39,11 +39,6 @@ func main() {
app.Usage = usage
app.Version = fmt.Sprintf("%s\nspec version %s", version, specs.Version)
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "id",
Value: getDefaultID(),
Usage: "specify the ID to be used for the container",
},
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output for logging",

View File

@ -7,10 +7,6 @@ import (
"github.com/codegangsta/cli"
)
func getDefaultID() string {
return ""
}
var (
checkpointCommand cli.Command
eventsCommand cli.Command

View File

@ -66,6 +66,10 @@ var restoreCommand = cli.Command{
},
Action: func(context *cli.Context) {
imagePath := context.String("image-path")
id := context.Args().First()
if id == "" {
fatal(errEmptyID)
}
if imagePath == "" {
imagePath = getDefaultImagePath(context)
}
@ -79,7 +83,7 @@ var restoreCommand = cli.Command{
if err != nil {
fatal(err)
}
config, err := createLibcontainerConfig(context.GlobalString("id"), spec)
config, err := createLibcontainerConfig(id, spec)
if err != nil {
fatal(err)
}
@ -92,14 +96,17 @@ var restoreCommand = cli.Command{
}
func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *configs.Config, imagePath string) (code int, err error) {
rootuid := 0
var (
rootuid = 0
id = context.Args().First()
)
factory, err := loadFactory(context)
if err != nil {
return -1, err
}
container, err := factory.Load(context.GlobalString("id"))
container, err := factory.Load(id)
if err != nil {
container, err = factory.Create(context.GlobalString("id"), config)
container, err = factory.Create(id, config)
if err != nil {
return -1, err
}
@ -111,7 +118,7 @@ func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *confi
logrus.Error(err)
}
if status == libcontainer.Running {
fatal(fmt.Errorf("Container with id %s already running", context.GlobalString("id")))
fatal(fmt.Errorf("Container with id %s already running", id))
}
setManageCgroupsMode(context, options)

View File

@ -93,13 +93,17 @@ func init() {
}
func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) {
config, err := createLibcontainerConfig(context.GlobalString("id"), spec)
id := context.Args().First()
if id == "" {
return -1, errEmptyID
}
config, err := createLibcontainerConfig(id, spec)
if err != nil {
return -1, err
}
if _, err := os.Stat(config.Rootfs); err != nil {
if os.IsNotExist(err) {
return -1, fmt.Errorf("Rootfs (%q) does not exist", config.Rootfs)
return -1, fmt.Errorf("rootfs (%q) does not exist", config.Rootfs)
}
return -1, err
}
@ -111,7 +115,7 @@ func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) {
if err != nil {
return -1, err
}
container, err := factory.Create(context.GlobalString("id"), config)
container, err := factory.Create(id, config)
if err != nil {
return -1, err
}

View File

@ -3,6 +3,7 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -17,6 +18,8 @@ import (
const wildcard = -1
var errEmptyID = errors.New("container id cannot be empty")
var allowedDevices = []*configs.Device{
// allow mknod for any device
{
@ -160,15 +163,15 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
// getContainer returns the specified container instance by loading it from state
// with the default factory.
func getContainer(context *cli.Context) (libcontainer.Container, error) {
id := context.Args().First()
if id == "" {
return nil, errEmptyID
}
factory, err := loadFactory(context)
if err != nil {
return nil, err
}
container, err := factory.Load(context.GlobalString("id"))
if err != nil {
return nil, err
}
return container, nil
return factory.Load(id)
}
// fatal prints the error's details if it is a libcontainer specific error type
@ -182,17 +185,6 @@ func fatal(err error) {
os.Exit(1)
}
// getDefaultID returns a string to be used as the container id based on the
// current working directory of the runc process. This function panics
// if the cwd is unable to be found based on a system error.
func getDefaultID() string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
return filepath.Base(cwd)
}
func getDefaultImagePath(context *cli.Context) string {
cwd, err := os.Getwd()
if err != nil {