Skip to content

Commit c7f97d4

Browse files
authored
Merge pull request #92 from AET-DevOps25/feature/setup-ansible-for-aws
Feature/setup ansible for aws with monitoring.
2 parents 903884b + c8f55e6 commit c7f97d4

File tree

33 files changed

+1544
-158
lines changed

33 files changed

+1544
-158
lines changed

.github/workflows/aws-ec2-deploy.yml

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: Manual Aws Ec2 deployment
33
on:
44
workflow_dispatch:
55
inputs:
6-
private_key:
7-
description: 'aws ssh key in a pem file'
6+
SSH_PRIVATE_KEY_B64:
7+
description: "Base64-encoded private SSH key"
88
required: true
99
AWS_ACCESS_KEY_ID:
1010
description: 'aws access key id'
@@ -15,6 +15,9 @@ on:
1515
AWS_SESSION_TOKEN:
1616
description: 'running aws session token'
1717
required: true
18+
GHCR_TOKEN:
19+
description: 'Personal Access Token for the GHCR'
20+
required: true
1821

1922
jobs:
2023
deploy:
@@ -30,17 +33,25 @@ jobs:
3033
echo "::add-mask::${{ github.event.inputs.AWS_ACCESS_KEY_ID }}"
3134
echo "::add-mask::${{ github.event.inputs.AWS_SECRET_ACCESS_KEY }}"
3235
echo "::add-mask::${{ github.event.inputs.AWS_SESSION_TOKEN }}"
36+
echo "::add-mask::${{ github.event.inputs.GHCR_TOKEN }}"
37+
3338
34-
- name: Write ssh key to infra/priv.pem
39+
- name: Decode and write SSH private key
3540
run: |
36-
echo "${{ github.event.inputs.private_key }}" > infra/priv.pem
41+
mkdir -p infra
42+
echo "${{ github.event.inputs.SSH_PRIVATE_KEY_B64 }}" | base64 -d > infra/priv.pem
3743
chmod 400 infra/priv.pem
3844
3945
- name: Install Terraform
4046
uses: hashicorp/setup-terraform@v3
4147
with:
4248
terraform_version: 1.12.1
4349

50+
- name: Install Ansible
51+
uses: alex-oleshkevich/setup-ansible@v1.0.1
52+
with:
53+
version: "11.6.0"
54+
4455
- name: Create EC2
4556
env:
4657
AWS_ACCESS_KEY_ID: ${{ github.event.inputs.AWS_ACCESS_KEY_ID }}
@@ -49,3 +60,49 @@ jobs:
4960
run: |
5061
cd infra
5162
make deploy
63+
64+
- name: Wait for EC2 to be ready
65+
run: |
66+
cd infra
67+
IP=$(terraform output -raw ip)
68+
echo "$IP" > instance_ip.txt
69+
echo "Waiting for SSH on $IP..."
70+
for i in {1..5}; do
71+
nc -z -v -w5 $IP 22 && echo "SSH is ready!" && exit 0
72+
echo "Retry $i: SSH not up yet"
73+
sleep 10
74+
done
75+
echo "SSH never became available"
76+
exit 1
77+
78+
- name: Inject IP into Ansible inventory
79+
run: |
80+
cd infra
81+
ip=$(cat instance_ip.txt)
82+
sed -i "s|\${ip}|$ip|g" inventory.ini
83+
84+
# - name: Test ssh connection
85+
# run: |
86+
# echo "${{ github.event.inputs.private_key }}" > infra/priv.pem
87+
# chmod 400 infra/priv.pem
88+
# ssh -o StrictHostKeyChecking=no -i infra/priv.pem admin@$(cat instance_ip.txt) 'echo SSH connection successful'
89+
90+
- name: Test SSH connection with debugging
91+
run: |
92+
ls -la infra/
93+
cd infra
94+
IP=$(terraform output -raw ip)
95+
echo "Testing SSH connection to $IP..."
96+
echo "Key file permissions:"
97+
ls -l priv.pem
98+
echo "Key file content (first line):"
99+
head -n 1 priv.pem
100+
echo "Attempting SSH connection..."
101+
ssh -v -o StrictHostKeyChecking=no -i priv.pem ubuntu@$IP 'echo "SSH connection successful!"'
102+
103+
- name: Provision EC2
104+
run: |
105+
cd infra
106+
echo "cr_username: ${{ github.actor }}" >> token.yml
107+
echo "token: ${{ github.event.inputs.GHCR_TOKEN }}" >> token.yml
108+
make ansible

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ celerybeat.pid
291291

292292
# Environments
293293
.secrets.env
294+
.env
295+
!docker/backend_config_files/.env
294296
.venv
295297
env/
296298
venv/
@@ -376,4 +378,9 @@ sketch
376378
### Vue ###
377379
# gitignore template for Vue.js projects
378380
#
379-
# Recommended template: Node.gitignore
381+
# Recommended template: Node.gitignore
382+
383+
384+
token.yml
385+
# Helm
386+
values.yaml

compose.prod.yml

Lines changed: 0 additions & 121 deletions
This file was deleted.

compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ services:
108108

109109
volumes:
110110
db_data:
111-
ollama_data:
111+
ollama_data:

infra/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
PLAYBOOK ?= playbook.yml
2+
13
ssh:
24
chmod 400 priv.pem
35
ssh -i priv.pem admin@$(shell terraform output -raw ip)
@@ -8,5 +10,9 @@ init:
810
deploy: init
911
terraform apply -input=false --auto-approve
1012

13+
ansible:
14+
ansible-playbook -i inventory.ini $(PLAYBOOK) -vvv
15+
1116
teardown:
12-
terraform destroy -input=false --auto-approve
17+
terraform destroy -input=false --auto-approve
18+

infra/ansible.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[defaults]
2+
inventory = hosts.ini
3+
remote_user = admin

0 commit comments

Comments
 (0)