diff --git a/playbooks/files/get_backup_info.py b/playbooks/files/get_backup_info.py index a70e39a..85916d6 100755 --- a/playbooks/files/get_backup_info.py +++ b/playbooks/files/get_backup_info.py @@ -8,7 +8,14 @@ if len(sys.argv) > 1: query = sys.argv[1] +else: + query = None +try: + print(json.dumps(conn.block_storage.get_backup(query).to_dict())) +except: for backup in conn.block_storage.backups(): - if backup.name == query or backup.id == query: + if query and backup.name == query: print(json.dumps(backup.to_dict())) - exit(0) + elif not query: + print(json.dumps(backup.to_dict())) +exit(0) diff --git a/playbooks/files/restore_backup.py b/playbooks/files/restore_backup.py index f08f7fe..90224fb 100755 --- a/playbooks/files/restore_backup.py +++ b/playbooks/files/restore_backup.py @@ -9,11 +9,18 @@ conn = openstack.connect() #Until ES150 is completed this query can be done only in old way #backups = list(conn.block_storage.backups(name=sys.argv[1])) -backups = list(conn.block_storage.backups()) -backup = [detail for detail in backups if detail.name == sys.argv[1]] -backup = next(iter(backup),None) -backup_id = backup.id -volume_id = backup.volume_id + +try: + backup = conn.block_storage.get_backup(sys.argv[1]) + backup_id = backup.id + volume_id = backup.volume_id + +except: + backups = list(conn.block_storage.backups()) + backup = [detail for detail in backups if detail.name == sys.argv[1]] + backup = next(iter(backup),None) + backup_id = backup.id + volume_id = backup.volume_id backup = conn.block_storage.restore_backup(backup_id, volume_id, name=sys.argv[2]) conn.block_storage.wait_for_status(backup,status='available', wait=600) diff --git a/playbooks/scenario3b_backup_az.yaml b/playbooks/scenario3b_backup_az.yaml new file mode 100644 index 0000000..206915a --- /dev/null +++ b/playbooks/scenario3b_backup_az.yaml @@ -0,0 +1,94 @@ +--- + +- name: Scenario 3 - Backup per AZ + hosts: localhost + collections: + - opentelekomcloud.cloud + vars: + prefix: scenario3b- + tasks: + - set_fact: + prefix: "{{ (prefix + ( lookup('env', 'TASK_EXECUTOR_JOB_ID') | default(99999999 | random | to_uuid | hash('md5'), true) ) ) }}" + + - set_fact: + volume_suffix: "{{ (prefix + '-test_volume_apimon') }}" + snapshot_suffix: "{{ (prefix + '-test_snapshot_apimon') }}" + backup_suffix: "{{ (prefix + '-test_backup_apimon') }}" + + - name: Rescue block + block: + - name: Get network facts + os_networks_facts: + name: admin_external_net + + - set_fact: + network_azs: "{{ openstack_networks[0]['availability_zones'] }}" + + - name: "Create Volume in {{item}}" + os_volume: + availability_zone: "{{ item }}" + size: 10 + display_name: "{{ (item + '.' + volume_suffix) }}" + loop: "{{ network_azs }}" + + - name: "Create Snapshot in {{item}}" + os_volume_snapshot: + display_name: "{{ (item + '.' + snapshot_suffix) }}" + volume: "{{ (item + '.' + volume_suffix) }}" + loop: "{{ network_azs }}" + + - name: "Create Backup in {{item}}" + include_role: + name: backup_create_restore_delete + vars: + volume_name: "{{ (item + '.' + volume_suffix) }}" + new_volume_name: "{{ (volume_name + 'new') }}" + snapshot_name: "{{ (item + '.' + snapshot_suffix) }}" + backup_name: "{{ (item + '.' + backup_suffix) }}" + availability_zone: "{{ item }}" + loop: "{{ network_azs }}" + + rescue: + - name: "Get info about Volume in {{item}}" + script: "get_volume_info.py {{ (item + '.' + volume_suffix) }}" + register: volume + tags: 'service=block_storage' + loop: "{{ network_azs }}" + + - name: "Print Volume info in {{item}}" + debug: + var: "{{ ('volume_info_' + item + '.stdout') }}" + loop: "{{ network_azs }}" + + always: + # If we failed - cleanup what we can + - name: "Delete original Snapshot in {{item}}" + vars: + volume_name: "{{ (item + '.' + volume_suffix) }}" + snapshot_name: "{{ (item + '.' + snapshot_suffix) }}" + os_volume_snapshot: + state: absent + display_name: "{{ snapshot_name }}" + volume: "{{volume_name}}" + ignore_errors: True + loop: "{{ network_azs }}" + + - name: "Delete new Snapshot in {{item}}" + vars: + volume_name: "{{ (item + '.' + volume_suffix) }}" + snapshot_name: "{{ (item + '.' + snapshot_suffix) }}" + os_volume_snapshot: + state: absent + display_name: "{{ (snapshot_name + '_new') }}" + volume: "{{volume_name}}" + ignore_errors: True + loop: "{{ network_azs }}" + + - name: "Delete Volume in {{item}}" + vars: + volume_name: "{{ (item + '.' + volume_suffix) }}" + os_volume: + state: absent + display_name: "{{volume_name}}" + ignore_errors: True + loop: "{{ network_azs }}" diff --git a/roles/backup_create_restore_delete/tasks/main.yml b/roles/backup_create_restore_delete/tasks/main.yml new file mode 100644 index 0000000..115a3e1 --- /dev/null +++ b/roles/backup_create_restore_delete/tasks/main.yml @@ -0,0 +1,37 @@ +--- +# tasks file for roles/backup_create_restore_delete + +- block: + - name: Create Backup in {{ availability_zone }} + opentelekomcloud.cloud.volume_backup: + volume: "{{ volume_name }}" + snapshot: "{{ snapshot_name }}" + display_name: "{{ backup_name }}" + tags: 'service=block_storage' + register: backup + + - name: get backup id + set_fact: + backup_id: "{{ backup.id }}" + + - name: Restore Backup in {{ availability_zone }} + script: "restore_backup.py {{backup_id}} {{volume_name}} {{ new_volume_name }}" + tags: 'service=block_storage' + + - name: Get info about Backup in {{ availability_zone }} + script: "get_backup_info.py {{ backup_id }}" + register: backup + tags: 'service=block_storage' + + always: + - name: Debug backup info + debug: + var: backup + + - name: Delete Backup in {{ availability_zone}} + opentelekomcloud.cloud.volume_backup: + display_name: "{{ backup_name }}" + state: absent + tags: 'service=block_storage' + ignore_errors: True +