...
1
2
3 package main
4
5 import (
6 "fmt"
7 "os"
8
9 "github.com/Microsoft/hcsshim/internal/appargs"
10 "github.com/Microsoft/hcsshim/internal/regstate"
11 "github.com/urfave/cli"
12 )
13
14 var deleteCommand = cli.Command{
15 Name: "delete",
16 Usage: "delete any resources held by the container often used with detached container",
17 ArgsUsage: `<container-id>
18
19 Where "<container-id>" is the name for the instance of the container.
20
21 EXAMPLE:
22 For example, if the container id is "ubuntu01" and runhcs list currently shows the
23 status of "ubuntu01" as "stopped" the following will delete resources held for
24 "ubuntu01" removing "ubuntu01" from the runhcs list of containers:
25
26 # runhcs delete ubuntu01`,
27 Flags: []cli.Flag{
28 cli.BoolFlag{
29 Name: "force, f",
30 Usage: "Forcibly deletes the container if it is still running (uses SIGKILL)",
31 },
32 },
33 Before: appargs.Validate(argID),
34 Action: func(context *cli.Context) error {
35 id := context.Args().First()
36 force := context.Bool("force")
37 container, err := getContainer(id, false)
38 if err != nil {
39 if _, ok := err.(*regstate.NoStateError); ok {
40 if e := stateKey.Remove(id); e != nil {
41 fmt.Fprintf(os.Stderr, "remove %s: %v\n", id, e)
42 }
43 if force {
44 return nil
45 }
46 }
47 return err
48 }
49 defer container.Close()
50 s, err := container.Status()
51 if err != nil {
52 return err
53 }
54
55 kill := false
56 switch s {
57 case containerStopped:
58 case containerCreated:
59 kill = true
60 default:
61 if !force {
62 return fmt.Errorf("cannot delete container %s that is not stopped: %s", id, s)
63 }
64 kill = true
65 }
66
67 if kill {
68 err = container.Kill()
69 if err != nil {
70 return err
71 }
72 }
73 return container.Remove()
74 },
75 }
76
View as plain text