Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description: With this change users can create a new cloud instance, by calling `kurtosis cloud add`. Callers must define the `KURTOSIS_CLOUD_API_KEY` env var. ## Is this change user facing? YES ## References (if applicable): <!-- Add relevant Github Issues, Discord threads, or other helpful information. -->
- Loading branch information
1 parent
828f366
commit 48aecd0
Showing
6 changed files
with
137 additions
and
42 deletions.
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,73 @@ | ||
package add | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/cloud" | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel" | ||
"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" | ||
cloudhelper "github.com/kurtosis-tech/kurtosis/cli/cli/helpers/cloud" | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_user_id_store" | ||
api "github.com/kurtosis-tech/kurtosis/cloud/api/golang/kurtosis_backend_server_rpc_api_bindings" | ||
"github.com/kurtosis-tech/stacktrace" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var AddCmd = &lowlevel.LowlevelKurtosisCommand{ | ||
CommandStr: command_str_consts.CloudAddCmdStr, | ||
ShortDescription: "Create a new Kurtosis Cloud instance", | ||
LongDescription: "Create a new remote Kurtosis Cloud instance", | ||
Flags: []*flags.FlagConfig{}, | ||
Args: nil, | ||
PreValidationAndRunFunc: nil, | ||
RunFunc: run, | ||
PostValidationAndRunFunc: nil, | ||
} | ||
|
||
func run(ctx context.Context, _ *flags.ParsedFlags, _ *args.ParsedArgs) error { | ||
logrus.Info("Creating a new remote Kurtosis Cloud instance") | ||
apiKey, err := cloudhelper.LoadApiKey() | ||
if err != nil { | ||
return stacktrace.Propagate(err, "Could not load an API Key. Check that it's defined using the "+ | ||
"%s env var and it's a valid (active) key", cloudhelper.KurtosisCloudApiKeyEnvVarArg) | ||
} | ||
|
||
// Use metrics id for now until we replace with a proper auth'd id: | ||
metricsUserIdStore := metrics_user_id_store.GetMetricsUserIDStore() | ||
metricsUserId, err := metricsUserIdStore.GetUserID() | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while getting the user's id") | ||
} | ||
|
||
cloudConfig, err := cloudhelper.GetCloudConfig() | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while loading the Cloud Config") | ||
} | ||
// Create the connection | ||
connectionStr := fmt.Sprintf("%s:%d", cloudConfig.ApiUrl, cloudConfig.Port) | ||
client, err := cloud.CreateCloudClient(connectionStr, cloudConfig.CertificateChain) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "Error building client for Kurtosis Cloud") | ||
} | ||
|
||
getConfigArgs := &api.CreateCloudInstanceConfigArgs{ | ||
ApiKey: *apiKey, | ||
UserId: metricsUserId, | ||
} | ||
result, err := client.CreateCloudInstance(ctx, getConfigArgs) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while calling the Kurtosis Cloud API") | ||
} | ||
|
||
instanceId := result.GetInstanceId() | ||
logrus.Infof("Success! The Kurtosis Cloud instance is being created with id: %s", instanceId) | ||
logrus.Infof("The Kurtosis cloud instance is currently being created and it should take about 2-3 mins to become ready. "+ | ||
"Once ready, load the instance by calling: kurtosis %s %s %s", | ||
command_str_consts.CloudCmdStr, | ||
command_str_consts.CloudLoadCmdStr, | ||
instanceId, | ||
) | ||
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
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,48 @@ | ||
package cloud | ||
|
||
import ( | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_config" | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_config/resolved_config" | ||
"github.com/kurtosis-tech/stacktrace" | ||
"github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
const ( | ||
KurtosisCloudApiKeyEnvVarArg = "KURTOSIS_CLOUD_API_KEY" | ||
) | ||
|
||
func LoadApiKey() (*string, error) { | ||
apiKey := os.Getenv(KurtosisCloudApiKeyEnvVarArg) | ||
if len(apiKey) < 1 { | ||
return nil, stacktrace.NewError("No API Key was found. An API Key must be provided as env var %s", KurtosisCloudApiKeyEnvVarArg) | ||
} | ||
logrus.Info("Successfully Loaded API Key...") | ||
return &apiKey, nil | ||
} | ||
|
||
func GetCloudConfig() (*resolved_config.KurtosisCloudConfig, error) { | ||
// Get the configuration | ||
kurtosisConfigStore := kurtosis_config.GetKurtosisConfigStore() | ||
configProvider := kurtosis_config.NewKurtosisConfigProvider(kurtosisConfigStore) | ||
kurtosisConfig, err := configProvider.GetOrInitializeConfig() | ||
if err != nil { | ||
return nil, stacktrace.Propagate(err, "Failed to get or initialize Kurtosis configuration") | ||
} | ||
if kurtosisConfig.GetCloudConfig() == nil { | ||
return nil, stacktrace.Propagate(err, "No cloud config was found. This is an internal Kurtosis error.") | ||
} | ||
cloudConfig := kurtosisConfig.GetCloudConfig() | ||
|
||
if cloudConfig.Port == 0 { | ||
cloudConfig.Port = resolved_config.DefaultCloudConfigPort | ||
} | ||
if len(cloudConfig.ApiUrl) < 1 { | ||
cloudConfig.ApiUrl = resolved_config.DefaultCloudConfigApiUrl | ||
} | ||
if len(cloudConfig.CertificateChain) < 1 { | ||
cloudConfig.CertificateChain = resolved_config.DefaultCertificateChain | ||
} | ||
|
||
return cloudConfig, nil | ||
} |