Skip to content

Commit

Permalink
Require containerd id as arg 1
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
crosbymichael committed Feb 9, 2016
1 parent 8e8d01d commit a7278ca
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var killCommand = cli.Command{
fatal(err)
}

sigstr := context.Args().First()
sigstr := context.Args().Get(1)
if sigstr == "" {
sigstr = "SIGTERM"
}
Expand Down
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
Expand All @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions main_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"github.com/codegangsta/cli"
)

func getDefaultID() string {
return ""
}

var (
checkpointCommand cli.Command
eventsCommand cli.Command
Expand Down
17 changes: 12 additions & 5 deletions restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
24 changes: 8 additions & 16 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -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
{
Expand Down Expand Up @@ -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) {
factory, err := loadFactory(context)
if err != nil {
return nil, err
id := context.Args().First()
if id == "" {
return nil, errEmptyID
}
container, err := factory.Load(context.GlobalString("id"))
factory, err := loadFactory(context)
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
Expand All @@ -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 {
Expand Down

0 comments on commit a7278ca

Please sign in to comment.