diff --git a/.gitignore b/.gitignore index dca2ce003..892535d7f 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ pytestdebug.log # doc doc/build/* + +# JetBrain +.idea/ diff --git a/tripleo_ansible/playbooks/cli-overcloud-backup.yaml b/tripleo_ansible/playbooks/cli-overcloud-backup.yaml new file mode 100644 index 000000000..2ad4d4ab9 --- /dev/null +++ b/tripleo_ansible/playbooks/cli-overcloud-backup.yaml @@ -0,0 +1,24 @@ +--- +# Copyright 2020 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +- name: TripleO Controller backup. + connection: "{{ (tripleo_target_host is defined) | ternary('ssh', 'local') }}" + hosts: Controller + remote_user: "{{ tripleo_target_user | default(lookup('env', 'USER')) }}" + become: true + roles: + - role: backup_and_restore + tags: + - bar_create_recover_image diff --git a/tripleo_ansible/playbooks/cli-undercloud-backup-legacy.yaml b/tripleo_ansible/playbooks/cli-undercloud-backup-legacy.yaml new file mode 100644 index 000000000..7130be226 --- /dev/null +++ b/tripleo_ansible/playbooks/cli-undercloud-backup-legacy.yaml @@ -0,0 +1,162 @@ +--- +# Copyright 2019 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +- name: TripleO Undercloud backup workflows + connection: "{{ (tripleo_target_host is defined) | ternary('ssh', 'local') }}" + hosts: "{{ tripleo_target_host | default('localhost') }}" + remote_user: "{{ tripleo_target_user | default(lookup('env', 'USER')) }}" + gather_facts: "{{ (tripleo_target_host is defined) | ternary(true, false) }}" + any_errors_fatal: true + vars: + sources_path: "{{ lookup('env', 'HOME') }}" + handlers: + # Perform some cleanup + - name: cleanup the backup + become: true + file: + path: "{{ item }}" + state: absent + with_items: + - "{{ bkp_path }}" + - "{{ tmpdir.path }}/" + + pre_tasks: + - name: Set local connection user facts + set_fact: + ansible_home: "{{ lookup('env', 'HOME') }}" + ansible_user: "{{ lookup('env', 'USER') }}" + run_once: true + when: + - (tripleo_target_host is defined) | ternary('ssh', 'local') == 'local' + + # Action to know if there is enough available space + # to run the Undercloud backup + - name: Get free space + shell: | + set -o pipefail + if [[ $(df -T /var/tmp | tail -n 1 | awk '{print $2}') == "btrfs" ]]; then + btrfs fi usage --gbytes / | awk '/^.*Free / {print $3}'| sed 's/\..*//' + else + df -BG /var/tmp | awk '!/^Filesystem/ {print $4}' | sed 's/G//' + fi + register: var_space_available + + - name: Fail if any of the volumes are too small + fail: + msg: > + Minimum free space required for /var/tmp: 10G + - current free space: {{ var_space_available.stdout|int |round(1) }}G + when: + - (var_space_available.stdout | int) < 10 + tasks: + - name: Create a timestamp variable + set_fact: + timestamp: "{{ lookup('pipe','date +%Y%m%d%H%M%S') }}" + + - name: Create staging directory + tempfile: + state: directory + prefix: "undercloud-backup-" + path: "/var/tmp" + register: tmpdir + notify: + - cleanup the backup + + - name: Mysql root password block + block: + - name: Read tripleo password file + slurp: + src: "{{ ansible_home }}/tripleo-undercloud-passwords.yaml" + register: tripleo_undercloud_passwords + no_log: true + + - name: Set mysql root password + set_fact: + MysqlRootPassword: "{{ (tripleo_undercloud_passwords['content'] | b64decode | from_yaml)['parameter_defaults']['MysqlRootPassword'] }}" + no_log: "{{ not ((ansible_verbosity | int) >= 2) | bool }}" + rescue: + - name: Set mysql root password (fallback) + set_fact: + MysqlRootPassword: "{{ lookup('ini', 'undercloud_mysql_root_password section=auth file=' ~ ansible_home ~ '/undercloud-passwords.conf') }}" + no_log: "{{ not ((ansible_verbosity | int) >= 2) | bool }}" + + - name: Create the names for the temporary backup files + set_fact: + db_path: "{{ tmpdir.path }}/all-databases-{{ timestamp }}.sql.gz" + fs_path: "{{ tmpdir.path }}/filesystem-{{ timestamp }}.sql.gz" + bkp_path: "/tmp/UC-backup-{{ timestamp }}.tar" + + # We use the undercloud database password for the root + # to backup the databases + - name: Backup the databases + become: true + shell: |- + set -o pipefail + podman exec -i mysql mysqldump \ + -u root \ + -p{{ MysqlRootPassword }} \ + --opt \ + --all-databases | gzip > {{ db_path }} + no_log: true + + - name: Backup the filesystem + become: true + shell: | + tar --xattrs --ignore-failed-read -C / -cf {{ fs_path }} {{ sources_path }} + chown {{ lookup('env', 'USER') }} {{ fs_path }} + + - name: compress all the files in tar.gz + archive: + path: "{{ tmpdir.path }}" + dest: "{{ bkp_path }}" + format: tar + notify: + - cleanup the backup + + - name: Swift save block + when: + - save_swift is defined and save_swift + block: + - name: Save the backup to swift + shell: |- + swift upload --header "X-Delete-After: 86400" undercloud-backups {{ bkp_path }} + + - name: Backup saved + debug: + msg: "The undercloud backup was saved to swift" + + - name: Local save block + when: + - save_swift is not defined or not save_swift + block: + - name: Create the backup directory + become: true + file: + path: /var/lib/tripleo/backups + state: directory + mode: '0755' + + - name: Copy backup file + become: true + copy: + src: "{{ bkp_path }}" + dest: "/var/lib/tripleo/backups/{{ bkp_path | basename }}" + remote_src: true + + - name: Print out the backup location + debug: + msg: >- + The undercloud backup was saved in + "/var/lib/tripleo/backups/{{ bkp_path | basename }}" diff --git a/tripleo_ansible/playbooks/cli-undercloud-backup.yaml b/tripleo_ansible/playbooks/cli-undercloud-backup.yaml index 56f289125..406ca722a 100644 --- a/tripleo_ansible/playbooks/cli-undercloud-backup.yaml +++ b/tripleo_ansible/playbooks/cli-undercloud-backup.yaml @@ -1,5 +1,5 @@ --- -# Copyright 2019 Red Hat, Inc. +# Copyright 2020 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -13,151 +13,12 @@ # License for the specific language governing permissions and limitations # under the License. -- name: TripleO Undercloud backup workflows +- name: TripleO Undercloud backup. connection: "{{ (tripleo_target_host is defined) | ternary('ssh', 'local') }}" - hosts: "{{ tripleo_target_host | default('localhost') }}" + hosts: Undercloud remote_user: "{{ tripleo_target_user | default(lookup('env', 'USER')) }}" - gather_facts: "{{ (tripleo_target_host is defined) | ternary(true, false) }}" - any_errors_fatal: true - vars: - sources_path: "{{ lookup('env', 'HOME') }}" - hide_sensitive_logs: true - handlers: - # Perform some cleanup - - name: cleanup the backup - become: true - file: - path: "{{ item }}" - state: absent - with_items: - - "{{ bkp_path }}" - - "{{ tmpdir.path }}/" - - pre_tasks: - - name: Set local connection user facts - set_fact: - ansible_home: "{{ lookup('env', 'HOME') }}" - ansible_user: "{{ lookup('env', 'USER') }}" - run_once: true - when: - - (tripleo_target_host is defined) | ternary('ssh', 'local') == 'local' - - # Action to know if there is enough available space - # to run the Undercloud backup - - name: Get free space - shell: | - set -o pipefail - if [[ $(df -T /var/tmp | tail -n 1 | awk '{print $2}') == "btrfs" ]]; then - btrfs fi usage --gbytes / | awk '/^.*Free / {print $3}'| sed 's/\..*//' - else - df -BG /var/tmp | awk '!/^Filesystem/ {print $4}' | sed 's/G//' - fi - register: var_space_available - - - name: Fail if any of the volumes are too small - fail: - msg: > - Minimum free space required for /var/tmp: 10G - - current free space: {{ var_space_available.stdout|int |round(1) }}G - when: - - (var_space_available.stdout | int) < 10 - tasks: - - name: Create a timestamp variable - set_fact: - timestamp: "{{ lookup('pipe','date +%Y%m%d%H%M%S') }}" - - - name: Create staging directory - tempfile: - state: directory - prefix: "undercloud-backup-" - path: "/var/tmp" - register: tmpdir - notify: - - cleanup the backup - - - name: Mysql root password block - block: - - name: Read tripleo password file - slurp: - src: "{{ ansible_home }}/tripleo-undercloud-passwords.yaml" - register: tripleo_undercloud_passwords - no_log: "{{ hide_sensitive_logs | bool }}" - - - name: Set mysql root password - set_fact: - MysqlRootPassword: "{{ (tripleo_undercloud_passwords['content'] | b64decode | from_yaml)['parameter_defaults']['MysqlRootPassword'] }}" - no_log: "{{ hide_sensitive_logs | bool }}" - rescue: - - name: Set mysql root password (fallback) - set_fact: - MysqlRootPassword: "{{ lookup('ini', 'undercloud_mysql_root_password section=auth file=' ~ ansible_home ~ '/undercloud-passwords.conf') }}" - no_log: "{{ hide_sensitive_logs | bool }}" - - - name: Create the names for the temporary backup files - set_fact: - db_path: "{{ tmpdir.path }}/all-databases-{{ timestamp }}.sql.gz" - fs_path: "{{ tmpdir.path }}/filesystem-{{ timestamp }}.sql.gz" - bkp_path: "/tmp/UC-backup-{{ timestamp }}.tar" - - # We use the undercloud database password for the root - # to backup the databases - - name: Backup the databases - become: true - shell: |- - set -o pipefail - podman exec -i mysql mysqldump \ - -u root \ - -p{{ MysqlRootPassword }} \ - --opt \ - --all-databases | gzip > {{ db_path }} - no_log: "{{ hide_sensitive_logs | bool }}" - - - name: Backup the filesystem - become: true - shell: | - tar --xattrs --ignore-failed-read -C / -cf {{ fs_path }} {{ sources_path }} - chown {{ lookup('env', 'USER') }} {{ fs_path }} - - - name: compress all the files in tar.gz - archive: - path: "{{ tmpdir.path }}" - dest: "{{ bkp_path }}" - format: tar - notify: - - cleanup the backup - - - name: Swift save block - when: - - save_swift is defined and save_swift - block: - - name: Save the backup to swift - shell: |- - swift upload --header "X-Delete-After: 86400" undercloud-backups {{ bkp_path }} - - - name: Backup saved - debug: - msg: "The undercloud backup was saved to swift" - - - name: Local save block - when: - - save_swift is not defined or not save_swift - block: - - name: Create the backup directory - become: true - file: - path: /var/lib/tripleo/backups - state: directory - mode: '0755' - - - name: Copy backup file - become: true - copy: - src: "{{ bkp_path }}" - dest: "/var/lib/tripleo/backups/{{ bkp_path | basename }}" - remote_src: true - - - name: Print out the backup location - debug: - msg: >- - The undercloud backup was saved in - "/var/lib/tripleo/backups/{{ bkp_path | basename }}" + become: true + roles: + - role: backup_and_restore + tags: + - bar_create_recover_image diff --git a/tripleo_ansible/playbooks/prepare-overcloud-backup.yaml b/tripleo_ansible/playbooks/prepare-overcloud-backup.yaml new file mode 100644 index 000000000..1434827c1 --- /dev/null +++ b/tripleo_ansible/playbooks/prepare-overcloud-backup.yaml @@ -0,0 +1,24 @@ +--- +# Copyright 2020 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +- name: TripleO Controller backup system check. + connection: "{{ (tripleo_target_host is defined) | ternary('ssh', 'local') }}" + hosts: Controller + remote_user: "{{ tripleo_target_user | default(lookup('env', 'USER')) }}" + become: true + roles: + - role: backup_and_restore + tags: + - bar_setup_rear diff --git a/tripleo_ansible/playbooks/prepare-undercloud-backup.yaml b/tripleo_ansible/playbooks/prepare-undercloud-backup.yaml new file mode 100644 index 000000000..517ff789e --- /dev/null +++ b/tripleo_ansible/playbooks/prepare-undercloud-backup.yaml @@ -0,0 +1,25 @@ +--- +# Copyright 2020 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +- name: TripleO Undercloud backup system check. + connection: "{{ (tripleo_target_host is defined) | ternary('ssh', 'local') }}" + hosts: Undercloud + remote_user: "{{ tripleo_target_user | default(lookup('env', 'USER')) }}" + become: true + roles: + - role: backup_and_restore + tags: + - bar_setup_rear + - bar_setup_nfs_server