From c3f67d4582fbfa6717e0ad98e315bc507bee00a9 Mon Sep 17 00:00:00 2001 From: tiago Date: Thu, 7 Dec 2023 12:35:17 +0000 Subject: [PATCH] checkpoint 2 part 1 finished, needs to be properly tested --- inventory/gcp.yml | 2 +- laravelio-deploy.yml | 8 ++- roles/laravelio_deploy/tasks/main.yml | 50 +++++++++++++++++ .../templates/laravelio-deployment.yml | 29 ++++++++++ .../templates/laravelio-service.yml | 16 ++++++ roles/mysql_deploy/tasks/main.yml | 19 +++++++ .../templates/mysql-deployment.yml | 42 +++++++++++++++ roles/mysql_deploy/templates/mysql-pvc.yml | 13 +++++ .../mysql_deploy/templates/mysql-service.yml | 16 ++++++ roles/post_deploy/tasks/main.yml | 26 +++++++++ roles/test_laravelio/tasks/main.yml | 14 ----- .../test_laravelio/test_access/tasks/main.yml | 14 +++++ .../test_laravelio/test_login/tasks/main.yml | 53 +++++++++++++++++++ test-all.yml | 30 +++++++---- 14 files changed, 306 insertions(+), 26 deletions(-) create mode 100644 roles/laravelio_deploy/tasks/main.yml create mode 100644 roles/laravelio_deploy/templates/laravelio-deployment.yml create mode 100644 roles/laravelio_deploy/templates/laravelio-service.yml create mode 100644 roles/mysql_deploy/tasks/main.yml create mode 100644 roles/mysql_deploy/templates/mysql-deployment.yml create mode 100644 roles/mysql_deploy/templates/mysql-pvc.yml create mode 100644 roles/mysql_deploy/templates/mysql-service.yml create mode 100644 roles/post_deploy/tasks/main.yml delete mode 100644 roles/test_laravelio/tasks/main.yml create mode 100644 roles/test_laravelio/test_access/tasks/main.yml create mode 100644 roles/test_laravelio/test_login/tasks/main.yml diff --git a/inventory/gcp.yml b/inventory/gcp.yml index 4e50d9f..b666389 100644 --- a/inventory/gcp.yml +++ b/inventory/gcp.yml @@ -16,7 +16,7 @@ all: # APP variables app_ip: # Needs to be updated - app_port: # Needs to be updated + app_port: 30000 # Needs to be updated # Additional variables diff --git a/laravelio-deploy.yml b/laravelio-deploy.yml index 2aa6972..10af14f 100644 --- a/laravelio-deploy.yml +++ b/laravelio-deploy.yml @@ -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 diff --git a/roles/laravelio_deploy/tasks/main.yml b/roles/laravelio_deploy/tasks/main.yml new file mode 100644 index 0000000..f86dfb2 --- /dev/null +++ b/roles/laravelio_deploy/tasks/main.yml @@ -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" diff --git a/roles/laravelio_deploy/templates/laravelio-deployment.yml b/roles/laravelio_deploy/templates/laravelio-deployment.yml new file mode 100644 index 0000000..c1dc4e8 --- /dev/null +++ b/roles/laravelio_deploy/templates/laravelio-deployment.yml @@ -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 diff --git a/roles/laravelio_deploy/templates/laravelio-service.yml b/roles/laravelio_deploy/templates/laravelio-service.yml new file mode 100644 index 0000000..7500b07 --- /dev/null +++ b/roles/laravelio_deploy/templates/laravelio-service.yml @@ -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 diff --git a/roles/mysql_deploy/tasks/main.yml b/roles/mysql_deploy/tasks/main.yml new file mode 100644 index 0000000..8b230a1 --- /dev/null +++ b/roles/mysql_deploy/tasks/main.yml @@ -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 diff --git a/roles/mysql_deploy/templates/mysql-deployment.yml b/roles/mysql_deploy/templates/mysql-deployment.yml new file mode 100644 index 0000000..918d7f8 --- /dev/null +++ b/roles/mysql_deploy/templates/mysql-deployment.yml @@ -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 diff --git a/roles/mysql_deploy/templates/mysql-pvc.yml b/roles/mysql_deploy/templates/mysql-pvc.yml new file mode 100644 index 0000000..99af15a --- /dev/null +++ b/roles/mysql_deploy/templates/mysql-pvc.yml @@ -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 diff --git a/roles/mysql_deploy/templates/mysql-service.yml b/roles/mysql_deploy/templates/mysql-service.yml new file mode 100644 index 0000000..48f3f43 --- /dev/null +++ b/roles/mysql_deploy/templates/mysql-service.yml @@ -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 diff --git a/roles/post_deploy/tasks/main.yml b/roles/post_deploy/tasks/main.yml new file mode 100644 index 0000000..7d46872 --- /dev/null +++ b/roles/post_deploy/tasks/main.yml @@ -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 diff --git a/roles/test_laravelio/tasks/main.yml b/roles/test_laravelio/tasks/main.yml deleted file mode 100644 index 097aceb..0000000 --- a/roles/test_laravelio/tasks/main.yml +++ /dev/null @@ -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 \ No newline at end of file diff --git a/roles/test_laravelio/test_access/tasks/main.yml b/roles/test_laravelio/test_access/tasks/main.yml new file mode 100644 index 0000000..59e6af0 --- /dev/null +++ b/roles/test_laravelio/test_access/tasks/main.yml @@ -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 diff --git a/roles/test_laravelio/test_login/tasks/main.yml b/roles/test_laravelio/test_login/tasks/main.yml new file mode 100644 index 0000000..c35d006 --- /dev/null +++ b/roles/test_laravelio/test_login/tasks/main.yml @@ -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 + + diff --git a/test-all.yml b/test-all.yml index 55b8b87..42005d2 100644 --- a/test-all.yml +++ b/test-all.yml @@ -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' \ No newline at end of file +- 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']