Skip to content

Commit

Permalink
Merge pull request #19 from GoogleCloudPlatform/tomerlf-async-backup
Browse files Browse the repository at this point in the history
added async backup with create snapshot task
  • Loading branch information
runxinw authored Jul 13, 2023
2 parents a08b87f + 293f165 commit 0d03805
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
22 changes: 12 additions & 10 deletions gce_rescue/tasks/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ def _list_tasks(vm: Instance, action: str) -> List:
'vm': vm
}]
},
{
'name': take_snapshot,
'args': [{
'vm': vm
}]
},
{
'name': create_rescue_disk,
'args': [{
Expand Down Expand Up @@ -127,9 +121,13 @@ def _list_tasks(vm: Instance, action: str) -> List:
def call_tasks(vm: Instance, action: str) -> None:
""" Loop tasks dict and execute """
tasks = _list_tasks(vm = vm, action = action)
if get_config('skip-snapshot'):
_logger.info(f'Skipping snapshot backup.')
tasks = [task for task in tasks if task['name'].__name__ != 'take_snapshot']
async_backup_thread = None
if action == 'set_rescue_mode':
if get_config('skip-snapshot'):
_logger.info(f'Skipping snapshot backup.')
else:
take_snapshot(vm)
async_backup_thread = True
total_tasks = len(tasks)

tracker = Tracker(total_tasks)
Expand All @@ -142,4 +140,8 @@ def call_tasks(vm: Instance, action: str) -> None:
execute(**args)
tracker.advance(step = 1)

tracker.finish()
if async_backup_thread:
_logger.info(f'Waiting for async backup to finish')
take_snapshot(vm, join_snapshot=True)
_logger.info('done.')
tracker.finish()
11 changes: 9 additions & 2 deletions gce_rescue/tasks/disks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from typing import Dict
import logging
from threading import Thread

import googleapiclient.errors

Expand All @@ -25,6 +26,7 @@
from googleapiclient.errors import HttpError

_logger = logging.getLogger(__name__)
snapshot_thread = None

def _create_rescue_disk(vm, source_disk: str) -> Dict:
""" Create new temporary rescue disk based on source_disk.
Expand Down Expand Up @@ -178,8 +180,13 @@ def _detach_disk(vm, disk: str) -> Dict:
return result


def take_snapshot(vm) -> None:
create_snapshot(vm)
def take_snapshot(vm, join_snapshot=None) -> None:
global snapshot_thread
if not join_snapshot:
snapshot_thread = Thread(target=create_snapshot, args=(vm,), daemon=True)
snapshot_thread.start()
else:
snapshot_thread.join()


def create_rescue_disk(vm) -> None:
Expand Down

0 comments on commit 0d03805

Please sign in to comment.