From bf31505b49277f4c27ea52ce2b688b9dcab5903f Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Mon, 23 Oct 2023 18:44:21 -0500 Subject: [PATCH] getting the args file from a URL --- .circleci/config.yml | 7 +++++ cli/cli/commands/run/run.go | 52 ++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7b65289a3f..8afa149a3f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -198,6 +198,9 @@ parameters: starlark-test-args-file-test-args-json-relative-path: type: string default: "internal_testsuites/starlark/ci_tests/simple_arg_test/args.json" + starlark-test-args-file-url: + type: string + default: "https://raw.githubusercontent.com/kurtosis-tech/sample-startosis-load/main/args.json" rendertemplate-cli-test-template-relative-path: type: string default: "internal_testsuites/resources/render_template_cli_test/template.txt" @@ -323,6 +326,7 @@ jobs: - "<< pipeline.parameters.file-artifacts-expander-image-filename >>" - "<< pipeline.parameters.starlark-test-args-file-test-main-star-relative-path >>" - "<< pipeline.parameters.starlark-test-args-file-test-args-json-relative-path >>" + - "<< pipeline.parameters.starlark-test-args-file-url >>" build_core_launcher: docker: @@ -768,6 +772,9 @@ jobs: # Execute Simple Starlark Script to test --args-file - run: "${KURTOSIS_BINPATH} run --enclave args-file-test << pipeline.parameters.workspace-with-cli-binary-and-images-mountpoint >>/<< pipeline.parameters.starlark-test-args-file-test-main-star-relative-path >> --args-file << pipeline.parameters.workspace-with-cli-binary-and-images-mountpoint >>/<< pipeline.parameters.starlark-test-args-file-test-args-json-relative-path >>" + # Execute Simple Starlark Script to test --args-file from URL + - run: "${KURTOSIS_BINPATH} run --enclave args-file-test-from-url << pipeline.parameters.workspace-with-cli-binary-and-images-mountpoint >>/<< pipeline.parameters.starlark-test-args-file-test-main-star-relative-path >> --args-file << pipeline.parameters.starlark-test-args-file-url >>" + # Execute github starlark module - run: "${KURTOSIS_BINPATH} run --enclave test-datastore github.com/kurtosis-tech/datastore-army-package '{\"num_datastores\": 2}'" diff --git a/cli/cli/commands/run/run.go b/cli/cli/commands/run/run.go index 3af1903912..64e2a9d429 100644 --- a/cli/cli/commands/run/run.go +++ b/cli/cli/commands/run/run.go @@ -6,6 +6,9 @@ import ( "fmt" "github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config" "gopkg.in/yaml.v2" + "io" + "net/http" + "net/url" "os" "os/signal" "path" @@ -292,15 +295,10 @@ func run( if packageArgs == inputArgsAreEmptyBracesByDefault && packageArgsFile != packageArgsFileDefaultValue { logrus.Debugf("'%v' is empty but '%v' is provided so we will go with the '%v' value", inputArgsArgKey, packageArgsFileFlagKey, packageArgsFileFlagKey) - packageArgsFileBytes, err := os.ReadFile(packageArgsFile) + packageArgs, err = getArgsFromFilepathOrURL(packageArgsFile) if err != nil { - return stacktrace.Propagate(err, "attempted to read file provided by flag '%v' with path '%v' but failed", packageArgsFileFlagKey, packageArgsFile) + return stacktrace.Propagate(err, "An error occurred while getting the package args from filepath or URL '%s'", packageArgsFile) } - packageArgsFileStr := string(packageArgsFileBytes) - if packageArgParsingErr := validateSerializedArgs(packageArgsFileStr); packageArgParsingErr != nil { - return stacktrace.Propagate(err, "attempted to validate '%v' but failed", packageArgsFileFlagKey) - } - packageArgs = packageArgsFileStr } else if packageArgs != inputArgsAreEmptyBracesByDefault && packageArgsFile != packageArgsFileDefaultValue { logrus.Debugf("'%v' arg is not empty; ignoring value of '%v' flag as '%v' arg takes precedence", inputArgsArgKey, packageArgsFileFlagKey, inputArgsArgKey) } @@ -665,3 +663,43 @@ func validateSerializedArgs(serializedArgs string) error { fmt.Errorf("JSON parsing error '%v', YAML parsing error '%v'", jsonError, yamlError), "Error validating args, because it is not a valid JSON or YAML.") } + +func getArgsFromFilepathOrURL(packageArgsFile string) (string, error) { + var packageArgsFileBytes []byte + isFileURL := true + _, err := os.Stat(packageArgsFile) + if err == nil { + isFileURL = false + packageArgsFileBytes, err = os.ReadFile(packageArgsFile) + if err != nil { + return "", stacktrace.Propagate(err, "attempted to read file provided by flag '%v' with path '%v' but failed", packageArgsFileFlagKey, packageArgsFile) + } + } + if err != nil && !os.IsNotExist(err) { + return "", stacktrace.Propagate(err, "An error occurred checking for argument's file existence on '%s'", packageArgsFile) + } + + if isFileURL { + argsFileURL, parseErr := url.Parse(packageArgsFile) + if parseErr != nil { + return "", stacktrace.Propagate(parseErr, "An error occurred while parsing file args URL '%s'", argsFileURL) + } + response, getErr := http.Get(argsFileURL.String()) + if getErr != nil { + return "", stacktrace.Propagate(getErr, "An error occurred getting the args file content from URL '%s'", argsFileURL.String()) + } + defer response.Body.Close() + responseBodyBytes, readAllErr := io.ReadAll(response.Body) + if readAllErr != nil { + return "", stacktrace.Propagate(readAllErr, "An error occurred reading the args file content") + } + packageArgsFileBytes = responseBodyBytes + } + + packageArgsFileStr := string(packageArgsFileBytes) + if packageArgParsingErr := validateSerializedArgs(packageArgsFileStr); packageArgParsingErr != nil { + return "", stacktrace.Propagate(err, "attempted to validate '%v' but failed", packageArgsFileFlagKey) + } + + return packageArgsFileStr, nil +}