Skip to content

Commit

Permalink
Merge pull request ethereum#184 from XinFinOrg/setup-terraform-1
Browse files Browse the repository at this point in the history
Setup terraform 1
  • Loading branch information
wjrjerome authored Sep 25, 2022
2 parents cdd457d + 11cd203 commit e9a4601
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ profile.cov

**/yarn-error.log
coverage.txt
go.sum
go.sum


cicd/devnet/terraform/.terraform
53 changes: 50 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ env:
global:
- GOPROXY=https://proxy.golang.org
- GO111MODULE=on
# Terraform env
- tf_version=1.3.0
# Setting terraform init CLI options - https://www.terraform.io/docs/commands/init.html
- tf_init_cli_options=" -input=false"
# Set terraform validation CLI options - https://www.terraform.io/docs/commands/validate.html
- tf_validation_cli_options=""
# Set terraform plan CLI options - https://www.terraform.io/docs/commands/plan.html
- tf_plan_cli_options=" -lock=false -input=false"
# Set terraform apply CLI options - https://www.terraform.io/docs/commands/apply.html
- tf_apply_cli_options=" -auto-approve -input=false"


jobs:
Expand Down Expand Up @@ -83,9 +93,43 @@ jobs:
env:
- GO111MODULE=auto
name: T-Z tests

- stage: (Devnet)Terraform plan
if: branch = dev-upgrade AND type = pull_request
dist: xenial
language: bash
install:
- wget https://releases.hashicorp.com/terraform/"$tf_version"/terraform_"$tf_version"_linux_amd64.zip
- unzip terraform_"$tf_version"_linux_amd64.zip
- sudo mv terraform /usr/local/bin/
- rm terraform_"$tf_version"_linux_amd64.zip
script:
- echo "Pull request detected, creating change plan(Devnet)"
- cd cicd/devnet/terraform
# Terraform init, validate, then create change plan. If any fail, fail validation
- terraform init $tf_init_cli_options
- terraform validate $tf_validation_cli_options
- terraform plan $tf_plan_cli_options

- stage: (Devnet)Terraform apply
if: branch = dev-upgrade AND type = push AND tag IS blank
dist: xenial
language: bash
install:
# Download and install terraform before each run
- wget https://releases.hashicorp.com/terraform/"$tf_version"/terraform_"$tf_version"_linux_amd64.zip
- unzip terraform_"$tf_version"_linux_amd64.zip
- sudo mv terraform /usr/local/bin/
- rm terraform_"$tf_version"_linux_amd64.zip
script:
- echo "Merge detected, executing changes(Devnet)"
- cd cicd/devnet/terraform
# Terraform init and then apply changes to environment
- terraform init $tf_init_cli_options
- terraform apply $tf_apply_cli_options

- stage: (Devnet) Build and push image
if: branch = dev-upgrade
- stage: (Devnet) Build, push and deploy
if: branch = dev-upgrade AND type = push AND tag IS blank
services:
- docker
install: skip
Expand All @@ -97,4 +141,7 @@ jobs:
- export PATH=$PATH:$HOME/.local/bin # put aws in the path
- eval $(aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ECR_BASE_URI > /dev/null 2>&1) #needs AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY envvars
- docker tag xdc-devnet:latest $ECR_BASE_URI/$ECR_REPO_NAME:latest # Need ECR_REPO_NAME
- docker push $ECR_BASE_URI/$ECR_REPO_NAME:latest
- docker push $ECR_BASE_URI/$ECR_REPO_NAME:latest
- aws ecs update-service --region us-east-1 --cluster devnet --service devnet-group-1 --force-new-deployment #TODO: Temporary solution until we have proper automated scripts ready


4 changes: 4 additions & 0 deletions cicd/devnet/start.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

echo "Preparing to start the XDC chain, it's likely to take up to 1 minute"
# Sleep for > 30 as we need to wait for the ECS tasks container being killed by fargate. Otherwise it will ended up with two same nodes running on a single /work/xdcchain directory
sleep 45

if [ ! -d /work/xdcchain/XDC/chaindata ]
then
# Randomly select a key from environment variable, seperated by ','
Expand Down
22 changes: 22 additions & 0 deletions cicd/devnet/terraform/.terraform.lock.hcl

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

78 changes: 78 additions & 0 deletions cicd/devnet/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}

required_version = ">= 1.2.0"
}

provider "aws" {
region = "us-east-1"
}

# This bucket had to be created before you can run the terraform init
resource "aws_s3_bucket" "terraform_s3_bucket" {
bucket = "terraform-devnet-bucket"
versioning {
enabled = true
}
}

# Bucket need to be created first. If first time run terraform init, need to comment out the below section
terraform {
backend "s3" {
bucket = "terraform-devnet-bucket"
key = "tf/terraform.tfstate"
region = "us-east-1"
encrypt = true
}
}

resource "aws_vpc" "devnet_vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"

tags = {
Name = "TfDevnetVpc"
}
}

resource "aws_subnet" "devnet_subnet" {
vpc_id = aws_vpc.devnet_vpc.id
cidr_block = "10.0.0.0/20"
map_public_ip_on_launch = true
availability_zone = "us-east-1a"

tags = {
Name = "TfDevnetVpcSubnet"
}
}

resource "aws_internet_gateway" "devnet_gatewat" {
vpc_id = aws_vpc.devnet_vpc.id

tags = {
Name = "TfDevnetGateway"
}
}

resource "aws_route_table" "devnet_route_table" {
vpc_id = aws_vpc.devnet_vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.devnet_gatewat.id
}

tags = {
Name = "TfDevnetVpcRoutingTable"
}
}

resource "aws_route_table_association" "devnet_route_table_association" {
subnet_id = aws_subnet.devnet_subnet.id
route_table_id = aws_route_table.devnet_route_table.id
}

0 comments on commit e9a4601

Please sign in to comment.