Skip to content

Commit

Permalink
Merge branch 'develop' into ckb2021-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed May 31, 2021
2 parents 10ffedf + 4260d6d commit f70a3e8
Show file tree
Hide file tree
Showing 65 changed files with 2,338 additions and 145 deletions.
9 changes: 9 additions & 0 deletions .github/actions/terraform/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM hashicorp/terraform:0.15.0

RUN apk add bash

COPY main.sh /terraform/main.sh
COPY apply.sh /terraform/apply.sh
COPY cleanup.sh /terraform/cleanup.sh

ENTRYPOINT [ "/terraform/main.sh" ]
47 changes: 47 additions & 0 deletions .github/actions/terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Terraform

Suppose you have a job that applies AWS resources, runs your tests on these resources, and finally, destroys them whatever succeeded, failed or aborted. You can do this safely by writing Terraform configuration files and passing the directory path.

This action use [`post`](https://github.com/actions/runner/blob/be9632302ceef50bfb36ea998cea9c94c75e5d4d/docs/adrs/0361-wrapper-action.md) keyword to ensure that clean work always be done.

# Usage

<!-- start usage -->
```yaml
- uses: ./.github/actions/terraform
with:
# Required, directory path to Terraform configuration files
terraform_dir: /path/to/terraform/configuration/directory
```
<!-- end usage -->

# Examples

```yaml
# Generate a random SSH key pair to access instances, such as Ansible, via SSH
- run: ssh-keygen -N "" -f ${{ env.PRIVATE_KEY_PATH }}

- name: Apply Resources
uses: ./.github/actions/terraform
env:
# You may declare Terraform variables in `variables.tf`.
#
# Setting declared variables as environment variables is a common practice.
#
# [Learn more about Terraform variables](https://www.terraform.io/docs/language/values/variables.html#environment-variables)
TF_VAR_access_key: ${{ secrets.AWS_ACCESS_KEY }}
TF_VAR_secret_key: ${{ secrets.AWS_SECRET_KEY }}
TF_VAR_private_key_path: ${{ env.PRIVATE_KEY_PATH }}
TF_VAR_public_key_path: ${{ env.PUBLIC_KEY_PATH }}
TF_VAR_instance_type: t3.micro
with:
terraform_dir: /path/to/terraform/configuration/directory

- name: Output
working-directory: /path/to/terraform/configuration/directory
run: |
terraform output -raw "<ansible inventory defined as Terraform output>" >> /path/to/ansible/inventory-file
# Ansible-playbook do tests
- run: ansible-playbook playbook.yml -i /path/to/ansible/inventory-file ...
```
14 changes: 14 additions & 0 deletions .github/actions/terraform/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: 'Terraform'

description: 'Apply Terraform defined Infrastructures on-demand, destroy at job end'

inputs:
terraform_dir:
description: 'Directory path to Terraform configuration files'
required: true

runs:
using: 'docker'
image: 'Dockerfile'
entrypoint: '/terraform/apply.sh'
post-entrypoint: '/terraform/cleanup.sh'
6 changes: 6 additions & 0 deletions .github/actions/terraform/apply.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# Call by action `entrypoint`

SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
${SCRIPT_PATH}/main.sh apply
6 changes: 6 additions & 0 deletions .github/actions/terraform/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# Call by action `post_entrypoint`

SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
${SCRIPT_PATH}/main.sh cleanup
45 changes: 45 additions & 0 deletions .github/actions/terraform/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

set -euo pipefail

# Path to location of terraform files
TERRAFORM_DIR=${INPUT_TERRAFORM_DIR:-${TERRAFORM_DIR}}

function apply_infrastructure () {
cd ${TERRAFORM_DIR}
terraform init
terraform plan
terraform apply -auto-approve
}

function cleanup_infrastructure () {
cd ${TERRAFORM_DIR}
terraform init
terraform destroy -auto-approve
}

function cleanup_terraform_footprint () {
rm -rf ${TERRAFORM_DIR}/.terraform
rm -rf ${TERRAFORM_DIR}/terraform.tfstate
}

function main () {
[ $1 ] || { echo "Wrong usage"; exit 1; }

local command="${1}"
shift 1
case ${command} in
apply )
apply_infrastructure
;;
cleanup )
cleanup_infrastructure
cleanup_terraform_footprint
;;
* )
echo "Wrong usage"; exit 1;
;;
esac
}

main "$@"
131 changes: 131 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
name: Benchmark

on:
# Submit your review with a comment body containing "#benchmark"
pull_request_review:
types: [ submitted ]

jobs:
build_modified_ckb:
name: Build Modified CKB
runs-on: [ self-hosted, Linux, X64, building ]
if: |
contains(github.event.review.body, '#benchmark') &&
contains(fromJson('[ "janx", "doitian", "quake", "xxuejie", "zhangsoledad", "jjyr", "TheWaWaR", "driftluo", "keroro520", "yangby-cryptape", "liya2017" ]'), github.actor)
env:
CARGO_TARGET_DIR: "${{ github.workspace }}/../target"
steps:
- uses: actions/checkout@v2
- name: Modify Consensus Parameters And Build CKB
run: |
sed -i 's/const TWO_IN_TWO_OUT_COUNT: u64 = .*;$/const TWO_IN_TWO_OUT_COUNT: u64 = 8_000;/g' spec/src/consensus.rs
sed -i 's/const MAX_BLOCK_PROPOSALS_LIMIT: u64 = .*;$/const MAX_BLOCK_PROPOSALS_LIMIT: u64 = 12_000;/g' spec/src/consensus.rs
make build
cd ${{ env.CARGO_TARGET_DIR }}/release
tar cfJ ckb.tar.xz ckb
mv ckb.tar.xz ${{ github.workspace }}
- uses: actions/upload-artifact@v2
with:
name: ckb.tar.xz
path: ckb.tar.xz

benchmark:
name: Benchmark
runs-on: [ self-hosted, Linux, X64, operating ]
needs: [ build_modified_ckb ]
env:
TERRAFORM_DIR: ${{ github.workspace }}/.github/workflows/benchmark/terraform
ANSIBLE_DIR: ${{ github.workspace }}/.github/workflows/benchmark/ansible
ANSIBLE_INVENTORY: ${{ github.workspace }}/.github/workflows/benchmark/ansible/inventory.yml
PRIVATE_KEY_PATH: ${{ github.workspace }}/id_rsa
PUBLIC_KEY_PATH: ${{ github.workspace }}/id_rsa.pub
steps:
- uses: actions/checkout@v2
- name: Prepare - Download CKB Tarball
uses: actions/download-artifact@v2
with:
name: ckb.tar.xz

# Prepare
- name: Prepare - Generate Random SSH Key
run: ssh-keygen -N "" -f ${{ env.PRIVATE_KEY_PATH }}
- name: Prepare - Apply Resources Based on Terraform Files
uses: ./.github/actions/terraform
env:
# Environment variables used inside terraform/variables.tf
TF_VAR_access_key: ${{ secrets.AWS_ACCESS_KEY }}
TF_VAR_secret_key: ${{ secrets.AWS_SECRET_KEY }}
TF_VAR_prefix: benchmark-${{ github.repository }}-${{ github.run_id }}
TF_VAR_instances_count: 3
TF_VAR_instance_type: c5.xlarge
TF_VAR_instance_type_bastion: t2.xlarge
TF_VAR_private_key_path: ${{ env.PRIVATE_KEY_PATH }}
TF_VAR_public_key_path: ${{ env.PUBLIC_KEY_PATH }}
with:
terraform_dir: ${{ env.TERRAFORM_DIR }}
- name: Prepare - Output Ansible Inventory Based on Terraform State
working-directory: ${{ env.TERRAFORM_DIR }}
run: |
# `ansible_hosts` is defined in terraform/main.tf
terraform output -raw ansible_hosts > ${{ env.ANSIBLE_INVENTORY }}
terraform output -raw ansible_hosts
# Run
- name: Run Ansible Playbook
shell: bash
working-directory: ${{ env.ANSIBLE_DIR }}
env:
ANSIBLE_PRIVATE_KEY_FILE: ${{ env.PRIVATE_KEY_PATH }}
run: |
ansible-galaxy install --roles-path roles --role-file requirements.yml
# Install CKB on group instance
ansible-playbook playbook.yml \
-e 'hostname=instances' \
-e 'ckb_local_source=${{ github.workspace }}/ckb.tar.xz' \
-t ckb_install,ckb_configure
# Install CKB-Benchmark on hosts bastion-0
ansible-playbook playbook.yml -e 'hostname=bastions' -t ckb_benchmark_install,ckb_benchmark_configure
# Connect all CKB nodes into a network.
#
# In order to resolve network issues caused by IBD, we allowed instance-0 out
# of IBD, then restarted the other nodes to allow them to connect.
ansible-playbook playbook.yml -e 'hostname=instances' -t ckb_stop
ansible-playbook playbook.yml -e 'hostname=instance-0' -t ckb_start
ansible-playbook playbook.yml -e 'hostname=instance-0' -t ckb_miner_start
sleep 5
ansible-playbook playbook.yml -e 'hostname=instance-0' -t ckb_miner_stop
ansible-playbook playbook.yml -e 'hostname=instances' -t ckb_start
# Start benchmark
ansible-playbook playbook.yml -e 'hostname=bastions' -t ckb_benchmark_start
# Fetch and process result
# It will produce `report.yml`, `metrics.yml` and `result.tar.xz`
ansible-playbook playbook.yml -e 'hostname=bastions' -t fetch_ckb_benchmark_logfiles
ansible-playbook playbook.yml -e 'hostname=instances' -t fetch_ckb_logfiles
ansible-playbook playbook.yml -e 'hostname=instances' -t process_result
- name: Post Run - Upload Result `result.tar.xz`
uses: actions/upload-artifact@v2
with:
name: result.tar.xz
path: ${{ env.ANSIBLE_DIR }}/result.tar.xz
- name: Post Run - Construct Report
run: |
echo 'BENCHMARK_REPORT<<EOF' >> $GITHUB_ENV
cat ${ANSIBLE_DIR}/report.yml >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Post Run - Comment Report
uses: peter-evans/create-or-update-comment@v1
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
**Benchmark Report**: https://www.github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
```yaml
${{ env.BENCHMARK_REPORT }}
```
73 changes: 73 additions & 0 deletions .github/workflows/benchmark/ansible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Ansible Playbook Used For Benchmark Workflow

This Ansible playbook is part of the "Benchmark" workflow.

## Pre-Install

```
ansible-galaxy install -r requirements.yml
```

## Inventory File

To use this playbook, the provided inventory file must have 2 groups of hosts:
* Group "instances" indicates CKB nodes and hosts named by "instance-0", "instance-1" and so on.

* Group "bastions" indicates CKB-Benchmark nodes, only have one host, named "bastion-0"

All hosts must have `instance_type` variables, which are used to generate the benchmark report.

Here is an example:

```yaml
instances:
hosts:
instance-0:
ansible_user: ubuntu
ansible_host: 1.23.45.123
instance_type: c5.large
instance-0:
ansible_user: ubuntu
ansible_host: 1.23.45.124
instance_type: c5.large

bastions:
hosts:
bastion-0:
ansible_user: ubuntu
ansible_host: 1.23.45.125
instance_type: t2.large
```

## Usage Example

```bash
export ANSIBLE_INVENTORY=<path to your inventory file>
export ANSIBLE_PRIVATE_KEY_FILE=<path to your privary key file>

# Install CKB on group instances
ansible-playbook playbook.yml -e 'hostname=instances' -t ckb_install,ckb_configure

# Install CKB-Benchmark on hosts bastion-0
ansible-playbook playbook.yml -e 'hostname=bastions' -t ckb_benchmark_install,ckb_benchmark_configure

# Connect all CKB nodes into a network.
#
# In order to resolve network issues caused by IBD, we allowed instance-0 out
# of IBD, then restarted the other nodes to allow them to connect.
ansible-playbook playbook.yml -e 'hostname=instances' -t ckb_stop
ansible-playbook playbook.yml -e 'hostname=instance-0' -t ckb_start
ansible-playbook playbook.yml -e 'hostname=instance-0' -t ckb_miner_start
sleep 5
ansible-playbook playbook.yml -e 'hostname=instance-0' -t ckb_miner_stop
ansible-playbook playbook.yml -e 'hostname=instances' -t ckb_start

# Start benchmark
ansible-playbook playbook.yml -e 'hostname=bastions' -t ckb_benchmark_start

# Fetch and process result
# It will produce `report.yml`, `metrics.yml` and `result.tar.xz`
ansible-playbook playbook.yml -e 'hostname=bastions' -t fetch_ckb_benchmark_logfiles
ansible-playbook playbook.yml -e 'hostname=instances' -t fetch_ckb_logfiles
ansible-playbook playbook.yml -e 'hostname=instances' -t process_result
```
4 changes: 4 additions & 0 deletions .github/workflows/benchmark/ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[defaults]
host_key_checking = False
gathering = explicit
timeout = 60
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mb�\7�JS��_C&������8P��ׂ]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`&{R}�m@�g �G�1t�\��6�Y���l��
Loading

0 comments on commit f70a3e8

Please sign in to comment.