checkpoint 2 part 1 finished, needs to be properly tested

This commit is contained in:
Tiago Sousa 2023-12-07 12:35:17 +00:00
parent ce3ea05831
commit c3f67d4582
14 changed files with 306 additions and 26 deletions

View file

@ -16,7 +16,7 @@ all:
# APP variables
app_ip: <APP_SERVICE_IP> # Needs to be updated
app_port: <APP_PORT> # Needs to be updated
app_port: 30000 # Needs to be updated
# Additional variables

View file

@ -1,3 +1,7 @@
---
# Playbook to deploy laravel.io and its components
# TO DO
- name: Deploy Laravelio
hosts: localhost
gather_facts: false
roles:
- mysql_deploy
- laravelio_deploy

View file

@ -0,0 +1,50 @@
---
- name: Get MySQL Service Info
kubernetes.core.k8s_info:
api_version: v1
kind: Service
name: mysql-service
namespace: default
register: service_info
- name: Extract ClusterIP from MySQL service
set_fact:
cluster_ip: "{{ service_info.resources[0].spec.clusterIP }}"
- name: Print ClusterIP
debug:
var: cluster_ip
- name: Create ConfigMap for laravelio
kubernetes.core.k8s:
name: laravelio-config
namespace: default
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: laravelio-config
data:
DB_HOST: "{{ cluster_ip }}"
DB_DATABASE: laravelio
DB_USERNAME: laraveliouser
DB_PASSWORD: "123456"
- name: Create laravelio Deployment
kubernetes.core.k8s:
namespace: default
state: present
src: roles/laravelio_deploy/templates/laravelio-deployment.yml
- name: Create laravelio Service
kubernetes.core.k8s:
namespace: default
state: present
src: roles/laravelio_deploy/templates/laravelio-service.yml
wait: yes
- name: Seed database
kubernetes.core.k8s_exec:
namespace: default
pod: laravelio
command: "php artisan db:seed"
when: seed_database is defined and seed_database == "true"

View file

@ -0,0 +1,29 @@
---
# Deployment for Laravelio
apiVersion: apps/v1
kind: Deployment
metadata:
name: laravelio-deployment
labels:
app: laravelio
tier: app
spec:
selector:
matchLabels:
app: laravelio
strategy:
type: Recreate
template:
metadata:
labels:
app: laravelio
tier: app
spec:
containers:
- name: laravelio
image: crispyjeasus/laravelio:latest
envFrom: # The variables below could be set on a ConfigMap object
- configMapRef:
name: laravelio-config
ports:
- containerPort: 8000

View file

@ -0,0 +1,16 @@
---
# Service for exposing Laravel
apiVersion: v1
kind: Service
metadata:
name: laravelio-service # logical name of the service, which will also become the DNS name of the service when it is created.
labels:
app: laravelio
tier: app
spec:
type: LoadBalancer
selector:
app: laravelio
ports:
- port: 30000
targetPort: 8000

View file

@ -0,0 +1,19 @@
---
- name: Create MySQL Persistent Volume Claim
kubernetes.core.k8s:
namespace: default
state: present
src: roles/mysql_deploy/templates/mysql-pvc.yml
- name: Create MySQL Deployment
kubernetes.core.k8s:
namespace: default
state: present
src: roles/mysql_deploy/templates/mysql-deployment.yml
- name: Create MySQL Service
kubernetes.core.k8s:
namespace: default
state: present
src: roles/mysql_deploy/templates/mysql-service.yml
wait: yes

View file

@ -0,0 +1,42 @@
---
# Deployment for MySQL
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
tier: database
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
tier: database
spec:
containers:
- name: mysql
image: mysql:latest
env: # The variables below could be set on a ConfigMap object
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "true"
- name: MYSQL_DATABASE
value: laravelio
- name: MYSQL_USER
value: laraveliouser
- name: MYSQL_PASSWORD
value: "123456" # Use Secret object in real usage
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim

View file

@ -0,0 +1,13 @@
---
# Persistent Volume Claim for MySQL pod
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 20Gi

View file

@ -0,0 +1,16 @@
---
# Service for exposing MySQL
apiVersion: v1
kind: Service
metadata:
name: mysql-service # logical name of the service, which will also become the DNS name of the service when it is created.
labels:
app: mysql
tier: database
spec:
type: ClusterIP
selector:
app: mysql
ports:
- targetPort: 3306 # port that containers are listening on
port: 3306 # port number exposed internally in the cluster

View file

@ -0,0 +1,26 @@
---
- name: Get laravelio Service Info
kubernetes.core.k8s_info:
api_version: v1
kind: Service
namespace: default
label_selectors:
- "app=laravelio"
register: service_info
until: service_facts.resources[0].status.loadBalancer.ingress[0].ip is defined
retries: 60
delay: 10
- name: Store app_ip
set_fact:
app_ip: "{{ service_facts.resources[0].status.loadBalancer.ingress[0].ip }}"
- name: Update app_ip in Inventory
lineinfile:
path: inventory/gcp.yml
regexp: '^ app_ip: (.*)$'
line: ' app_ip: {{ app_ip }}'
backrefs: yes
- name: Refresh Inventory
meta: refresh_inventory

View file

@ -1,14 +0,0 @@
---
- name: Refresh inventory
meta: refresh_inventory
- name: Check that you can connect (GET) to App and it returns a status 200
ansible.builtin.uri:
url: "http://{{ app_ip }}:{{app_port}}/"
method: GET
status_code: 200
register: result
until: result.status == 200
retries: 3
delay: 5

View file

@ -0,0 +1,14 @@
---
- name: Refresh inventory
meta: refresh_inventory
- name: Test Access - Check access to App (HTTP GET should return {{app_status_code}})
ansible.builtin.uri:
url: "http://{{ app_ip }}:{{app_port}}/"
method: GET
status_code: "{{app_status_code}}"
register: result
until: result.status == app_status_code
retries: 6
delay: 30

View file

@ -0,0 +1,53 @@
---
- name: Refresh inventory
meta: refresh_inventory
- name: Test Login - Get Login page (HTTP GET should return 200)
ansible.builtin.uri:
url: "http://{{ app_ip }}:{{app_port}}/login"
method: GET
status_code: 200
timeout: 300
dest: /tmp/index.html
register: result
- name: Test Login - Extract CSRF token
shell: cat /tmp/index.html | grep "csrf-token" | sed "s/.* content=\"\(.*\)\".*/\1/"
register: parse_res
- set_fact:
token: "{{parse_res.stdout_lines[0]}}"
- name: Test Login - Login as testing user (HTTP POST should return 302)
ansible.builtin.uri:
url: "http://{{ app_ip }}:{{app_port}}/login"
method: POST
body:
username: "testing"
password: "password"
_token: "{{token}}"
body_format: json
return_content: true
dest: /tmp/login.html
status_code: 302
timeout: 300
headers:
Cookie: "{{ result.cookies_string }}"
Cache-Control: no-cache
Content-Type: 'application/json; charset=UTF-8'
validate_certs: false
register: login
- name: Test Login - Get User page (HTTP GET should return 200)
ansible.builtin.uri:
url: "http://{{ app_ip }}:{{app_port}}/user"
method: GET
status_code: 200
follow_redirects: true
dest: /tmp/index.html
headers:
Cookie: "{{ login.set_cookie }}"
register: user_page

View file

@ -1,22 +1,34 @@
---
# Playbook to test the full deployment of laravel.io (from creation to destruction)
# Checkpoint #2
- import_playbook: laravelio-deploy.yml
vars:
seed_database: 'true'
tags: ['test1']
- name: Tests
hosts: localhost
gather_facts: true
roles:
- { role: test_app }
- import_playbook: laravelio-undeploy.yml
- import_playbook: laravelio-deploy.yml
- { role: test_laravelio/test_access, app_status_code: 200 }
tags: ['test2']
- name: Tests
hosts: localhost
gather_facts: yes
gather_facts: true
roles:
- { role: test_app }
- { role: test_laravelio/test_login }
tags: ['test3']
- import_playbook: laravelio-undeploy.yml delete_data='true'
- import_playbook: laravelio-undeploy.yml
vars:
delete_data: 'true'
tags: ['test4']
- name: Tests
hosts: localhost
gather_facts: true
roles:
- { role: test_laravelio/test_access, app_status_code: -1 }
tags: ['test5']