-
Notifications
You must be signed in to change notification settings - Fork 38
/
rm.go
176 lines (154 loc) · 6.01 KB
/
rm.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package rm
import (
"context"
"errors"
"fmt"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/kurtosis_engine_rpc_api_bindings"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/kurtosis_context"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/highlevel/enclave_id_arg"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/highlevel/engine_consuming_kurtosis_command"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
metrics_client "github.com/kurtosis-tech/metrics-library/golang/lib/client"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"sort"
"strings"
)
const (
enclaveIdentifiersArgKey = "enclaves"
isEnclaveIdArgOptional = false
isEnclaveIdArgGreedy = true
shouldForceRemoveFlagKey = "force"
defaultShouldForceRemove = "false"
kurtosisBackendCtxKey = "kurtosis-backend"
engineClientCtxKey = "engine-client"
)
var EnclaveRmCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCommand{
CommandStr: command_str_consts.EnclaveRmCmdStr,
ShortDescription: "Destroys the specified enclaves",
LongDescription: "Destroys the specified enclaves, removing all resources associated with them",
KurtosisBackendContextKey: kurtosisBackendCtxKey,
EngineClientContextKey: engineClientCtxKey,
Flags: []*flags.FlagConfig{
{
Key: shouldForceRemoveFlagKey,
Usage: "Deletes all enclaves, regardless of whether they're already stopped",
Shorthand: "f",
Type: flags.FlagType_Bool,
Default: defaultShouldForceRemove,
},
},
Args: []*args.ArgConfig{
enclave_id_arg.NewEnclaveIdentifierArg(
enclaveIdentifiersArgKey,
engineClientCtxKey,
isEnclaveIdArgOptional,
isEnclaveIdArgGreedy,
),
},
RunFunc: run,
}
func run(
ctx context.Context,
_ backend_interface.KurtosisBackend,
_ kurtosis_engine_rpc_api_bindings.EngineServiceClient,
metricsClient metrics_client.MetricsClient,
flags *flags.ParsedFlags,
args *args.ParsedArgs,
) error {
enclaveIdentifiers, err := args.GetGreedyArg(enclaveIdentifiersArgKey)
if err != nil {
return stacktrace.Propagate(err, "Expected a value for greedy enclave identifier arg '%v' but none was found; this is a bug in the Kurtosis CLI!", enclaveIdentifiersArgKey)
}
shouldForceRemove, err := flags.GetBool(shouldForceRemoveFlagKey)
if err != nil {
return stacktrace.Propagate(err, "An error occurred getting the force-removal flag value using key '%v'; this is a bug in Kurtosis!", shouldForceRemoveFlagKey)
}
kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine()
if err != nil {
return stacktrace.Propagate(err, "An error occurred creating Kurtosis Context from local engine")
}
logrus.Debugf("inputted enclave UUIDs: %+v", enclaveIdentifiers)
// Condense the enclave UUIDs down into a unique set, so we don't try to double-destroy an enclave
enclaveIdsToDestroy := getUniqueSortedEnclaveIdentifiers(enclaveIdentifiers)
logrus.Debugf("Unique enclave UUIDs to destroy: %+v", enclaveIdsToDestroy)
logrus.Info("Destroying enclaves...")
enclaveDestructionErrorStrs := []string{}
for _, enclaveId := range enclaveIdsToDestroy {
if err = metricsClient.TrackDestroyEnclave(enclaveId); err != nil {
logrus.Warnf("An error occurred while logging the destroy enclave event for enclave '%v'", enclaveId)
}
if err := destroyEnclave(ctx, kurtosisCtx, enclaveId, shouldForceRemove); err != nil {
enclaveDestructionErrorStrs = append(
enclaveDestructionErrorStrs,
fmt.Sprintf(
">>>>>>>>>>>>>>>>> %v <<<<<<<<<<<<<<<<<\n%v",
enclaveId,
err.Error(),
),
)
}
}
if len(enclaveDestructionErrorStrs) > 0 {
errorStr := fmt.Sprintf(
"One or more errors occurred destroying the enclaves:\n%v",
strings.Join(enclaveDestructionErrorStrs, "\n\n"),
)
return errors.New(errorStr)
}
logrus.Info("Enclaves successfully destroyed")
return nil
}
// ====================================================================================================
//
// Private helper methods
//
// ====================================================================================================
func getUniqueSortedEnclaveIdentifiers(rawInput []string) []string {
uniqueEnclaveIds := map[string]bool{}
for _, inputId := range rawInput {
uniqueEnclaveIds[inputId] = true
}
result := []string{}
for inputId := range uniqueEnclaveIds {
result = append(result, inputId)
}
sort.Strings(result)
return result
}
func destroyEnclave(
ctx context.Context,
kurtosisContext *kurtosis_context.KurtosisContext,
enclaveIdentifier string,
shouldForceRemove bool,
) error {
enclaveInfo, err := kurtosisContext.GetEnclave(ctx, enclaveIdentifier)
if err != nil {
return stacktrace.NewError("No enclave '%v' exists", enclaveIdentifier)
}
enclaveStatus := enclaveInfo.ContainersStatus
var isEnclaveRemovableWithoutForce bool
switch enclaveStatus {
case kurtosis_engine_rpc_api_bindings.EnclaveContainersStatus_EnclaveContainersStatus_EMPTY, kurtosis_engine_rpc_api_bindings.EnclaveContainersStatus_EnclaveContainersStatus_STOPPED:
isEnclaveRemovableWithoutForce = true
case kurtosis_engine_rpc_api_bindings.EnclaveContainersStatus_EnclaveContainersStatus_RUNNING:
isEnclaveRemovableWithoutForce = false
default:
return stacktrace.NewError("Unrecognized enclave status '%v'; this is a bug in Kurtosis", enclaveStatus)
}
if !shouldForceRemove && !isEnclaveRemovableWithoutForce {
return stacktrace.NewError(
"Refusing to destroy enclave '%v' because its status is '%v'; to force its removal, rerun this command with the '%v' flag",
enclaveIdentifier,
enclaveStatus,
shouldForceRemoveFlagKey,
)
}
if err = kurtosisContext.DestroyEnclave(ctx, enclaveIdentifier); err != nil {
return stacktrace.Propagate(err, "An error occurred destroying enclave '%v'", enclaveIdentifier)
}
return nil
}