-
Notifications
You must be signed in to change notification settings - Fork 63
/
delete.go
113 lines (96 loc) · 2.68 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package delete
import (
"context"
"fmt"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
"kraftkit.sh/cmdfactory"
libcontainer "kraftkit.sh/libmocktainer"
"kraftkit.sh/log"
)
const (
flagRoot = "root"
)
// DeleteOptions implements the OCI "delete" command.
type DeleteOptions struct {
Force bool `long:"force" short:"f" usage:"forcibly delete the unikernel if it is still running"`
rootDir string
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&DeleteOptions{}, cobra.Command{
Short: "Delete a unikernel",
Args: cobra.ExactArgs(1),
Aliases: []string{"del"},
Use: "delete <unikernel-id>",
Long: "The delete command deletes any resources held by a unikernel.",
Example: heredoc.Doc(`
# Delete a unikernel
$ runu delete my-unikernel
`),
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *DeleteOptions) Pre(cmd *cobra.Command, args []string) error {
opts.rootDir = cmd.Flag(flagRoot).Value.String()
if opts.rootDir == "" {
return fmt.Errorf("state directory (--%s flag) is not set", flagRoot)
}
return nil
}
func (opts *DeleteOptions) Run(ctx context.Context, args []string) (retErr error) {
defer func() {
// Make sure the error is written to the configured log destination, so
// that the message gets propagated through the caller (e.g. containerd-shim)
if retErr != nil {
log.G(ctx).Error(retErr)
}
}()
cID := args[0]
c, err := libcontainer.Load(opts.rootDir, cID)
if err != nil {
return fmt.Errorf("loading container from saved state: %w", err)
}
st, err := c.Status()
if err != nil {
return fmt.Errorf("getting container status: %w", err)
}
switch st {
case libcontainer.Stopped:
return destroyContainer(c)
case libcontainer.Created:
return killContainer(c)
default:
if opts.Force {
return killContainer(c)
}
return fmt.Errorf("container is not stopped: %s", st)
}
}
// destroyContainer destroys the given container.
func destroyContainer(c *libcontainer.Container) error {
if err := c.Destroy(); err != nil {
return fmt.Errorf("destroying container: %w", err)
}
return nil
}
// killContainer sends a SIGKILL to the container process.
func killContainer(c *libcontainer.Container) error {
_ = c.Signal(unix.SIGKILL)
for i := 0; i < 100; i++ {
time.Sleep(100 * time.Millisecond)
if err := c.Signal(unix.Signal(0)); err != nil {
_ = destroyContainer(c)
return nil
}
}
return fmt.Errorf("container init still running")
}