Skip to content

Commit be619cd

Browse files
Merge pull request #32 from AET-DevOps25/28-set-up-terraform-and-ansible-scripts
28 set up terraform and ansible scripts
2 parents 1e551ea + 01eb01d commit be619cd

File tree

7 files changed

+360
-2
lines changed

7 files changed

+360
-2
lines changed

client/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ You can check out [the Next.js GitHub repository](https://github.com/vercel/next
3131

3232
## Deploy on Vercel
3333

34+
3435
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
3536

3637
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.

compose.aws.yml

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ services:
2323
server:
2424
image: ghcr.io/aet-devops25/team-server-down/server:latest
2525
environment:
26-
- SPRING_PROFILES_ACTIVE=docker
26+
DB_HOST: ${DB_HOST:-db}
27+
DB_PORT: ${DB_PORT:-5432}
28+
DB_NAME: ${DB_NAME:-postgres}
29+
DB_USER: ${DB_USER:-postgres}
30+
DB_PASSWORD: ${DB_PASSWORD:-postgres}
31+
depends_on:
32+
db:
33+
condition: service_healthy
2734
restart: unless-stopped
35+
networks:
36+
- server
2837
labels:
2938
- "traefik.enable=true"
3039
- "traefik.http.routers.server.rule=Host(`${SERVER_HOST}`)"
@@ -47,4 +56,28 @@ services:
4756
- "traefik.http.routers.client.tls.certresolver=letsencrypt"
4857
- "traefik.http.middlewares.client-compress.compress=true"
4958
- "traefik.http.routers.client.middlewares=client-compress"
50-
- "traefik.http.routers.client.priority=1"
59+
- "traefik.http.routers.client.priority=1"
60+
61+
db:
62+
image: postgres:16.2-bullseye
63+
restart: unless-stopped
64+
environment:
65+
POSTGRES_USER: postgres
66+
POSTGRES_PASSWORD: postgres
67+
healthcheck:
68+
test: [ "CMD-SHELL", "sh -c 'pg_isready -U postgres -d postgres'" ]
69+
interval: 10s
70+
timeout: 3s
71+
retries: 3
72+
ports:
73+
- "5432:5432"
74+
volumes:
75+
- db-data:/var/lib/postgresql/data
76+
networks:
77+
- server
78+
79+
volumes:
80+
db-data:
81+
82+
networks:
83+
server:
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
- hosts: all
2+
become: true
3+
vars:
4+
container_count: 4
5+
default_container_name: docker
6+
default_container_image: ubuntu
7+
default_container_command: sleep 1
8+
9+
- name: Install Docker, Docker Compose
10+
hosts: all
11+
become: true
12+
tasks:
13+
- name: Install aptitude
14+
apt:
15+
name: aptitude
16+
state: latest
17+
update_cache: true
18+
19+
- name: Install required system packages
20+
apt:
21+
pkg:
22+
- apt-transport-https
23+
- ca-certificates
24+
- curl
25+
- software-properties-common
26+
- python3-pip
27+
- virtualenv
28+
- python3-setuptools
29+
state: latest
30+
update_cache: true
31+
32+
- name: Add Docker GPG apt Key
33+
apt_key:
34+
url: https://download.docker.com/linux/ubuntu/gpg
35+
state: present
36+
37+
- name: Add Docker Repository
38+
apt_repository:
39+
repo: deb https://download.docker.com/linux/ubuntu focal stable
40+
state: present
41+
42+
- name: Update apt and install docker-ce
43+
apt:
44+
name: docker-ce
45+
state: latest
46+
update_cache: true
47+
48+
- name: Install Docker module for Python
49+
apt:
50+
name: python3-docker
51+
state: present
52+
become: true
53+
54+
- name: Start docker daemon
55+
systemd:
56+
name: docker
57+
state: started
58+
59+
- name: Create a new Linux User
60+
hosts: all
61+
become: yes
62+
tasks:
63+
- name: Create new Linux User
64+
user:
65+
name: teamserverdown
66+
groups: adm,docker
67+
append: yes
68+
69+
- name: Reconnect to server session
70+
meta: reset_connection
71+
72+
- name: Start Project
73+
hosts: all
74+
become: yes
75+
tasks:
76+
- name: Clone GitHub repository
77+
git:
78+
repo: https://github.com/AET-DevOps25/team-server-down
79+
dest: /home/ubuntu/team-server-down/
80+
clone: yes
81+
update: yes
82+
83+
- name: Get the public IP address of the network.
84+
uri:
85+
url: https://api.ipify.org?format=json
86+
method: Get
87+
changed_when: false
88+
register: public_ip
89+
until: public_ip.status == 200
90+
retries: 6
91+
delay: 10
92+
93+
- name: Create .env.prod
94+
ansible.builtin.shell: |
95+
cd team-server-down
96+
rm .env.prod
97+
touch .env.prod
98+
echo "CLIENT_HOST=client.{{ public_ip.json.ip }}.nip.io" >> .env.prod
99+
echo "SERVER_HOST=api.{{ public_ip.json.ip }}.nip.io" >> .env.prod
100+
echo "PUBLIC_API_URL=https://api.{{ public_ip.json.ip }}.nip.io/api" >> .env.prod
101+
102+
- name: Start Container
103+
community.docker.docker_compose_v2:
104+
project_src: /home/ubuntu/team-server-down
105+
files: compose.aws.yml
106+
env_files: .env.prod
107+
register: output
108+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
crash.*.log
11+
12+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
13+
# password, private keys, and other secrets. These should not be part of version
14+
# control as they are data points which are potentially sensitive and subject
15+
# to change depending on the environment.
16+
*.tfvars
17+
*.tfvars.json
18+
19+
# Ignore override files as they are usually used to override resources locally and so
20+
# are not checked in
21+
override.tf
22+
override.tf.json
23+
*_override.tf
24+
*_override.tf.json
25+
26+
# Include override files you do wish to add to version control using negated pattern
27+
# !example_override.tf
28+
29+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
30+
# example: *tfplan*
31+
32+
# Ignore CLI configuration files
33+
.terraformrc
34+
terraform.rc

infrastructure/terraform/.terraform.lock.hcl

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infrastructure/terraform/main.tf

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
variable vpc_cidr_block {}
6+
variable subnet_1_cidr_block {}
7+
variable avail_zone {}
8+
variable env_prefix {}
9+
variable instance_type {}
10+
variable ssh_key {}
11+
# variable my_ip {}
12+
variable ssh_private_key{}
13+
14+
resource "aws_vpc" "teamserverdown-vpc" {
15+
cidr_block = var.vpc_cidr_block
16+
tags = {
17+
Name = "${var.env_prefix}-vpc"
18+
}
19+
}
20+
21+
resource "aws_subnet" "teamserverdown-subnet-1" {
22+
vpc_id = aws_vpc.teamserverdown-vpc.id
23+
cidr_block = var.subnet_1_cidr_block
24+
availability_zone = var.avail_zone
25+
tags = {
26+
Name = "${var.env_prefix}-subnet-1"
27+
}
28+
}
29+
30+
resource "aws_security_group" "teamserverdown-sg" {
31+
name = "teamserverdown-sg"
32+
vpc_id = aws_vpc.teamserverdown-vpc.id
33+
34+
ingress {
35+
from_port = 22
36+
to_port = 22
37+
protocol = "tcp"
38+
cidr_blocks = ["0.0.0.0/0"] # Change to my_ip
39+
}
40+
41+
ingress {
42+
from_port = 8080
43+
to_port = 8080
44+
protocol = "tcp"
45+
cidr_blocks = ["0.0.0.0/0"]
46+
}
47+
48+
egress {
49+
from_port = 0
50+
to_port = 0
51+
protocol = "-1"
52+
cidr_blocks = ["0.0.0.0/0"]
53+
prefix_list_ids = []
54+
}
55+
56+
tags = {
57+
Name = "${var.env_prefix}-sg"
58+
}
59+
}
60+
61+
resource "aws_internet_gateway" "teamserverdown-igw" {
62+
vpc_id = aws_vpc.teamserverdown-vpc.id
63+
64+
tags = {
65+
Name = "${var.env_prefix}-internet-gateway"
66+
}
67+
}
68+
69+
resource "aws_route_table" "teamserverdown-route-table" {
70+
vpc_id = aws_vpc.teamserverdown-vpc.id
71+
72+
route {
73+
cidr_block = "0.0.0.0/0"
74+
gateway_id = aws_internet_gateway.teamserverdown-igw.id
75+
}
76+
77+
# default route, mapping VPC CIDR block to "local", created implicitly and cannot be specified.
78+
79+
tags = {
80+
Name = "${var.env_prefix}-route-table"
81+
}
82+
}
83+
84+
# Associate subnet with Route Table
85+
resource "aws_route_table_association" "a-rtb-subnet" {
86+
subnet_id = aws_subnet.teamserverdown-subnet-1.id
87+
route_table_id = aws_route_table.teamserverdown-route-table.id
88+
}
89+
90+
resource "aws_key_pair" "ssh-key" {
91+
key_name = "teamserverdown-key"
92+
public_key = file(var.ssh_key)
93+
}
94+
95+
output "server-ip" {
96+
value = aws_instance.teamserverdown-server.public_ip
97+
}
98+
99+
resource "aws_instance" "teamserverdown-server" {
100+
ami = "ami-084568db4383264d4"
101+
instance_type = var.instance_type
102+
key_name = "teamserverdown-key"
103+
associate_public_ip_address = true
104+
subnet_id = aws_subnet.teamserverdown-subnet-1.id
105+
vpc_security_group_ids = [aws_security_group.teamserverdown-sg.id]
106+
availability_zone = var.avail_zone
107+
108+
tags = {
109+
Name = "${var.env_prefix}-server"
110+
}
111+
}
112+
113+
resource "null_resource" "wait_for_ssh" {
114+
depends_on = [aws_instance.teamserverdown-server]
115+
116+
provisioner "local-exec" {
117+
command = "bash -c 'until nc -zv ${aws_instance.teamserverdown-server.public_ip} 22; do sleep 5; done'"
118+
}
119+
}
120+
121+
resource "null_resource" "configure_server" {
122+
depends_on = [null_resource.wait_for_ssh]
123+
124+
triggers = {
125+
trigger = aws_instance.teamserverdown-server.public_ip
126+
}
127+
provisioner "local-exec" {
128+
working_dir = "/Users/leonliang/tum-informatik/SS25/DevOps/team-server-down/infrastructure/ansible"
129+
command = "ansible-playbook --inventory ${aws_instance.teamserverdown-server.public_ip}, --private-key ${var.ssh_private_key} --user ubuntu playbook.yml --ssh-extra-args='-o StrictHostKeyChecking=no'"
130+
}
131+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vpc_cidr_block = "10.0.0.0/16"
2+
subnet_1_cidr_block = "10.0.0.0/24"
3+
avail_zone = "us-east-1a"
4+
env_prefix = "dev"
5+
instance_type = "t2.micro"
6+
ssh_key = "/Users/leonliang/.ssh/teamserverdown.pub"
7+
# my_ip = "your IP"
8+
ssh_private_key = "/Users/leonliang/.ssh/teamserverdown"

0 commit comments

Comments
 (0)