summaryrefslogtreecommitdiff
path: root/ansible
diff options
context:
space:
mode:
Diffstat (limited to 'ansible')
-rw-r--r--ansible/gunicorn.service.j213
-rw-r--r--ansible/hosts.ini2
-rw-r--r--ansible/nginx.conf.j213
-rw-r--r--ansible/setup-deployment.yml108
-rw-r--r--ansible/setup-docker.yml27
5 files changed, 163 insertions, 0 deletions
diff --git a/ansible/gunicorn.service.j2 b/ansible/gunicorn.service.j2
new file mode 100644
index 0000000..fe9c69c
--- /dev/null
+++ b/ansible/gunicorn.service.j2
@@ -0,0 +1,13 @@
+[Unit]
+Description=gunicorn daemon
+After=network.target
+
+[Service]
+User=ec2-user
+Group=nginx
+WorkingDirectory=/opt/image_processor
+ExecStart=/opt/image_processor/venv/bin/gunicorn --workers 3 --bind unix:{{ app_directory }}/myapp.sock -m 007 wsgi:app
+
+[Install]
+WantedBy=multi-user.target
+
diff --git a/ansible/hosts.ini b/ansible/hosts.ini
new file mode 100644
index 0000000..c3896e7
--- /dev/null
+++ b/ansible/hosts.ini
@@ -0,0 +1,2 @@
+[ec2_instances]
+44.202.210.65 ansible_user=ec2-user ansible_ssh_private_key_file=~/keypair_amazon/deployer_key
diff --git a/ansible/nginx.conf.j2 b/ansible/nginx.conf.j2
new file mode 100644
index 0000000..a890814
--- /dev/null
+++ b/ansible/nginx.conf.j2
@@ -0,0 +1,13 @@
+server {
+ listen 80;
+ ec2_user {{ server_ip }};
+
+ location / {
+ proxy_pass http://unix:/opt/image_processor/myapp.sock;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ }
+}
+
diff --git a/ansible/setup-deployment.yml b/ansible/setup-deployment.yml
new file mode 100644
index 0000000..feb42c5
--- /dev/null
+++ b/ansible/setup-deployment.yml
@@ -0,0 +1,108 @@
+---
+- name: Deploy Flask Application on Amazon Linux
+ hosts: all
+ become: yes
+
+ vars:
+ app_directory: "/opt/image_processor"
+ requirements:
+ - Flask==2.0.1
+ - flask-cors
+ - boto3
+ - opencv-python-headless
+ - numpy
+ - gunicorn
+
+ tasks:
+ - name: Update all system packages
+ yum:
+ name: "*"
+ state: latest
+ update_cache: yes
+
+ - name: Install essential packages
+ yum:
+ name:
+ - gcc
+ - gcc-c++
+ - git
+ state: present
+
+ - name: enable Nginx using amazon-linux-extras
+ command: amazon-linux-extras enable nginx1.12
+
+ - name: Install nginx
+ yum:
+ name: nginx
+ state: present
+
+ - name: Upgrade pip
+ command: pip3 install --upgrade pip
+
+ - name: Install virtualenv using pip
+ command: pip3 install virtualenv
+
+ - name: Create application directory
+ file:
+ path: "{{ app_directory }}"
+ state: directory
+ mode: '0755'
+
+ - name: Remove existing virtual environment
+ file:
+ path: "{{ app_directory }}/venv"
+ state: absent
+
+ - name: Create a virtual environment using virtualenv
+ command: virtualenv -p python {{ app_directory }}/venv
+
+ - name: Install Python packages in the virtual environment
+ pip:
+ name: "{{ item }}"
+ virtualenv: "{{ app_directory }}/venv"
+ loop: "{{ requirements }}"
+
+ - name: Copy application files to the server
+ copy:
+ src: "{{ item }}"
+ dest: "{{ app_directory }}"
+ mode: '0644'
+ with_fileglob:
+ - "../../CloudRender/backend/*.py"
+
+ - name: Setup Gunicorn systemd service
+ template:
+ src: gunicorn.service.j2
+ dest: /etc/systemd/system/gunicorn.service
+ notify:
+ - Reload systemd
+ - Restart Gunicorn
+
+ - name: Setup Nginx configuration
+ template:
+ src: nginx.conf.j2
+ dest: /etc/nginx/conf.d/my_flask_app.conf
+ notify:
+ - Restart nginx
+
+ - name: Ensure nginx is running and enabled
+ systemd:
+ name: nginx
+ state: started
+ enabled: true
+
+ handlers:
+ - name: Restart nginx
+ systemd:
+ name: nginx
+ state: restarted
+
+ - name: Reload systemd
+ systemd:
+ daemon_reload: yes
+
+ - name: Restart Gunicorn
+ systemd:
+ name: gunicorn
+ state: restarted
+
diff --git a/ansible/setup-docker.yml b/ansible/setup-docker.yml
new file mode 100644
index 0000000..e4cac93
--- /dev/null
+++ b/ansible/setup-docker.yml
@@ -0,0 +1,27 @@
+- name: Install Docker on EC2 instances
+ hosts: ec2_instances
+ become: yes
+
+ tasks:
+ - name: Update yum package manager
+ yum:
+ name: '*'
+ state: latest
+
+ - name: Install Docker
+ yum:
+ name: docker
+ state: present
+
+ - name: Start and enable Docker service
+ service:
+ name: docker
+ state: started
+ enabled: yes
+
+ - name: Add ec2-user to the docker group
+ user:
+ name: ec2-user
+ groups: docker
+ append: yes
+