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

Add remote config generation #9

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions .github/actions/amplify-environment-variable-generation/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Amplify Environment Variable Generation
description: Generates environment variables for Amplify
inputs:
environment:
description: "Name of the environment e.g. dev, staging, prod"
required: true
github_token:
description: "GitHub token"
required: true

runs:
using: "composite"
steps:
- name: Setup build environment
id: setup
run: |
# Variables
environment="${{ inputs.environment }}"
config_file_name=${{ inputs.environment == 'dev' && 'development' || inputs.environment }}.json
target_branch=${{ inputs.environment == 'prod' && 'main' || inputs.environment }}

# Clone Perps Hedger Config GitHub repo
git clone -b $target_branch https://${{ inputs.github_token }}@github.com/orbs-network/perps-hedger-config.git

# Python setup and generate environment variables tfvars file
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python amplify-environment-variable-generation.py --config ./perps-hedger-config/appconfig/$config_file_name --environment $environment
mv terraform.tfvars ../../../../infrastructure/terraform.tfvars
shell: bash
working-directory: ./.github/actions/amplify-environment-variable-generation/amplify-environment-variable-generation
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import argparse
import json


def main():
# Parse arguments
parser = argparse.ArgumentParser()

parser.add_argument(
"--config", help="Path to the AWS ECS Scaling definition YAML file.", required=True)
parser.add_argument(
"--environment", help="Specify the environment", required=True)
args = parser.parse_args()

# Parse the config file as JSON
with open(args.config, "r") as file:
config = json.load(file)
# Access symmIdToMultiaccounts from globals
symm_accounts = config['globals']['symmIdToMultiaccounts']
env_strings = []
# Iterate and print key + first address
for symm_id, addresses in symm_accounts.items():
hedger_addresses = addresses
party_b_address = config['perSymmId'][symm_id]['symm']['partyBAddress']
symm_core_address = config['perSymmId'][symm_id]['symm']['symmCoreAddress']
env_strings.append(generate_templates(args.environment, symm_id, hedger_addresses, party_b_address, symm_core_address))
write_tfvars_file(env_strings)


def generate_templates(environment: str, symm_id: str, hedger_addresses: list[str], party_b_address: str, symm_core_address: str) -> str:
environment = environment + '.' if environment != 'prod' else ''
all_templates = []

for hedger_address in hedger_addresses:
templates = [
f'"NEXT_PUBLIC_{symm_id}_{hedger_address}_HEDGER_URL" = "www.{environment}perps-streaming.com/v1/{symm_id}/{hedger_address}/"',
f'"NEXT_PUBLIC_{symm_id}_{hedger_address}_MULTIACCOUNT" = "{hedger_address}"',
f'"NEXT_PUBLIC_{symm_id}_{hedger_address}_PARTY_B" = "{party_b_address}"',
f'"NEXT_PUBLIC_{symm_id}_{hedger_address}_DIAMOND_ADDRESS" = "{symm_core_address}"',
]
all_templates.extend(templates)

return '\n'.join(all_templates)


def write_tfvars_file(env_vars_list, output_path="terraform.tfvars"):
"""
Write environment variables to a tfvars file in the correct format.

Args:
env_vars_list (list): List of environment variable strings
output_path (str): Path to output tfvars file
"""
try:
# Format the content as a Terraform map variable
content = 'amplify_remote_environment_variables = {\n'
content += '\n'.join(f' {var}' for var in env_vars_list)
content += '\n}'

# Write to file
with open(output_path, 'w') as f:
f.write(content)

return True
except Exception as e:
print(f"Error writing tfvars file: {str(e)}")
return False


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
certifi==2024.12.14
charset-normalizer==3.4.0
idna==3.10
requests==2.32.3
urllib3==2.2.3
5 changes: 4 additions & 1 deletion .github/actions/aws-terraform/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ inputs:
amplify_basic_auth_credentials:
description: "Basic auth credentials for Amplify"
required: true
additional_tfvars_path:
description: "Path to additional tfvars file"
required: false

runs:
using: "composite"
Expand Down Expand Up @@ -67,7 +70,7 @@ runs:
run: |
export TF_VAR_environment=${environment}
${{ github.action_path }}/box.sh "Running Terraform plan"
plan_command="terraform plan -var-file='./environments/${{ inputs.environment }}/${{ inputs.environment }}.tfvars' -var 'region=${{ inputs.aws_deploy_region }}' -var 'amplify_custom_domain=${{inputs.amplify_custom_domain}}' -var 'amplify_basic_auth_credentials=${{inputs.amplify_basic_auth_credentials}}' -input=false -out=plan.out"
plan_command="terraform plan -var-file='./environments/${{ inputs.environment }}/${{ inputs.environment }}.tfvars' $([ -n "${{inputs.additional_tfvars_path}}" ] && echo "-var-file=${{inputs.additional_tfvars_path}}") -var 'region=${{ inputs.aws_deploy_region }}' -var 'amplify_custom_domain=${{inputs.amplify_custom_domain}}' -var 'amplify_basic_auth_credentials=${{inputs.amplify_basic_auth_credentials}}' -input=false -out=plan.out"
if [ "${{ github.event_name }}" == "workflow_dispatch" ] || [ "${{ inputs.branch }}" == "develop" ]; then
eval $plan_command
else
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/symmio_frontend_sdk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ jobs:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME_NONPROD }}
role-session-name: terraform-gha

- name: Generate Amplify Environment Variables (Dev)
uses: ./.github/actions/amplify-environment-variable-generation
with:
environment: "dev"
github_token: ${{ secrets.PERPS_HEDGER_CONFIG_GITHUB_TOKEN }}

- name: Run AWS Terraform
uses: ./.github/actions/aws-terraform
with:
Expand All @@ -49,6 +55,7 @@ jobs:
branch: ${GITHUB_REF##*/}
amplify_custom_domain: ${{ secrets.AMPLIFY_CUSTOM_DOMAIN_DEV }}
amplify_basic_auth_credentials: ${{ secrets.AMPLIFY_BASIC_AUTH_CREDENTIALS_DEV }}
additional_tfvars_path: "./terraform.tfvars"

aws-terraform-staging:
name: Run AWS Terraform (Staging)
Expand All @@ -64,6 +71,12 @@ jobs:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME_NONPROD }}
role-session-name: terraform-gha

- name: Generate Amplify Environment Variables (Dev)
uses: ./.github/actions/amplify-environment-variable-generation
with:
environment: "staging"
github_token: ${{ secrets.PERPS_HEDGER_CONFIG_GITHUB_TOKEN }}

- name: Run AWS Terraform
uses: ./.github/actions/aws-terraform
with:
Expand All @@ -76,6 +89,7 @@ jobs:
branch: ${GITHUB_REF##*/}
amplify_custom_domain: ${{ secrets.AMPLIFY_CUSTOM_DOMAIN_STAGING }}
amplify_basic_auth_credentials: ${{ secrets.AMPLIFY_BASIC_AUTH_CREDENTIALS_STAGING }}
additional_tfvars_path: "./terraform.tfvars"

aws-terraform-prod:
name: Run AWS Terraform (Prod)
Expand All @@ -91,6 +105,12 @@ jobs:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME_NONPROD }}
role-session-name: terraform-gha

- name: Generate Amplify Environment Variables (Staging)
uses: ./.github/actions/amplify-environment-variable-generation
with:
environment: "prod"
github_token: ${{ secrets.PERPS_HEDGER_CONFIG_GITHUB_TOKEN }}

- name: Run AWS Terraform
uses: ./.github/actions/aws-terraform
with:
Expand All @@ -103,6 +123,7 @@ jobs:
branch: ${GITHUB_REF##*/}
amplify_custom_domain: ${{ secrets.AMPLIFY_CUSTOM_DOMAIN_PROD }}
amplify_basic_auth_credentials: ${{ secrets.AMPLIFY_BASIC_AUTH_CREDENTIALS_PROD }}
additional_tfvars_path: "./terraform.tfvars"

check-for-failure:
name: Check for failure
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/amplify.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ resource "aws_amplify_app" "symmio_frontend_sdk" {
target = custom_rule.value.target
}
}
environment_variables = merge(var.amplify_environment_variables, var.amplify_remote_environment_variables)
environment_variables = merge(var.amplify_remote_environment_variables, var.amplify_environment_variables)
iam_service_role_arn = aws_iam_role.symmio_frontend_sdk_role.arn
platform = var.amplify_platform
}
Expand Down
19 changes: 0 additions & 19 deletions infrastructure/environments/dev/dev.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,7 @@ environment_name = "dev"
amplify_environment_variables = {
"AMPLIFY_DIFF_DEPLOY" = "false"
"AMPLIFY_MONOREPO_APP_ROOT" = "apps/Cloverfield"
"NEXT_PUBLIC_ARBITRUM_HEDGER_URL" = "www.dev.perps-streaming.com/v1/42161a/0x6273242a7E88b3De90822b31648C212215caaFE4/"
"NEXT_PUBLIC_ARBITRUM_INSTANT_BASE_URL" = "www.dev.perps-streaming.com/instant/v1/42161a/0xdB91D232E93969130272De309d3d914547604426/"
"NEXT_PUBLIC_ARBITRUM_MULTIACCOUNT" = "0x6273242a7E88b3De90822b31648C212215caaFE4"
"NEXT_PUBLIC_ARBITRUM_PARTY_B" = "0xdB91D232E93969130272De309d3d914547604426"
"NEXT_PUBLIC_BASE_HEDGER_URL" = "www.dev.perps-streaming.com/v1/8453a/0x8Ab178C07184ffD44F0ADfF4eA2ce6cFc33F3b86/"
"NEXT_PUBLIC_BASE_MULTIACCOUNT" = "0x8Ab178C07184ffD44F0ADfF4eA2ce6cFc33F3b86"
"NEXT_PUBLIC_BASE_PARTY_B" = "0xB6e3b44975f2966707a91747F89D2002ff8d62Db"
"NEXT_PUBLIC_BSC_HEDGER_URL" = "www.dev.perps-streaming.com/v1/56a/0x650a2D6C263A93cFF5EdD41f836ce832F05A1cF3/"
"NEXT_PUBLIC_BSC_INSTANT_BASE_URL" = "www.dev.perps-streaming.com/instant/v1/56a/0xdF077F5f52bc41A9072F9d0e5fB281770bCD1142/"
"NEXT_PUBLIC_BSC_MULTIACCOUNT" = "0x650a2D6C263A93cFF5EdD41f836ce832F05A1cF3"
"NEXT_PUBLIC_BSC_PARTY_B" = "0xdF077F5f52bc41A9072F9d0e5fB281770bCD1142"
"NEXT_PUBLIC_INFURA_KEY" = "a4bbaca8728a48a19acc9313ae5cc787"
"NEXT_PUBLIC_MANTLE_HEDGER_URL" = "www.dev.perps-streaming.com/v1/5000a/0xECbd0788bB5a72f9dFDAc1FFeAAF9B7c2B26E456/"
"NEXT_PUBLIC_MANTLE_INSTANT_BASE_URL" = "www.dev.perps-streaming.com/instant/v1/5000a/0xF9e39B4B30E26c18d2a725c0397Ed5A925efE46B/"
"NEXT_PUBLIC_MANTLE_MULTIACCOUNT" = "0xECbd0788bB5a72f9dFDAc1FFeAAF9B7c2B26E456"
"NEXT_PUBLIC_MANTLE_PARTY_B" = "0xF9e39B4B30E26c18d2a725c0397Ed5A925efE46B"
"NEXT_PUBLIC_MODE_HEDGER_URL" = "www.dev.perps-streaming.com/v1/34443a/0xC0ff4B56f62f20bA45f4229CC6BAaD986FA2a904/"
"NEXT_PUBLIC_MODE_INSTANT_BASE_URL" = "www.dev.perps-streaming.com/instant/v1/34443a/0x78E76Ac7fEc050cA785c19fFADDF57137b890543/"
"NEXT_PUBLIC_MODE_MULTIACCOUNT" = "0xC0ff4B56f62f20bA45f4229CC6BAaD986FA2a904"
"NEXT_PUBLIC_MODE_PARTY_B" = "0x78E76Ac7fEc050cA785c19fFADDF57137b890543"
"NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID" = "d78971d2807551a41e4caffa9ffa58c0"
}
# AWS IAM
Expand Down
16 changes: 1 addition & 15 deletions infrastructure/environments/prod/prod.tfvars
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
# General
environment_name = "prod"
# AWS Amplify
amplify_environment_variables = {
"AMPLIFY_DIFF_DEPLOY" = "false"
"AMPLIFY_MONOREPO_APP_ROOT" = "apps/Cloverfield"
"NEXT_PUBLIC_ARBITRUM_HEDGER_URL" = "www.perps-streaming.com/v1/42161a/0x6273242a7E88b3De90822b31648C212215caaFE4/"
"NEXT_PUBLIC_ARBITRUM_MULTIACCOUNT" = "0x6273242a7E88b3De90822b31648C212215caaFE4"
"NEXT_PUBLIC_ARBITRUM_PARTY_B" = "0x00c069d68bc7420740460DBC3cc3fFF9b3742421"
"NEXT_PUBLIC_BASE_HEDGER_URL" = "www.perps-streaming.com/v1/8453a/0x8Ab178C07184ffD44F0ADfF4eA2ce6cFc33F3b86//"
"NEXT_PUBLIC_BASE_MULTIACCOUNT" = "0x8Ab178C07184ffD44F0ADfF4eA2ce6cFc33F3b86"
"NEXT_PUBLIC_BASE_PARTY_B" = "0x1EcAbF0Eba136920677C9575FAccee36f30592cf"
"NEXT_PUBLIC_BSC_HEDGER_URL" = "www.perps-streaming.com/v1/56a/0x650a2D6C263A93cFF5EdD41f836ce832F05A1cF3/"
"NEXT_PUBLIC_BSC_MULTIACCOUNT" = "0x650a2D6C263A93cFF5EdD41f836ce832F05A1cF3"
"NEXT_PUBLIC_BSC_PARTY_B" = "0xD5A075C88A4188d666FA1e4051913BE6782982DA"
"NEXT_PUBLIC_INFURA_KEY" = "a4bbaca8728a48a19acc9313ae5cc787"
"NEXT_PUBLIC_MANTLE_HEDGER_URL" = "www.perps-streaming.com/v1/5000a/0xECbd0788bB5a72f9dFDAc1FFeAAF9B7c2B26E456/"
"NEXT_PUBLIC_MANTLE_MULTIACCOUNT" = "0xECbd0788bB5a72f9dFDAc1FFeAAF9B7c2B26E456f"
"NEXT_PUBLIC_MANTLE_PARTY_B" = "0x614bB1F3e0Ae5A393979468ED89088F05277312c"
"NEXT_PUBLIC_MODE_HEDGER_URL" = "www.perps-streaming.com/v1/34443a/0xC0ff4B56f62f20bA45f4229CC6BAaD986FA2a904/"
"NEXT_PUBLIC_MODE_MULTIACCOUNT" = "0xC0ff4B56f62f20bA45f4229CC6BAaD986FA2a904"
"NEXT_PUBLIC_MODE_PARTY_B" = "0x87FC464fa528260F1eeaB94FA20F73FeD8536Eb7"
"NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID" = "d78971d2807551a41e4caffa9ffa58c0"
}
# AWS IAM
Expand Down
20 changes: 1 addition & 19 deletions infrastructure/environments/staging/staging.tfvars
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
# General
environment_name = "staging"
# AWS Amplify
amplify_environment_variables = {
"AMPLIFY_DIFF_DEPLOY" = "false"
"AMPLIFY_MONOREPO_APP_ROOT" = "apps/Cloverfield"
"NEXT_PUBLIC_ARBITRUM_HEDGER_URL" = "www.staging.perps-streaming.com/v1/42161a/0x6273242a7E88b3De90822b31648C212215caaFE4/"
"NEXT_PUBLIC_ARBITRUM_INSTANT_BASE_URL" = "www.staging.perps-streaming.com/instant/v1/42161a/0x141269E29a770644C34e05B127AB621511f20109/"
"NEXT_PUBLIC_ARBITRUM_MULTIACCOUNT" = "0x6273242a7E88b3De90822b31648C212215caaFE4"
"NEXT_PUBLIC_ARBITRUM_PARTY_B" = "0x7D387771f6E23f353a4afCE21af521875C0825D0"
"NEXT_PUBLIC_BASE_HEDGER_URL" = "www.staging.perps-streaming.com/v1/8453a/0x8Ab178C07184ffD44F0ADfF4eA2ce6cFc33F3b86/"
"NEXT_PUBLIC_BASE_MULTIACCOUNT" = "0x8Ab178C07184ffD44F0ADfF4eA2ce6cFc33F3b86"
"NEXT_PUBLIC_BASE_PARTY_B" = "0xFC4aC3aF357eBe6d556DCD72453E9B30F6dC6873"
"NEXT_PUBLIC_BSC_HEDGER_URL" = "www.staging.perps-streaming.com/v1/56a/0x650a2D6C263A93cFF5EdD41f836ce832F05A1cF3/"
"NEXT_PUBLIC_BSC_INSTANT_BASE_URL" = "www.staging.perps-streaming.com/instant/v1/56a/0xdfeD11fE4af63B059EDBBDf53e9C633B331ed432/"
"NEXT_PUBLIC_BSC_MULTIACCOUNT" = "0x650a2D6C263A93cFF5EdD41f836ce832F05A1cF3"
"NEXT_PUBLIC_BSC_PARTY_B" = "0xdfeD11fE4af63B059EDBBDf53e9C633B331ed432"
"NEXT_PUBLIC_INFURA_KEY" = "a4bbaca8728a48a19acc9313ae5cc787"
"NEXT_PUBLIC_MANTLE_HEDGER_URL" = "www.staging.perps-streaming.com/v1/5000a/0xECbd0788bB5a72f9dFDAc1FFeAAF9B7c2B26E456/"
"NEXT_PUBLIC_MANTLE_INSTANT_BASE_URL" = "www.staging.perps-streaming.com/instant/v1/5000a/0x12De0352dd4187AF5797F5147c4179F9624346E2/"
"NEXT_PUBLIC_MANTLE_MULTIACCOUNT" = "0xECbd0788bB5a72f9dFDAc1FFeAAF9B7c2B26E456"
"NEXT_PUBLIC_MANTLE_PARTY_B" = "0x12De0352dd4187AF5797F5147c4179F9624346E2"
"NEXT_PUBLIC_MODE_HEDGER_URL" = "www.staging.perps-streaming.com/v1/34443a/0xC0ff4B56f62f20bA45f4229CC6BAaD986FA2a904/"
"NEXT_PUBLIC_MODE_INSTANT_BASE_URL" = "www.staging.perps-streaming.com/instant/v1/34443a/0x3f1913e75A892310EF0C8D519F427ADbCA4373e0/"
"NEXT_PUBLIC_MODE_MULTIACCOUNT" = "0xC0ff4B56f62f20bA45f4229CC6BAaD986FA2a904"
"NEXT_PUBLIC_MODE_PARTY_B" = "0x3f1913e75A892310EF0C8D519F427ADbCA4373e0"
"NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID" = "d78971d2807551a41e4caffa9ffa58c0"
}
# AWS IAM
Expand Down
Loading