deploy/deploy.yml
changeset 180 62bffc051e1c
child 182 2dbb6ddbf645
equal deleted inserted replaced
179:e7c7e6e0a8bc 180:62bffc051e1c
       
     1 ---
       
     2 # - hosts: all
       
     3 #   tasks:
       
     4 #     - name: Print some debug information
       
     5 #       vars:
       
     6 #         msg: |
       
     7 #             Module Variables ("vars"):
       
     8 #             --------------------------------
       
     9 #             {{ vars | to_nice_json }}
       
    10 
       
    11 #             Environment Variables ("environment"):
       
    12 #             --------------------------------
       
    13 #             {{ environment | to_nice_json }}
       
    14 
       
    15 #             GROUP NAMES Variables ("group_names"):
       
    16 #             --------------------------------
       
    17 #             {{ group_names | to_nice_json }}
       
    18 
       
    19 #             GROUPS Variables ("groups"):
       
    20 #             --------------------------------
       
    21 #             {{ groups | to_nice_json }}
       
    22 
       
    23 #             HOST Variables ("hostvars"):
       
    24 #             --------------------------------
       
    25 #             {{ hostvars | to_nice_json }}
       
    26 
       
    27 #       debug:
       
    28 #         msg: "{{ msg.split('\n') }}"
       
    29 #       tags: debug_info
       
    30 #
       
    31 # Localhost actions
       
    32 #
       
    33 - hosts: localhost
       
    34   connection: local
       
    35   vars:
       
    36     irinotes_build_dir: "{{playbook_dir}}/build/tmp/irinotes/"
       
    37     backend_build_dir: "{{playbook_dir}}/build/tmp/irinotes/src"
       
    38     client_dir: "{{playbook_dir}}/build/tmp/irinotes/client"
       
    39   tasks:
       
    40 
       
    41     - name: mkdir build folder
       
    42       file:
       
    43         path: "{{playbook_dir}}/build/tmp"
       
    44         state: directory
       
    45 
       
    46     - name: clear irinotes checkout dir if exists
       
    47       file:
       
    48         path: "{{irinotes_build_dir}}"
       
    49         state: absent
       
    50 
       
    51     - name: checkout irinotes
       
    52       hg:
       
    53         repo: "{{irinotes_repo}}"
       
    54         version: "{{irinotes_version}}"
       
    55         force: yes
       
    56         dest: "{{irinotes_build_dir}}"
       
    57 
       
    58     - name: make irinotes py package
       
    59       command: python setup.py sdist chdir="{{backend_build_dir}}"
       
    60 
       
    61 #
       
    62 # all actions
       
    63 #
       
    64 - hosts: remote_backend
       
    65   vars:
       
    66     backend_build_dir: "{{playbook_dir}}/build/tmp/irinotes/src"
       
    67     client_dir: "{{playbook_dir}}/build/tmp/irinotes/client"
       
    68   tasks:
       
    69 
       
    70     - name: get backend version
       
    71       local_action: command python setup.py -V chdir="{{backend_build_dir}}"
       
    72       register: backend_version
       
    73 
       
    74     - name: get discussion package version
       
    75       set_fact:
       
    76         backend_pkg_version: "{{backend_version.stdout}}"
       
    77 
       
    78 #
       
    79 # Localhost actions
       
    80 #
       
    81 - hosts: localhost
       
    82   connection: local
       
    83   vars:
       
    84     backend_build_dir: "{{playbook_dir}}/build/tmp/irinotes/src"
       
    85     client_dir: "{{playbook_dir}}/build/tmp/irinotes/client"
       
    86   tasks:
       
    87 
       
    88 # build client js
       
    89     - name: remove build
       
    90       file: path="{{client_dir}}/build" state=absent
       
    91 
       
    92     - name: client yarn install
       
    93       command: npm install --no-save yarn
       
    94       args:
       
    95         chdir: "{{client_dir}}"
       
    96 
       
    97     - name: client dependencies install
       
    98       command: npx yarn install
       
    99       args:
       
   100         chdir: "{{client_dir}}"
       
   101 
       
   102     - name: client build
       
   103       command: npx yarn build
       
   104       args:
       
   105         chdir: "{{client_dir}}"
       
   106       environment:
       
   107         REACT_APP_API_ROOT_URL: "{{api_root_url}}"
       
   108         REACT_APP_BASENAME: "{{api_root_url}}"
       
   109         REACT_APP_NETWORK_STATUS_INTERVAL: "{{network_status_interval}}"
       
   110         REACT_APP_NETWORK_STATUS_TIMEOUT: "{{network_status_timeout}}"
       
   111         REACT_APP_SYNC_INTERVAL: "{{sync_interval}}"
       
   112 
       
   113 #
       
   114 # Static server
       
   115 #
       
   116 - hosts: remote_static
       
   117   become: yes
       
   118   vars:
       
   119     backend_build_dir: "{{playbook_dir}}/build/tmp/irinotes/src"
       
   120     client_dir: "{{playbook_dir}}/build/tmp/irinotes/client"
       
   121   tasks:
       
   122 
       
   123     - name: create dest static directory content
       
   124       file:
       
   125         path: "{{remote_static_path}}"
       
   126         state: directory
       
   127         owner: "{{static_http_user}}"
       
   128         group: "{{static_http_group}}"
       
   129 
       
   130     - name: remove dest static directory content
       
   131       command: /bin/rm -fr "{{remote_static_path}}/*" warn=False
       
   132       when: remote_static_path != ""
       
   133 
       
   134     - name: transfert static dist to remote
       
   135       copy:
       
   136         src: "{{client_dir}}/build/"
       
   137         dest: "{{remote_static_path}}/"
       
   138         owner: "{{static_http_user}}"
       
   139         group: "{{static_http_group}}"
       
   140       notify:
       
   141         - restart static nginx
       
   142 
       
   143     - name: create refresh nginx config
       
   144       template:
       
   145         src: "{{static_nginx_use_ssl | ternary('nginx.static.ssl.conf.j2', 'nginx.static.conf.j2')}}"
       
   146         dest: "{{static_nginx_config | default('/etc/nginx/site-available/'+static_server_name, true)}}"
       
   147       notify:
       
   148         - restart static nginx
       
   149 
       
   150   handlers:
       
   151     - name: restart static nginx
       
   152       service:
       
   153         name: "{{static_http_service}}"
       
   154         state: restarted
       
   155       ignore_errors: yes
       
   156 
       
   157 #
       
   158 # backend servers
       
   159 #
       
   160 - hosts: remote_backend
       
   161   become: yes
       
   162   vars:
       
   163     backend_build_dir: "{{playbook_dir}}/build/tmp/irinotes/src"
       
   164   tasks:
       
   165 
       
   166     - name: add iri group
       
   167       group:
       
   168         name: iri
       
   169 
       
   170     - name: add uwsgi user
       
   171       user:
       
   172         name: uwsgi
       
   173         group: iri
       
   174         home: /var/www
       
   175         create_home: no
       
   176         system: yes
       
   177         shell: /bin/false
       
   178 
       
   179     - name: create log dir
       
   180       file:
       
   181         path: "{{log_base_path}}"
       
   182         state: directory
       
   183         owner: uwsgi
       
   184         group: iri
       
   185         mode: 0755
       
   186 
       
   187     - name: create config dir
       
   188       file:
       
   189         path: "{{backend_config_base}}"
       
   190         state: directory
       
   191         owner: uwsgi
       
   192         group: iri
       
   193         mode: 0755
       
   194 
       
   195     - name: create config dir requirements
       
   196       file:
       
   197         path: "{{backend_config_base}}/requirements"
       
   198         state: directory
       
   199         owner: uwsgi
       
   200         group: iri
       
   201         mode: 0755
       
   202 
       
   203     - name: create static folder
       
   204       file:
       
   205         path: "{{backend_static_root}}"
       
   206         state: directory
       
   207         owner: uwsgi
       
   208         group: iri
       
   209         mode: 0755
       
   210 
       
   211     - name: create media folder
       
   212       file:
       
   213         path: "{{backend_media_root}}"
       
   214         state: directory
       
   215         owner: uwsgi
       
   216         group: iri
       
   217         mode: 0755
       
   218 
       
   219     - name: install backend venv
       
   220       block:
       
   221         - name: register backend virtualenv stats
       
   222           stat: path="{{backend_venv}}"
       
   223           register: backend_venv_stats
       
   224 
       
   225         - name: copy backend base requirement file
       
   226           copy:
       
   227               src: "{{backend_build_dir}}/requirements/base.txt"
       
   228               dest: "{{backend_config_base}}/requirements/base.txt"
       
   229           register: copy_backend_requirements_base
       
   230 
       
   231         - name: copy backend prod requirement file
       
   232           copy:
       
   233               src: "{{backend_build_dir}}/requirements/prod.txt"
       
   234               dest: "{{backend_config_base}}/requirements/prod.txt"
       
   235           register: copy_backend_requirements_prod
       
   236 
       
   237 
       
   238         - name: remove annotation api venv
       
   239           file:
       
   240               path: "{{backend_venv}}"
       
   241               state: absent
       
   242           when: copy_backend_requirements_base.changed or copy_backend_requirements_prod.changed
       
   243 
       
   244         - name: create backend virtualenv
       
   245           pip:
       
   246               virtualenv_command: /usr/bin/python3 -m venv
       
   247               virtualenv: "{{backend_venv}}"
       
   248               name: pip
       
   249               state: latest
       
   250           when: (copy_backend_requirements_base.changed or copy_backend_requirements_prod.changed) or (not backend_venv_stats.stat.exists)
       
   251 
       
   252         - name: virtualenv install requirements
       
   253           pip:
       
   254               virtualenv: "{{backend_venv}}"
       
   255               requirements: "{{backend_config_base}}/requirements/prod.txt"
       
   256           when: (copy_backend_requirements_base.changed or copy_backend_requirements_prod.changed) or (not backend_venv_stats.stat.exists)
       
   257           notify: restart irinotes backend service
       
   258 
       
   259         - name: copy backend config
       
   260           template:
       
   261             src: "backend_settings.ini.j2"
       
   262             dest: "{{backend_config_base}}/settings.ini"
       
   263           notify: restart irinotes backend service
       
   264 
       
   265         # install irinotes
       
   266         - name: create temporary install directory
       
   267           tempfile:
       
   268               state: directory
       
   269               suffix: build
       
   270           register: backend_temp_folder
       
   271 
       
   272         - name: copy irinotes backend build
       
   273           copy:
       
   274               src: "{{backend_build_dir}}/dist/irinotes-{{backend_pkg_version}}.tar.gz"
       
   275               dest: "{{backend_temp_folder.path}}/irinotes-{{backend_pkg_version}}.tar.gz"
       
   276 
       
   277         - name: virtualenv remove irinotes
       
   278           pip:
       
   279               virtualenv: "{{backend_venv}}"
       
   280               name: irinotes
       
   281               state: absent
       
   282           notify: restart irinotes backend service
       
   283 
       
   284         - name: virtualenv install irinotes
       
   285           pip:
       
   286               virtualenv: "{{backend_venv}}"
       
   287               name: "file://{{backend_temp_folder.path}}/irinotes-{{backend_pkg_version}}.tar.gz"
       
   288           notify: restart irinotes backend service
       
   289 
       
   290         - name: irinotes migrate
       
   291           command: "{{backend_venv}}/bin/django-admin migrate --noinput"
       
   292           args:
       
   293             chdir: "{{backend_venv}}"
       
   294           environment:
       
   295             DJANGO_SETTINGS_MODULE: "irinotes.settings"
       
   296             IRINOTES_CONFIG_BASE: "{{backend_config_base}}"
       
   297           become: yes
       
   298           become_user: uwsgi
       
   299           notify: restart irinotes backend service
       
   300 
       
   301         - name: irinotes collectstatic
       
   302           command: "{{backend_venv}}/bin/django-admin collectstatic --noinput -c"
       
   303           args:
       
   304             chdir: "{{backend_venv}}"
       
   305           environment:
       
   306             DJANGO_SETTINGS_MODULE: "irinotes.settings"
       
   307             IRINOTES_CONFIG_BASE: "{{backend_config_base}}"
       
   308           become: yes
       
   309           become_user: uwsgi
       
   310           notify: restart irinotes backend service
       
   311 
       
   312       always:
       
   313         - name: remove temp folder
       
   314           file:
       
   315             state: absent
       
   316             path: "{{backend_temp_folder.path}}"
       
   317           when: backend_temp_folder is defined
       
   318 
       
   319 
       
   320     - name: copy backend uwsgi config
       
   321       template:
       
   322         src: "uwsgi_irinotes.yml.j2"
       
   323         dest: "{{backend_config_base}}/uwsgi_irinotes.yml"
       
   324       notify: restart irinotes backend service
       
   325 
       
   326     - name: copy annotations api supervisor config
       
   327       template:
       
   328         src: "supervisord_irinotes_backend.ini.j2"
       
   329         dest: "{{backend_supervisor_conf_path}}/irinotes_backend.ini"
       
   330       notify: restart irinotes backend service
       
   331 
       
   332     - name: Add uwsgi app (reread + add)
       
   333       supervisorctl: name=irinotes_backend state=present
       
   334 
       
   335   handlers:
       
   336     - name: reload backend service supervisorctl
       
   337       listen: "restart irinotes backend service"
       
   338       supervisorctl:
       
   339         name: irinotes_backend
       
   340         state: restarted
       
   341       ignore_errors: yes