Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically reset Moodle instances #26

Merged
merged 5 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/workflows/reset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
# This file is part of Learning Sandbox Online.
#
# Learning Sandbox Online is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Learning Sandbox Online is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Learning Sandbox Online. If not, see <https://www.gnu.org/licenses/>.

##
# GitHub Actions workflow for resetting the Moodle instances on the public
# website every 30 minutes past the hour.
#
# @copyright 2023 Geoffrey Bernardo van Wyk (https://geoffreyvanwyk.dev)
# @see GitHub Actions {@link https://docs.github.com/en/actions}
##

name: Reset
on:

Check warning on line 26 in .github/workflows/reset.yml

View workflow job for this annotation

GitHub Actions / YAML Lint

26:1 [truthy] truthy value should be one of [false, no, true, yes]
schedule:
- cron: '30 * * * *'

jobs:
ansible-playbook:
name: Run playbook
runs-on: ubuntu-22.04

steps:
- name: Check out the codebase.
uses: actions/checkout@v2

- name: Set up Python 3.
uses: actions/setup-python@v2
with:
python-version: "3.x"

- name: Install Ansible.
run: pip3 install ansible

- name: Install playbooks dependencies
run: ansible-galaxy role install --role-file requirements.yml

- name: Install SSH key.
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: ${{ secrets.KNOWN_HOSTS }}

- name: Run plays.
run: >
ansible-playbook moodle_reset.playbook.yml
--inventory ${{ secrets.ANSIBLE_INVENTORY }},
--user ${{ secrets.ANSIBLE_USER }}
env:
PY_COLORS: "1"
ANSIBLE_FORCE_COLOR: "1"
2 changes: 1 addition & 1 deletion molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Default Scenario
- name: Converge
hosts: all
tasks:
- name: Override default variables with facts
Expand Down
2 changes: 2 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ provisioner:
host_vars:
learningsandboxonline_molecule_${MOLECULE_DISTRO:-ubuntu2204}:
ansible_user: ubuntu
playbooks:
side_effect: side_effect.yml
verifier:
name: ansible
15 changes: 15 additions & 0 deletions molecule/default/side_effect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Side-Effect
hosts: all
tasks:
- name: Override default variables with facts
ansible.builtin.set_fact:
sandbox_domain: learningsandbox.test
sandbox_domain_aliases: []
sandbox_environment: development
sandbox_versions:
- branch: MOODLE_39_STABLE
version: 3.9

- name: Import reset playbook
ansible.builtin.import_playbook: ../../moodle_reset.playbook.yml
34 changes: 34 additions & 0 deletions moodle_reset.playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# This file is part of Learning Sandbox Online.
#
# Learning Sandbox Online is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Learning Sandbox Online is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Learning Sandbox Online. If not, see <https://www.gnu.org/licenses/>.

##
# Playbook for resetting the Moodle instances.
#
# @copyright 2023 Geoffrey Bernardo van Wyk (https://geoffreyvanwyk.dev)
##

- name: Reset Moodle instances
hosts: all

vars_files:
- defaults/main.yml

tasks:
- name: Reset Moodle instances
loop: "{{ sandbox_versions }}"
loop_control:
loop_var: sandbox_item
ansible.builtin.include_tasks: tasks/reset_moodle.yml
72 changes: 72 additions & 0 deletions tasks/install_moodle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,75 @@
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: 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(".", "") }}
95 changes: 95 additions & 0 deletions tasks/reset_moodle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
# This file is part of Learning Sandbox Online.
#
# Learning Sandbox Online is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Learning Sandbox Online is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Learning Sandbox Online. If not, see <https://www.gnu.org/licenses/>.

##
# Tasks for resetting a particular version of Moodle.
#
# @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: 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 }}

- 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: 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: Rename next database to "current" {{ sandbox_item.version }}
become: yes
become_user: postgres
community.postgresql.postgresql_db:
name: moodle-{{ sandbox_item.version | replace(".", "") }}-next
state: rename
target: moodle-{{ sandbox_item.version | replace(".", "") }}

- name: Remove old moodledata {{ sandbox_item.version }}
become: yes
ansible.builtin.file:
path: /var/www/moodledata-old-{{ sandbox_moodle_instance }}
state: absent

- 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: Extract fresh moodledata {{ sandbox_item.version }}
become: yes
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 }}
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 }}
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
Loading