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

adding new scenarion for backup across multiple AZs #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions playbooks/files/get_backup_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
17 changes: 12 additions & 5 deletions playbooks/files/restore_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
94 changes: 94 additions & 0 deletions playbooks/scenario3b_backup_az.yaml
Original file line number Diff line number Diff line change
@@ -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 }}"
37 changes: 37 additions & 0 deletions roles/backup_create_restore_delete/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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