summaryrefslogtreecommitdiff
path: root/ansible/setup-deployment.yml
blob: feb42c5d6d44a1f8827ad3702d8128c38fbf6041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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