Skip to content

Commit

Permalink
Add option to set persist_memorystate for snapshot creation
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
lukas-bednar committed Mar 24, 2017
1 parent 8445f2b commit 1e3b29e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
15 changes: 14 additions & 1 deletion backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ def create_argparser():
type=float,
default=None,
)
mscg.add_argument(
"--persist-memorystate",
help="If set, the VM is being paused during snapshot creation.",
dest="persist_memorystate",
action="store_true",
default=None,
)

lg = p.add_argument_group("Logging related options")
lg.add_argument(
Expand Down Expand Up @@ -268,7 +275,13 @@ def main(argv):
try:
logger.info("Snapshot creation started ...")
if not config.get_dry_run():
vm.snapshots.add(params.Snapshot(description=config.get_snapshot_description(), vm=vm))
vm.snapshots.add(
params.Snapshot(
description=config.get_snapshot_description(),
vm=vm,
persist_memorystate=config.get_persist_memorystate(),
)
)
VMTools.wait_for_snapshot_operation(vm, config, "creation")
logger.info("Snapshot created")
except Exception as e:
Expand Down
12 changes: 7 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DEFAULTS = {
"logger_fmt": "%(asctime)s: %(message)s",
"logger_file_path": None,
"persist_memorystate": "false",
}


Expand All @@ -30,7 +31,7 @@ def __init__(self, fd, debug, arguments):
# Update with options passed from CLI interface
for key, val in arguments.items():
if val is not None:
config_parser.set(section, key, val)
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 @@ -51,10 +52,8 @@ def __init__(self, fd, debug, arguments):
self.__storage_space_threshold = config_parser.getfloat(section, "storage_space_threshold")
self.__logger_fmt = config_parser.get(section, "logger_fmt")
self.__logger_file_path = config_parser.get(section, "logger_file_path")
except NoSectionError as e:
print str(e)
sys.exit(1)
except NoOptionError as e:
self.__persist_memorystate = config_parser.getboolean(section, "persist_memorystate")
except (NoSectionError, NoOptionError) as e:
print str(e)
sys.exit(1)

Expand Down Expand Up @@ -143,6 +142,9 @@ def get_logger_fmt(self):
def get_logger_file_path(self):
return self.__logger_file_path

def get_persist_memorystate(self):
return self.__persist_memorystate

def write_update(self, filename):
"""
This method takes name of config file and update it according
Expand Down
3 changes: 3 additions & 0 deletions config_example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ logger_fmt=%(asctime)s: %(message)s
# This value is path to log file, where you want to store log from this script,
# By default it writes to stdout.
logger_file_path=

# If this value is True, the VM is being paused during snapshot creation.
persist_memorystate=False
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
argparse; python_version < '2.7'
# ovirt-engine-sdk >= 3.6 # It is not available in pip repo
ovirt-engine-sdk-python == 3.6.9.2
29 changes: 26 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,36 @@ def test_config_overwitten_from_cli():


def test_config_rewrite_vm_names(tmpdir):
new_vms = ["new_vm1", "new_vm2"]
p = tmpdir.join("config.cfg")
p.write("\n".join(default_config_data))
with p.open() as fh:
c = Config(fh, False, dict())
assert p.check()
c.set_vm_names(["new_vm1", "new_vm2"])
assert c.get_vm_names() == ["new_vm1", "new_vm2"]
c.set_vm_names(new_vms)
assert c.get_vm_names() == new_vms
c.write_update(str(p))
data = p.read()
assert '["new_vm1", "new_vm2"]' in data
assert 'vm_names = ["new_vm1", "new_vm2"]' in data
with p.open() as fh:
c = Config(fh, False, dict())
assert c.get_vm_names() == new_vms


def test_config_persist_memory():
# Test default
data_stream = StringIO("\n".join(default_config_data))
c = Config(data_stream, False, dict())
assert c.get_persist_memorystate() is False
# Test setting from config file
data_stream = StringIO(
"\n".join(
default_config_data + ['persist_memorystate=True']
)
)
c = Config(data_stream, False, dict())
assert c.get_persist_memorystate() is True
# Test setting from CLI
data_stream = StringIO("\n".join(default_config_data))
c = Config(data_stream, False, {"persist_memorystate": True})
assert c.get_persist_memorystate() is True

0 comments on commit 1e3b29e

Please sign in to comment.