Skip to content

Commit

Permalink
Merge pull request #46 from rhevm-qe-automation/keep_number_of_backups
Browse files Browse the repository at this point in the history
New option: Keep number of backups
  • Loading branch information
wefixit-AT committed Dec 9, 2017
2 parents 63a985b + 1aae322 commit 5acd25a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -24,6 +24,7 @@ Take a look at the example "config_example.cfg"
* Create a snapshot
* Clone the snapshot into a new VM
* Delete the snapshot
* Delete previous backups (if set)
* Export the VM to the NFS share
* Delete the VM

Expand Down
5 changes: 4 additions & 1 deletion backup.py
Expand Up @@ -331,7 +331,10 @@ def main(argv):
VMTools.delete_snapshots(vm, config, vm_from_list)

# Delete old backups
VMTools.delete_old_backups(api, config, vm_from_list)
if (config.get_backup_keep_count()):
VMTools.delete_old_backups(api, config, vm_from_list)
if (config.get_backup_keep_count_by_number()):
VMTools.delete_old_backups_by_number(api, config, vm_from_list)

# Export the VM
try:
Expand Down
13 changes: 11 additions & 2 deletions config.py
Expand Up @@ -23,7 +23,6 @@ class Config(object):
"""
def __init__(self, fd, debug, arguments):
try:

self._cp = config_parser = RawConfigParser(defaults=DEFAULTS)
config_parser.readfp(fd)

Expand All @@ -32,6 +31,7 @@ def __init__(self, fd, debug, arguments):
for key, val in arguments.items():
if val is not None:
config_parser.set(section, key, str(val))

self.__vm_names = json.loads(config_parser.get(section, "vm_names"))
self.__vm_middle = config_parser.get(section, "vm_middle")
self.__vm_suffix = "_"
Expand All @@ -43,7 +43,8 @@ def __init__(self, fd, debug, arguments):
self.__cluster_name = config_parser.get(section, "cluster_name")
self.__export_domain = config_parser.get(section, "export_domain")
self.__timeout = config_parser.getint(section, "timeout")
self.__backup_keep_count = config_parser.getint(section, "backup_keep_count")
self.__backup_keep_count = config_parser.get(section, "backup_keep_count")
self.__backup_keep_count_by_number = config_parser.get(section, "backup_keep_count_by_number")
self.__dry_run = config_parser.getboolean(section, "dry_run")
self.__debug = debug
self.__vm_name_max_length = config_parser.getint(section, "vm_name_max_length")
Expand Down Expand Up @@ -109,9 +110,17 @@ def get_timeout(self):


def get_backup_keep_count(self):
if self.__backup_keep_count:
self.__backup_keep_count = int(self.__backup_keep_count)
return self.__backup_keep_count


def get_backup_keep_count_by_number(self):
if self.__backup_keep_count_by_number:
self.__backup_keep_count_by_number = int(self.__backup_keep_count_by_number)
return self.__backup_keep_count_by_number


def get_dry_run(self):
return self.__dry_run

Expand Down
4 changes: 4 additions & 0 deletions config_example.cfg
Expand Up @@ -32,6 +32,10 @@ cluster_name=local_cluster

# How long backups should be keeped, this is in days
backup_keep_count=3
# How many backups should be kept, this is the number of backups to keep
backup_keep_count_by_number=3
# Notice: While the above 2 params are not mutually exclusive, it is important to note that if you are using both,
# backup_keep_count will get applied first. To disable one or both, keep the value empty

# If set to "True" no creation, deletion and other operations will be executed
dry_run=True
Expand Down
22 changes: 22 additions & 0 deletions vmtools.py
Expand Up @@ -2,9 +2,11 @@
import time
import sys
import datetime
import operator

logger = logging.getLogger()


class VMTools:
"""
Class which holds static methods which are used more than once
Expand Down Expand Up @@ -158,6 +160,26 @@ def delete_old_backups(api, config, vm_name):
logger.debug("Delete old backup (%s) in progress ..." % vm_name_export)
time.sleep(config.get_timeout())

@staticmethod
def delete_old_backups_by_number(api, config, vm_name):
"""
Delete old backups from the export domain by number of requested
:param api: ovirtsdk api
:param config: Configuration
"""
vm_search_regexp = ("%s%s*" % (vm_name, config.get_vm_middle())).encode('ascii', 'ignore')
exported_vms = api.storagedomains.get(config.get_export_domain()).vms.list(name=vm_search_regexp)
exported_vms.sort(key=lambda x: x.get_creation_time())
while len(exported_vms) > config.get_backup_keep_count_by_number():
i = exported_vms.pop(0)
vm_name_export = str(i.get_name())
logger.info("Backup deletion started for backup: %s", vm_name_export)
if not config.get_dry_run():
i.delete()
while api.storagedomains.get(vm_name_export) is not None:
logger.debug("Delete old backup (%s) in progress ..." % vm_name_export)
time.sleep(config.get_timeout())

@staticmethod
def check_free_space(api, config, vm):
"""
Expand Down

0 comments on commit 5acd25a

Please sign in to comment.