-
Notifications
You must be signed in to change notification settings - Fork 1
/
uninstall.go
97 lines (82 loc) · 2.34 KB
/
uninstall.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package workflow
import (
"errors"
"fmt"
"io/fs"
"path/filepath"
"github.com/ava-labs/avalanchego/database"
"github.com/spf13/afero"
"github.com/ava-labs/apm/storage"
"github.com/ava-labs/apm/types"
)
var _ Workflow = &Uninstall{}
func NewUninstall(config UninstallConfig) *Uninstall {
return &Uninstall{
name: config.Name,
repoAlias: config.RepoAlias,
plugin: config.Plugin,
vmStorage: config.VMStorage,
installedVMs: config.InstalledVMs,
fs: config.Fs,
pluginPath: config.PluginPath,
}
}
type UninstallConfig struct {
Name string
Plugin string
RepoAlias string
VMStorage storage.Storage[storage.Definition[types.VM]]
InstalledVMs storage.Storage[storage.InstallInfo]
Fs afero.Fs
PluginPath string
}
type Uninstall struct {
name string
plugin string
repoAlias string
vmStorage storage.Storage[storage.Definition[types.VM]]
installedVMs storage.Storage[storage.InstallInfo]
fs afero.Fs
pluginPath string
}
func (u Uninstall) Execute() error {
ok, err := u.installedVMs.Has([]byte(u.name))
if err != nil {
return err
}
if !ok {
fmt.Printf("VM %s is already not installed. Skipping.\n", u.name)
return nil
}
vm, err := u.vmStorage.Get([]byte(u.plugin))
if err == database.ErrNotFound {
// If we don't have the definition, provide a warning log. It's possible
// this used to exist and was removed for whatever reason. In that case,
// we should still remove it from our installation registry to unblock
// the user.
fmt.Printf("Virtual machine %s doesn't exist under the repository for %s. Continuing uninstall anyways...\n", u.plugin, u.repoAlias)
} else if err != nil {
return err
}
vmPath := filepath.Join(u.pluginPath, vm.Definition.GetID())
switch _, err := u.fs.Stat(vmPath); err {
case nil:
fmt.Printf("Deleting %s...\n", vmPath)
if err := u.fs.Remove(vmPath); err != nil {
return err
}
default:
if errors.Is(err, fs.ErrNotExist) {
fmt.Printf("%s doesn't exist already. Nothing to delete here.\n", vmPath)
} else {
return err
}
}
if err := u.installedVMs.Delete([]byte(u.name)); err != nil {
return err
}
fmt.Printf("Successfully uninstalled %s.\n", u.name)
return nil
}