From 280557a4bbc74c481d186ac9c9f13ded052559b3 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Wed, 1 May 2024 11:29:45 -0400 Subject: [PATCH 1/9] fix set service arg parsing --- .../components/enclaveBuilder/utils.ts | 2 +- .../components/widgets/ImageButton.tsx | 73 +++++++++++-------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/enclaveBuilder/utils.ts b/enclave-manager/web/packages/app/src/emui/enclaves/components/enclaveBuilder/utils.ts index 26d451ecd1..feb174821a 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/enclaveBuilder/utils.ts +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/enclaveBuilder/utils.ts @@ -314,7 +314,7 @@ export function generateStarlarkFromGraph( o.forEach((arrayValue) => { result += `${objectToStarlark(arrayValue, indent + 4)},\n`; }); - result += `${padLeft}],\n`; + result += `${padLeft}]\n`; return result; } if (typeof o === "number") { diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index c60e955092..cbaa9a9c8d 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -12,11 +12,12 @@ import { ModalOverlay, } from "@chakra-ui/react"; import { isDefined, RemoveFunctions } from "kurtosis-ui-components"; -import { useMemo, useState } from "react"; +import {useCallback, useMemo, useState} from "react"; import { IoLogoDocker } from "react-icons/io5"; import { useNavigate } from "react-router-dom"; import { useEnclavesContext } from "../../EnclavesContext"; import { EnclaveFullInfo } from "../../types"; +import {useKurtosisPackageIndexerClient} from "../../../../client/packageIndexer/KurtosisPackageIndexerClientContext"; function getUrlForImage(image: string): string | URL | undefined { const [imageName] = image.split(":"); @@ -41,14 +42,14 @@ function objectToStarlark(o: any, indent: number) { o.forEach((arrayValue) => { result += `${objectToStarlark(arrayValue, indent + 4)},\n`; }); - result += `${padLeft}],\n`; + result += `${padLeft}]\n`; return result; } if (typeof o === "number") { return `${o}`; } if (typeof o === "string") { - return `${o}`; + return `"${o}"`; } if (typeof o === "boolean") { return o ? "True" : "False"; @@ -56,32 +57,16 @@ function objectToStarlark(o: any, indent: number) { if (typeof o === "object") { let result = "{"; Object.entries(o).forEach(([key, value]) => { - result += `\n${padLeft}"${key}": "${objectToStarlark(value, indent + 4)}",`; + result += `\n${padLeft}"${key}": ${objectToStarlark(value, indent + 4)},`; }); result += `${padLeft}}`; return result; } } -function deserializeParams(serializedParams: string): Record { - try { - const parsedParams = JSON.parse(serializedParams); - if (typeof parsedParams === "object" && parsedParams !== null) { - const deserialized: Record = {}; - for (const key in parsedParams) { - if (typeof parsedParams[key] === "string") { - deserialized[key] = parsedParams[key]; - } else { - throw new Error("Value is not a string."); - } - } - return deserialized; - } else { - throw new Error("Invalid JSON format."); - } - } catch (error) { - console.error("Error deserializing params:", error); - return {}; +function wrapWithArgs(args: Record) { + return { + "args": args, } } @@ -97,8 +82,16 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl const { runStarlarkScript } = useEnclavesContext(); // Assuming this is defined elsewhere const [error, setError] = useState(null); const [newImage, setNewImage] = useState(""); + const packageIndexerClient = useKurtosisPackageIndexerClient(); const navigator = useNavigate(); + const getPackageInfo = useCallback( + async (packageName: string) => { + return await packageIndexerClient.readPackage(packageName); + }, + [packageIndexerClient], + ); + const handleSetImageSubmit = async (e: React.FormEvent) => { e.preventDefault(); @@ -109,17 +102,37 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl } const packageId = starlarkRun.value.packageId; + const packageInfoResponse = await getPackageInfo(packageId); + if(packageInfoResponse.isErr) { + setError(`Error occurred getting info about ${packageId} from indexer.`) + return; + } + if (!packageInfoResponse.value.package) { + setError(`Could not find package ${packageId}`); + return; + } + const packageArgs = packageInfoResponse.value.package.args; - let argsRecord; + let argsJson; if (starlarkRun.value.initialSerializedParams) { - console.log(`initial serialized params: ${starlarkRun.value.initialSerializedParams}`); - argsRecord = deserializeParams(starlarkRun.value.initialSerializedParams); + argsJson = starlarkRun.value.initialSerializedParams; + } else { + argsJson = starlarkRun.value.serializedParams; + } + const argsRecord = JSON.parse(argsJson); + if (typeof argsRecord !== "object" || argsRecord === null) { + setError("Error: deserializing initial params of starlark package run failed.") + return; + } + + let args; + console.log(packageArgs) + if(packageArgs.length == 2 && packageArgs[1].name == "args") { + args = objectToStarlark(wrapWithArgs(argsRecord), 4); } else { - console.log(`serialized params: ${starlarkRun.value.serializedParams}`); - argsRecord = deserializeParams(starlarkRun.value.serializedParams); + args = objectToStarlark(argsRecord, 4); } - const args = objectToStarlark(argsRecord, 8); - console.log(`initial args used to start package:\n${args}`); + console.log(`args used to start package:\n${args}`); const updateImageStarlarkScript = ` package = import_module("${packageId}/main.star") From efa9b5139b6646ae4d3a7b6613ef16d88d6290d6 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Wed, 1 May 2024 11:57:50 -0400 Subject: [PATCH 2/9] prettier fix --- .../components/widgets/ImageButton.tsx | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index cbaa9a9c8d..33a36b7a8b 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -12,12 +12,12 @@ import { ModalOverlay, } from "@chakra-ui/react"; import { isDefined, RemoveFunctions } from "kurtosis-ui-components"; -import {useCallback, useMemo, useState} from "react"; +import { useCallback, useMemo, useState } from "react"; import { IoLogoDocker } from "react-icons/io5"; import { useNavigate } from "react-router-dom"; +import { useKurtosisPackageIndexerClient } from "../../../../client/packageIndexer/KurtosisPackageIndexerClientContext"; import { useEnclavesContext } from "../../EnclavesContext"; import { EnclaveFullInfo } from "../../types"; -import {useKurtosisPackageIndexerClient} from "../../../../client/packageIndexer/KurtosisPackageIndexerClientContext"; function getUrlForImage(image: string): string | URL | undefined { const [imageName] = image.split(":"); @@ -66,8 +66,8 @@ function objectToStarlark(o: any, indent: number) { function wrapWithArgs(args: Record) { return { - "args": args, - } + args: args, + }; } export type SetImageModalProps = { @@ -86,10 +86,10 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl const navigator = useNavigate(); const getPackageInfo = useCallback( - async (packageName: string) => { - return await packageIndexerClient.readPackage(packageName); - }, - [packageIndexerClient], + async (packageName: string) => { + return await packageIndexerClient.readPackage(packageName); + }, + [packageIndexerClient], ); const handleSetImageSubmit = async (e: React.FormEvent) => { @@ -103,8 +103,8 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl const packageId = starlarkRun.value.packageId; const packageInfoResponse = await getPackageInfo(packageId); - if(packageInfoResponse.isErr) { - setError(`Error occurred getting info about ${packageId} from indexer.`) + if (packageInfoResponse.isErr) { + setError(`Error occurred getting info about ${packageId} from indexer.`); return; } if (!packageInfoResponse.value.package) { @@ -121,16 +121,16 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl } const argsRecord = JSON.parse(argsJson); if (typeof argsRecord !== "object" || argsRecord === null) { - setError("Error: deserializing initial params of starlark package run failed.") + setError("Error: deserializing initial params of starlark package run failed."); return; } let args; - console.log(packageArgs) - if(packageArgs.length == 2 && packageArgs[1].name == "args") { - args = objectToStarlark(wrapWithArgs(argsRecord), 4); + console.log(packageArgs); + if (packageArgs.length == 2 && packageArgs[1].name == "args") { + args = objectToStarlark(wrapWithArgs(argsRecord), 4); } else { - args = objectToStarlark(argsRecord, 4); + args = objectToStarlark(argsRecord, 4); } console.log(`args used to start package:\n${args}`); From 0cf0244b6ac41a15a2b914d09a7f2aaa4fa12107 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Wed, 1 May 2024 12:22:00 -0400 Subject: [PATCH 3/9] fix eslint err --- .../app/src/emui/enclaves/components/widgets/ImageButton.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index 33a36b7a8b..c9fbacddc6 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -127,7 +127,7 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl let args; console.log(packageArgs); - if (packageArgs.length == 2 && packageArgs[1].name == "args") { + if (packageArgs.length === 2 && packageArgs[1].name === "args") { args = objectToStarlark(wrapWithArgs(argsRecord), 4); } else { args = objectToStarlark(argsRecord, 4); From 34d0a0751e63a8427c9c52f1728c66d096889fb7 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Wed, 1 May 2024 13:51:14 -0400 Subject: [PATCH 4/9] fix initial params set logic --- .../server/api_container_service.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/core/server/api_container/server/api_container_service.go b/core/server/api_container/server/api_container_service.go index c1de99148a..78f6395a3f 100644 --- a/core/server/api_container/server/api_container_service.go +++ b/core/server/api_container/server/api_container_service.go @@ -115,7 +115,9 @@ func NewApiContainerService( metricsClient metrics_client.MetricsClient, githubAuthProvider *git_package_content_provider.GitHubPackageAuthProvider, ) (*ApiContainerService, error) { - var emptyInitialSerializedParams *string + var emptyInitialSerializedParams string + emptyInitialSerializedParamsPtr := &emptyInitialSerializedParams + *emptyInitialSerializedParamsPtr = "" service := &ApiContainerService{ filesArtifactStore: filesArtifactStore, serviceNetwork: serviceNetwork, @@ -132,7 +134,7 @@ func NewApiContainerService( MainFunctionName: "", ExperimentalFeatures: []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{}, RestartPolicy: kurtosis_core_rpc_api_bindings.RestartPolicy_NEVER, - InitialSerializedParams: emptyInitialSerializedParams, + InitialSerializedParams: emptyInitialSerializedParamsPtr, }, metricsClient: metricsClient, githubAuthProvider: githubAuthProvider, @@ -175,9 +177,8 @@ func (apicService *ApiContainerService) RunStarlarkScript(args *kurtosis_core_rp args.GetExperimentalFeatures(), stream) - var initialSerializedParams *string if apicService.starlarkRun.InitialSerializedParams == nil || *apicService.starlarkRun.InitialSerializedParams == "" { - initialSerializedParams = &apicService.starlarkRun.SerializedParams + apicService.starlarkRun.InitialSerializedParams = &serializedParams } apicService.starlarkRun = &kurtosis_core_rpc_api_bindings.GetStarlarkRunResponse{ PackageId: apicService.starlarkRun.PackageId, @@ -188,7 +189,7 @@ func (apicService *ApiContainerService) RunStarlarkScript(args *kurtosis_core_rp MainFunctionName: mainFuncName, ExperimentalFeatures: experimentalFeatures, RestartPolicy: apicService.restartPolicy, - InitialSerializedParams: initialSerializedParams, + InitialSerializedParams: apicService.starlarkRun.InitialSerializedParams, } return nil @@ -323,9 +324,8 @@ func (apicService *ApiContainerService) RunStarlarkPackage(args *kurtosis_core_r serializedParams) apicService.runStarlark(parallelism, dryRun, detectedPackageId, detectedPackageReplaceOptions, mainFuncName, actualRelativePathToMainFile, scriptWithRunFunction, serializedParams, downloadMode, nonBlockingMode, args.ExperimentalFeatures, stream) - var initialSerializedParams *string if apicService.starlarkRun.InitialSerializedParams == nil || *apicService.starlarkRun.InitialSerializedParams == "" { - initialSerializedParams = &apicService.starlarkRun.SerializedParams + apicService.starlarkRun.InitialSerializedParams = &serializedParams } apicService.starlarkRun = &kurtosis_core_rpc_api_bindings.GetStarlarkRunResponse{ PackageId: packageIdFromArgs, @@ -336,7 +336,7 @@ func (apicService *ApiContainerService) RunStarlarkPackage(args *kurtosis_core_r MainFunctionName: mainFuncName, ExperimentalFeatures: args.ExperimentalFeatures, RestartPolicy: apicService.restartPolicy, - InitialSerializedParams: initialSerializedParams, + InitialSerializedParams: apicService.starlarkRun.InitialSerializedParams, } return nil } From e4022f4511d07f1baf92414d9996f9dbc4b85558 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Wed, 1 May 2024 13:53:17 -0400 Subject: [PATCH 5/9] only set initial params --- .../emui/enclaves/components/widgets/ImageButton.tsx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index c9fbacddc6..5a152abca5 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -113,13 +113,11 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl } const packageArgs = packageInfoResponse.value.package.args; - let argsJson; - if (starlarkRun.value.initialSerializedParams) { - argsJson = starlarkRun.value.initialSerializedParams; - } else { - argsJson = starlarkRun.value.serializedParams; + if(!starlarkRun.value.initialSerializedParams) { + setError(`Error occurred getting initial params used to start package.`) + return; } - const argsRecord = JSON.parse(argsJson); + const argsRecord = JSON.parse(starlarkRun.value.initialSerializedParams); if (typeof argsRecord !== "object" || argsRecord === null) { setError("Error: deserializing initial params of starlark package run failed."); return; From b0ed90b51fad962450c680354f632e29f5569165 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Wed, 1 May 2024 14:15:45 -0400 Subject: [PATCH 6/9] prettier fix --- .../app/src/emui/enclaves/components/widgets/ImageButton.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index 5a152abca5..ac34ff2d99 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -113,8 +113,9 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl } const packageArgs = packageInfoResponse.value.package.args; - if(!starlarkRun.value.initialSerializedParams) { - setError(`Error occurred getting initial params used to start package.`) + console.log(`initial serialized params: ${starlarkRun.value.initialSerializedParams}`); + if (!starlarkRun.value.initialSerializedParams) { + setError(`Error occurred getting initial params used to start package.`); return; } const argsRecord = JSON.parse(starlarkRun.value.initialSerializedParams); From cfd5a846404989cfebc9ca0a034f0b3324f0e671 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Wed, 1 May 2024 15:00:53 -0400 Subject: [PATCH 7/9] remove redundant console log --- .../app/src/emui/enclaves/components/widgets/ImageButton.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index ac34ff2d99..53375d09be 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -125,7 +125,6 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl } let args; - console.log(packageArgs); if (packageArgs.length === 2 && packageArgs[1].name === "args") { args = objectToStarlark(wrapWithArgs(argsRecord), 4); } else { From ed0affe65ed8a9bef62417b17d3f58225396b1f8 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Thu, 2 May 2024 08:12:53 -0400 Subject: [PATCH 8/9] layer set service calls --- .../emui/enclaves/components/widgets/ImageButton.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index 53375d09be..fda2dcfa1b 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -132,13 +132,21 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl } console.log(`args used to start package:\n${args}`); - const updateImageStarlarkScript = ` -package = import_module("${packageId}/main.star") + // if we've already run an update image, just append another set service instruction, otherwise create a new set service script + let updateImageStarlarkScript; + console.log(`serialized script: ${starlarkRun.value.serializedScript}`) + console.log(starlarkRun.value.serializedScript.slice(0, 7).toLowerCase()) + if(starlarkRun.value.serializedScript.slice(0, 7).toLowerCase() === "package") { + const latestSetService = `\n\n plan.set_service(name="${serviceName}", config=ServiceConfig(image="${newImage}"))`; + updateImageStarlarkScript = starlarkRun.value.serializedScript + latestSetService; + } else { + updateImageStarlarkScript = `package = import_module("${packageId}/main.star") def run(plan, args): package.run(plan, **${args}) plan.set_service(name="${serviceName}", config=ServiceConfig(image="${newImage}"))`; + } console.log(`starlark script to service ${serviceName} to image ${newImage}\n${updateImageStarlarkScript}`); try { From 5375d842c0c0f0b497ac1cc94b0fb623fae31dce Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Thu, 2 May 2024 08:16:49 -0400 Subject: [PATCH 9/9] prettier fix --- .../src/emui/enclaves/components/widgets/ImageButton.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx index fda2dcfa1b..ff2b509cdf 100644 --- a/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx +++ b/enclave-manager/web/packages/app/src/emui/enclaves/components/widgets/ImageButton.tsx @@ -134,9 +134,9 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl // if we've already run an update image, just append another set service instruction, otherwise create a new set service script let updateImageStarlarkScript; - console.log(`serialized script: ${starlarkRun.value.serializedScript}`) - console.log(starlarkRun.value.serializedScript.slice(0, 7).toLowerCase()) - if(starlarkRun.value.serializedScript.slice(0, 7).toLowerCase() === "package") { + console.log(`serialized script: ${starlarkRun.value.serializedScript}`); + console.log(starlarkRun.value.serializedScript.slice(0, 7).toLowerCase()); + if (starlarkRun.value.serializedScript.slice(0, 7).toLowerCase() === "package") { const latestSetService = `\n\n plan.set_service(name="${serviceName}", config=ServiceConfig(image="${newImage}"))`; updateImageStarlarkScript = starlarkRun.value.serializedScript + latestSetService; } else {