Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsonnet config #52

Merged
merged 24 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ dmypy.json
cython_debug/

# VS Code configuration
.vscode
.vscode
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ Remember to run `poetry env info` after `poetry install` and update this `python
}
```

## Test
### Install jsonnet
More about [jsonnet](https://jsonnet.org).
```
make test
```


## FAQ

Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ deepdiff = "^5.6.0"
flake8 = "^4.0.1"
black = "^21.12b0"
isort = "^5.10.1"
jsonnet = "^0.18.0"

[tool.poetry.scripts]
pystarport = "pystarport.cli:main"
Expand Down
10 changes: 7 additions & 3 deletions pystarport/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
import threading
import time
from pathlib import Path
from pathlib import Path, PurePosixPath
yihuang marked this conversation as resolved.
Show resolved Hide resolved
from typing import List

import durations
Expand All @@ -24,7 +24,7 @@
from . import ports
from .app import CHAIN, IMAGE, SUPERVISOR_CONFIG_FILE
from .cosmoscli import ChainCommand, CosmosCLI, ModuleAccount, module_address
from .expansion import expand_yaml
from .expansion import expand_jsonnet, expand_yaml
from .ledger import ZEMU_BUTTON_PORT, ZEMU_HOST
from .utils import format_doc_string, interact, write_ini

Expand Down Expand Up @@ -924,7 +924,11 @@ def init_cluster(
cmd=None,
gen_compose_file=False,
):
config = expand_yaml(config_path, dotenv)
extension = PurePosixPath(config_path).suffix
yihuang marked this conversation as resolved.
Show resolved Hide resolved
if extension == ".jsonnet":
config = expand_jsonnet(config_path, dotenv)
else:
config = expand_yaml(config_path, dotenv)

relayer_config = config.pop("relayer", {})
for chain_id, cfg in config.items():
Expand Down
54 changes: 32 additions & 22 deletions pystarport/expansion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import os
from pathlib import Path
from typing import Any, Mapping, Optional, Text

import _jsonnet
import jsonmerge
import yaml
from dotenv import dotenv_values, load_dotenv
Expand Down Expand Up @@ -44,12 +46,34 @@ def _expand(value, variables):
return "".join([str(atom.resolve(variables)) for atom in atoms])


def expand(config, dotenv, path):
config_vars = dict(os.environ) # load system env

if dotenv is not None:
if "dotenv" in config:
_ = config.pop("dotenv", {}) # remove dotenv field if exists
elif "dotenv" in config:
dotenv = config.pop("dotenv", {}) # pop dotenv field if exists

if dotenv:
if not isinstance(dotenv, str):
raise ValueError(f"Invalid value passed to dotenv: {dotenv}")
env_path = path.parent / dotenv
if not env_path.is_file():
raise ValueError(
f"Dotenv specified in config but not found at path: {env_path}"
)
config_vars.update(dotenv_values(dotenv_path=env_path)) # type: ignore
load_dotenv(dotenv_path=env_path)

return expand_posix_vars(config, config_vars)


def expand_yaml(config_path, dotenv):
path = Path(config_path)
parent = path.parent
YamlIncludeConstructor.add_to_loader_class(
loader_class=yaml.FullLoader,
base_dir=parent,
base_dir=path.parent,
)

with open(path) as f:
Expand All @@ -59,26 +83,12 @@ def expand_yaml(config_path, dotenv):
if include:
config = jsonmerge.merge(include, config)

def expand(dotenv):
if not isinstance(dotenv, str):
raise ValueError(f"Invalid value passed to dotenv: {dotenv}")
config_vars = dict(os.environ) # load system env
env_path = parent.joinpath(dotenv)
if not env_path.is_file():
raise ValueError(
f"Dotenv specified in config but not found at path: {env_path}"
)
config_vars.update(dotenv_values(dotenv_path=env_path)) # type: ignore
load_dotenv(dotenv_path=env_path)
return expand_posix_vars(config, config_vars)
config = expand(config, dotenv, path)
return config

if dotenv is not None:
if "dotenv" in config:
_ = config.pop("dotenv", {}) # remove dotenv field if exists
dotenv_path = dotenv
config = expand(dotenv_path)
elif "dotenv" in config:
dotenv_path = config.pop("dotenv", {}) # pop dotenv field if exists
config = expand(dotenv_path)

def expand_jsonnet(config_path, dotenv):
path = Path(config_path)
config = json.loads(_jsonnet.evaluate_file(config_path))
config = expand(config, dotenv, path)
return config
1 change: 1 addition & 0 deletions pystarport/tests/test_expansion/base.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './default.jsonnet'
67 changes: 33 additions & 34 deletions pystarport/tests/test_expansion/base.yaml
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
cronos_777-1:
cmd: cronosd
start-flags: "--trace"
accounts:
- coins: "10000000000000000000000basetcro"
mnemonic: "notable error gospel wave pair ugly measure elite toddler cost various fly make eye ketchup despair slab throw tribe swarm word fruit into inmate"
name: "community"
- coins: "20000000000000000000000basetcro"
mnemonic: "shed crumble dismiss loyal latin million oblige gesture shrug still oxygen custom remove ribbon disorder palace addict again blanket sad flock consider obey popular"
name: "signer1"
- coins: "30000000000000000000000basetcro"
mnemonic: "night renew tonight dinner shaft scheme domain oppose echo summer broccoli agent face guitar surface belt veteran siren poem alcohol menu custom crunch index"
name: "signer2"
app-config:
minimum-gas-prices: 5000000000000basetcro
json-rpc:
address: "0.0.0.0:{EVMRPC_PORT}"
ws-address: "0.0.0.0:{EVMRPC_PORT_WS}"
validators:
- coins: 1000000000000000000stake,10000000000000000000000basetcro
staked: 1000000000000000000stake
mnemonic: visit craft resemble online window solution west chuckle music diesel vital settle comic tribe project blame bulb armed flower region sausage mercy arrive release
- coins: 1000000000000000000stake,10000000000000000000000basetcro
staked: 1000000000000000000stake
mnemonic: direct travel shrug hand twice agent sail sell jump phone velvet pilot mango charge usual multiply orient garment bleak virtual action mention panda vast
accounts:
- name: community
coins: 10000000000000000000000basetcro
mnemonic: "notable error gospel wave pair ugly measure elite toddler cost various fly make eye ketchup despair slab throw tribe swarm word fruit into inmate"
- name: signer1
coins: 20000000000000000000000basetcro
mnemonic: shed crumble dismiss loyal latin million oblige gesture shrug still oxygen custom remove ribbon disorder palace addict again blanket sad flock consider obey popular
- name: signer2
coins: 30000000000000000000000basetcro
mnemonic: night renew tonight dinner shaft scheme domain oppose echo summer broccoli agent face guitar surface belt veteran siren poem alcohol menu custom crunch index

minimum-gas-prices: "5000000000000basetcro"
cmd: "cronosd"
genesis:
consensus_params:
block:
max_bytes: "1048576"
max_gas: "81500000"
app_state:
evm:
params:
evm_denom: basetcro
cronos:
params:
cronos_admin: crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp
cronos_admin: "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp"
enable_auto_deployment: true
ibc_cro_denom: ibc/6411AE2ADA1E73DB59DB151A8988F9B7D5E7E233D8414DB6817F8F1A01611F86
ibc_cro_denom: "ibc/6411AE2ADA1E73DB59DB151A8988F9B7D5E7E233D8414DB6817F8F1A01611F86"
evm:
params:
evm_denom: "basetcro"
gov:
voting_params:
voting_period: "10s"
deposit_params:
max_deposit_period: "10s"
min_deposit:
- denom: "basetcro"
amount: "1"
- amount: "1"
denom: "basetcro"
voting_params:
voting_period: "10s"
transfer:
params:
receive_enabled: true
send_enabled: true
consensus_params:
block:
max_bytes: "1048576"
max_gas: "81500000"
start-flags: "--trace"
validators:
- coins: "1000000000000000000stake,10000000000000000000000basetcro"
mnemonic: "visit craft resemble online window solution west chuckle music diesel vital settle comic tribe project blame bulb armed flower region sausage mercy arrive release"
staked: "1000000000000000000stake"
- coins: "1000000000000000000stake,10000000000000000000000basetcro"
mnemonic: "direct travel shrug hand twice agent sail sell jump phone velvet pilot mango charge usual multiply orient garment bleak virtual action mention panda vast"
staked: "1000000000000000000stake"
1 change: 1 addition & 0 deletions pystarport/tests/test_expansion/base_yaml.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.manifestYamlDoc(import './base.jsonnet', true, false)
40 changes: 40 additions & 0 deletions pystarport/tests/test_expansion/cronos_has_dotenv.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local config = import './default.jsonnet';
local Utils = import 'utils.jsonnet';

config {
dotenv+: 'dotenv',
'cronos_777-1'+: {
validators: [
Utils.validator('${VALIDATOR1_MNEMONIC}'),
Utils.validator('${VALIDATOR2_MNEMONIC}'),
],
accounts: [
Utils.account(
'community',
'10000000000000000000000basetcro',
'${COMMUNITY_MNEMONIC}',
),
Utils.account(
'signer1',
'20000000000000000000000basetcro',
'${SIGNER1_MNEMONIC}',
),
Utils.account(
'signer2',
'30000000000000000000000basetcro',
'${SIGNER2_MNEMONIC}',
),
],
genesis+: {
app_state+: {
cronos: {
params: {
cronos_admin: '${CRONOS_ADMIN}',
enable_auto_deployment: true,
ibc_cro_denom: '${IBC_CRO_DENOM}',
},
},
},
},
},
}
67 changes: 46 additions & 21 deletions pystarport/tests/test_expansion/cronos_has_dotenv.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
include: !include base.yaml
yihuang marked this conversation as resolved.
Show resolved Hide resolved
dotenv: dotenv
cronos_777-1:
validators:
- coins: 1000000000000000000stake,10000000000000000000000basetcro
staked: 1000000000000000000stake
mnemonic: ${VALIDATOR1_MNEMONIC}
- coins: 1000000000000000000stake,10000000000000000000000basetcro
staked: 1000000000000000000stake
mnemonic: ${VALIDATOR2_MNEMONIC}
accounts:
- name: community
coins: 10000000000000000000000basetcro
mnemonic: ${COMMUNITY_MNEMONIC}
- name: signer1
coins: 20000000000000000000000basetcro
mnemonic: ${SIGNER1_MNEMONIC}
- name: signer2
coins: 30000000000000000000000basetcro
mnemonic: ${SIGNER2_MNEMONIC}

- coins: "10000000000000000000000basetcro"
mnemonic: "${COMMUNITY_MNEMONIC}"
name: "community"
- coins: "20000000000000000000000basetcro"
mnemonic: "${SIGNER1_MNEMONIC}"
name: "signer1"
- coins: "30000000000000000000000basetcro"
mnemonic: "${SIGNER2_MNEMONIC}"
name: "signer2"
app-config:
json-rpc:
address: "0.0.0.0:{EVMRPC_PORT}"
ws-address: "0.0.0.0:{EVMRPC_PORT_WS}"
minimum-gas-prices: "5000000000000basetcro"
cmd: "cronosd"
genesis:
app_state:
cronos:
params:
cronos_admin: ${CRONOS_ADMIN}
ibc_cro_denom: ${IBC_CRO_DENOM}
cronos_admin: "${CRONOS_ADMIN}"
enable_auto_deployment: true
ibc_cro_denom: "${IBC_CRO_DENOM}"
evm:
params:
evm_denom: "basetcro"
gov:
deposit_params:
max_deposit_period: "10s"
min_deposit:
- amount: "1"
denom: "basetcro"
voting_params:
voting_period: "10s"
transfer:
params:
receive_enabled: true
send_enabled: true
consensus_params:
block:
max_bytes: "1048576"
max_gas: "81500000"
start-flags: "--trace"
validators:
- coins: "1000000000000000000stake,10000000000000000000000basetcro"
mnemonic: "${VALIDATOR1_MNEMONIC}"
staked: "1000000000000000000stake"
- coins: "1000000000000000000stake,10000000000000000000000basetcro"
mnemonic: "${VALIDATOR2_MNEMONIC}"
staked: "1000000000000000000stake"
dotenv: "dotenv"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.manifestYamlDoc(import './cronos_has_dotenv.jsonnet', true, false)
Loading