An environment variable propagation method, system and electronic device of a cloud sandbox
By parsing and persisting the template environment variable set in the cloud sandbox system, and merging it with the created environment variable set to form a three-level overriding semantic, the problems of environment variable loss and unclear overriding priority are solved, and the complete preservation and consistency of environment variables during the sandbox runtime are achieved.
Patent Information
- Application Number
- CN202611423861.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-14
- Publication Date
- 2026-11-24
AI Technical Summary
Existing cloud sandbox systems suffer from issues such as lost environment variables in image declarations, unhandled cold start paths, and unclear environment variable overriding priorities. These issues cause applications that rely on image declarations to fail to initialize properly, requiring users to manually redeclare variables, and the system's behavior is opaque.
By parsing and persisting the template environment variable set during the template construction phase, and merging it with the creation environment variable set carried by the sandbox creation request according to the rule of overriding with the same name and retaining with different names, a three-level overriding semantic is formed, ensuring the complete retention and consistency of environment variables during the sandbox runtime.
It achieves full retention of environment variables declared in the mirror during the sandbox runtime, eliminates the semantic gap between the build and runtime, ensures the consistency and predictability of user process behavior, and reduces the risk of data competition in high-concurrency scenarios.
Smart Images

Figure CN123158618A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of cloud computing technology, and in particular to a method, system and electronic device for propagating environment variables in a cloud sandbox. Background Technology
[0002] Cloud-based code sandboxes are core infrastructure for AI Agents, CI / CD pipelines, and online programming platforms. Their typical architecture usually comprises three layers: a template building layer, an orchestration layer, and a process daemon layer. The template building layer constructs a snapshottable root file system from the user-provided image build file and extracts image metadata, saving it to the template metadata store. The orchestration layer receives sandbox creation requests, restores the file system from the template snapshot, and passes startup parameters to the process daemon. The process daemon resides within the sandbox and injects environment variables into the user process when it starts. The environment variable declaration directives in the image build file are a standard mechanism for containerized applications to declare runtime dependencies, widely used to configure critical runtime parameters such as dependency package paths, application configuration file paths, and service addresses.
[0003] Regarding the propagation of environment variables during cloud sandbox runtime, existing technologies have the following drawbacks: First, runtime environment variables are lost. When the sandbox is instantiated, the orchestration layer only passes the environment variables explicitly passed by the caller, and the environment variables of the image declarations already stored in the template metadata are discarded as a whole. This causes applications that depend on image declarations to fail to initialize properly at runtime, and users have to manually redeclare all variables each time. Second, cold start paths are not handled. The cold start path of the sandbox file system is implemented independently, and the environment variables in the template metadata are also not restored under this path. Repairing the standard path cannot benefit the cold start path. Third, the priority of environment variable overriding is unclear. It is not defined which variable with the same name takes precedence. The actual behavior depends on the overriding order of the underlying data structure. Users cannot predictably override the default values of image declarations, and the system behavior is difficult to test and document. Summary of the Invention
[0004] This application provides a method, system, and electronic device for propagating environment variables in a cloud sandbox, which solves the problem in the prior art where environment variables declared by the image are lost during the sandbox runtime, causing applications that depend on the image declaration to fail to initialize properly. The method extends the environment variables declared by the image from the build stage to the runtime stage, so that users can make the application run normally in the sandbox without having to declare them again.
[0005] Firstly, this application provides a method for propagating environment variables in a cloud sandbox, comprising the following steps: In response to a sandbox creation request, the template metadata of the target template specified in the sandbox creation request is obtained; wherein, the template metadata includes a set of template environment variables, which is a set of environment variable key-value pairs obtained by parsing the image configuration corresponding to the target template during the template building phase; The template environment variable set is merged with the creation environment variable set carried in the sandbox creation request to obtain the merged environment variable set; The merged set of environment variables is sent to the process daemon within the sandbox; the merged set of environment variables is used as the default set of environment variables for user processes started within the sandbox.
[0006] In one possible implementation, before obtaining the template metadata of the target template, the method further includes: during the template construction phase, building the image build file corresponding to the target template into a snapshottable root file system and generating the image configuration corresponding to the image build file; wherein, the environment field of the image configuration records the environment variables declared by the environment variable declaration instructions in the image build file; parsing the key-value pairs of each environment variable from the environment field of the image configuration to obtain the template environment variable set; writing the template environment variable set into the template metadata, and storing the template metadata and the snapshot file corresponding to the root file system together in the object storage; obtaining the template metadata of the target template includes: obtaining the template metadata of the target template from the object storage.
[0007] In another possible implementation, the template environment variable set is merged with the creation environment variable set carried in the sandbox creation request to obtain a merged environment variable set. This includes: copying the template environment variable set to obtain an independent copy set; writing each environment variable key-value pair from the creation environment variable set into the independent copy set; wherein, for environment variable keys with the same name, the value in the creation environment variable set overwrites the value of the corresponding environment variable key in the independent copy set; for environment variable keys with different names, both the environment variable key-value pairs in the template environment variable set and the environment variable key-value pairs in the creation environment variable set are retained in the independent copy set; the independent copy set after writing is determined as the merged environment variable set; wherein, the template environment variable set in the template metadata remains unchanged during the merging process.
[0008] In another possible implementation, if the template environment variable set is empty, the method further includes sending the creation environment variable set carried in the sandbox creation request as the default environment variable set for user processes started within the sandbox to the process daemon.
[0009] In another possible implementation, the sandbox instantiation path includes a standard snapshot recovery path and a file system cold start path. The standard snapshot recovery path is used to restore the sandbox based on memory snapshots and file system snapshots, while the file system cold start path is used to reboot the sandbox if the target template has a file system snapshot but not a memory snapshot. In both the standard snapshot recovery path and the file system cold start path, after obtaining the template metadata of the target template and before instantiating the sandbox, the template environment variable set and the creation environment variable set are merged to obtain a merged environment variable set.
[0010] In another possible implementation, after sending the merged environment variable set to the process daemon within the sandbox, the method further includes: the process daemon determining the received merged environment variable set as the default environment variable set; the process daemon receiving a process start request; wherein the process start request is used to start a user process within the sandbox, and the process start request carries the process environment variable set; the process daemon merging the process environment variable set with the default environment variable set to obtain a final environment variable set; wherein, for environment variable keys with the same name, the values in the process environment variable set overwrite the values in the default environment variable set, and for environment variable keys with different names, the environment variable key-value pairs in the process environment variable set and the default environment variable set are retained; and the user process is started based on the final environment variable set.
[0011] Secondly, this application provides a cloud-based sandbox environment variable propagation system, comprising: The metadata acquisition module is used to respond to the sandbox creation request and acquire the template metadata of the target template specified by the sandbox creation request. The template metadata includes a set of template environment variables, which is a set of environment variable key-value pairs obtained by parsing the image configuration corresponding to the target template during the template construction phase. The environment variable merging module is used to merge the template environment variable set with the creation environment variable set carried in the sandbox creation request to obtain a merged environment variable set; The environment variable sending module is used to send a merged set of environment variables to the process daemon within the sandbox; the merged set of environment variables is used as the default set of environment variables for user processes started within the sandbox.
[0012] Thirdly, this application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the cloud sandbox environment variable propagation method as described above.
[0013] Fourthly, this application also provides a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the environment variable propagation method of the cloud sandbox as described above.
[0014] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, implements the cloud sandbox environment variable propagation method as described above.
[0015] This application provides a method, system, and electronic device for propagating environment variables in a cloud sandbox. By using the template environment variable set parsed during the template construction phase as the merging basis, and merging it with the creation environment variable set carried in the sandbox creation request according to the rule of overwriting with the same name and retaining with different names, a merged environment variable set is formed. The merged environment variable set is then distributed to the process daemon in the sandbox as the default environment variable set. This achieves complete retention of the environment variables declared in the image during the sandbox runtime, ensuring that the behavior of user processes started in the sandbox is consistent with that when the image is run directly. Compared with existing technologies, this application enables users to predictably override the default values of image declarations by merging environment variables at both the template layer and the creation layer. By copying the template environment variable set to obtain an independent copy before merging, the template metadata remains unchanged during the merging process, avoiding data competition in high-concurrency sandbox creation scenarios. By performing the same merging in the standard snapshot recovery path and the file system cold start path, the environment variable behavior under the two instantiation methods of the sandbox remains consistent. When the template environment variable set is empty, the merging is skipped entirely, achieving zero additional overhead. When the process starts, the process environment variable set overrides the default environment variable set again, forming a three-level overriding semantic from low to high at the template layer, creation layer, and process layer. Users can customize at any level without affecting the semantic stability of other levels. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 This is a schematic diagram of the architecture of the cloud-based sandbox environment variable propagation system provided in this application; Figure 2 This is one of the flowcharts illustrating the environment variable propagation method for the cloud sandbox provided in this application; Figure 3 This is the second flowchart illustrating the environment variable propagation method for the cloud sandbox provided in this application; Figure 4 This is the third flowchart illustrating the cloud-based sandbox environment variable propagation method provided in this application; Figure 5 This is the fourth flowchart illustrating the environmental variable propagation method for the cloud sandbox provided in this application; Figure 6 This is a schematic diagram of the environmental variable propagation device for the cloud sandbox provided in this application; Figure 7 This is a schematic diagram of the structure of the electronic device provided in this application. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0019] The following is combined with Figures 1 to 7 This application describes a method, system, and electronic device for propagating environmental variables in a cloud sandbox.
[0020] Cloud Sandbox is a core infrastructure for scenarios such as AI Agent, CI / CD pipeline, and online programming platform. Its typical architecture includes three layers: template building layer, orchestration layer, and process protection layer.
[0021] The template building layer receives the image build file (such as Dockerfile) provided by the user, builds the image build file into a snapshottable root file system (rootfs), and extracts the image metadata and saves it to the template metadata storage; the orchestration layer receives the sandbox creation request initiated by the software development kit (SDK) caller, restores the file system from the template snapshot, and passes the startup parameters to the process daemon; the process daemon (envd) resides inside the sandbox, receives initialization parameters through the initialization interface, and injects environment variables into the user process when it starts.
[0022] Environment variable declaration instructions in image build files (such as the ENV instruction in Dockerfile) are a standard mechanism for containerized applications to declare runtime dependencies. They are widely used to configure critical runtime parameters such as dependency package paths (such as PYTHONPATH), application configuration file paths, and API service addresses.
[0023] Existing cloud-based sandbox systems suffer from a semantic gap between the build and runtime phases in handling environment variables, which manifests in three aspects: First, runtime environment variables are lost: During the template build phase, the environment variables declared by the image are correctly parsed and stored in the template metadata. Each build command during the build phase can correctly read these variables. However, when the sandbox is instantiated at runtime, the orchestration layer only sends the environment variables explicitly passed by the SDK caller through the sandbox creation request to the process daemon. The environment variables declared by the image stored in the template metadata are completely discarded. This causes applications that depend on the image environment variable declarations to fail to initialize properly when the sandbox is running. Users have to manually repeat the declaration of all image environment variables in each sandbox creation call, causing serious development experience problems and configuration maintenance burden.
[0024] Second, the cold start path is not handled: There are two creation paths for the sandbox: standard snapshot recovery and file system cold start. The standard snapshot recovery path restores the sandbox from a memory snapshot, while the file system cold start path only has a file system snapshot and no memory image, requiring a kernel reboot. Even if the standard path is supplemented with the recovery logic of metadata such as default user and default working directory, environment variables are not propagated in either path. Furthermore, the cold start path is implemented independently and cannot benefit from the repair of the standard path, resulting in a double omission.
[0025] Third, the priority of environment variable overriding is unclear: the existing system does not define which one to take precedence for environment variables with the same name. The actual behavior depends on the overriding order of the underlying data structure, which is not transparent to the user. Users cannot predictably overridden the default values declared in the image, and the system behavior is also difficult to test and document.
[0026] In view of the deficiencies in the existing technology, this application proposes a method, system, and electronic device for propagating environment variables in a cloud sandbox. The method unifies the merging and priority arbitration of environment variable sources at the cloud sandbox orchestration layer: the template environment variable set parsed and persisted during the template construction phase serves as the low-priority base layer, while the creation environment variable set carried in the sandbox creation request serves as the high-priority overlay layer. The two layers are merged according to the rule of overriding with the same name and retaining different names, and then distributed to the process daemon within the sandbox as the default environment variable set for user processes started within the sandbox. When a user process starts, the process daemon overwrites the default environment variable set with process-level passed-in environment variables, thus forming a three-level overlay semantic from low to high: template layer, creation layer, and process layer. This fully preserves the environment variables declared in the image while maintaining the user's precise control over environment variables.
[0027] For example, Figure 1 This is a schematic diagram of the architecture of the cloud-based sandbox environment variable propagation system provided in this application. For example... Figure 1As shown, the system includes: a metadata acquisition module 101, an environment variable merging module 102, and an environment variable sending module 103.
[0028] The metadata acquisition module 101, the environment variable merging module 102, and the environment variable sending module 103 are connected in sequence. The system interacts with the template metadata storage and the process daemon in the sandbox. The template metadata storage stores the template metadata generated by each template during the construction phase.
[0029] The metadata acquisition module 101 is used to acquire the template metadata of the target template specified in the sandbox creation request in response to the sandbox creation request.
[0030] The template metadata includes a set of template environment variables, which is a collection of key-value pairs of environment variables obtained by parsing the image configuration corresponding to the target template during the template construction phase.
[0031] Specifically, the sandbox creation request can be initiated by the SDK caller. The caller specifies the target template in the creation request and can also carry the environment variables that it wants to inject into the sandbox.
[0032] For example, the template metadata is stored in object storage, along with the snapshot file corresponding to the root file system of the target template. The metadata acquisition module 101 reads the template metadata of the target template from the object storage based on the template identifier in the creation request.
[0033] The environment variable merging module 102 is used to merge the template environment variable set with the creation environment variable set carried in the sandbox creation request to obtain a merged environment variable set.
[0034] The merging process follows the rule of overwriting identical names and retaining non-identical names. The template environment variable set serves as the basis for the merging, and environment variable keys with the same name as the template environment variable set are created in the environment variable set to create side values that overwrite template side values.
[0035] Specifically, the merge is performed after reading the template metadata and before instantiating the sandbox; the same merge is performed in both the standard snapshot recovery path and the file system cold start path.
[0036] For example, the environment variable merging module 102 first copies the template environment variable set to obtain an independent copy set, then writes each environment variable key-value pair in the created environment variable set into the independent copy set, and determines the independent copy set after writing as the merged environment variable set. The template metadata is not modified during the merging process.
[0037] Environment variable sending module 103 is used to send the merged set of environment variables to the process daemon in the sandbox.
[0038] The merged environment variable set is used as the default environment variable set for user processes launched within the sandbox.
[0039] For example, the environment variable sending module 103 sends the merged environment variable set to the sandbox through the initialization interface exposed by the process daemon. The process daemon determines the merged environment variable set as the default environment variable set. When the user process starts, it overwrites the default environment variable set with the process environment variable set carried by the process startup request. The final environment variable set after overwriting is used as the environment variable of the user process. The specific injection process will be described in subsequent embodiments and will not be repeated here.
[0040] The cloud sandbox environment variable propagation system provided in this embodiment achieves a complete propagation chain by working collaboratively with the metadata acquisition module 101, the environment variable merging module 102, and the environment variable sending module 103. This chain restores the template environment variable set from the template metadata, merges it with the created environment variable set according to priority, and sends it to the process daemon within the sandbox. This ensures that the environment variables declared in the image are fully preserved during the sandbox runtime, effectively improving the consistency and predictability of cloud sandbox environment variable propagation.
[0041] It should be understood that the execution entity of the environment variable propagation method for the cloud sandbox provided in this application can be a cloud sandbox environment variable propagation device, which may include the aforementioned metadata acquisition module 101, environment variable merging module 102, and environment variable sending module 103. Alternatively, the environment variable propagation device can be an electronic device or a server, which can be deployed in the orchestration layer of the cloud sandbox and have the functions of the aforementioned modules; for example, it can be an orchestration device for the cloud sandbox. Furthermore, the environment variable propagation device can also be the central processing unit (CPU) of the electronic device, or a processing module within the electronic device used to implement the environment variable propagation method for the cloud sandbox. This application uses the execution of the environment variable propagation method for the cloud sandbox by an environment variable propagation device as an example to illustrate the environment variable propagation method for the cloud sandbox provided in this application.
[0042] For example, Figure 2 This is one of the flowcharts illustrating the environment variable propagation method for cloud sandboxes provided in this application. The method, which involves merging and distributing environment variables in response to a sandbox creation request, includes the following steps: Step 201: In response to the sandbox creation request, obtain the template metadata of the target template specified in the sandbox creation request.
[0043] The sandbox creation request, initiated by the SDK caller, requests the creation of a cloud sandbox. The request specifies the target template used to create the sandbox and may include environment variables that the caller wishes to inject into the sandbox. The environment variable propagation mechanism responds to the sandbox creation request by retrieving the template metadata corresponding to the target template. This template metadata includes a set of template environment variables. This set of environment variables is a collection of key-value pairs obtained during the template construction phase by parsing the image configuration corresponding to the target template. It represents the lowest priority environment variable layer and is used to provide default configurations for image declarations.
[0044] Specifically, the template metadata of the target template is stored together with the snapshot file in the object storage. The environment variable propagation device reads the template metadata of the target template from the object storage based on the template identifier in the sandbox creation request.
[0045] Step 202: Merge the template environment variable set with the creation environment variable set carried in the sandbox creation request to obtain the merged environment variable set.
[0046] The environment variable set to be created consists of environment variables explicitly passed in when the SDK caller initiates the sandbox creation request, and has higher priority than the template environment variable set. The merging process is based on the template environment variable set and overlaid with the environment variable set to be created: for environment variable keys with the same name, the values in the created environment variable set override the values in the template environment variable set; for environment variable keys with different names, both the template and created environment variable sets retain their key-value pairs. In other words, the merging result is the union of the two layers of environment variables, with the creation layer only overriding variables with the same name and not deleting other variables from the template layer.
[0047] For example, if the PYTHONPATH value in the template environment variable set is / workspace / lib: / workspace / src and the HF_HOME value is / cache / huggingface, and the PYTHONPATH value in the creation environment variable set is / custom / path, then the PYTHONPATH value in the merged environment variable set will be / custom / path, and the HF_HOME value will remain / cache / huggingface. The caller can override PYTHONPATH with a custom value, while HF_HOME will continue to use the default value declared in the image.
[0048] In one possible implementation, the environmental variable propagation device merges step 202, which may specifically include the following process: Step 202a: Copy the template environment variable set to obtain an independent copy set.
[0049] The copying is a deep copy, and the resulting set of independent copies is independent of the set of template environment variables in the template metadata. Subsequent writing to the set of independent copies does not affect the template metadata.
[0050] For example, a set of template environment variables can be copied using a mapping clone function, such as creating a single, independent copy containing all key-value pairs using the `maps.Clone` function in the Go programming language standard library. Using a deep copy instead of in-place modification ensures that multiple concurrent sandbox creation requests sharing the same template metadata object do not interfere with each other.
[0051] Step 202b: Write each environment variable key-value pair in the created environment variable set to an independent copy set.
[0052] Specifically, for environment variable keys with the same name, the value in the created environment variable set overrides the value of the corresponding environment variable key in the independent copy set; for environment variable keys with different names, both the environment variable key-value pairs in the template environment variable set and the environment variable key-value pairs in the created environment variable set are retained in the independent copy set.
[0053] For example, a set of environment variables can be created and written to a set of independent copies using a mapping merge function. For instance, the maps.Copy function in the Go programming language standard library can be used to write each key-value pair of the creation layer to a copy, with keys with the same name being overwritten directly and keys with different names being appended and retained.
[0054] Step 202c: Determine the set of independent copies after the writing is completed as the set of merged environment variables.
[0055] Since the merging process is based on a set of independent copies, the set of template environment variables in the template metadata remains unchanged during the merging process.
[0056] It should be noted that after the above steps 202a to 202c, the merging is completed based on template layer variables and incrementally with creation layer variables. The template metadata is not modified throughout the process, which can safely support high-concurrency sandbox creation scenarios.
[0057] Optionally, if the template environment variable set is empty, it indicates that the image corresponding to the target template has not declared any environment variables through the environment variable declaration instruction. In this case, there is no need to perform the merging in step 202 above. The environment variable propagation device directly sends the creation environment variable set carried in the sandbox creation request as the default environment variable set for the user processes started within the sandbox to the process daemon. This processing method ensures that templates without declared environment variables do not incur any merging overhead during propagation.
[0058] Step 203: Send the merged set of environment variables to the process daemon in the sandbox.
[0059] The process daemon resides within the sandbox and is responsible for injecting startup and initialization parameters for user processes within the sandbox. The merged environment variable set serves as the default environment variable set for user processes started within the sandbox; that is, the environment variables used by default by user processes started within the sandbox when no other environment variables are specified.
[0060] Specifically, the environment variable propagation device distributes the merged set of environment variables to the sandbox through the initialization interface exposed by the process daemon, for example, through the process daemon's initialization request interface (such as the HTTP / init interface); the process daemon determines the received merged set of environment variables as the default set of environment variables for user processes started in the sandbox.
[0061] Based on the above technical solution, this embodiment extends the environment variables declared in the image from the build phase to the runtime phase by using the template environment variable set as the merging basis and overriding the created environment variable set according to the same-name overriding rule. This ensures that the behavior of user processes started within the sandbox is consistent with that when running the image directly, and allows the caller to override the image's default values as needed. Compared with the existing technology that only issues environment variables explicitly passed by the caller, this embodiment eliminates the need for users to repeatedly declare image environment variables each time the sandbox is created, thus eliminating the semantic gap between the build and runtime phases. Furthermore, the merging is completed based on independent copies, and the template metadata remains unchanged during the merging process. This avoids data races in high-concurrency sandbox creation scenarios and ensures the correctness of the system under extreme loads.
[0062] For example, Figure 3 This is the second flowchart illustrating the environment variable propagation method for the cloud sandbox provided in this application. This method executes before obtaining the template metadata of the target template. It is the process of generating and storing template metadata during the template construction phase, performed by the template construction layer of the cloud sandbox, and includes the following steps: Step 301: Build the image build file corresponding to the target template into a snapshottable root file system, and generate the image configuration corresponding to the image build file.
[0063] The image build file is a declarative build file used to build the sandbox template, such as a Dockerfile. The image build file declares the environment variables required for application runtime through environment variable declaration instructions, such as the ENV instruction in the Dockerfile. The image configuration and root filesystem are both artifacts of the image build process. The image configuration records the image's metadata in structured data format, and its environment field records the environment variables declared by the environment variable declaration instructions in the image build file.
[0064] For example, the image configuration can be an Open Container Initiative (OCI) image configuration, whose environment field corresponds to the Config.Env field in the OCI image configuration. This field stores the environment variables declared in the image as a string array in the form of "KEY=VALUE". The root file system is a snapshottable root file system. After the build is complete, a snapshot can be performed on the root file system to generate a snapshot file used to restore the file system when the sandbox is instantiated.
[0065] Step 302: Parse the key-value pairs of each environment variable from the environment field of the image configuration to obtain the template environment variable set.
[0066] The environment variables stored in the environment field are in string format and need to be parsed into key-value pairs before they can participate in the subsequent merging process.
[0067] For example, the string in the environment field can be parsed one by one by the environment parsing function, and each "KEY=VALUE" can be split into data items with variable name as key and variable value as value, to obtain a template environment variable set organized by key-value pairs.
[0068] Specifically, take the following Dockerfile as an example: FROM python:3.11-slim ENV PYTHONPATH= / workspace / lib: / workspace / src ENV HF_HOME= / cache / huggingface The image configuration built from the above Dockerfile contains two environment variables, PYTHONPATH and HF_HOME, in its Config.Env field. After parsing, a template set of environment variables is obtained, where PYTHONPATH is set to / workspace / lib: / workspace / src and HF_HOME is set to / cache / huggingface.
[0069] Step 303: Write the template environment variable set into the template metadata.
[0070] The template metadata contains descriptive information about the sandbox template. In addition to environment variables, it can also record information such as the template's default user and default working directory.
[0071] For example, template metadata is maintained in the form of structured data objects, and the template environment variable set is saved as the environment variable field of the template metadata after being written, for example, as the Context.EnvVars field in the template metadata.
[0072] Step 304: Store the template metadata and the snapshot file corresponding to the root file system together in the object storage.
[0073] Among them, object storage is used to persist the data of the sandbox template, and snapshot files are files formed after the root file system is snapshotted. When the sandbox is instantiated, the file system is restored based on the snapshot files.
[0074] For example, template metadata and snapshot files can be stored in the same storage space of the object storage and associated with each other through a template identifier. The object storage can be a distributed object storage service such as GCS or S3.
[0075] It should be noted that after steps 301 to 304 above, the template construction phase completes the generation and persistence of template metadata. The set of template environment variables in the template metadata can be read and used during subsequent sandbox instantiation, without the need to maintain the environment variables declared in the image separately outside the construction phase.
[0076] Based on the above technical solution, this embodiment resolves the environment variables declared in the image to a set of template environment variables, and persists them along with the template metadata and snapshot files, ensuring that the environment variable declarations in the image are fully solidified during the template construction phase. Compared with the existing technology where image environment variables only apply during the construction period and are nowhere to be found after the construction is completed, this embodiment provides a reliable data foundation for restoring the environment variables declared in the image during runtime, effectively avoiding the semantic gap of environment variables between the construction and runtime periods.
[0077] For example, Figure 4 This is the third flowchart illustrating the environment variable propagation method for cloud sandboxes provided in this application. The process of merging environment variables under different instantiation paths of the sandbox includes the following steps: Step 401: Determine the sandbox instantiation path corresponding to the target template.
[0078] The instantiation path of the sandbox includes the standard snapshot recovery path and the file system cold start path.
[0079] It should be noted that the standard snapshot recovery path is used to restore the sandbox based on memory snapshots and file system snapshots, and is suitable for situations where the target template has a memory snapshot; the file system cold boot path is used to reboot the sandbox when the target template has a file system snapshot but no memory snapshot, and is suitable for situations where the target template only has a file system snapshot and requires a cold boot, in which case the sandbox kernel needs to be rebooted.
[0080] Specifically, when a snapshot is generated during the template building phase, if a memory snapshot is also retained, the sandbox instantiation can follow the standard snapshot recovery path; if only a file system snapshot is available without a memory image, the sandbox instantiation can only follow the file system cold start path. In both paths, the environment variable propagation device first receives the sandbox creation request and obtains the template metadata of the target template before executing its respective instantiation process.
[0081] Step 402: In the standard snapshot recovery path, after obtaining the template metadata of the target template and before instantiating the sandbox, merge the template environment variable set and the creation environment variable set to obtain the merged environment variable set.
[0082] The instantiation of the standard snapshot recovery path is based on the memory snapshot to restore the sandbox running state. Before the restoration, the initialization parameters of the sandbox need to be configured, including the restoration of the default user, default working directory and environment variables.
[0083] Specifically, in the standard snapshot recovery path, after the environment variable propagation device obtains the template metadata, it first restores the default user, default working directory, and other metadata in the template metadata. Then, according to the merging rules in step 202 above, it merges the set of template environment variables in the template metadata with the set of creation environment variables carried in the sandbox creation request, and sends the merge result as the initialization parameters of the process daemon. Subsequently, it performs a memory snapshot restore to complete the sandbox instantiation. The merging rules are the same as in step 202 above, and will not be repeated here.
[0084] Step 403: In the cold start path of the file system, after obtaining the template metadata of the target template and before instantiating the sandbox, merge the template environment variable set and the creation environment variable set to obtain the merged environment variable set.
[0085] The instantiation of the file system cold start path is based on the file system snapshot to reboot the sandbox. The initialization parameters of the sandbox also need to be configured before booting.
[0086] Specifically, in the file system cold boot path, after the environment variable propagation device obtains the template metadata, it first restores the default user, default working directory and other metadata in the template metadata, and then performs the same merging as step 402, merging the template environment variable set with the created environment variable set, and sending the merging result as the initialization parameter of the process daemon; then it reboots the sandbox kernel and mounts the root file system to complete the sandbox instantiation.
[0087] It should be noted that after steps 402 to 403 above, the standard snapshot recovery path and the file system cold start path use semantically equivalent merging rules. Both paths, when implemented independently, contain environment variable recovery logic, ensuring that the environment variable behavior is completely consistent regardless of how the sandbox is instantiated.
[0088] Based on the above technical solution, this embodiment performs the same environment variable merging in both the standard snapshot recovery path and the file system cold start path after obtaining the template metadata of the target template and before instantiating the sandbox, making the environment variable behavior completely symmetrical under the two sandbox instantiation paths. Compared with the existing technology where the cold start path is implemented independently and environment variables are doubly omitted in the two paths, this embodiment avoids the differences in environment variables caused by different sandbox instantiation methods, reducing the opacity to users and the debugging complexity of the production environment.
[0089] For example, Figure 5 This is the fourth flowchart illustrating the environment variable propagation method for the cloud sandbox provided in this application. This method executes after the aforementioned step of sending and merging the environment variable set, describing the environment variable injection process on the process daemon side before the user process starts, including the following steps: Step 501: The process daemon determines the received merged environment variable set as the default environment variable set.
[0090] The process daemon is a process management component residing within the sandbox. It receives a merged set of environment variables from the environment variable propagation device through an initialization interface and uses it as the default environment for processes within the sandbox. The merged environment variable set contains all environment variable key-value pairs merged from the template layer and the creation layer.
[0091] For example, the process daemon receives an initialization request, parses the merged environment variable set from the request body, and writes it into the process daemon's default environment variable storage as the process's default environment variable set.
[0092] Step 502: The process daemon receives the process start request.
[0093] The process startup request is used to start a user process within the sandbox. It can be initiated by the SDK caller and carries a set of process environment variables in the request.
[0094] Specifically, the process environment variable set is the set of environment variables passed in by the caller for a single process execution, such as through the `envs` parameter of the process startup interface. The process environment variable set only applies to the user process launched this time, has higher priority than the default environment variable set, and does not affect the environment variables of other user processes within the sandbox.
[0095] Step 503: The process daemon merges the process environment variable set with the default environment variable set to obtain the final environment variable set.
[0096] Specifically, for environment variable keys with the same name, the value in the process environment variable set is used to overwrite the value in the default environment variable set; for environment variable keys with different names, the environment variable key-value pairs in the process environment variable set and the default environment variable set are retained.
[0097] For example, if the default environment variable set has PYTHONPATH set to / workspace / lib: / workspace / src and HF_HOME set to / cache / huggingface, and the process environment variable set has PYTHONPATH set to / custom / run, then the final environment variable set after merging will have PYTHONPATH set to / custom / run and HF_HOME set to / cache / huggingface. A single process execution can further override the sandbox-level default values without affecting other processes.
[0098] Specifically, after merging steps 501 to 503 above, the final scope of environment variables covers three levels: template-level variables provide default values for image declarations, creation-level variables override template-level variables when the sandbox is created, and process-level variables override variables from the first two levels when a single process starts. Customization at any level does not affect the persistent data corresponding to other levels. Template metadata, the default sandbox environment, and the single-process environment are independent of each other, and the overriding relationship is clear and predictable.
[0099] Step 504: Start the user process based on the final set of environment variables.
[0100] In this context, the user process uses the final set of environment variables as its process environment, and the values of each environment variable read within the user process are consistent with the final set of environment variables.
[0101] For example, when a process daemon starts a user process, it writes the final set of environment variables into the user process's environment variable area. The user process can then read the overwritten values of each environment variable through a standard interface.
[0102] Based on the above technical solution, this embodiment determines the final set of environment variables according to process-level overriding rules on the process daemon side, using the default set of environment variables as a basis, before starting the user process. This allows for fine-grained overriding of sandbox-level environment variables during a single process execution. Compared to existing technologies where the order of environment variable overriding is determined by the underlying data structure and is opaque to the user, this embodiment features a clear top-down overriding relationship between the template layer, creation layer, and process layer. Users can precisely control the environment of a single process execution without modifying the image or affecting other processes. The system behavior is easy to test and document.
[0103] The environment variable propagation device for the cloud sandbox provided in this application is described below. The environment variable propagation device for the cloud sandbox described below can be referred to in correspondence with the environment variable propagation method for the cloud sandbox described above.
[0104] For example, Figure 6 This is a schematic diagram of the environmental variable propagation device for the cloud sandbox provided in this application. Figure 6 As shown, the device includes: an acquisition unit 601 and a processing unit 602.
[0105] The acquisition unit 601 is configured to, in response to a sandbox creation request, acquire the template metadata of the target template specified in the sandbox creation request. The template metadata includes a set of template environment variables, which is a collection of key-value pairs of environment variables obtained by parsing the image configuration corresponding to the target template during the template construction phase. For example, the template metadata is stored in object storage, and the acquisition unit 601 reads the template metadata of the target template from the object storage. This device can be deployed in the orchestration layer of a cloud sandbox, for example, implemented as an orchestration device for a cloud sandbox.
[0106] Processing unit 602 is used to merge the template environment variable set with the creation environment variable set carried in the sandbox creation request to obtain a merged environment variable set. Accordingly, processing unit 602 performs a merge on the template environment variable set obtained by acquisition unit 601 and the creation environment variable set carried in the sandbox creation request, and provides the merge result to processing unit 602.
[0107] Processing unit 602 is further configured to send the merged environment variable set to the process daemon within the sandbox. The merged environment variable set is used as the default environment variable set for user processes started within the sandbox. Accordingly, processing unit 602 distributes the merged environment variable set determined by itself to the process daemon within the sandbox.
[0108] Optionally, processing unit 602 is further configured to, during the template construction phase, construct the image build file corresponding to the target template into a snapshottable root file system and generate the image configuration corresponding to the image build file; parse the environment variable key-value pairs from the environment field of the image configuration to obtain a template environment variable set; write the template environment variable set into the template metadata, and store the template metadata and the snapshot file corresponding to the root file system together in object storage. The environment field of the image configuration records the environment variables declared by the environment variable declaration instructions in the image build file. Correspondingly, acquisition unit 601 is specifically used to acquire the template metadata of the target template from object storage.
[0109] Optionally, processing unit 602 is further configured to copy the template environment variable set to obtain an independent copy set; write each environment variable key-value pair in the created environment variable set into the independent copy set, wherein, for environment variable keys with the same name, the value in the created environment variable set overwrites the value of the corresponding environment variable key in the independent copy set; for environment variable keys with different names, both the environment variable key-value pairs in the template environment variable set and the environment variable key-value pairs in the created environment variable set are retained in the independent copy set; and determine the independent copy set after writing is completed as the merged environment variable set. The template environment variable set in the template metadata remains unchanged during the merging process.
[0110] Optionally, the processing unit 602 is further configured to send the set of creation environment variables carried in the sandbox creation request as the default set of environment variables for user processes started within the sandbox to the process daemon when the template environment variable set is empty.
[0111] Optionally, the processing unit 602 is further configured to, when the sandbox instantiation path is either a standard snapshot recovery path or a file system cold start path, merge the template environment variable set and the creation environment variable set after obtaining the template metadata of the target template and before instantiating the sandbox, to obtain a merged environment variable set. The standard snapshot recovery path is used to restore the sandbox based on memory snapshots and file system snapshots, while the file system cold start path is used to reboot the sandbox when the target template has a file system snapshot but not a memory snapshot.
[0112] Optionally, the processing unit 602 is further configured to send the merged environment variable set to the process daemon, so that the process daemon determines the received merged environment variable set as the default environment variable set; after receiving a process start request carrying the process environment variable set, the process daemon merges the process environment variable set with the default environment variable set to obtain the final environment variable set, and starts the user process according to the final environment variable set. Specifically, for environment variable keys with the same name, the values in the process environment variable set overwrite the values in the default environment variable set; for environment variable keys with different names, the environment variable key-value pairs in the process environment variable set and the default environment variable set are retained.
[0113] For example, Figure 7 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 7 As shown, the electronic device may include a processor 710, a communications interface 720, a memory 730, and a communication bus 740, wherein the processor 710, the communications interface 720, and the memory 730 communicate with each other via the communication bus 740. The processor 710 can call logical instructions in the memory 730 to execute any of the aforementioned cloud sandbox environment variable propagation methods.
[0114] Furthermore, the logical instructions in the aforementioned memory 730 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0115] On the other hand, this application also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer is able to execute any of the above-mentioned cloud sandbox environment variable propagation methods.
[0116] In another aspect, this application also provides a non-transitory computer-readable storage medium storing a computer program that, when executed by a processor, is implemented to perform any of the above-described cloud sandbox environment variable propagation methods.
[0117] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0118] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0119] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A method for propagating environmental variables in a cloud sandbox, characterized in that, The method includes: In response to a sandbox creation request, the template metadata of the target template specified in the sandbox creation request is obtained; wherein, the template metadata includes a set of template environment variables, which is a set of environment variable key-value pairs obtained by parsing the image configuration corresponding to the target template during the template construction phase; The template environment variable set is merged with the creation environment variable set carried in the sandbox creation request to obtain a merged environment variable set; The merged set of environment variables is sent to the process daemon within the sandbox; wherein the merged set of environment variables is used as the default set of environment variables for user processes started within the sandbox.
2. The method according to claim 1, characterized in that, Before obtaining the template metadata of the target template, the method further includes: During the template building phase, the image build file corresponding to the target template is built into a snapshottable root file system, and the image configuration corresponding to the image build file is generated; wherein, the environment field of the image configuration records the environment variables declared by the environment variable declaration instructions in the image build file; The key-value pairs of each environment variable are parsed from the environment fields of the image configuration to obtain the template environment variable set; Write the template environment variable set into the template metadata, and store the template metadata and the snapshot file corresponding to the root file system together in the object storage; The step of obtaining the template metadata of the target template includes: obtaining the template metadata of the target template from the object storage.
3. The method according to claim 1, characterized in that, The step of merging the template environment variable set with the creation environment variable set carried in the sandbox creation request to obtain a merged environment variable set includes: Copy the set of template environment variables to obtain a set of independent copies; Each environment variable key-value pair in the created environment variable set is written to the independent copy set; wherein, for environment variable keys with the same name, the value in the created environment variable set overwrites the value of the corresponding environment variable key in the independent copy set; for environment variable keys with different names, both the environment variable key-value pairs in the template environment variable set and the environment variable key-value pairs in the created environment variable set are retained in the independent copy set; The set of independent copies after the writing is completed is determined as the merged environment variable set; wherein, the template environment variable set in the template metadata remains unchanged during the merging process.
4. The method according to claim 1, characterized in that, If the template environment variable set is empty, the method further includes: The set of environment variables carried in the sandbox creation request is sent to the process daemon as the default set of environment variables for user processes started within the sandbox.
5. The method according to claim 1, characterized in that, The instantiation path of the sandbox includes a standard snapshot recovery path and a file system cold start path; wherein, the standard snapshot recovery path is used to restore the sandbox based on memory snapshots and file system snapshots, and the file system cold start path is used to reboot the sandbox when the target template has a file system snapshot but no memory snapshot; In both the standard snapshot recovery path and the file system cold start path, after obtaining the template metadata of the target template and before instantiating the sandbox, the template environment variable set and the creation environment variable set are merged to obtain the merged environment variable set.
6. The method according to claim 1, characterized in that, After sending the merged set of environment variables to the process daemon within the sandbox, the method further includes: The process daemon will determine the received merged environment variable set as the default environment variable set; The process daemon receives a process start request; wherein the process start request is used to start a user process within the sandbox, and the process start request carries a set of process environment variables; The process daemon merges the process environment variable set with the default environment variable set to obtain a final environment variable set; wherein, for environment variable keys with the same name, the values in the process environment variable set are overwritten with the values in the default environment variable set, and for environment variable keys with different names, the environment variable key-value pairs in the process environment variable set and the default environment variable set are retained; The user process is started based on the final set of environment variables.
7. An environment variable propagation system for a cloud-based sandbox, characterized in that, The system includes: The metadata acquisition module is used to respond to a sandbox creation request and acquire the template metadata of the target template specified by the sandbox creation request; wherein, the template metadata includes a template environment variable set, which is a set of environment variable key-value pairs obtained by parsing the image configuration corresponding to the target template during the template construction phase; The environment variable merging module is used to merge the template environment variable set with the creation environment variable set carried in the sandbox creation request to obtain a merged environment variable set; An environment variable sending module is used to send the merged environment variable set to the process daemon within the sandbox; wherein the merged environment variable set is used as the default environment variable set for user processes started within the sandbox.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the environment variable propagation method of the cloud sandbox as described in any one of claims 1 to 6.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the environment variable propagation method of the cloud sandbox as described in any one of claims 1 to 6.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the environment variable propagation method of the cloud sandbox as described in any one of claims 1 to 6.
