Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Local Terraform directories
.terraform/
.terraform.lock.hcl
terraform.tfstate
terraform.tfstate.backup
**/.terraform/
*.tfstate
*.tfstate.*
crash.log

# Crash logs
crash.log

# Override files
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# CLI configuration files
.terraformrc
terraform.rc
5 changes: 5 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[defaults]
inventory = hosts
host_key_checking = False

ssh_args = -F ~/.ssh/config
37 changes: 37 additions & 0 deletions ansible/docker-images/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Ironhack note - This file is for running in docker compose with prebuilt images
# Use these if you're stuck, or if you want to start slowly migrating from external images into your own.

services:
worker:
# build: ./worker
image: cocomo1/worker:latest
environment:
REDIS_HOST: ${REDIS_HOST}
REDIS_PORT: ${REDIS_PORT}
PG_HOST: ${DB_HOST}
DB_HOST: ${DB_HOST}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME}
networks:
- back-tier

redis:
image: redis:alpine
ports:
- "6379:6379"
volumes:
- "./healthchecks:/healthchecks"
healthcheck:
test: /healthchecks/redis.sh
interval: "5s"

networks:
- back-tier


volumes:
db-data:

networks:
back-tier:
25 changes: 25 additions & 0 deletions ansible/docker-images/db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Ironhack note - This file is for running in docker compose with prebuilt images
# Use these if you're stuck, or if you want to start slowly migrating from external images into your own.

services:
db:
image: postgres:15-alpine
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- "db-data:/var/lib/postgresql/data"
- "./healthchecks:/healthchecks"
healthcheck:
test: /healthchecks/postgres.sh
interval: "5s"
networks:
- back-tier

volumes:
db-data:

networks:
back-tier:
33 changes: 33 additions & 0 deletions ansible/docker-images/frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Ironhack note - This file is for running in docker compose with prebuilt images
# Use these if you're stuck, or if you want to start slowly migrating from external images into your own.

services:
vote:
image: cocomo1/vote:latest
ports:
- "80:80"
environment:
REDIS_HOST: ${REDIS_HOST}
REDIS_PORT: ${REDIS_PORT}
networks:
- back-tier

result:
image: cocomo1/result:latest
ports:
- "81:80"
environment:
PG_HOST: ${PG_HOST}
PG_PORT: "5432"
PG_USER: "postgres"
PG_PASSWORD: "postgres"
PG_DATABASE: "postgres"

networks:
- back-tier

volumes:
db-data:

networks:
back-tier:
8 changes: 8 additions & 0 deletions ansible/inventory/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[frontend]
frontend-instance-1

[backend]
backend-instance-1

[db]
db-instance-1
169 changes: 169 additions & 0 deletions ansible/playbooks/deploy-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
- name: Setup Docker and Docker Compose on Ubuntu
hosts: all
vars_files:
- vars.yml
become: yes
tasks:
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
update_cache: yes

- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable"
state: present

- name: Install Docker
apt:
name: docker-ce
state: present
update_cache: yes

- name: Start and enable Docker service
systemd:
name: docker
state: started
enabled: yes

- name: Install Docker Compose plugin
apt:
name: docker-compose-plugin
state: present
update_cache: yes

- name: Ensure docker compose command works
command: docker compose version
register: compose_version

- debug:
var: compose_version.stdout

- name: Ensure docker group exists
group:
name: docker
state: present

- name: Add ubuntu user to docker group
user:
name: ubuntu
groups: docker
append: yes

- name: Setup database
hosts: db
vars_files:
- vars.yml
become: yes
tasks:
- name: Create application directory
file:
path: /home/ubuntu/healthchecks
state: directory

- name: Copy local file to remote
copy:
src: ../../healthchecks/postgres.sh # path on your control node
dest: /home/ubuntu/healthchecks/postgres.sh # destination path on remote Ubuntu
owner: ubuntu # optional: set owner
group: ubuntu # optional: set group
mode: '0755'

- name: Copy local file to remote
copy:
src: ../docker-images/db.yml # path on your control node
dest: /home/ubuntu/docker-compose.yml # destination path on remote Ubuntu
owner: ubuntu # optional: set owner
group: ubuntu # optional: set group
mode: '0644'

- name: Run docker compose with env vars
command: docker compose up -d
args:
chdir: /home/ubuntu
environment:
POSTGRES_USER: "{{DB_USERNAME}}"
POSTGRES_PASSWORD: "{{DB_PASSWORD}}"



- name: Setup backend
hosts: backend
vars_files:
- vars.yml
become: yes
tasks:

- name: Create healthchecks directory
file:
path: /home/ubuntu/healthchecks
state: directory

- name: Copy healthcheck file
copy:
src: ../../healthchecks/postgres.sh # path on your control node
dest: /home/ubuntu/healthchecks/postgres.sh # destination path on remote Ubuntu
owner: ubuntu # optional: set owner
group: ubuntu # optional: set group
mode: '0755'

- name: Copy docker compose file
copy:
src: ../docker-images/backend.yml # path on your control node
dest: /home/ubuntu/docker-compose.yml # destination path on remote Ubuntu
owner: ubuntu # optional: set owner
group: ubuntu # optional: set group
mode: '0644'

- name: Run docker compose with env vars
command: docker compose up -d
args:
chdir: /home/ubuntu
environment:
REDIS_HOST: "{{REDIS_HOST}}"
REDIS_PORT: "{{REDIS_PORT}}"
PG_HOST: "{{DB_HOST}}"
DB_HOST: "{{DB_HOST}}"
DB_USERNAME: "{{DB_USERNAME}}"
DB_PASSWORD: "{{DB_PASSWORD}}"
DB_NAME: "{{DB_NAME}}"


- name: Setup frontend
hosts: frontend
vars_files:
- vars.yml
become: yes
tasks:
- name: Copy local file to remote
copy:
src: ../docker-images/frontend.yml # path on your control node
dest: /home/ubuntu/docker-compose.yml # destination path on remote Ubuntu
owner: ubuntu # optional: set owner
group: ubuntu # optional: set group
mode: '0644'

- name: Run docker compose with env vars
command: docker compose up -d
args:
chdir: /home/ubuntu
environment:
REDIS_HOST: "{{REDIS_HOST}}"
REDIS_PORT: "{{REDIS_PORT}}"
PG_HOST: "{{DB_HOST}}"
PG_PORT: "{{DB_PORT}}"
PG_USER: "{{DB_USERNAME}}"
PG_PASSWORD: "{{DB_PASSWORD}}"
PG_DATABASE: "{{DB_NAME}}"


Loading