-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added a way to restart services #1920
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
db4e63d
added service restart
h4ck3rk3y 0c99f32
added to the cli
h4ck3rk3y 46b62ea
added restart to test
h4ck3rk3y 872ab97
change text
h4ck3rk3y 906c765
fix more words
h4ck3rk3y f3d8db8
Update cli/cli/commands/service/restart/restart.go
h4ck3rk3y 6b004df
Update cli/cli/commands/service/restart/restart.go
h4ck3rk3y afbf377
Update cli/cli/commands/service/restart/restart.go
h4ck3rk3y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package restart | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config" | ||
"github.com/sirupsen/logrus" | ||
|
||
"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/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/highlevel/service_identifier_arg" | ||
"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" | ||
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client" | ||
"github.com/kurtosis-tech/stacktrace" | ||
) | ||
|
||
const ( | ||
enclaveIdentifierArgKey = "enclave" | ||
isEnclaveIdArgOptional = false | ||
isEnclaveIdArgGreedy = false | ||
|
||
serviceIdentifierArgKey = "service" | ||
isServiceIdentifierArgOptional = false | ||
isServiceIdentifierArgGreedy = true | ||
|
||
kurtosisBackendCtxKey = "kurtosis-backend" | ||
engineClientCtxKey = "engine-client" | ||
|
||
starlarkScript = ` | ||
def run(plan, args): | ||
plan.stop_service(name=args["service_name"]) | ||
plan.start_service(name=args["service_name"]) | ||
` | ||
) | ||
|
||
var ServiceRestartCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCommand{ | ||
CommandStr: command_str_consts.ServiceRestartCmdStr, | ||
ShortDescription: "Restarts one or many services", | ||
LongDescription: "Restarts services with the given service identifiers in the given enclave", | ||
KurtosisBackendContextKey: kurtosisBackendCtxKey, | ||
EngineClientContextKey: engineClientCtxKey, | ||
Args: []*args.ArgConfig{ | ||
enclave_id_arg.NewEnclaveIdentifierArg( | ||
enclaveIdentifierArgKey, | ||
engineClientCtxKey, | ||
isEnclaveIdArgOptional, | ||
isEnclaveIdArgGreedy, | ||
), | ||
service_identifier_arg.NewServiceIdentifierArg( | ||
serviceIdentifierArgKey, | ||
enclaveIdentifierArgKey, | ||
isServiceIdentifierArgOptional, | ||
isServiceIdentifierArgGreedy, | ||
), | ||
}, | ||
Flags: []*flags.FlagConfig{}, | ||
RunFunc: run, | ||
} | ||
|
||
func run( | ||
ctx context.Context, | ||
_ backend_interface.KurtosisBackend, | ||
_ kurtosis_engine_rpc_api_bindings.EngineServiceClient, | ||
_ metrics_client.MetricsClient, | ||
_ *flags.ParsedFlags, | ||
args *args.ParsedArgs, | ||
) error { | ||
enclaveIdentifier, err := args.GetNonGreedyArg(enclaveIdentifierArgKey) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred getting the enclave identifier value using key '%v'", enclaveIdentifierArgKey) | ||
} | ||
|
||
serviceIdentifiers, err := args.GetGreedyArg(serviceIdentifierArgKey) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred getting the service identifiers values using key '%v'", serviceIdentifierArgKey) | ||
} | ||
|
||
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) | ||
} | ||
|
||
for _, serviceIdentifier := range serviceIdentifiers { | ||
logrus.Infof("Restarting service '%v'", serviceIdentifier) | ||
serviceContext, err := enclaveCtx.GetServiceContext(serviceIdentifier) | ||
if err != nil { | ||
return stacktrace.NewError("Couldn't validate whether the service exists for identifier '%v'", serviceIdentifier) | ||
} | ||
|
||
serviceName := serviceContext.GetServiceName() | ||
|
||
if err := restartServiceStarlarkCommand(ctx, enclaveCtx, serviceName); err != nil { | ||
return stacktrace.Propagate(err, "An error occurred restarting service '%v' from enclave '%v'", serviceIdentifier, enclaveIdentifier) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func restartServiceStarlarkCommand(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, serviceName services.ServiceName) error { | ||
serviceNameString := fmt.Sprintf(`{"service_name": "%s"}`, serviceName) | ||
runResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, starlarkScript, starlark_run_config.NewRunStarlarkConfig(starlark_run_config.WithSerializedParams(serviceNameString))) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An unexpected error occurred on Starlark for restarting service") | ||
} | ||
if runResult.ExecutionError != nil { | ||
return stacktrace.NewError("An error occurred during Starlark script execution for restarting service: %s", runResult.ExecutionError.GetErrorMessage()) | ||
} | ||
if runResult.InterpretationError != nil { | ||
return stacktrace.NewError("An error occurred during Starlark script interpretation for restarting service: %s", runResult.InterpretationError.GetErrorMessage()) | ||
} | ||
if len(runResult.ValidationErrors) > 0 { | ||
return stacktrace.NewError("An error occurred during Starlark script validation for restarting service: %v", runResult.ValidationErrors) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
title: service restart | ||
sidebar_label: service restart | ||
slug: /service-restart | ||
--- | ||
|
||
Services can be restarted like so: | ||
|
||
```bash | ||
kurtosis service restart $THE_ENCLAVE_IDENTIFIER $THE_SERVICE_IDENTIFIER | ||
``` | ||
|
||
where `$THE_ENCLAVE_IDENTIFIER` and the `$THE_SERVICE_IDENTIFIER` are [resource identifiers](../advanced-concepts/resource-identifier.md) for the enclave and service, respectively. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we mention that a user can pass more than one service identifier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do that!