Skip to content

Commit ed6de14

Browse files
committed
Add worflows to create and configure and destroy the cluster (#6)
1 parent b68a584 commit ed6de14

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

.github/scripts/create_cluster.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
# Terraform install
4+
wget -q https://releases.hashicorp.com/terraform/1.0.11/terraform_1.0.11_linux_amd64.zip
5+
unzip -o terraform_1.0.11_linux_amd64.zip -d ~
6+
7+
# Use Terraform to create the cluster
8+
cd terraform/terraform_aws
9+
~/terraform init
10+
~/terraform apply -var="cluster_size=3" -auto-approve
11+
~/terraform output | grep '"' | awk -F '"' 'BEGIN {i=1; print "[cluster]"} {print "node0" i++ " ansible_host="$2}' > ../../terraform/ansible/hosts.ini
12+
cd ../..
13+
14+
# Wait until docker is installed in all the nodes
15+
echo "Waiting 30s for docker install process in the cluster nodes"
16+
sleep 30
17+
18+
# Ansible configuration
19+
mkdir ~/.ssh
20+
21+
echo $VAULT_PASS > ~/vault_passowrd_file.txt
22+
23+
ansible-vault decrypt \
24+
--vault-password-file=~/vault_passowrd_file.txt terraform/ansible/id_rsa_devops_encrypted \
25+
--output=~/.ssh/id_rsa
26+
27+
rm ~/vault_passowrd_file.txt
28+
29+
# Use Ansible to configure the cluster
30+
ansible-playbook -u ubuntu -i terraform/ansible/hosts.ini terraform/ansible/swarm/create_swarm.yml

.github/scripts/destroy_cluster.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
# Terraform install
4+
wget -q https://releases.hashicorp.com/terraform/1.0.11/terraform_1.0.11_linux_amd64.zip
5+
unzip -o terraform_1.0.11_linux_amd64.zip -d ~
6+
7+
# Use Terraform to collect the cluster ips
8+
cd terraform/pipeline
9+
~/terraform init
10+
~/terraform apply -auto-approve
11+
~/terraform output | grep '"' | awk -F '"' 'BEGIN {i=1; print "[cluster]"} {print "node0" i++ " ansible_host="$2}' > ../../terraform/ansible/hosts.ini
12+
cd ../..
13+
14+
# Ansible configuration
15+
mkdir ~/.ssh
16+
17+
echo $VAULT_PASS > ~/vault_passowrd_file.txt
18+
19+
ansible-vault decrypt \
20+
--vault-password-file=~/vault_passowrd_file.txt terraform/ansible/id_rsa_devops_encrypted \
21+
--output=~/.ssh/id_rsa
22+
23+
rm ~/vault_passowrd_file.txt
24+
25+
# Use Ansible to configure the cluster
26+
ansible-playbook -u ubuntu -i terraform/ansible/hosts.ini terraform/ansible/swarm/destroy_swarm.yml
27+
28+
# Use Terraform to destroy the cluster
29+
cd terraform/terraform_aws
30+
~/terraform init
31+
~/terraform destroy -auto-approve

.github/workflows/create-cluster.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Create the cluster
2+
on:
3+
release:
4+
jobs:
5+
deploy:
6+
runs-on: ubuntu-20.04
7+
steps:
8+
- name: Check out repository code
9+
uses: actions/checkout@v2
10+
11+
- name: Create the cluster
12+
run: .github/scripts/create_cluster.sh
13+
env:
14+
VAULT_PASS: ${{ secrets.VAULT_PASS }}
15+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
16+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
17+

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Execute Ansible scripts
1+
name: Deploy services to cluster
22
on:
33
push:
44
branches:

.github/workflows/destroy-cluster.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Destroy the cluster
2+
on:
3+
release:
4+
jobs:
5+
deploy:
6+
runs-on: ubuntu-20.04
7+
steps:
8+
- name: Check out repository code
9+
uses: actions/checkout@v2
10+
11+
- name: Destroy the cluster
12+
run: .github/scripts/destroy_cluster.sh
13+
env:
14+
VAULT_PASS: ${{ secrets.VAULT_PASS }}
15+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
16+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
17+

terraform/terraform_aws/inputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ variable "tag" {
1111
variable "cluster_size" {
1212
type = number
1313
description = "Number of nodes in the cluster"
14-
default = 5
14+
default = 2
1515
}

0 commit comments

Comments
 (0)