|
| 1 | +- name: Show Docker Compose Running Status |
| 2 | + hosts: all |
| 3 | + become: true |
| 4 | + become_user: root |
| 5 | + vars: |
| 6 | + deploy_tag: ${{ github.event.inputs.deployTag }} |
| 7 | + docker_compose_dir: /data/coseeing-server |
| 8 | + secret_name: prod/rdsuser/coseeing |
| 9 | + secret_region: ap-northeast-1 |
| 10 | + webroot_path: '/var/www/html' # the root path of your site |
| 11 | + certbot_source_directory: /usr/local/certbot-src |
| 12 | + certbot_executable_path: "{{ certbot_source_directory }}/venv/bin/certbot" |
| 13 | + domain: api.coseeing.org |
| 14 | + email: tsengwoody@coseeing.org |
| 15 | + ecr_location: 622913514517.dkr.ecr.ap-northeast-1.amazonaws.com |
| 16 | + image_name: "{{ ecr_location }}/coseeing-be:{{ deploy_tag }}" |
| 17 | + collections: |
| 18 | + - community.docker |
| 19 | + - community.aws |
| 20 | + tasks: |
| 21 | + |
| 22 | + - name: Set ansible_python_interpreter to use the installed Python |
| 23 | + set_fact: |
| 24 | + ansible_python_interpreter: /usr/bin/python3 |
| 25 | + |
| 26 | + - name: Update apt repo and cache on all Debian/Ubuntu boxes |
| 27 | + apt: update_cache=yes force_apt_get=yes cache_valid_time=3600 |
| 28 | + become: true |
| 29 | + |
| 30 | + - name: Upgrade all apt packages |
| 31 | + apt: upgrade=yes force_apt_get=yes |
| 32 | + become: true |
| 33 | + |
| 34 | + - name: Install Python pip |
| 35 | + apt: name={{ item }} update_cache=true state=present force_apt_get=yes |
| 36 | + with_items: |
| 37 | + - python3-pip |
| 38 | + become: true |
| 39 | + |
| 40 | + - name: Install Python packages using apt |
| 41 | + apt: |
| 42 | + name: |
| 43 | + - python3-docker |
| 44 | + - python3-boto3 |
| 45 | + - python3-botocore |
| 46 | + - python3-venv |
| 47 | + - gcc |
| 48 | + - libaugeas0 |
| 49 | + - libssl-dev |
| 50 | + - libffi-dev |
| 51 | + - ca-certificates |
| 52 | + - openssl |
| 53 | + - git |
| 54 | + state: present |
| 55 | + update_cache: yes |
| 56 | + become: true |
| 57 | + |
| 58 | + - name: Get info from AWS secret manager |
| 59 | + set_fact: |
| 60 | + secret_data: "{{ lookup('amazon.aws.aws_secret', secret_name, region=secret_region) | from_json }}" |
| 61 | + |
| 62 | + - name: Set fact from secret_json |
| 63 | + set_fact: |
| 64 | + SECRET_KEY: "{{ secret_data.SECRET_KEY }}" |
| 65 | + MARIADB_USER: "{{ secret_data.username }}" |
| 66 | + MARIADB_PASSWORD: "{{ secret_data.password }}" |
| 67 | + MARIADB_HOST: "{{ secret_data.host }}" |
| 68 | + MARIADB_PORT: "{{ secret_data.port }}" |
| 69 | + MARIADB_DATABASE: "{{ secret_data.database }}" |
| 70 | + |
| 71 | + - name: Ensure docker compose directory exists |
| 72 | + file: |
| 73 | + path: "{{ docker_compose_dir }}" |
| 74 | + state: directory |
| 75 | + mode: '0755' |
| 76 | + become: true |
| 77 | + |
| 78 | + - name: Ensure docker compose directory exists |
| 79 | + file: |
| 80 | + path: "{{ docker_compose_dir }}/data" |
| 81 | + state: directory |
| 82 | + mode: '0755' |
| 83 | + become: true |
| 84 | + |
| 85 | + - name: Create .env file |
| 86 | + copy: |
| 87 | + dest: "{{ docker_compose_dir }}/.env" |
| 88 | + content: | |
| 89 | + SECRET_KEY={{ SECRET_KEY }} |
| 90 | + MARIADB_USER={{ MARIADB_USER }} |
| 91 | + MARIADB_PASSWORD={{ MARIADB_PASSWORD }} |
| 92 | + MARIADB_HOST={{ MARIADB_HOST }} |
| 93 | + MARIADB_PORT={{ MARIADB_PORT }} |
| 94 | + MARIADB_DATABASE={{ MARIADB_DATABASE }} |
| 95 | + ALLOWED_HOSTS=* |
| 96 | + HOST=https://coseeing.org |
| 97 | +
|
| 98 | + - name: Copy docker-compose.yml Document |
| 99 | + copy: |
| 100 | + dest: "{{ docker_compose_dir }}/docker-compose.yml" |
| 101 | + content: | |
| 102 | + services: |
| 103 | + coseeing-server: |
| 104 | + container_name: coseeing-server |
| 105 | + image: {{ image_name }} |
| 106 | + restart: always |
| 107 | + volumes: |
| 108 | + - my-volume:/app/data |
| 109 | + networks: |
| 110 | + - default |
| 111 | + - entry |
| 112 | + labels: |
| 113 | + - "traefik.enable=true" |
| 114 | + - "traefik.http.routers.api-coseeing.rule=Host(`api.coseeing.org`)" |
| 115 | + - "traefik.http.routers.api-coseeing.entrypoints=websecure" |
| 116 | + - "traefik.http.routers.api-coseeing.tls.certresolver=api-coseeing" |
| 117 | + - "traefik.docker.network=entry" |
| 118 | + deploy: |
| 119 | + resources: |
| 120 | + limits: |
| 121 | + cpus: '0.20' |
| 122 | + memory: 0.3G |
| 123 | + command: ["sh", "-c", "python manage.py runserver 0.0.0.0:8000"] |
| 124 | + environment: |
| 125 | + - SECRET_KEY=${SECRET_KEY} |
| 126 | + - MARIADB_USER=${MARIADB_USER} |
| 127 | + - MARIADB_PASSWORD=${MARIADB_PASSWORD} |
| 128 | + - MARIADB_HOST=${MARIADB_HOST} |
| 129 | + - MARIADB_PORT=${MARIADB_PORT} |
| 130 | + - MARIADB_DATABASE=${MARIADB_DATABASE} |
| 131 | + - ALLOWED_HOSTS=${ALLOWED_HOSTS} |
| 132 | + - HOST=${HOST} |
| 133 | +
|
| 134 | + volumes: |
| 135 | + my-volume: |
| 136 | + driver: local |
| 137 | + driver_opts: |
| 138 | + type: none |
| 139 | + device: data |
| 140 | + o: bind |
| 141 | +
|
| 142 | + networks: |
| 143 | + entry: |
| 144 | + driver: bridge |
| 145 | + name: entry |
| 146 | +
|
| 147 | + - name: Update the repository cache and update package "unzip" to latest version using default |
| 148 | + apt: |
| 149 | + name: unzip |
| 150 | + state: latest |
| 151 | + update_cache: yes |
| 152 | + |
| 153 | + - name: Install AWS CLI v2 |
| 154 | + shell: | |
| 155 | + curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip" |
| 156 | + unzip /tmp/awscliv2.zip -d /tmp |
| 157 | + sudo /tmp/aws/install |
| 158 | + rm -f /tmp/awscliv2.zip |
| 159 | + rm -rf /tmp/aws |
| 160 | + args: |
| 161 | + creates: /usr/local/bin/aws |
| 162 | + |
| 163 | + - name: Login ECR using AWS CLI |
| 164 | + shell: | |
| 165 | + aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin {{ ecr_location }} |
| 166 | + register: ecr_login |
| 167 | + no_log: false |
| 168 | + |
| 169 | + - name: Check if image exists |
| 170 | + docker_image_info: |
| 171 | + name: "{{ image_name }}" |
| 172 | + register: image_info |
| 173 | + |
| 174 | + - name: Untag existing image if it exists |
| 175 | + docker_image: |
| 176 | + name: "{{ image_name }}" |
| 177 | + state: absent |
| 178 | + force_absent: true |
| 179 | + when: image_info.images | length > 0 |
| 180 | + |
| 181 | + - name: Run |
| 182 | + docker_compose_v2: |
| 183 | + project_src: "{{ docker_compose_dir }}" |
| 184 | + state: present |
| 185 | + register: compose_result |
| 186 | + |
| 187 | + - name: Show compose_result Detail info |
| 188 | + debug: |
| 189 | + var: compose_result |
0 commit comments