Skip to content
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: kurtosis run command now accepts URLs with the 'args-file' argument #1607

Merged
merged 4 commits into from Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .circleci/config.yml
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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-from-url-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.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}'"

Expand Down
52 changes: 45 additions & 7 deletions cli/cli/commands/run/run.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}