-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuninstall.go
More file actions
79 lines (63 loc) · 2.06 KB
/
uninstall.go
File metadata and controls
79 lines (63 loc) · 2.06 KB
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
package main
import (
"fmt"
"log/slog"
"os"
"path"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spinframework/runtime-class-manager/internal/containerd"
"github.com/spinframework/runtime-class-manager/internal/shim"
)
// uninstallCmd represents the uninstall command.
var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall containerd shims",
Run: func(_ *cobra.Command, _ []string) {
rootFs := afero.NewOsFs()
hostFs := afero.NewBasePathFs(rootFs, config.Host.RootPath)
distro, err := DetectDistro(config, hostFs)
if err != nil {
slog.Error("failed to detect containerd config", "error", err)
os.Exit(1)
}
config.Runtime.ConfigPath = distro.ConfigPath
config.Runtime.Options, err = RuntimeOptions()
if err != nil {
slog.Error("failed to get runtime options", "error", err)
os.Exit(1)
}
if err := RunUninstall(config, rootFs, hostFs, distro.Restarter); err != nil {
slog.Error("failed to uninstall", "error", err)
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(uninstallCmd)
}
func RunUninstall(config Config, rootFs, hostFs afero.Fs, restarter containerd.Restarter) error {
slog.Info("uninstall called", "shim", config.Runtime.Name)
shimName := config.Runtime.Name
runtimeName := path.Join(config.RCM.Path, "bin", shimName)
containerdConfig := containerd.NewConfig(hostFs, config.Runtime.ConfigPath, restarter, config.Runtime.Options)
shimConfig := shim.NewConfig(rootFs, hostFs, config.RCM.AssetPath, config.RCM.Path)
binPath, err := shimConfig.Uninstall(shimName)
if err != nil {
return fmt.Errorf("failed to delete shim '%s': %w", runtimeName, err)
}
configChanged, err := containerdConfig.RemoveRuntime(binPath)
if err != nil {
return fmt.Errorf("failed to write containerd config for shim '%s': %w", runtimeName, err)
}
if !configChanged {
slog.Info("nothing changed, nothing more to do")
return nil
}
slog.Info("restarting containerd")
err = containerdConfig.RestartRuntime()
if err != nil {
return fmt.Errorf("failed to restart containerd: %w", err)
}
return nil
}