Skip to content

Commit

Permalink
Merge pull request #1041 from IntersectMBO/chore/update-backend-config
Browse files Browse the repository at this point in the history
Pull test branch changes to develop
  • Loading branch information
mesudip authored May 19, 2024
2 parents 9646306 + 6124a93 commit c5a97e3
Show file tree
Hide file tree
Showing 34 changed files with 1,734 additions and 553 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/build-and-deploy-test-stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Build and deploy GovTool test stack
run-name: Deploy by @${{ github.actor }}

on:
push:
branches:
- test

env:
ENVIRONMENT: "test"
CARDANO_NETWORK: "sanchonet"

jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
env:
GRAFANA_ADMIN_PASSWORD: ${{ secrets.GRAFANA_ADMIN_PASSWORD }}
GRAFANA_SLACK_RECIPIENT: ${{ secrets.GRAFANA_SLACK_RECIPIENT }}
GRAFANA_SLACK_OAUTH_TOKEN: ${{ secrets.GRAFANA_SLACK_OAUTH_TOKEN }}
SENTRY_DSN_BACKEND: ${{ secrets.SENTRY_DSN_BACKEND }}
GTM_ID: ${{ secrets.GTM_ID }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN_FRONTEND }}
PIPELINE_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
USERSNAP_SPACE_API_KEY: ${{ secrets.USERSNAP_SPACE_API_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup SSH agent
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.TEST_STACK_SSH_KEY }}

- name: Run Ansible playbook
uses: dawidd6/action-ansible-playbook@v2
with:
playbook: playbook.yml
directory: ./tests/test-infrastructure
key: ${{ secrets.TEST_STACK_SSH_KEY }}
inventory: |
[test_server]
${{ secrets.TEST_STACK_SERVER_IP }} ansible_user=ec2-user
options: |
--verbose
env:
GOVTOOL_TAG: ${{ github.sha }}
37 changes: 8 additions & 29 deletions .github/workflows/lighthouse.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
name: Lighthouse

on:
push:
paths:
- govtool/frontend/**
- .github/workflows/lighthouse.yml
workflow_run:
workflows:
- Build and deploy GovTool test stack
types:
- completed

jobs:
lighthouse:
runs-on: ubuntu-latest
env:
NODE_OPTIONS: --max_old_space_size=4096
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16

- name: Install dependencies
run: npm install
working-directory: ./govtool/frontend

- name: Cache npm dependencies
id: npm-cache
uses: actions/cache@v3
with:
path: |
~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('govtool/frontend/package-lock.json', 'tests/govtool-frontend/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- run: npm install -g @lhci/[email protected]

- name: Run build and lighthouse task
- name: Run lighthouse task
working-directory: ./govtool/frontend
run: |
npm install
VITE_BASE_URL=https://staging.govtool.byron.network/ npm run build
lhci collect
- name: Evaluate reports
if: github.repository_owner != 'IntersectMBO'
working-directory: ./govtool/frontend
run: |
lhci assert --preset "lighthouse:recommended"
Expand All @@ -50,9 +34,4 @@ jobs:
if: github.repository_owner == 'IntersectMBO'
run: |
lhci assert --preset lighthouse:recommended || echo "LightHouse Assertion error ignored ..."
lhci upload --githubAppToken="${{ secrets.LHCI_GITHUB_APP_TOKEN }}" --token="${{ secrets.LHCI_SERVER_TOKEN }}" --serverBaseUrl=https://lighthouse.cardanoapi.io --ignoreDuplicateBuildFailure
curl -X POST https://ligththouse.cardanoapi.io/api/metrics/build-reports \
-d "@./lighthouseci/$(ls ./.lighthouseci |grep 'lhr.*\.json' | head -n 1)" \
-H "commit-hash: $(git rev-parse HEAD)" \
-H "secret-token: ${{ secrets.METRICS_SERVER_SECRET_TOKEN }}" \
-H 'Content-Type: application/json' || echo "Metric Upload error ignored ..."
lhci upload --githubAppToken="${{ secrets.LHCI_GITHUB_APP_TOKEN }}" --token="${{ secrets.LHCI_SERVER_TOKEN }}" --serverBaseUrl=https://lighthouse-govtool.cardanoapi.io --ignoreDuplicateBuildFailure
2 changes: 1 addition & 1 deletion .github/workflows/test_integration_playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
paths:
- .github/workflows/test_integration_playwright.yml
workflow_run:
workflows: ["Build and deploy GovTool to TEST server"]
workflows: ["Build and deploy GovTool test stack"]
types: [completed]

jobs:
Expand Down
4 changes: 0 additions & 4 deletions gov-action-loader/backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
KUBER_API_URL=https://sanchonet.kuber.cardanoapi.io
KUBER_API_KEY=xxxxxxxxxxxxx

## Not required anymore
BLOCKFROST_API_URL=
BLOCKFROST_PROJECT_ID=
6 changes: 1 addition & 5 deletions gov-action-loader/backend/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

class Settings(BaseSettings):
kuber_api_url: str
kuber_api_key: str

blockfrost_api_url: str
blockfrost_project_id: str

kuber_api_key: str = ''

settings = Settings()
5 changes: 3 additions & 2 deletions govtool/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ARG BASE_IMAGE_TAG
FROM 733019650473.dkr.ecr.eu-west-1.amazonaws.com/backend-base:$BASE_IMAGE_TAG
ARG BASE_IMAGE_TAG=latest
ARG BASE_IMAGE_REPO=733019650473.dkr.ecr.eu-west-1.amazonaws.com/backend-base
FROM $BASE_IMAGE_REPO:$BASE_IMAGE_TAG
WORKDIR /src
COPY . .
RUN cabal build
Expand Down
5 changes: 3 additions & 2 deletions govtool/frontend/.lighthouserc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ci:
collect:
staticDistDir: "./dist"
url:
- "http://localhost"
- https://govtool.cardanoapi.io
- https://govtool.cardanoapi.io/drep_directory
- https://govtool.cardanoapi.io/governance_actions
8 changes: 4 additions & 4 deletions tests/test-infrastructure/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
STACK_NAME=govtool
BASE_DOMAIN=cardanoapi.io
BLOCKFROST_API_URL=""
BLOCKFROST_PROJECT_ID=""
PROJECT_NAME=govtool
CARDANO_NETWORK=sanchonet
BASE_DOMAIN=govtool.cardanoapi.io
GOVTOOL_TAG=test
5 changes: 2 additions & 3 deletions tests/test-infrastructure/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
secrets/
configs/
docker-compose-rendered.yml
docker-compose-swarm-rendered.yml
docker-compose-services-rendered.yml
/*-rendered.yml

155 changes: 31 additions & 124 deletions tests/test-infrastructure/README.md
Original file line number Diff line number Diff line change
@@ -1,134 +1,41 @@
GovTool Test Infrastructure
====================

Services required for testing GovTool
Compose files and scripts to deploy and test environment of govtool.
Additionally, it deploys services required to perform integration test on the environment

## 1. Setting up the services
## Compose files and services
1. [basic-services](./docker-compose-basic-services.yml) : postgres and gateway
2. [cardano](./docker-compose-cardano.yml) : node, dbsync and kuber
3. [govtool](./docker-compose-govtool.yml) : govtool-frontend and govtool-backend
4. [govaction-loader](./docker-compose-govaction-loader.yml) : govaction-loader frontend and badkcne
5. [test](./docker-compose-test.yml) : lighthouse-server and metadata-api

## Setting up the services

#### a. Deploy with docker on swarm mode.

#### a. Update .env file and DNS records

- Create `.env` file by copying `.env.example` and update it.
- Make sure that DNS is pointed to the right server. Following are the domains used.
- lighthouse.BASE_DOMAIN
- metabase.BASE_DOMAIN
- sonarqube.BASE_DOMAIN
- metrics.BASE_DOMAIN
- kuber.BASE_DOMAIN


`docker stack deploy` command doesn't support `.env` file secret/config files.
There's a helper script `deploy-swarm.sh` to load the environment variables from `.env` file and generate rendered docker compose file.
```bash
cd ./test/test-infrastructire # cd into the test-infrastructure folder
docker swarm init # if swarm mode is not enabled yet.
docker compose build # build the images
docker node update xxxx --label-add govtool-test-stack=true ## set the node to be used for deploying the services
./gen-configs.sh # generate configs and secrets.
./deploy-swarm.sh prepare # start postgres and nginx
sleep 30 # wait for 30 secs for postgres to be healthy
./deploy-swarm.sh finalize # deploy all the required services.
```

#### b. Setup
When the stack is ready, further configuration is required it the services and github repo secrets and workflow files.

# 2. Services List

## SonarQube Server
#### Requires
- postgres database

#### Used by
- Github Action to submit sonar-sacanner result

`sonar-scanner` is used for static analysis of code.
The analysis generated by sonar-scanner is saved to SonarQube server for better visibility and to see progress over time.


**Docker Image:** [mc1arke/sonarqube-with-community-branch-plugin:9.9-community](https://hub.docker.com/layers/mc1arke/sonarqube-with-community-branch-plugin/9.9-community/images/sha256-b91ac551bea0fc3b394eaf7f82ea79115e03db9ab47d26610b9e1566723a07a5?context=explore)

**See :** [sonar-scanner](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/scanners/sonarscanner/), [actions/sonar-scanner](https://github.com/marketplace/actions/sonar-scanner)

### Initial configuration.

- Login and change the initial password.
```
username: admin
password: admin
```
- Create new project and set the projectKey in file [govtool/frontend/sonar-project.properties](../../govtool/frontend/sonar-project.properties)
- Update the github action secrets
- SONAR_HOST_URL
- SONAR_TOKEN


## Metabase Server
#### Requires
- postgres database

Metabase provides UI to show graphs and visualization from different datasource.
It is used for visualizing the test metrics and the api response times over time.

**Docker Image:** [metabase/metabase:v0.46.6.4](https://hub.docker.com/layers/metabase/metabase/v0.46.6.4/images/sha256-95c60db0c87c5da9cb81f6aefd0cd548fe2c14ff8c8dcba2ea58a338865cdbd9?context=explore)

### Initial Configuration
- Setup initial account for ligin via the webapp.
- Under database section in admin settings, add the `govtool_lithghouse` and `govtool_metrics` databases
- Select the database and add visualizations, queries for the data.

## LightHouse Report Server
#### Requires
- postgres database

#### Used by
- Github Action to submit lighthouse report.

Lighthouse has audits for performance, accessibility, progressive web apps, SEO, and more.
Lighthouse-Server is used to host and display the audits generated by lighthouse.

**Docker Image:** [patrickhulce/lhci-server:0.12.0](https://hub.docker.com/r/patrickhulce/lhci-server)

### Initial Configuration
- install lhci locally and run `lhci wizard` to setup project
- update `--serverBaseUrl={{...}}` parameter in [.github/workflows/lighthouse.yml](../../.github/workflows/lighthouse.yml)
- update `LHCI_SERVER_TOKEN` in github secrets.
- install lighthouse github app on the repo
- obtain app token from lighthouse app and update `LHCI_GITHUB_APP_TOKEN` secret

See: **[lighthouse-server-docs](https://googlechrome.github.io/lighthouse-ci/docs/server.html)**


## Metrics API Server
#### Requires
- postgres database
- metabase *(for result visualization)


#### Used by
- Github Action - backend test to submit test metrics.

Metrics API Server receives metrics collected during backend test and saves them to database.
The results are visualized in metabase.

### Initial Configuration
- update `RECORD_METRICS_API` variable in file [.github/workflows/test_backend.yml](../../.github/workflows/test_backend.yml)


**Source Code:** [tests/test-metrics-api](../test-metrics-api)

## Kuber Server
#### Requires
- cardano-node's socket connection

#### Used by
- Cypress integration test
- Governance Data Loader

Opensource API server for transaction building and querying the ledger .
Kuber makes it easy to construct and submit transaction from the frontend.

**Docker Image:** [dquadrant/kuber:70be9b0166177eab5cf33e603fd3dc579e14cf31](https://hub.docker.com/layers/dquadrant/kuber/70be9b0166177eab5cf33e603fd3dc579e14cf31/images/sha256-d3b3f7c2304da8c4777155b26220238b682c81a3ff2b14753a5dc41c4f151364?context=explore)
- lighthouse-{BASE_DOMAIN}
- kuber-{BASE_DOMAIN}
- metadata-{BASE_DOMAIN}
- governance-{BASE_DOMAIN}

### b. Prepare the machine.
- Buy a virtual server
- Install `docker` and enable `docker compose` plugin.
- execute `docker swarm init` command.

### c. One time setup on the machine.
- Generate secrets and configurations required by the services
`./gen-configs.sh`
- Mark the nodes with labels to specify where the services should be run. In case of single node
docker swarm, all labels can be set to single node.
`./deploy.sh prepare`

### d. Build images and deploy the stacks.
- `./build-images.sh`
- `./deploy.sh stack all`

### Initial Configuration
- update `CYPRESS_kuberApiUrl` variable in [.github/workflows/test_integration_cypress.yml](../../.github/workflows/test_integration_cypress.yml)
37 changes: 37 additions & 0 deletions tests/test-infrastructure/build-and-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
export BASE_IMAGE_NAME=govtool
export PROJECT_NAME=govtool
export CARDANO_NETWORK=sanchonet
export BASE_DOMAIN=govtool.cardanoapi.io

if [ -z "$GOVTOOL_TAG" ]; then
GOVTOOL_TAG="$(git rev-parse HEAD)"
fi
export GOVTOOL_TAG

. ./scripts/deploy-stack.sh

check_env

# Build images
./build-images.sh
function update-service(){
docker service update --image "$2" "$1"
}

if [[ "$1" == "update-images" ]]
then
update-service govtool_backend "$BASE_IMAGE_NAME"/backend:${GOVTOOL_TAG}
update-service govtool_frontend "$BASE_IMAGE_NAME"/frontend:${GOVTOOL_TAG}
update-service govtool_metadata-validation "$BASE_IMAGE_NAME"/metadata-validation:${GOVTOOL_TAG}

update-service govaction-loader_backend "$BASE_IMAGE_NAME"/gov-action-loader-backend:${GOVTOOL_TAG}
update-service govaction-loader_frontend "$BASE_IMAGE_NAME"/gov-action-loader-frontend:${GOVTOOL_TAG}

# test metadata API
update-service test_metadata-api "$BASE_IMAGE_NAME"/metadata-api:${GOVTOOL_TAG}

elif [[ $1 == "full" ]]
then
./deploy.sh stack all
fi
Loading

0 comments on commit c5a97e3

Please sign in to comment.