From b79a3dee26130f54b87ddba8a3051412a4c20751 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 7 Nov 2023 12:19:53 -0300 Subject: [PATCH 1/2] Call service stop for each service first --- cli/cli/commands/enclave/stop/stop.go | 24 ++++++++++++++++++++++++ cli/cli/commands/service/stop/stop.go | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cli/cli/commands/enclave/stop/stop.go b/cli/cli/commands/enclave/stop/stop.go index 43e4191693..04101c361a 100644 --- a/cli/cli/commands/enclave/stop/stop.go +++ b/cli/cli/commands/enclave/stop/stop.go @@ -4,11 +4,13 @@ import ( "context" "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/cli/cli/commands/service/stop" "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface" metrics_client "github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/client" "github.com/kurtosis-tech/stacktrace" @@ -66,6 +68,28 @@ func run( if err = metricsClient.TrackStopEnclave(enclaveIdentifier); err != nil { logrus.Warnf("An error occurred while logging the stop enclave event for enclave '%v'", enclaveIdentifier) } + + kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine() + if err != nil { + return stacktrace.Propagate(err, "An error occurred creating Kurtosis Context from local engine") + } + + enclaveCtx, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier) + if err != nil { + return stacktrace.Propagate(err, "An error occurred getting an enclave context from enclave info for enclave '%v'", enclaveIdentifier) + } + + allEnclaveServices, err := enclaveCtx.GetServices() + if err != nil { + return stacktrace.Propagate(err, "TO COMPLETE") //TODO complete + } + + for serviceName := range allEnclaveServices { + if err := stop.StopServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { + return stacktrace.Propagate(err, "TO COMPLETE") //TODO complete + } + } + if _, err := engineClient.StopEnclave(ctx, stopArgs); err != nil { wrappedErr := stacktrace.Propagate(err, "An error occurred stopping enclave '%v'", enclaveIdentifier) stopEnclaveErrorStrs = append(stopEnclaveErrorStrs, wrappedErr.Error()) diff --git a/cli/cli/commands/service/stop/stop.go b/cli/cli/commands/service/stop/stop.go index 33844a1115..8bd7165dcf 100644 --- a/cli/cli/commands/service/stop/stop.go +++ b/cli/cli/commands/service/stop/stop.go @@ -99,14 +99,14 @@ func run( serviceName := serviceContext.GetServiceName() - if err := stopServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { + if err := StopServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { return stacktrace.Propagate(err, "An error occurred stopping service '%v' from enclave '%v'", serviceIdentifier, enclaveIdentifier) } } return nil } -func stopServiceStarlarkCommand(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, serviceName services.ServiceName) error { +func StopServiceStarlarkCommand(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, serviceName services.ServiceName) error { params := fmt.Sprintf(`{"service_name": "%s"}`, serviceName) runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, starlarkScript, starlark_run_config.NewRunStarlarkConfig(starlark_run_config.WithSerializedParams(params))) if err != nil { From 559d4d3c578add5864802365aabdd05984961ba4 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 7 Nov 2023 17:24:17 -0300 Subject: [PATCH 2/2] move script call into a helper --- cli/cli/commands/enclave/stop/stop.go | 47 +++++++++++-------- cli/cli/commands/service/stop/stop.go | 30 +----------- .../shared_starlark_calls.go | 35 ++++++++++++++ 3 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 cli/cli/helpers/shared_starlark_calls/shared_starlark_calls.go diff --git a/cli/cli/commands/enclave/stop/stop.go b/cli/cli/commands/enclave/stop/stop.go index 04101c361a..05bfd294af 100644 --- a/cli/cli/commands/enclave/stop/stop.go +++ b/cli/cli/commands/enclave/stop/stop.go @@ -10,7 +10,7 @@ import ( "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/cli/cli/commands/service/stop" + "github.com/kurtosis-tech/kurtosis/cli/cli/helpers/shared_starlark_calls" "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface" metrics_client "github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/client" "github.com/kurtosis-tech/stacktrace" @@ -69,25 +69,8 @@ func run( logrus.Warnf("An error occurred while logging the stop enclave event for enclave '%v'", enclaveIdentifier) } - kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine() - if err != nil { - return stacktrace.Propagate(err, "An error occurred creating Kurtosis Context from local engine") - } - - enclaveCtx, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier) - if err != nil { - return stacktrace.Propagate(err, "An error occurred getting an enclave context from enclave info for enclave '%v'", enclaveIdentifier) - } - - allEnclaveServices, err := enclaveCtx.GetServices() - if err != nil { - return stacktrace.Propagate(err, "TO COMPLETE") //TODO complete - } - - for serviceName := range allEnclaveServices { - if err := stop.StopServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { - return stacktrace.Propagate(err, "TO COMPLETE") //TODO complete - } + if err = stopAllEnclaveServices(ctx, enclaveIdentifier); err != nil { + return stacktrace.Propagate(err, "An error occurred stopping all enclave services") } if _, err := engineClient.StopEnclave(ctx, stopArgs); err != nil { @@ -112,3 +95,27 @@ func run( return nil } + +func stopAllEnclaveServices(ctx context.Context, enclaveIdentifier string) error { + kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine() + if err != nil { + return stacktrace.Propagate(err, "An error occurred creating Kurtosis Context from local engine") + } + + enclaveCtx, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier) + if err != nil { + return stacktrace.Propagate(err, "An error occurred getting an enclave context from enclave info for enclave '%v'", enclaveIdentifier) + } + + allEnclaveServices, err := enclaveCtx.GetServices() + if err != nil { + return stacktrace.Propagate(err, "An error occurred getting all enclave services") + } + + for serviceName := range allEnclaveServices { + if err := shared_starlark_calls.StopServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { + return stacktrace.Propagate(err, "An error occurred stopping service '%s'", serviceName) + } + } + return nil +} diff --git a/cli/cli/commands/service/stop/stop.go b/cli/cli/commands/service/stop/stop.go index 8bd7165dcf..065bd9f1b4 100644 --- a/cli/cli/commands/service/stop/stop.go +++ b/cli/cli/commands/service/stop/stop.go @@ -2,10 +2,6 @@ package stop import ( "context" - "fmt" - "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves" - "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/services" - "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config" "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" @@ -14,6 +10,7 @@ import ( "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/cli/cli/helpers/shared_starlark_calls" "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface" metrics_client "github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/client" "github.com/kurtosis-tech/stacktrace" @@ -31,11 +28,6 @@ const ( kurtosisBackendCtxKey = "kurtosis-backend" engineClientCtxKey = "engine-client" - - starlarkScript = ` -def run(plan, args): - plan.stop_service(name=args["service_name"]) -` ) var ServiceStopCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCommand{ @@ -99,27 +91,9 @@ func run( serviceName := serviceContext.GetServiceName() - if err := StopServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { + if err := shared_starlark_calls.StopServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { return stacktrace.Propagate(err, "An error occurred stopping service '%v' from enclave '%v'", serviceIdentifier, enclaveIdentifier) } } return nil } - -func StopServiceStarlarkCommand(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, serviceName services.ServiceName) error { - params := fmt.Sprintf(`{"service_name": "%s"}`, serviceName) - runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, starlarkScript, starlark_run_config.NewRunStarlarkConfig(starlark_run_config.WithSerializedParams(params))) - if err != nil { - return stacktrace.Propagate(err, "An unexpected error occurred on Starlark for stopping service") - } - if runResult.ExecutionError != nil { - return stacktrace.NewError("An error occurred during Starlark script execution for stopping service: %s", runResult.ExecutionError.GetErrorMessage()) - } - if runResult.InterpretationError != nil { - return stacktrace.NewError("An error occurred during Starlark script interpretation for stopping service: %s", runResult.InterpretationError.GetErrorMessage()) - } - if len(runResult.ValidationErrors) > 0 { - return stacktrace.NewError("An error occurred during Starlark script validation for stopping service: %v", runResult.ValidationErrors) - } - return nil -} diff --git a/cli/cli/helpers/shared_starlark_calls/shared_starlark_calls.go b/cli/cli/helpers/shared_starlark_calls/shared_starlark_calls.go new file mode 100644 index 0000000000..370d5ab105 --- /dev/null +++ b/cli/cli/helpers/shared_starlark_calls/shared_starlark_calls.go @@ -0,0 +1,35 @@ +package shared_starlark_calls + +import ( + "context" + "fmt" + "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves" + "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/services" + "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config" + "github.com/kurtosis-tech/stacktrace" +) + +const ( + stopServiceStarlarkScript = ` +def run(plan, args): + plan.stop_service(name=args["service_name"]) +` +) + +func StopServiceStarlarkCommand(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, serviceName services.ServiceName) error { + params := fmt.Sprintf(`{"service_name": "%s"}`, serviceName) + runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, stopServiceStarlarkScript, starlark_run_config.NewRunStarlarkConfig(starlark_run_config.WithSerializedParams(params))) + if err != nil { + return stacktrace.Propagate(err, "An unexpected error occurred on Starlark for stopping service") + } + if runResult.ExecutionError != nil { + return stacktrace.NewError("An error occurred during Starlark script execution for stopping service: %s", runResult.ExecutionError.GetErrorMessage()) + } + if runResult.InterpretationError != nil { + return stacktrace.NewError("An error occurred during Starlark script interpretation for stopping service: %s", runResult.InterpretationError.GetErrorMessage()) + } + if len(runResult.ValidationErrors) > 0 { + return stacktrace.NewError("An error occurred during Starlark script validation for stopping service: %v", runResult.ValidationErrors) + } + return nil +}