Skip to content

Commit

Permalink
bugfix: Cannot copy database
Browse files Browse the repository at this point in the history
* bugfix: Cannot copy database

The additional fix is to restart the database to kill connections to it.

All reset code has been moved into one place.

* chore(lint): ``become_user`` should have a corresponding ``become`` at the play or task level

* bugfix: Variable sandbox_database does not exist

* fix: Undefined variable sandbox_moodle_instance
  • Loading branch information
geoffreyvanwyk authored Nov 23, 2023
1 parent 2bf6c24 commit 19a8e9b
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 162 deletions.
93 changes: 0 additions & 93 deletions tasks/install_moodle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,96 +33,3 @@
moodle_cfg_disableupdateautodeploy: true
moodle_cfg_noemailever: true
moodle_cfg_preventexecpath: true

- name: Set instance identifier for Moodle {{ sandbox_item.version }}
ansible.builtin.set_fact:
sandbox_moodle_instance: >-
{{ sandbox_domain }}-moodle-{{ sandbox_item.version }}
- name: Set PHP version
ansible.builtin.set_fact:
sandbox_php_version: "{{
'8.1' if (sandbox_item.branch == 'MOODLE_402_STABLE') or
(sandbox_item.branch == 'MOODLE_401_STABLE')
else '8.0' if (sandbox_item.branch == 'MOODLE_400_STABLE') or
(sandbox_item.branch == 'MOODLE_311_STABLE')
else '7.4' }}"

- name: Enable Moodle's maintenance mode {{ sandbox_item.version }}
ansible.builtin.command:
chdir: /var/www/html/moodle-{{ sandbox_item.version }}
cmd: php{{ sandbox_php_version }} admin/cli/maintenance.php --enable
changed_when: no # Set for passing Molecule idempotency test.

- name: Does archive of moodledata exists for Moodle {{ sandbox_item.version }}?
ansible.builtin.stat:
path: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}.tar.gz
register: moodledata_archive

- name: Prepare for reset of moodledata {{ sandbox_item.version }}
when: not moodledata_archive.stat.exists
become: yes
block:
- name: Rename moodledata as fresh {{ sandbox_item.version }}
ansible.builtin.command:
cmd: >
mv /var/www/moodledata-{{ sandbox_moodle_instance }}
/var/www/moodledata-fresh-{{ sandbox_moodle_instance }}
creates: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}

- name: Archive fresh moodledata {{ sandbox_item.version }}
community.general.archive:
path: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}
dest: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}.tar.gz
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: u=rw,g=r,o=r

- name: Rename moodledata as current {{ sandbox_item.version }}
ansible.builtin.command:
cmd: >
mv /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}
/var/www/moodledata-current-{{ sandbox_moodle_instance }}
creates: /var/www/moodledata-current-{{ sandbox_moodle_instance }}

- name: Turn moodledata into link {{ sandbox_item.version }}
ansible.builtin.file:
src: /var/www/moodledata-current-{{ sandbox_moodle_instance }}
dest: /var/www/moodledata-{{ sandbox_moodle_instance }}
state: link

- name: Extract fresh moodledata {{ sandbox_item.version }}
ansible.builtin.unarchive:
remote_src: yes
src: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}.tar.gz
dest: /var/www

- name: Set permissions for fresh moodledata {{ sandbox_item.version }}
ansible.builtin.file:
path: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}
state: directory
owner: "{{ ansible_user }}"
group: www-data
mode: u=rwX,g=rwX,o=rwX

- name: Prepare for reset of database {{ sandbox_item.version }}
become: yes
become_user: postgres
block:
- name: Copy fresh database {{ sandbox_item.version }}
community.postgresql.postgresql_db:
name: moodle-{{ sandbox_item.version | replace(".", "") }}-fresh
state: present
template: moodle-{{ sandbox_item.version | replace(".", "") }}

- name: Copy next database {{ sandbox_item.version }}
community.postgresql.postgresql_db:
name: moodle-{{ sandbox_item.version | replace(".", "") }}-next
state: present
template: moodle-{{ sandbox_item.version | replace(".", "") }}

- name: Disable Moodle's maintenance mode {{ sandbox_item.version }}
ansible.builtin.command:
chdir: /var/www/html/moodle-{{ sandbox_item.version }}
cmd: php{{ sandbox_php_version }} admin/cli/maintenance.php --disable
changed_when: no # Set for passing Molecule idempotency test.
239 changes: 170 additions & 69 deletions tasks/reset_moodle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,97 +20,198 @@
# @copyright 2023 Geoffrey Bernardo van Wyk (https://geoffreyvanwyk.dev)
##

- name: Set instance identifier for Moodle {{ sandbox_item.version }}
ansible.builtin.set_fact:
sandbox_moodle_instance: >-
{{ sandbox_domain }}-moodle-{{ sandbox_item.version }}
- name: Set PHP version
- name: Set independent variables [{{ sandbox_item.version }}]
ansible.builtin.set_fact:
sandbox_database: >-
moodle-{{ sandbox_item.version | replace('.', '') }}
sandbox_moodle_html: /var/www/html/moodle-{{ sandbox_item.version }}
sandbox_moodledata: >-
/var/www/moodledata-{{ sandbox_domain }}-moodle-{{ sandbox_item.version }}
sandbox_php_version: "{{
'8.1' if (sandbox_item.branch == 'MOODLE_402_STABLE') or
(sandbox_item.branch == 'MOODLE_401_STABLE')
else '8.0' if (sandbox_item.branch == 'MOODLE_400_STABLE') or
(sandbox_item.branch == 'MOODLE_311_STABLE')
else '7.4' }}"

- name: Enable Moodle's maintenance mode {{ sandbox_item.version }}
ansible.builtin.command:
chdir: /var/www/html/moodle-{{ sandbox_item.version }}
cmd: php{{ sandbox_php_version }} admin/cli/maintenance.php --enable
changed_when: no # Set for passing Molecule idempotency test.
- name: Set dependent variables [{{ sandbox_item.version }}]
ansible.builtin.set_fact:
sandbox_database_fresh: "{{ sandbox_database }}-fresh"
sandbox_database_next: "{{ sandbox_database }}-next"
sandbox_database_old: "{{ sandbox_database }}-old"
sandbox_moodledata_fresh: "{{ sandbox_moodledata }}-fresh"
sandbox_moodledata_archive: "{{ sandbox_moodledata }}-fresh.tar.gz"
sandbox_moodledata_current: "{{ sandbox_moodledata }}-current"
sandbox_moodledata_old: "{{ sandbox_moodledata }}-old"

- name: Rename current moodledata to old {{ sandbox_item.version }}
become: yes
ansible.builtin.command:
cmd: >
mv /var/www/moodledata-current-{{ sandbox_moodle_instance }}
/var/www/moodledata-old-{{ sandbox_moodle_instance }}
creates: /var/www/moodledata-old-{{ sandbox_moodle_instance }}
# Additional database connections will prevent the copying of the database.
- name: Ensure no extra database connections [{{ sandbox_item.version }}]
block:
# Moodle maintenance mode is enabled to stop the scheduled tasks which might
# make connections to the database.
# yamllint disable-line rule:line-length
# @see {@link https://docs.moodle.org/403/en/Maintenance_mode#CLI_maintenance_mode}
- name: Enable Moodle's maintenance mode [{{ sandbox_item.version }}]
ansible.builtin.command:
chdir: "{{ sandbox_moodle_html }}"
cmd: php{{ sandbox_php_version }} admin/cli/maintenance.php --enable
# `changed_when` is actually always `yes` in this case, but it is set to
# `no` to pass Molecule's idempotency test. The other option of adding a
# tag `molecule-idempotency-notest` would not work, because with that tag
# Molecule would skip this task which will make the playbook fail for the
# very reason the task is here in the first place.
changed_when: no

- name: Rename fresh moodledata to current {{ sandbox_item.version }}
become: yes
ansible.builtin.command:
cmd: >
mv /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}
/var/www/moodledata-current-{{ sandbox_moodle_instance }}
creates: /var/www/moodledata-current-{{ sandbox_moodle_instance }}
- name: Restart database to kill connections [{{ sandbox_item.version }}]
become: yes
ansible.builtin.sysvinit:
name: postgresql
state: restarted

- name: Rename "current" database to old {{ sandbox_item.version }}
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: moodle-{{ sandbox_item.version | replace(".", "") }}
state: rename
target: moodle-{{ sandbox_item.version | replace(".", "") }}-old
- name: Does archive of moodledata exists [{{ sandbox_item.version }}]?
ansible.builtin.stat:
path: "{{ sandbox_moodledata_archive }}"
register: sandbox_stat_moodledata_archive

- name: Rename next database to "current" {{ sandbox_item.version }}
- name: Reset Moodle [{{ sandbox_item.version }}]
when: sandbox_stat_moodledata_archive.stat.exists
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: moodle-{{ sandbox_item.version | replace(".", "") }}-next
state: rename
target: moodle-{{ sandbox_item.version | replace(".", "") }}
block:
- name: Rename current moodledata to old [{{ sandbox_item.version }}]
ansible.builtin.command:
cmd: mv {{ sandbox_moodledata_current }} {{ sandbox_moodledata_old }}
creates: "{{ sandbox_moodledata_old }}"

- name: Remove old moodledata {{ sandbox_item.version }}
become: yes
ansible.builtin.file:
path: /var/www/moodledata-old-{{ sandbox_moodle_instance }}
state: absent
- name: Rename fresh moodledata to current [{{ sandbox_item.version }}]
ansible.builtin.command:
cmd: mv {{ sandbox_moodledata_fresh }} {{ sandbox_moodledata_current }}
creates: "{{ sandbox_moodledata_current }}"

- name: Drop old database {{ sandbox_item.version }}
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: moodle-{{ sandbox_item.version | replace(".", "") }}-old
state: absent
- name: Rename current database to old [{{ sandbox_item.version }}]
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: "{{ sandbox_database }}"
state: rename
target: "{{ sandbox_database_old }}"

- name: Extract fresh moodledata {{ sandbox_item.version }}
- name: Rename next database to current [{{ sandbox_item.version }}]
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: "{{ sandbox_database_next }}"
state: rename
target: "{{ sandbox_database }}"

- name: Has Moodle been reset [{{ sandbox_item.version }}]?
ansible.builtin.stat:
path: "{{ sandbox_moodledata_old }}"
register: sandbox_stat_moodledata_old

- name: Prepare for next reset [{{ sandbox_item.version }}]
when: sandbox_stat_moodledata_old.stat.exists
become: yes
ansible.builtin.unarchive:
remote_src: yes
src: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}.tar.gz
dest: /var/www
block:
- name: Remove old moodledata [{{ sandbox_item.version }}]
ansible.builtin.file:
path: "{{ sandbox_moodledata_old }}"
state: absent

- name: Extract fresh moodledata [{{ sandbox_item.version }}]
ansible.builtin.unarchive:
remote_src: yes
src: "{{ sandbox_moodledata_archive }}"
dest: /var/www

- name: Set permissions for fresh moodledata [{{ sandbox_item.version }}]
ansible.builtin.file:
path: "{{ sandbox_moodledata_fresh }}"
state: directory
owner: "{{ ansible_user }}"
group: www-data
mode: u=rwX,g=rwX,o=rwX

- name: Set permissions for fresh moodledata {{ sandbox_item.version }}
- name: Drop old database [{{ sandbox_item.version }}]
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: "{{ sandbox_database_old }}"
state: absent

- name: Copy next database [{{ sandbox_item.version }}]
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: "{{ sandbox_database_next }}"
state: present
template: "{{ sandbox_database_fresh }}"

- name: Initialize moodledata for reset [{{ sandbox_item.version }}]
when: not sandbox_stat_moodledata_archive.stat.exists
become: yes
ansible.builtin.file:
path: /var/www/moodledata-fresh-{{ sandbox_moodle_instance }}
state: directory
owner: "{{ ansible_user }}"
group: www-data
mode: u=rwX,g=rwX,o=rwX

- name: Copy next database {{ sandbox_item.version }}
block:
- name: Rename moodledata as fresh [{{ sandbox_item.version }}]
ansible.builtin.command:
cmd: mv {{ sandbox_moodledata }} {{ sandbox_moodledata_fresh }}
creates: "{{ sandbox_moodledata_fresh }}"

- name: Archive fresh moodledata [{{ sandbox_item.version }}]
community.general.archive:
path: "{{ sandbox_moodledata_fresh }}"
dest: "{{ sandbox_moodledata_archive }}"
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: u=rw,g=r,o=r

- name: Rename fresh moodledata to current [{{ sandbox_item.version }}]
ansible.builtin.command:
cmd: mv {{ sandbox_moodledata_fresh }} {{ sandbox_moodledata_current }}
creates: "{{ sandbox_moodledata_current }}"

- name: Turn moodledata into link [{{ sandbox_item.version }}]
ansible.builtin.file:
src: "{{ sandbox_moodledata_current }}"
dest: "{{ sandbox_moodledata }}"
state: link

- name: Extract fresh moodledata [{{ sandbox_item.version }}]
ansible.builtin.unarchive:
remote_src: yes
src: "{{ sandbox_moodledata_archive }}"
dest: /var/www

- name: Set permissions for fresh moodledata [{{ sandbox_item.version }}]
ansible.builtin.file:
path: "{{ sandbox_moodledata_fresh }}"
state: directory
owner: "{{ ansible_user }}"
group: www-data
mode: u=rwX,g=rwX,o=rwX

- name: Initialize database for reset [{{ sandbox_item.version }}]
when: not sandbox_stat_moodledata_archive.stat.exists
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: moodle-{{ sandbox_item.version | replace(".", "") }}-next
state: present
template: moodle-{{ sandbox_item.version | replace(".", "") }}-fresh
block:
- name: Copy fresh database [{{ sandbox_item.version }}]
community.postgresql.postgresql_db:
name: "{{ sandbox_database_fresh }}"
state: present
template: "{{ sandbox_database }}"

- name: Copy next database [{{ sandbox_item.version }}]
community.postgresql.postgresql_db:
name: "{{ sandbox_database_next }}"
state: present
template: "{{ sandbox_database_fresh }}"

- name: Disable Moodle's maintenance mode {{ sandbox_item.version }}
ansible.builtin.command:
chdir: /var/www/html/moodle-{{ sandbox_item.version }}
chdir: "{{ sandbox_moodle_html }}"
cmd: php{{ sandbox_php_version }} admin/cli/maintenance.php --disable
changed_when: no # Set for passing Molecule idempotency test.
# `changed_when` is actually always `yes` in this case, but it is set to
# `no` to pass Molecule's idempotency test. The other option of adding a
# tag `molecule-idempotency-notest` would not work, because with that tag
# Molecule would skip this task which would leave the Moodle instance in
# maintenance mode, which is not what we might want with future tests.
changed_when: no

0 comments on commit 19a8e9b

Please sign in to comment.