Skip to content

Commit 6b8f696

Browse files
author
Mrunal Patel
authored
Merge pull request #1053 from datawolf/enhance-runc-delete
enhance runc delete command
2 parents 5653ced + d66ac3d commit 6b8f696

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

delete.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func killContainer(container libcontainer.Container) error {
2727

2828
var deleteCommand = cli.Command{
2929
Name: "delete",
30-
Usage: "delete any resources held by the container often used with detached containers",
31-
ArgsUsage: `<container-id>
30+
Usage: "delete any resources held by one container or more containers often used with detached containers",
31+
ArgsUsage: `container-id [container-id...]
3232
3333
Where "<container-id>" is the name for the instance of the container.
3434
@@ -45,32 +45,51 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
4545
},
4646
},
4747
Action: func(context *cli.Context) error {
48-
container, err := getContainer(context)
49-
if err != nil {
50-
if lerr, ok := err.(libcontainer.Error); ok && lerr.Code() == libcontainer.ContainerNotExists {
51-
// if there was an aborted start or something of the sort then the container's directory could exist but
52-
// libcontainer does not see it because the state.json file inside that directory was never created.
53-
path := filepath.Join(context.GlobalString("root"), context.Args().First())
54-
if err := os.RemoveAll(path); err != nil {
55-
return err
56-
}
57-
}
58-
return nil
48+
if !context.Args().Present() {
49+
return fmt.Errorf("runc: \"delete\" requires a minimum of 1 argument")
5950
}
60-
s, err := container.Status()
51+
52+
factory, err := loadFactory(context)
6153
if err != nil {
6254
return err
6355
}
64-
switch s {
65-
case libcontainer.Stopped:
66-
destroy(container)
67-
case libcontainer.Created:
68-
return killContainer(container)
69-
default:
70-
if context.Bool("force") {
71-
return killContainer(container)
56+
for _, id := range context.Args() {
57+
container, err := factory.Load(id)
58+
if err != nil {
59+
if lerr, ok := err.(libcontainer.Error); ok && lerr.Code() == libcontainer.ContainerNotExists {
60+
// if there was an aborted start or something of the sort then the container's directory could exist but
61+
// libcontainer does not see it because the state.json file inside that directory was never created.
62+
path := filepath.Join(context.GlobalString("root"), id)
63+
if err := os.RemoveAll(path); err != nil {
64+
fmt.Fprintf(os.Stderr, "remove %s: %v\n", path, err)
65+
}
66+
fmt.Fprintf(os.Stderr, "container %s is not exist\n", id)
67+
}
68+
continue
69+
}
70+
s, err := container.Status()
71+
if err != nil {
72+
fmt.Fprintf(os.Stderr, "status for %s: %v\n", id, err)
73+
continue
74+
}
75+
switch s {
76+
case libcontainer.Stopped:
77+
destroy(container)
78+
case libcontainer.Created:
79+
err := killContainer(container)
80+
if err != nil {
81+
fmt.Fprintf(os.Stderr, "kill container %s: %v\n", id, err)
82+
}
83+
default:
84+
if context.Bool("force") {
85+
err := killContainer(container)
86+
if err != nil {
87+
fmt.Fprintf(os.Stderr, "kill container %s: %v\n", id, err)
88+
}
89+
} else {
90+
fmt.Fprintf(os.Stderr, "cannot delete container %s that is not stopped: %s\n", id, s)
91+
}
7292
}
73-
return fmt.Errorf("cannot delete container that is not stopped: %s", s)
7493
}
7594
return nil
7695
},

0 commit comments

Comments
 (0)