Skip to content

Commit 191b6ba

Browse files
authored
Bootstrap new ARC Terraform (#344)
This bootstraps a new Terraform setup for the multicloud WG ARC work. This reuses some of the configuration from the ALI folder to bootstrap the S3 state bucket. Relates to pytorch-fdn/multicloud-ci-infra#15 Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
1 parent b88ff4d commit 191b6ba

13 files changed

Lines changed: 303 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Terraform Apply / Runners / Production - ARC
2+
3+
on:
4+
workflow_dispatch:
5+
6+
concurrency:
7+
group: "terraform-make-apply"
8+
cancel-in-progress: false
9+
10+
permissions:
11+
id-token: write
12+
contents: read
13+
14+
jobs:
15+
release:
16+
name: Terraform Apply
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- name: Checkout branch
21+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
22+
23+
- name: Install Terraform
24+
uses: opentofu/setup-opentofu@592200bd4b9bbf4772ace78f887668b1aee8f716 # v1.0.5
25+
with:
26+
terraform_version: 1.5.7
27+
terraform_wrapper: false
28+
29+
- name: configure aws credentials
30+
uses: aws-actions/configure-aws-credentials@b47578312673ae6fa5b5096b330d9fbac3d116df # v4.2.1
31+
with:
32+
role-to-assume: arn:aws:iam::${{ secrets.PY_FOUNDATION_AWS_ACC_ID }}:role/${{ secrets.PY_FOUNDATION_AWS_DEPLOY_ROLE }}
33+
aws-region: us-east-1
34+
35+
- name: Terraform Apply
36+
working-directory: arc/aws/391835788720/us-east-1
37+
shell: bash
38+
run: make apply
39+
env:
40+
AWS_DEFAULT_REGION: us-east-1
41+
GITHUB_TOKEN: ${{ secrets.LIST_PYTORCH_RUNNERS_GITHUB_TOKEN }}
42+
TERRAFORM_EXTRAS: -auto-approve -lock-timeout=15m

.github/workflows/arc-on-pr.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: TFLint & Plan - ARC
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '.github/workflows/arc-*'
7+
- 'arc/**'
8+
- 'modules/**'
9+
push:
10+
branches:
11+
- main
12+
13+
permissions:
14+
id-token: write
15+
contents: read
16+
17+
jobs:
18+
tflint-plan:
19+
name: ARC tflint + terraform plan
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout branch
24+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
25+
26+
- name: Install Terraform
27+
uses: opentofu/setup-opentofu@592200bd4b9bbf4772ace78f887668b1aee8f716 # v1.0.5
28+
with:
29+
terraform_version: 1.5.7
30+
terraform_wrapper: false
31+
32+
- uses: terraform-linters/setup-tflint@444635365d380c7363d1eaee4267317c2445717d # v2.0.1
33+
name: Setup TFLint
34+
with:
35+
github_token: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Install AWS CLI
38+
uses: unfor19/install-aws-cli-action@e8b481e524a99f37fbd39fdc1dcb3341ab091367 # v1.0.7
39+
with:
40+
arch: amd64
41+
42+
- name: configure aws credentials
43+
uses: aws-actions/configure-aws-credentials@b47578312673ae6fa5b5096b330d9fbac3d116df # v4.2.1
44+
with:
45+
role-to-assume: arn:aws:iam::${{ secrets.PY_FOUNDATION_AWS_ACC_ID }}:role/${{ secrets.PY_FOUNDATION_AWS_DEPLOY_ROLE }}
46+
aws-region: us-east-1
47+
48+
- name: "Run TFLint runners"
49+
shell: bash
50+
working-directory: arc
51+
run: make tflint
52+
53+
- name: Make plan
54+
shell: bash
55+
working-directory: arc
56+
run: make plan
57+
env:
58+
TERRAFORM_EXTRAS: -lock-timeout=15m

arc/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
__pycache__/
2+
.account-checked
3+
.DS_Store
4+
.idea/
5+
.terraform.lock.hcl
6+
.terraformrc
7+
*.bkp
8+
*.tfstate
9+
*.tfstate.*
10+
*.tfvars
11+
**/.terraform/*
12+
**/.venv/
13+
**/venv/
14+
ARC_NODE_CONFIG.yaml
15+
ARC_RUNNER_CONFIG.yaml
16+
assets/
17+
aws/*/*/backend-state.tf
18+
aws/*/*/backend.plan
19+
aws/*/*/backend.tf
20+
aws/*/*/dyn_locals.tf
21+
aws/*/*/inventory/
22+
crash.log
23+
modules/arc/.terraform.lock.hcl
24+
modules/arc/.terraform/
25+
modules/arc/tls/
26+
terraform.rc
27+
tf-modules/

arc/Makefile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
SHELL=/bin/bash -o pipefail
2+
3+
.PHONY: clean
4+
clean:
5+
$(RM) -r .terraform
6+
$(RM) -r tf-modules
7+
$(RM) -r venv
8+
9+
.PHONY: tflint
10+
tflint:
11+
cd modules ; for module in ./*/ ; do \
12+
pushd $$module ; \
13+
echo "==== START TFLINT: modules/$$module ============================================" ; \
14+
tofu init && tflint --init && tflint --module --color --minimum-failure-severity=warning --recursive ; \
15+
echo "==== END TFLINT: modules/$$module ============================================" ; \
16+
popd ; \
17+
done
18+
cd aws ; for account in ./*/ ; do \
19+
pushd $$account ; \
20+
for region in ./*/ ; do \
21+
pushd $$region ; \
22+
echo "==== START TFLINT: aws/$$account/$$region ============================================" ; \
23+
$(MAKE) tflint || exit 1 ; \
24+
echo "==== END TFLINT: aws/$$account/$$region ============================================" ; \
25+
popd ; \
26+
done ; \
27+
popd ; \
28+
done
29+
30+
.PHONY: plan
31+
plan:
32+
cd aws ; for account in ./*/ ; do \
33+
pushd $$account ; \
34+
for region in ./*/ ; do \
35+
pushd $$region ; \
36+
echo "==== START make plan: aws/$$account/$$region ============================================" ; \
37+
$(MAKE) plan || exit 1 ; \
38+
echo "==== END make plan: aws/$$account/$$region ============================================" ; \
39+
popd ; \
40+
done ; \
41+
popd ; \
42+
done
43+
44+
.PHONY: apply
45+
apply:
46+
cd aws ; for account in ./*/ ; do \
47+
pushd $$account ; \
48+
for region in ./*/ ; do \
49+
pushd $$region ; \
50+
echo "==== START make apply: aws/$$account/$$region ============================================" ; \
51+
$(MAKE) apply || exit 1 ; \
52+
echo "==== END make apply: aws/$$account/$$region ============================================" ; \
53+
popd ; \
54+
done ; \
55+
popd ; \
56+
done

arc/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# GitHub Actions Runner Controller (ARC)
2+
3+
This directory contains the GitHub ARC deployment managed by the
4+
multicloud working group.
5+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
SHELL=/bin/bash -o pipefail
2+
PROHOME := $(realpath ../../..)
3+
4+
REGION := $(notdir $(CURDIR))
5+
ACCOUNT := $(notdir $(patsubst %/,%,$(dir $(CURDIR))))
6+
7+
ifneq ($(GITHUB_ACTIONS),true)
8+
export AWS_PROFILE := $(ACCOUNT)
9+
endif
10+
11+
.PHONY: all
12+
all:
13+
echo "Please specify a target"
14+
exit 1
15+
16+
.terraform/modules/modules.json: backend.tf
17+
tofu init
18+
19+
.PHONY: init
20+
init: .terraform/modules/modules.json
21+
22+
.PHONY: clean
23+
clean:
24+
$(RM) -rf .terraform
25+
$(RM) -rf inventory
26+
$(RM) .terraform.lock.hcl
27+
$(RM) backend-state.tf
28+
$(RM) backend.plan
29+
$(RM) backend.tf
30+
$(RM) dyn_locals.tf
31+
$(RM) terraform.tfstate
32+
cd $(PROHOME)/ && make clean
33+
34+
.PHONY: backend-state
35+
backend-state: backend.tf
36+
37+
dyn_locals.tf:
38+
echo -e "locals {\n # tflint-ignore: terraform_unused_declarations\n aws_region = \"$(REGION)\"\n # tflint-ignore: terraform_unused_declarations\n aws_account_id = \"$(ACCOUNT)\"\n}\n" >dyn_locals.tf
39+
40+
backend.tf: backend-state.tf
41+
sed "s/#AWS_REGION/$(REGION)/g" <$(PROHOME)/modules/backend-file/backend.tf >backend.tf
42+
$(RM) terraform.tfstate
43+
44+
backend-state.tf: dyn_locals.tf
45+
sed "s/#AWS_REGION/$(REGION)/g" <$(PROHOME)/modules/backend-file/backend-state.tf >backend-state.tf
46+
tofu get -update
47+
tofu init -backend=false
48+
tofu plan -input=false -out=backend.plan -detailed-exitcode -target=module.backend-state -lock-timeout=15m ; \
49+
ext_code=$$? ; \
50+
if [ $$ext_code -eq 2 ] ; then \
51+
if [ "$$GITHUB_ACTIONS" != "true" ] ; then \
52+
echo "Backend state does not exist, creating" ; \
53+
tofu apply ${TERRAFORM_EXTRAS} backend.plan ; \
54+
else \
55+
echo "Backend state does not exist, should not do it on a github action!" ; \
56+
exit 1 ; \
57+
fi ; \
58+
elif [ $$ext_code -eq 0 ] ; then \
59+
echo "Backend state already exists" ; \
60+
else \
61+
echo "Unexpected exit code $$ext_code" ; \
62+
exit 1 ; \
63+
fi
64+
65+
.PHONY: plan
66+
plan: .terraform/modules/modules.json
67+
tofu plan $(TERRAFORM_EXTRAS)
68+
69+
.PHONY: apply
70+
apply: .terraform/modules/modules.json
71+
tofu apply ${TERRAFORM_EXTRAS}
72+
73+
.PHONY: destroy
74+
destroy: .terraform/modules/modules.json
75+
echo "To make sure you want to run this, go to the Makefile and comment out the destroy target" ; exit 1
76+
# terraform destroy ${TERRAFORM_EXTRAS}
77+
78+
.PHONY: tflint
79+
tflint: .terraform/modules/modules.json
80+
tflint --init
81+
tflint --call-module-type=all --color --minimum-failure-severity=warning --recursive
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
locals {
2+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_version = ">= 1.2"
3+
required_providers {
4+
random = {
5+
version = ">= 3.4"
6+
source = "hashicorp/random"
7+
}
8+
aws = {
9+
version = ">= 6.0"
10+
source = "hashicorp/aws"
11+
}
12+
}
13+
}

arc/aws/391835788720/us-east-1/variables.tf

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module "backend-state" {
2+
source = "../../../modules/backend-state"
3+
4+
aws_region = "#AWS_REGION"
5+
environment = "prod"
6+
project = "pyt-arc"
7+
}

0 commit comments

Comments
 (0)