From 687e17d7e646efc8e34a095b8352830224ebc74a Mon Sep 17 00:00:00 2001
From: matt01671 <matt01671@gmail.com>
Date: Thu, 7 May 2020 12:22:15 +1200
Subject: [PATCH] web server code

---
 ansible/build-web.sh                          |   3 +
 ansible/build-web.yaml                        |  23 ++
 ansible/create-instances.sh                   |   2 +-
 ansible/create-instances.yaml                 |   8 -
 ansible/host_vars/nectar.yaml                 |   6 -
 ansible/host_vars/react.yaml                  |   5 +-
 ansible/host_vars/web.yaml                    |  59 ++++
 ansible/hosts                                 |   1 +
 ansible/roles/react-build/tasks/main.yaml     | 256 ++++++++++++++++--
 .../slaves-cluster/templates/set-cluster-t.sh |   2 +-
 10 files changed, 325 insertions(+), 40 deletions(-)
 create mode 100755 ansible/build-web.sh
 create mode 100644 ansible/build-web.yaml
 create mode 100644 ansible/host_vars/web.yaml

diff --git a/ansible/build-web.sh b/ansible/build-web.sh
new file mode 100755
index 0000000..09f55a1
--- /dev/null
+++ b/ansible/build-web.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+. ./unimelb-comp90024-2020-grp-62-openrc.sh; ansible-playbook -i hosts --ask-become-pass build-web.yaml
\ No newline at end of file
diff --git a/ansible/build-web.yaml b/ansible/build-web.yaml
new file mode 100644
index 0000000..0e55144
--- /dev/null
+++ b/ansible/build-web.yaml
@@ -0,0 +1,23 @@
+# - hosts: localhost
+#   vars_files:
+#     - host_vars/web.yaml
+#   gather_facts: true
+
+#   roles:
+#     - role: openstack-common
+#     - role: openstack-volume
+#     - role: openstack-security-group
+#     - role: cluster-security-group
+#     - role: openstack-instance
+
+- hosts: web
+  vars_files:
+    - host_vars/react.yaml
+  gather_facts: true
+
+  roles:
+    # - role: openstack-proxy
+    # - role: all-dependencies
+    # - role: all-mount
+    # - role: all-docker
+    - role: react-build
\ No newline at end of file
diff --git a/ansible/create-instances.sh b/ansible/create-instances.sh
index 39bca1e..5df97af 100755
--- a/ansible/create-instances.sh
+++ b/ansible/create-instances.sh
@@ -1,3 +1,3 @@
 #!/usr/bin/env bash
 
-. ./unimelb-comp90024-2020-grp-62-openrc.sh; ansible-playbook -i hosts --ask-become-pass all-in-one.yaml
\ No newline at end of file
+. ./unimelb-comp90024-2020-grp-62-openrc.sh; ansible-playbook -i hosts --ask-become-pass create-instances.yaml
\ No newline at end of file
diff --git a/ansible/create-instances.yaml b/ansible/create-instances.yaml
index 8546ff2..fda796a 100755
--- a/ansible/create-instances.yaml
+++ b/ansible/create-instances.yaml
@@ -29,15 +29,7 @@
   roles:
     - role: react-build
 
-# - hosts: dbs
-  
-#   roles:
-#     - role: cluster-build
 
-# - hosts: master
-
-#   roles:
-#     - role: slaves-cluster
 
 
   
\ No newline at end of file
diff --git a/ansible/host_vars/nectar.yaml b/ansible/host_vars/nectar.yaml
index 5f1114c..99dd94c 100755
--- a/ansible/host_vars/nectar.yaml
+++ b/ansible/host_vars/nectar.yaml
@@ -9,8 +9,6 @@ volumes:
     vol_size: 50
   - vol_name: dbvolume3
     vol_size: 50
-  # - vol_name: webvolume
-  #   vol_size: 30
 
 # Security group
 security_groups:
@@ -67,10 +65,6 @@ instances:
     volumes: ['dbvolume3']
     meta:
       group: slaves
-  # - name: web
-  #   volumes: ['webvolume']
-  #   meta:
-  #     group: web
 
 instance_image: 45225edb-66d8-4fd0-bf41-132a31a18166
 instance_key_name: test-moh
diff --git a/ansible/host_vars/react.yaml b/ansible/host_vars/react.yaml
index b2f4719..2af5e43 100755
--- a/ansible/host_vars/react.yaml
+++ b/ansible/host_vars/react.yaml
@@ -1 +1,4 @@
-react_dir: react-app
+---
+volumes:
+    - device: /dev/vdb
+      mountpoint: /var/lib/react
diff --git a/ansible/host_vars/web.yaml b/ansible/host_vars/web.yaml
new file mode 100644
index 0000000..641bb0f
--- /dev/null
+++ b/ansible/host_vars/web.yaml
@@ -0,0 +1,59 @@
+# Common vars
+availability_zone: melbourne-qh2-uom
+
+# Volume
+volumes:
+  - vol_name: webvolume
+    vol_size: 30
+
+# Security group
+security_groups:
+  - name: ssh
+    description: "Security group for SSH access"
+    protocol: tcp
+    port_range_min: 22
+    port_range_max: 22
+    remote_ip_prefix: 0.0.0.0/0
+  - name: http
+    description: "Security group for HTTP"
+    protocol: tcp
+    port_range_min: 80
+    port_range_max: 3000
+    remote_ip_prefix: 0.0.0.0/0
+  - name: couchDB-cluster4
+    description: "Security group for CouchDB cluster"
+    protocol: tcp
+    port_range_min: 5984
+    port_range_max: 5984
+    remote_ip_prefix: 0.0.0.0/0
+
+cluster_security_group:
+  - name: couchDB-cluster1
+    description: "Security group for CouchDB cluster"
+    protocol: tcp
+    port_range_min: 5984
+    port_range_max: 5984
+    sg_remote_group: couchDB-cluster1
+  - name: couchDB-cluster2
+    description: "Security group for CouchDB cluster"
+    protocol: tcp
+    port_range_min: 9100
+    port_range_max: 9200
+    sg_remote_group: couchDB-cluster2
+  - name: couchDB-cluster3
+    description: "Security group for CouchDB cluster"
+    protocol: tcp
+    port_range_min: 4369
+    port_range_max: 4369
+    sg_remote_group: couchDB-cluster3
+
+# Instance
+instances:
+  - name: web
+    volumes: ['webvolume']
+    meta:
+      group: web
+
+instance_image: 45225edb-66d8-4fd0-bf41-132a31a18166
+instance_key_name: test-moh
+instance_flavor: uom.mse.2c9g
diff --git a/ansible/hosts b/ansible/hosts
index 3ea6080..8d4c6db 100644
--- a/ansible/hosts
+++ b/ansible/hosts
@@ -1,4 +1,5 @@
 [web]
+172.26.131.194 ansible_host=172.26.131.194
 
 [master]
 172.26.133.58 ansible_host=172.26.133.58
diff --git a/ansible/roles/react-build/tasks/main.yaml b/ansible/roles/react-build/tasks/main.yaml
index 6dc7c71..ccfdf8c 100644
--- a/ansible/roles/react-build/tasks/main.yaml
+++ b/ansible/roles/react-build/tasks/main.yaml
@@ -1,23 +1,233 @@
-- name: make sure the latest npm is installed
-  tags: 'node'
-  become: yes
-  npm:
-    name: npm
-    global: yes
-    state: latest
-
-- name: install pm2 server
-  tags: 'pm2'
-  become: yes
-  npm:
-    name: pm2
-    global: yes
-    state: latest
-
-- name: install react
-  tags: 'react'
-  become: yes
-  npm:
-    name: create-react-app
-    global: yes
-    state: present
\ No newline at end of file
+---
+  - name: check if packages exits
+    package_facts:
+      manager: auto
+  
+  - name: 'install all the dependencies '
+    tags: always
+    become: yes
+    apt:
+        name: ['nodejs','npm']
+        state: latest
+        install_recommends: no
+        update_cache: yes
+  - debug: 
+      msg: "node and npm installed"
+  
+  - name: make sure the latest npm is installed
+    tags: 'node'
+    become: yes
+    npm:
+      name: npm
+      global: yes
+      state: latest
+  
+  - name: install pm2 server
+    tags: 'pm2'
+    become: yes
+    npm:
+      name: pm2
+      global: yes
+      state: latest
+  
+  - name: install react
+    tags: 'react'
+    become: yes
+    npm:
+      name: create-react-app
+      global: yes
+      state: present
+  
+  # - name: Checking folders
+  #   tags: 'volumes'
+  #   become: yes
+  #   stat:
+  #     path: "{{ item.mountpoint }}"
+  #   register: directory_stats
+  #   with_items:
+  #     - "{{ volumes }}"
+  
+  # - name: Create directory
+  #   tags: 'volumes'
+  #   become: yes
+  #   file:
+  #     path: "{{ item.item.mountpoint }}"
+  #     recurse: yes
+  #     state: directory
+  #   when: item.stat.exists == false
+  #   with_items:
+  #     - "{{ directory_stats.results }}"
+  
+  # - name: Mount device
+  #   tags: 'volumes'
+  #   become: yes
+  #   mount:
+  #     path: "{{ item.mountpoint }}"
+  #     src: "{{ item.device }}"
+  #     fstype: xfs
+  #     state: mounted
+  #   when: item.device is defined
+  #   with_items:
+  #     - "{{ volumes }}"
+    
+  - name: traverse into the project directory.
+    become: yes
+    shell: cd
+    args:
+      chdir: /var/lib/react
+    register: dir_changed
+  
+  - name: clone git repo
+    become: yes
+    git:
+      repo: https://github.com/advait22/react-server.git
+      dest: /var/lib/react
+      force: yes
+  - debug:
+      msg: 'git repo cloned'
+    when: dir_changed.rc == 0 
+  
+  - name: change working directory to the app
+    become: yes
+    shell: cd
+    args:
+      chdir: /var/lib/react/express_react_example
+    register: server
+  
+  - name: install all the packages for server
+    become: yes
+    npm:
+      path: /var/lib/react/express_react_example/
+      name: install
+  - debug: 
+      msg: 'inside server'
+    when: server.rc == 0 
+  
+  - name: install dependencies
+    become: yes
+    command: npm install --save express
+  - debug:
+      msg: 'Client express installed'
+    when: server.rc == 0
+  
+  - name: change working directory to the client
+    become: yes
+    shell: cd
+    args:
+      chdir: /var/lib/react/express_react_example/client
+    register: client
+  
+  - name: install dependencies
+    become: yes
+    npm:
+      path: /var/lib/react/express_react_example/client
+      name: install
+  - debug:
+      msg: 'Client Dependencies installed'
+    when: client.rc == 0
+  
+  
+  - name: Ansible check directory exists build in client.
+    stat:
+      path: /var/lib/react/express_react_example/client/build
+    register: files_to_delete
+  
+  - name: delete previous build folder
+    become: yes
+    shell: rm -rf build
+    args:
+      chdir: /var/lib/react/express_react_example/client/
+    register: build_delete
+  - debug:
+      msg: "build deleted"
+    when: files_to_delete.stat.exists == true
+  
+    ###workaround for react js bug in build command
+  
+    
+  - name: delete dependencies on client
+    become: yes
+    shell: rm -rf node_modules
+    args:
+      chdir: /var/lib/react/express_react_example/client/
+    register: deleted_modules
+  - debug:
+      msg: 'Client Dependencies delete'
+  
+  - name: clear npm cache
+    tags: 'clear cache'
+    become: yes
+    shell: npm cache clean --force
+    args:
+      chdir: /var/lib/react/express_react_example/client
+    register: cache_cleaned
+    when: deleted_modules.rc == 0
+  
+  - name: install dependencies
+    become: yes
+    npm:
+      path: /var/lib/react/express_react_example/client
+      name: install
+  - debug:
+      msg: 'Client Dependencies installed'
+    when: cache_cleaned.rc == 0
+  
+  - name: install react scripts
+    tags: 'react scripts'
+    become: yes
+    shell: npm install --save react-scripts@3.0.1 react react-dom
+    args:
+      chdir: /var/lib/react/express_react_example/client
+    register: react_scripts
+    when: cache_cleaned.rc == 0
+    
+  #################################################################
+  
+  - name : compile the new build
+    tags: build
+    become: yes
+    shell: npm run build
+    args:
+      chdir: /var/lib/react/express_react_example/client
+  - debug:
+      msg: 'new build successfull'
+  
+  - name: Ansible check directory exists build in server.
+    stat:
+      path: /var/lib/react/express_react_example/build
+    register: files_to_delete_from_server
+  
+  
+  - name: delete previous build folder in server
+    become: yes
+    shell: rm -rf build
+    args:
+      chdir: /var/lib/react/express_react_example/
+    register: build_delete_server
+  - debug:
+      msg: "deleting old build from server"
+    when: files_to_delete_from_server.stat.exists == true
+  
+  - name: copy new build to parent folder
+    become: yes
+    command: mv /var/lib/react/express_react_example/client/build /var/lib/react/express_react_example
+    register: build_copy
+  - debug:
+      msg: 'build copied'
+    when: build_delete_server.rc == 0
+  
+  # - name: stop all the running server
+  #   become: yes
+  #   command: pm2 stop all
+  #   register: stop_server
+  # - debug: 
+  #     msg: 'all servers stopped'
+  # #   when: build_copy.rc == 0
+  
+  - name: start the server
+    become: yes
+    # command: node /var/lib/react/express_react_example/server.js &
+    command: pm2 start /var/lib/react/express_react_example/server.js
+  - debug: 
+      msg: 'yayyyyyyyyyyy app is running'
+    # when: stop_server.rc == 0  
\ No newline at end of file
diff --git a/ansible/roles/slaves-cluster/templates/set-cluster-t.sh b/ansible/roles/slaves-cluster/templates/set-cluster-t.sh
index 624fc50..7f480c5 100644
--- a/ansible/roles/slaves-cluster/templates/set-cluster-t.sh
+++ b/ansible/roles/slaves-cluster/templates/set-cluster-t.sh
@@ -17,7 +17,7 @@ export size=${#nodes[@]}
 #       echo ${node}
 #   done
 # echo ${masternode}
-for node in ${othernodes} 
+for node in ${othernodes}
 do
     curl -XPOST "http://${user}:${pass}@${masternode}:5984/_cluster_setup" \
       --header "Content-Type: application/json"\
-- 
GitLab