Skip to content

Commit

Permalink
feat: use Go templates for envvars (#126)
Browse files Browse the repository at this point in the history
This PR unifies the "magic variable" syntax from envvars with the Go
templating used for artifacts. That way we can support a more expressive
templating syntax out of the box.
  • Loading branch information
MegaRedHand authored Nov 27, 2024
1 parent 833696c commit 66e944b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 44 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ deployments:
# Key: env variable name
# Value: env variable's value
key: value
# Variables starting with `$` are magic variables and change to the real value at runtime
PRIVATE_KEY: "$deployer_private_key"
# Values inside double brackets '{{ }}' are expanded at runtime according to Go template syntax
PRIVATE_KEY: "{{.deployer_private_key}}"
# Input files to embed into the repo
input:
# Key: destination to insert the files in
Expand Down Expand Up @@ -271,9 +271,10 @@ services:
# Key: env variable name
# Value: env variable's value
key: value
# Values starting with `$` can be used to retrieve context information
# Values inside double brackets '{{ }}' are expanded at runtime according
# to Go template syntax.
# This example expands to the `ecdsa_keys` keystore's password
ECDSA_KEY_PASSWORD: $keys.ecdsa_keys.password
ECDSA_KEY_PASSWORD: "{{.keys.ecdsa_keys.password}}"
# Command to use when running the docker image
cmd: ["some", "option", "here"]

Expand Down
12 changes: 6 additions & 6 deletions examples/hello_world.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ deployments:
script: script/DeployEigenLayerCore.s.sol
env:
# NOTE: this is used inside the deployer script
PRIVATE_KEY: "$deployer_private_key"
PRIVATE_KEY: "0x{{.deployer_private_key}}"
# NOTE: these are needed because of repo's `foundry.toml`
HOLESKY_PRIVATE_KEY: ""
HOLESKY_RPC_URL: ""
Expand All @@ -22,7 +22,7 @@ deployments:
script: script/HelloWorldDeployer.s.sol
env:
# NOTE: this is used inside the deployer script
PRIVATE_KEY: "$deployer_private_key"
PRIVATE_KEY: "0x{{.deployer_private_key}}"
# NOTE: these are needed because of repo's `foundry.toml`
HOLESKY_PRIVATE_KEY: ""
HOLESKY_RPC_URL: ""
Expand All @@ -41,8 +41,8 @@ services:
/app/contracts/deployments/hello-world/: avs_addresses
env:
# This expands to the RPC node's URL
RPC_URL: "$http_rpc_url"
PRIVATE_KEY: "$keys.operator_ecdsa_keys.private_key"
RPC_URL: "{{.http_rpc_url}}"
PRIVATE_KEY: "{{.keys.operator_ecdsa_keys.private_key}}"
cmd: ["npm", "run", "start:operator"]

- name: traffic-generator
Expand All @@ -53,8 +53,8 @@ services:
/app/contracts/deployments/hello-world/: avs_addresses
env:
# This expands to the RPC node's URL
RPC_URL: "$http_rpc_url"
PRIVATE_KEY: "$keys.traffic_generator_ecdsa_keys.private_key"
RPC_URL: "{{.http_rpc_url}}"
PRIVATE_KEY: "{{.keys.traffic_generator_ecdsa_keys.private_key}}"
cmd: ["npm", "run", "start:traffic"]

keys:
Expand Down
12 changes: 6 additions & 6 deletions examples/hello_world_local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ deployments:
script: script/DeployEigenLayerCore.s.sol
env:
# NOTE: this is used inside the deployer script
PRIVATE_KEY: "$deployer_private_key"
PRIVATE_KEY: "0x{{.deployer_private_key}}"
# NOTE: these are needed because of repo's `foundry.toml`
HOLESKY_PRIVATE_KEY: ""
HOLESKY_RPC_URL: ""
Expand All @@ -29,7 +29,7 @@ deployments:
script: script/HelloWorldDeployer.s.sol
env:
# NOTE: this is used inside the deployer script
PRIVATE_KEY: "$deployer_private_key"
PRIVATE_KEY: "0x{{.deployer_private_key}}"
# NOTE: these are needed because of repo's `foundry.toml`
HOLESKY_PRIVATE_KEY: ""
HOLESKY_RPC_URL: ""
Expand All @@ -48,8 +48,8 @@ services:
/app/contracts/deployments/hello-world/: avs_addresses
env:
# This expands to the RPC node's URL
RPC_URL: "$http_rpc_url"
PRIVATE_KEY: "$keys.operator_ecdsa_keys.private_key"
RPC_URL: "{{.http_rpc_url}}"
PRIVATE_KEY: "{{.keys.operator_ecdsa_keys.private_key}}"
cmd: ["npm", "run", "start:operator"]

- name: traffic-generator
Expand All @@ -60,8 +60,8 @@ services:
/app/contracts/deployments/hello-world/: avs_addresses
env:
# This expands to the RPC node's URL
RPC_URL: "$http_rpc_url"
PRIVATE_KEY: "$keys.traffic_generator_ecdsa_keys.private_key"
RPC_URL: "{{.http_rpc_url}}"
PRIVATE_KEY: "{{.keys.traffic_generator_ecdsa_keys.private_key}}"
cmd: ["npm", "run", "start:traffic"]

keys:
Expand Down
4 changes: 2 additions & 2 deletions examples/incredible_squaring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ services:
- operator_bls_keys
- operator_ecdsa_keys
env:
OPERATOR_ECDSA_KEY_PASSWORD: $keys.operator_ecdsa_keys.password
OPERATOR_BLS_KEY_PASSWORD: $keys.operator_bls_keys.password
OPERATOR_ECDSA_KEY_PASSWORD: "{{.keys.operator_ecdsa_keys.password}}"
OPERATOR_BLS_KEY_PASSWORD: "{{.keys.operator_bls_keys.password}}"
cmd: ["--config", "/usr/src/app/config-files/operator-config.yaml"]

keys:
Expand Down
4 changes: 2 additions & 2 deletions examples/incredible_squaring_local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ services:
- operator_bls_keys
- operator_ecdsa_keys
env:
OPERATOR_ECDSA_KEY_PASSWORD: $keys.operator_ecdsa_keys.password
OPERATOR_BLS_KEY_PASSWORD: $keys.operator_bls_keys.password
OPERATOR_ECDSA_KEY_PASSWORD: "{{.keys.operator_ecdsa_keys.password}}"
OPERATOR_BLS_KEY_PASSWORD: "{{.keys.operator_bls_keys.password}}"
cmd: ["--config", "/usr/src/app/config-files/operator-config.yaml"]

keys:
Expand Down
2 changes: 1 addition & 1 deletion kurtosis_package/deployers/eigenlayer.star
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def whitelist_strategies(plan, context, strategies):
[data["addresses"][strategy["name"]] for strategy in strategies]
)
flag_params = ",".join(["true" for _ in strategies])
cmd = "set -e ; cast send --rpc-url {rpc} --private-key {pk} \
cmd = "set -e ; cast send --rpc-url {rpc} --private-key 0x{pk} \
{addr} 'addStrategiesToDepositWhitelist(address[],bool[])' '[{strategy_params}]' '[{flag_params}]'".format(
rpc=data["http_rpc_url"],
pk=data["deployer_private_key"],
Expand Down
2 changes: 1 addition & 1 deletion kurtosis_package/deployers/utils.star
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def deploy_generic_contract(plan, context, deployment):
contracts_path = deployment.get("contracts_path", ".")
script = deployment["script"]
extra_args = deployment.get("extra_args", "")
env_vars = shared_utils.generate_env_vars(context, deployment.get("env", {}))
env_vars = shared_utils.generate_env_vars(plan, context, deployment.get("env", {}))
verify = deployment.get("verify", False)
input = deployment.get("input", {})
output = deployment.get("output", {})
Expand Down
2 changes: 1 addition & 1 deletion kurtosis_package/main.star
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run(plan, args={}):
data = {
"http_rpc_url": http_rpc_url,
"ws_rpc_url": ws_url,
"deployer_private_key": "0x" + private_key,
"deployer_private_key": private_key,
"deployer_address": deployer_address,
}
plan.print("Initial data: " + json.indent(json.encode(data)))
Expand Down
4 changes: 3 additions & 1 deletion kurtosis_package/service_utils.star
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ def add_service(plan, service_args, context):
files = generate_input_files(plan, context, service_args.get("input", {}))

ports = generate_port_specs(service_args.get("ports", {}))
env_vars = shared_utils.generate_env_vars(context, service_args.get("env", {}))
env_vars = shared_utils.generate_env_vars(
plan, context, service_args.get("env", {})
)
config = ServiceConfig(
image=service_args["image"],
ports=ports,
Expand Down
38 changes: 18 additions & 20 deletions kurtosis_package/shared_utils.star
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,32 @@ def send_funds(plan, context, to, amount="10ether"):
)


def generate_env_vars(context, env_vars):
def generate_env_vars(plan, context, env_vars):
return {
env_var_name: expand(context, env_var_value)
env_var_name: expand(plan, context, env_var_value)
for env_var_name, env_var_value in env_vars.items()
}


def expand(context, var):
def expand(plan, context, var):
"""
Replaces values starting with `$` to their dynamically evaluated counterpart.
Values starting with `$$` are not expanded, and the leading `$` is removed.
Replaces templates containing double brackets ("{{") to their dynamically evaluated counterpart.
Example: "$service.some_service_name.ip_address" -> <some_service_name's ip address>
Example: "{{.service.some_service_name.ip_address}}" -> <some_service_name's ip address>
"""
if not var.startswith("$"):
# NOTE: this is just an optimization to avoid template rendering if it doesn't need it
if var.find("{{") == -1:
return var

if var.startswith("$$"):
return var[1:]
file_name = "expanded.txt"

path = var[1:].split(".")
value = context.data
for field in path:
value = value.get(field, None)
if value == None:
break

if value == None or type(value) == type({}):
fail("Invalid path: " + var)

return value
artifact = plan.render_templates(
config={file_name: struct(template=var, data=context.data)},
description="Expanding envvar '{}'".format(var),
)
result = plan.run_sh(
run="cat /artifact/" + file_name,
files={"/artifact": artifact},
)
expanded_value = result.output
return expanded_value

0 comments on commit 66e944b

Please sign in to comment.