Skip to content

Commit

Permalink
Merge pull request #425 from privacy-scaling-explorations/chore/merge…
Browse files Browse the repository at this point in the history
…-main-dev

Merge main into dev
  • Loading branch information
vplasencia authored Mar 5, 2024
2 parents 9057ec7 + 0cd1124 commit 0ff6435
Show file tree
Hide file tree
Showing 19 changed files with 373 additions and 210 deletions.
36 changes: 36 additions & 0 deletions .github/scripts/cloud-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

set -eux
trap 'poweroff' TERM EXIT INT

Repo=$(curl http://169.254.169.254/latest/meta-data/tags/instance/Repo)
Branch=$(curl http://169.254.169.254/latest/meta-data/tags/instance/Branch)

Local_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)

# Install pkgs
apt-get update
apt-get install -y net-tools
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
usermod -aG docker ubuntu

su -P ubuntu -c "git clone -b $Branch --single-branch https://github.com/ntampakas/$Repo.git /home/ubuntu/$Repo"
su -P ubuntu -c "cp /home/ubuntu/$Repo/apps/api/.env.example /home/ubuntu/$Repo/apps/api/.env"
su -P ubuntu -c "sed -i 's/\(DB_TYPE=\).*/\1postgres/' /home/ubuntu/$Repo/apps/api/.env"
su -P ubuntu -c "sed -i 's/\(DB_URL=\).*/\1postgres:\/\/root:helloworld\@postgres:5432\/bandada/' /home/ubuntu/$Repo/apps/api/.env"

su -P ubuntu -c "rm /home/ubuntu/$Repo/apps/{client,dashboard}/.env.production /home/ubuntu/$Repo/apps/{client,dashboard}/.env.staging"
su -P ubuntu -c "sed -i 's/localhost/$Local_IP/g' /home/ubuntu/$Repo/apps/{client,dashboard}/.env.local"

su -P ubuntu -c "cd /home/ubuntu/$Repo ; docker compose up -d"

sleep 6h
16 changes: 16 additions & 0 deletions .github/scripts/get_ip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

set -eux

TRIGGER=$1
ACTOR=$2
VPC="vpc-07d12b28fd72d152e"

# Stop here in schedule
[ ! $TRIGGER = "maintenance" ] || exit 0

sleep 10

EC2_IP=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=[running]" "Name=tag:Name,Values='bandada-ephemeral-$ACTOR-*'" "Name=network-interface.vpc-id,Values=[$VPC]" --query "Reservations[*].Instances[*].[NetworkInterfaces[*].[PrivateIpAddress]]" --output text)

echo "URL: http://$EC2_IP:3001"
47 changes: 47 additions & 0 deletions .github/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh

set -eux

TRIGGER=$1
BRANCH=$2
ACTOR=$3
SHA=$4
AMI="ami-04e601abe3e1a910f"
SG="sg-04d8c2eb4beca2de9"
SUBNET="subnet-066cf21c696ef55c8"
VPC="vpc-07d12b28fd72d152e"

# Kill old instances
CURRENT_TIME_EPOCH=$(date -d `date -Is` +"%s")
EC2=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=[running]" "Name=tag:Name,Values='bandada-ephemeral-*'" "Name=network-interface.vpc-id,Values=[$VPC]" --query "Reservations[*].Instances[*].InstanceId" --output text)
for i in $EC2; do
EC2_LAUNCH_TIME=$(aws ec2 describe-instances --instance-ids $i --query 'Reservations[*].Instances[*].LaunchTime' --output text)
LAUNCH_TIME_EPOCH=$(date -d $EC2_LAUNCH_TIME +"%s")
diff=$(expr $CURRENT_TIME_EPOCH - $LAUNCH_TIME_EPOCH)

if [ $diff -gt 21600 ]; then
aws ec2 terminate-instances --instance-ids $i
fi
done

# Stop here in schedule
[ ! $TRIGGER = "maintenance" ] || exit 0

# Check if actor ec2 exists
ACTOR_EC2=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=[running]" "Name=tag:Name,Values='bandada-ephemeral-$ACTOR-*'" "Name=network-interface.vpc-id,Values=[$VPC]" --query "Reservations[*].Instances[*].InstanceId" --output text)

[ -z $ACTOR_EC2 ] || aws ec2 terminate-instances --instance-ids $ACTOR_EC2

# Launch new instance
aws ec2 run-instances \
--user-data "file://.github/scripts/cloud-init.sh" \
--image-id $AMI \
--count 1 \
--instance-type t3a.large \
--key-name bandada \
--security-group-ids $SG \
--subnet-id $SUBNET \
--block-device-mappings "[{\"DeviceName\":\"/dev/sda1\",\"Ebs\":{\"VolumeSize\":32,\"DeleteOnTermination\":true}}]" \
--instance-initiated-shutdown-behavior terminate \
--tag-specification "ResourceType=instance,Tags=[{Key=Name,Value="bandada-ephemeral-$ACTOR-$SHA"},{Key=Repo,Value="bandada"},{Key=Branch,Value="$BRANCH"}]" \
--metadata-options "InstanceMetadataTags=enabled"
30 changes: 6 additions & 24 deletions .github/workflows/coverall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ on:
branches:
- main

env:
DEFAULT_NETWORK: localhost

jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -18,37 +15,22 @@ jobs:
- contracts

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.x
cache: "yarn"

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"

- name: Restore yarn cache
uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
- name: Build monorepo
run: yarn

- name: Build libraries
run: yarn workspaces foreach --no-private run build

- name: Test code
run: yarn test:${{ matrix.tests }}:coverage

- name: Coveralls
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: run-${{ matrix.tests }}
Expand All @@ -60,7 +42,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
fi
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
persist-credentials: false

Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/ephemeral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Ephemeral
on:
schedule:
- cron: "0 */6 * * *"
workflow_dispatch:
inputs:
action:
description: "Action"
required: true
default: "spawn"
type: choice
options:
- spawn

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
deploy:
timeout-minutes: 5
runs-on: ubuntu-latest
env:
DATA: ${{ github.event.inputs.action || 'maintenance' }}
permissions:
id-token: write
contents: read

steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::490752553772:role/bandada-ephemeral-deploy-slc
role-duration-seconds: 900
aws-region: eu-central-1

- name: Launch instance
run: |
.github/scripts/run.sh ${{ env.DATA }} $GITHUB_REF_NAME $GITHUB_TRIGGERING_ACTOR $GITHUB_SHA
- name: Instance IP
run: |
.github/scripts/get_ip.sh ${{ env.DATA }} $GITHUB_TRIGGERING_ACTOR
2 changes: 1 addition & 1 deletion .github/workflows/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
fi
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
persist-credentials: false

Expand Down
22 changes: 6 additions & 16 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,19 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.x
cache: "yarn"

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"

- name: Restore yarn cache
uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
- name: Build monorepo
run: yarn

- run: yarn version:release
Expand Down
23 changes: 4 additions & 19 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,17 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.x
cache: "yarn"

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"

- name: Restore yarn cache
uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
- name: Build monorepo
run: yarn

- name: Build
run: yarn build

- name: Run Prettier
run: yarn prettier

Expand Down
26 changes: 4 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,21 @@ on:
branches:
- main

env:
DEFAULT_NETWORK: localhost

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.x
cache: "yarn"

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"

- name: Restore yarn cache
uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
- name: Build monorepo
run: yarn

- name: Build libraries
run: yarn workspaces foreach --no-private run build

- name: Test code
run: yarn test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Please see the latest [documentation](https://pse-team.notion.site/Bandada-82d0d

## 🔧 Configuration

### Prerequisities
### Prerequisites

- [NodeJS](https://nodejs.org/en) >= v18.17.0

Expand Down Expand Up @@ -236,7 +236,7 @@ yarn test

## 🐳 Running in Docker

### Prerequisities
### Prerequisites

- [Docker](https://www.docker.com/) >= 4.26.1
- [docker-compose](https://docs.docker.com/compose/) >= 2.24.2
Expand Down
Loading

0 comments on commit 0ff6435

Please sign in to comment.