Changed docker to use a hosts file with various var files and to use roles.

This commit is contained in:
Amritanshu Agrawal 2023-07-23 08:46:07 +05:30
parent 16f29c4139
commit 26310dfc42
17 changed files with 103 additions and 298 deletions

View File

@ -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

14
docker/hosts Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -0,0 +1,2 @@
---
# Default variables for Nginx role

View File

@ -0,0 +1,6 @@
---
# Handlers for Nginx role
- name: Reload Nginx
service:
name: nginx
state: reloaded

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"