diff --git a/deploy.sh b/deploy.sh index 12c06b5..6664636 100755 --- a/deploy.sh +++ b/deploy.sh @@ -32,6 +32,4 @@ else fi cd "$parent_path/docker" || exit docker save barker:latest | bzip2 | pv | ssh tanshu@beacon.tanshu.com 'bunzip2 | sudo docker load' -ansible-playbook --limit=beacon playbook-mhl.yml -ansible-playbook --limit=beacon playbook-chd.yml -ansible-playbook --limit=beacon playbook-pkl.yml +ansible-playbook --inventory hosts playbook.yml diff --git a/docker/files/chd.env b/docker/files/.env-chd similarity index 100% rename from docker/files/chd.env rename to docker/files/.env-chd diff --git a/docker/files/mhl.env b/docker/files/.env-mhl similarity index 100% rename from docker/files/mhl.env rename to docker/files/.env-mhl diff --git a/docker/files/pkl.env b/docker/files/.env-pkl similarity index 100% rename from docker/files/pkl.env rename to docker/files/.env-pkl diff --git a/docker/hosts b/docker/hosts new file mode 100644 index 0000000..022b851 --- /dev/null +++ b/docker/hosts @@ -0,0 +1,14 @@ +# - Comments begin with the '#' character +# - Blank lines are ignored +# - Groups of hosts are delimited by [header] elements +# - You can enter hostnames or ip addresses +# - A hostname/ip can be a member of multiple groups + +[barker] +pkl ansible_host=beacon var_file=vars/pkl.yml +chd ansible_host=beacon var_file=vars/chd.yml +mhl ansible_host=beacon var_file=vars/mhl.yml +# hin ansible_host=beacon var_file=vars/hin.yml + +[all:vars] +ansible_python_interpreter=/usr/bin/python3 diff --git a/docker/playbook-chd.yml b/docker/playbook-chd.yml deleted file mode 100755 index 070d5b2..0000000 --- a/docker/playbook-chd.yml +++ /dev/null @@ -1,69 +0,0 @@ -################################################# -# DO Community Playbooks: Docker -################################################# ---- -- hosts: all - become: true - vars_files: - - vars/chd.yml - - tasks: - - name: Copy dockerfile - synchronize: src=app dest=/tmp - - - name: Build barker image - docker_image: - name: barker:latest - build: - path: /tmp/app/ - dockerfile: /tmp/app/Dockerfile - pull: yes - state: present - source: build - - - name: Upload the .env file - template: - src: "files/chd.env" - dest: "/var/lib/{{ host_directory }}/.env" - - - name: Create barker container - docker_container: - name: "{{ host_directory }}" - image: barker:latest - state: started - restart_policy: "unless-stopped" - env_file: "/var/lib/{{ host_directory }}/.env" - links: - - "postgres:db" - - "redis:redis" - published_ports: - - "127.0.0.1:{{ host_port }}:80" - volumes: - - "/var/lib/{{ host_directory }}/frontend:/frontend" - - - name: Check if Nginx conf file exists - stat: path="/etc/nginx/sites-available/{{ http_conf }}" - register: status - - - name: No need to reload Nginx - debug: msg= {{ "No need to reload Nginx as sites-available entries have already been created" }} - - - name: Set Nginx conf file - when: status.stat.exists == false - template: - src: "files/nginx.conf.j2" - dest: "/etc/nginx/sites-available/{{ http_conf }}" - - - name: Enable new site - when: status.stat.exists == false - file: - src: "/etc/nginx/sites-available/{{ http_conf }}" - dest: "/etc/nginx/sites-enabled/{{ http_conf }}" - state: link - notify: Reload Nginx - - handlers: - - name: Reload Nginx - service: - name: nginx - state: reloaded diff --git a/docker/playbook-mhl.yml b/docker/playbook-mhl.yml deleted file mode 100755 index eede660..0000000 --- a/docker/playbook-mhl.yml +++ /dev/null @@ -1,69 +0,0 @@ -################################################# -# DO Community Playbooks: Docker -################################################# ---- -- hosts: all - become: true - vars_files: - - vars/mhl.yml - - tasks: - - name: Copy dockerfile - synchronize: src=app dest=/tmp - - - name: Build barker image - docker_image: - name: barker:latest - build: - path: /tmp/app/ - dockerfile: /tmp/app/Dockerfile - pull: yes - state: present - source: build - - - name: Upload the .env file - template: - src: "files/mhl.env" - dest: "/var/lib/{{ host_directory }}/.env" - - - name: Create barker container - docker_container: - name: "{{ host_directory }}" - image: barker:latest - state: started - restart_policy: "unless-stopped" - env_file: "/var/lib/{{ host_directory }}/.env" - links: - - "postgres:db" - - "redis:redis" - published_ports: - - "127.0.0.1:{{ host_port }}:80" - volumes: - - "/var/lib/{{ host_directory }}/frontend:/frontend" - - - name: Check if Nginx conf file exists - stat: path="/etc/nginx/sites-available/{{ http_conf }}" - register: status - - - name: No need to reload Nginx - debug: msg= {{ "No need to reload Nginx as sites-available entries have already been created" }} - - - name: Set Nginx conf file - when: status.stat.exists == false - template: - src: "files/nginx.conf.j2" - dest: "/etc/nginx/sites-available/{{ http_conf }}" - - - name: Enable new site - when: status.stat.exists == false - file: - src: "/etc/nginx/sites-available/{{ http_conf }}" - dest: "/etc/nginx/sites-enabled/{{ http_conf }}" - state: link - notify: Reload Nginx - - handlers: - - name: Reload Nginx - service: - name: nginx - state: reloaded diff --git a/docker/playbook-pkl.yml b/docker/playbook-pkl.yml deleted file mode 100755 index 24c2786..0000000 --- a/docker/playbook-pkl.yml +++ /dev/null @@ -1,69 +0,0 @@ -################################################# -# DO Community Playbooks: Docker -################################################# ---- -- hosts: all - become: true - vars_files: - - vars/pkl.yml - - tasks: - - name: Copy dockerfile - synchronize: src=app dest=/tmp - - - name: Build barker image - docker_image: - name: barker:latest - build: - path: /tmp/app/ - dockerfile: /tmp/app/Dockerfile - pull: yes - state: present - source: build - - - name: Upload the .env file - template: - src: "files/pkl.env" - dest: "/var/lib/{{ host_directory }}/.env" - - - name: Create barker container - docker_container: - name: "{{ host_directory }}" - image: barker:latest - state: started - restart_policy: "unless-stopped" - env_file: "/var/lib/{{ host_directory }}/.env" - links: - - "postgres:db" - - "redis:redis" - published_ports: - - "127.0.0.1:{{ host_port }}:80" - volumes: - - "/var/lib/{{ host_directory }}/frontend:/frontend" - - - name: Check if Nginx conf file exists - stat: path="/etc/nginx/sites-available/{{ http_conf }}" - register: status - - - name: No need to reload Nginx - debug: msg= {{ "No need to reload Nginx as sites-available entries have already been created" }} - - - name: Set Nginx conf file - when: status.stat.exists == false - template: - src: "files/nginx.conf.j2" - dest: "/etc/nginx/sites-available/{{ http_conf }}" - - - name: Enable new site - when: status.stat.exists == false - file: - src: "/etc/nginx/sites-available/{{ http_conf }}" - dest: "/etc/nginx/sites-enabled/{{ http_conf }}" - state: link - notify: Reload Nginx - - handlers: - - name: Reload Nginx - service: - name: nginx - state: reloaded diff --git a/docker/playbook.yml b/docker/playbook.yml index 4dbaa2e..f6fb52a 100755 --- a/docker/playbook.yml +++ b/docker/playbook.yml @@ -2,94 +2,12 @@ # DO Community Playbooks: Docker ################################################# --- -- hosts: all +- hosts: barker become: true vars_files: - - vars/default.yml + - "{{ var_file }}" - tasks: - - name: Copy dockerfile - synchronize: src=app dest=/tmp - - - name: Build barker image - docker_image: - name: barker:latest - build: - path: /tmp/app/ - dockerfile: /tmp/app/Dockerfile - pull: yes - state: present - source: build - - - name: Build barker worker image - docker_image: - name: barker-worker:latest - build: - path: /tmp/app/ - dockerfile: /tmp/app/worker.Dockerfile - pull: yes - state: present - source: build - - - name: Upload the .env file - template: - src: "files/.env" - dest: "/var/lib/{{ host_directory }}/.env" - - - name: Upload the worker .env file - template: - src: "files/worker.env" - dest: "/var/lib/{{ host_directory }}/worker.env" - - - name: Create barker container - docker_container: - name: "{{ host_directory }}" - image: barker:latest - state: started - restart_policy: "unless-stopped" - env_file: "/var/lib/{{ host_directory }}/.env" - links: - - "postgres:db" - - "redis:redis" - published_ports: - - "127.0.0.1:{{ host_port }}:80" - volumes: - - "/var/lib/{{ host_directory }}/frontend:/frontend" - - - name: Create barker worker container - docker_container: - name: "{{ host_directory }}-worker" - image: barker-worker:latest - state: started - restart_policy: "unless-stopped" - env_file: "/var/lib/{{ host_directory }}/worker.env" - links: - - "redis:redis" - - - - name: Check if Nginx conf file exists - stat: path="/etc/nginx/sites-available/{{ http_conf }}" - register: status - - - name: No need to reload Nginx - debug: msg= {{ "No need to reload Nginx as sites-available entries have already been created" }} - - - name: Set Nginx conf file - when: status.stat.exists == false - template: - src: "files/nginx.conf.j2" - dest: "/etc/nginx/sites-available/{{ http_conf }}" - - - name: Enable new site - when: status.stat.exists == false - file: - src: "/etc/nginx/sites-available/{{ http_conf }}" - dest: "/etc/nginx/sites-enabled/{{ http_conf }}" - state: link - notify: Reload Nginx - - handlers: - - name: Reload Nginx - service: - name: nginx - state: reloaded + roles: + - upload + - docker + - nginx diff --git a/docker/roles/docker/tasks/main.yaml b/docker/roles/docker/tasks/main.yaml new file mode 100644 index 0000000..95bedea --- /dev/null +++ b/docker/roles/docker/tasks/main.yaml @@ -0,0 +1,26 @@ +--- +# Tasks for docker role +- name: Build barker image + docker_image: + name: barker:latest + build: + path: "/tmp/{{ host_directory }}/" + dockerfile: "/tmp/{{ host_directory }}/Dockerfile" + pull: yes + state: present + source: build + +- name: Create barker container + docker_container: + name: "{{ host_directory }}" + image: barker:latest + state: started + restart_policy: "unless-stopped" + env_file: "/var/lib/{{ host_directory }}/.env" + links: + - "postgres:db" + - "redis:redis" + published_ports: + - "127.0.0.1:{{ host_port }}:80" + volumes: + - "/var/lib/{{ host_directory }}/frontend:/frontend" diff --git a/docker/roles/nginx/defaults/main.yaml b/docker/roles/nginx/defaults/main.yaml new file mode 100644 index 0000000..85c5e6d --- /dev/null +++ b/docker/roles/nginx/defaults/main.yaml @@ -0,0 +1,2 @@ +--- +# Default variables for Nginx role diff --git a/docker/roles/nginx/handlers/main.yaml b/docker/roles/nginx/handlers/main.yaml new file mode 100644 index 0000000..622fd36 --- /dev/null +++ b/docker/roles/nginx/handlers/main.yaml @@ -0,0 +1,6 @@ +--- +# Handlers for Nginx role +- name: Reload Nginx + service: + name: nginx + state: reloaded diff --git a/docker/roles/nginx/tasks/main.yaml b/docker/roles/nginx/tasks/main.yaml new file mode 100644 index 0000000..4441fe7 --- /dev/null +++ b/docker/roles/nginx/tasks/main.yaml @@ -0,0 +1,24 @@ +--- +# Tasks for Nginx role +- name: Check if Nginx conf file exists + stat: + path: "/etc/nginx/sites-available/{{ http_conf }}" + register: status + +- name: No need to reload Nginx + debug: + msg: "No need to reload Nginx as sites-available entries have already been created" + +- name: Set Nginx conf file + when: status.stat.exists == false + template: + src: "files/nginx.conf.j2" + dest: "/etc/nginx/sites-available/{{ http_conf }}" + +- name: Enable new site + when: status.stat.exists == false + file: + src: "/etc/nginx/sites-available/{{ http_conf }}" + dest: "/etc/nginx/sites-enabled/{{ http_conf }}" + state: link + notify: Reload Nginx diff --git a/docker/roles/upload/tasks/main.yaml b/docker/roles/upload/tasks/main.yaml new file mode 100644 index 0000000..c5dd585 --- /dev/null +++ b/docker/roles/upload/tasks/main.yaml @@ -0,0 +1,21 @@ +--- +# Tasks for upload role +- name: Ensure Temporary Directory exists + file: + path: "/tmp/{{ host_directory }}" + state: directory + +- name: Copy dockerfile + synchronize: + src: app + dest: "/tmp/{{ host_directory }}" + +- name: Ensure Host Directory exists + file: + path: "/var/lib/{{ host_directory }}" + state: directory + +- name: Upload the .env file + template: + src: "{{ env_file }}" + dest: "/var/lib/{{ host_directory }}/.env" diff --git a/docker/vars/chd.yml b/docker/vars/chd.yml index 1f3fbdc..034538d 100644 --- a/docker/vars/chd.yml +++ b/docker/vars/chd.yml @@ -3,3 +3,4 @@ http_host: "knox.greatbear.in" http_conf: "knox.greatbear.in.conf" host_port: "8337" host_directory: "barker-chd" +env_file: "files/.env-chd" diff --git a/docker/vars/mhl.yml b/docker/vars/mhl.yml index 2ffb3aa..981c603 100644 --- a/docker/vars/mhl.yml +++ b/docker/vars/mhl.yml @@ -3,3 +3,4 @@ http_host: "knox.hngmohali.com" http_conf: "knox.hngmohali.com.conf" host_port: "8336" host_directory: "barker-mhl" +env_file: "files/.env-mhl" diff --git a/docker/vars/pkl.yml b/docker/vars/pkl.yml index 0814685..c9232a3 100644 --- a/docker/vars/pkl.yml +++ b/docker/vars/pkl.yml @@ -3,3 +3,4 @@ http_host: "knox.hopsngrains.com" http_conf: "knox.hopsngrains.com.conf" host_port: "8338" host_directory: "barker-pkl" +env_file: "files/.env-pkl"