Skip to content

Commit 33e422a

Browse files
committed
unified snapshot logic
1 parent 9dcdb1a commit 33e422a

5 files changed

Lines changed: 106 additions & 83 deletions

File tree

cloudify_libvirt/common.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,71 @@ def delete_binary_place(backup_dir, object_name):
128128
full_path = get_binary_place(backup_dir, object_name)
129129
if os.path.isfile(full_path):
130130
os.remove(full_path)
131+
132+
133+
def xml_snapshot_create(kwargs, resource_id, current_xmldump):
134+
snapshot_name = get_backupname(kwargs)
135+
if kwargs.get("snapshot_incremental"):
136+
backups = ctx.instance.runtime_properties.get("backups", {})
137+
if snapshot_name in backups:
138+
raise cfy_exc.NonRecoverableError(
139+
"Snapshot {snapshot_name} already exists."
140+
.format(snapshot_name=snapshot_name,))
141+
backups[snapshot_name] = current_xmldump
142+
ctx.instance.runtime_properties["backups"] = backups
143+
ctx.logger.info("Snapshot {snapshot_name} is created."
144+
.format(snapshot_name=snapshot_name,))
145+
else:
146+
if read_node_state(get_backupdir(kwargs), resource_id):
147+
raise cfy_exc.NonRecoverableError(
148+
"Backup {snapshot_name} already exists."
149+
.format(snapshot_name=snapshot_name,))
150+
save_node_state(get_backupdir(kwargs), resource_id, current_xmldump)
151+
ctx.logger.info("Backup {snapshot_name} is created."
152+
.format(snapshot_name=snapshot_name,))
153+
ctx.logger.debug("Current config {}".format(repr(current_xmldump)))
154+
155+
156+
def xml_snapshot_apply(kwargs, resource_id, current_xmldump):
157+
snapshot_name = get_backupname(kwargs)
158+
if kwargs.get("snapshot_incremental"):
159+
backups = ctx.instance.runtime_properties.get("backups", {})
160+
if snapshot_name not in backups:
161+
raise cfy_exc.NonRecoverableError(
162+
"No snapshots found with name: {snapshot_name}."
163+
.format(snapshot_name=snapshot_name,))
164+
xml_backup = backups[snapshot_name]
165+
else:
166+
xml_backup = read_node_state(get_backupdir(kwargs), resource_id)
167+
if not xml_backup:
168+
raise cfy_exc.NonRecoverableError(
169+
"No backups found with name: {snapshot_name}."
170+
.format(snapshot_name=snapshot_name,))
171+
172+
if xml_backup.strip() != current_xmldump.strip():
173+
ctx.logger.info("We have different configs,\n{}\nvs\n{}\n"
174+
.format(
175+
repr(xml_backup.strip()),
176+
repr(current_xmldump.strip())))
177+
else:
178+
ctx.logger.info("Already used such configuration: {}"
179+
.format(snapshot_name))
180+
181+
182+
def xml_snapshot_delete(kwargs, resource_id):
183+
snapshot_name = get_backupname(kwargs)
184+
if kwargs.get("snapshot_incremental"):
185+
backups = ctx.instance.runtime_properties.get("backups", {})
186+
if snapshot_name not in backups:
187+
raise cfy_exc.NonRecoverableError(
188+
"No snapshots found with name: {snapshot_name}."
189+
.format(snapshot_name=snapshot_name,))
190+
del backups[snapshot_name]
191+
ctx.instance.runtime_properties["backups"] = backups
192+
else:
193+
if not read_node_state(get_backupdir(kwargs), resource_id):
194+
raise cfy_exc.NonRecoverableError(
195+
"No backups found with name: {snapshot_name}."
196+
.format(snapshot_name=snapshot_name,))
197+
delete_node_state(get_backupdir(kwargs), resource_id)
198+
ctx.logger.info("Backup deleted: {}".format(snapshot_name))

cloudify_libvirt/network_tasks.py

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ def snapshot_create(**kwargs):
141141
# not uninstall workflow, raise exception
142142
raise cfy_exc.NonRecoverableError("No network for backup")
143143

144-
snapshot_name = common.get_backupname(kwargs)
145144
libvirt_auth, _ = common.get_libvirt_params(**kwargs)
146145
conn = libvirt.open(libvirt_auth)
147146
if conn is None:
@@ -158,28 +157,7 @@ def snapshot_create(**kwargs):
158157
'Failed to find the network: {}'.format(repr(e))
159158
)
160159

161-
net_backup = network.XMLDesc()
162-
if kwargs.get("snapshot_incremental"):
163-
backups = ctx.instance.runtime_properties.get("backups", {})
164-
if snapshot_name in backups:
165-
raise cfy_exc.NonRecoverableError(
166-
"Snapshot {snapshot_name} already exists."
167-
.format(snapshot_name=snapshot_name,))
168-
backups[snapshot_name] = net_backup
169-
ctx.instance.runtime_properties["backups"] = backups
170-
ctx.logger.info("Snapshot {snapshot_name} is created."
171-
.format(snapshot_name=snapshot_name,))
172-
else:
173-
if common.read_node_state(common.get_backupdir(kwargs),
174-
resource_id):
175-
raise cfy_exc.NonRecoverableError(
176-
"Backup {snapshot_name} already exists."
177-
.format(snapshot_name=snapshot_name,))
178-
common.save_node_state(common.get_backupdir(kwargs), resource_id,
179-
net_backup)
180-
ctx.logger.info("Backup {snapshot_name} is created."
181-
.format(snapshot_name=snapshot_name,))
182-
ctx.logger.debug("Current config {}".format(repr(net_backup)))
160+
common.xml_snapshot_create(kwargs, resource_id, network.XMLDesc())
183161
finally:
184162
conn.close()
185163

@@ -193,8 +171,6 @@ def snapshot_apply(**kwargs):
193171
# not uninstall workflow, raise exception
194172
raise cfy_exc.NonRecoverableError("No network for restore")
195173

196-
snapshot_name = common.get_backupname(kwargs)
197-
198174
libvirt_auth, _ = common.get_libvirt_params(**kwargs)
199175
conn = libvirt.open(libvirt_auth)
200176
if conn is None:
@@ -211,29 +187,7 @@ def snapshot_apply(**kwargs):
211187
'Failed to find the network: {}'.format(repr(e))
212188
)
213189

214-
if kwargs.get("snapshot_incremental"):
215-
backups = ctx.instance.runtime_properties.get("backups", {})
216-
if snapshot_name not in backups:
217-
raise cfy_exc.NonRecoverableError(
218-
"No snapshots found with name: {snapshot_name}."
219-
.format(snapshot_name=snapshot_name,))
220-
net_backup = backups[snapshot_name]
221-
else:
222-
net_backup = common.read_node_state(common.get_backupdir(kwargs),
223-
resource_id)
224-
if not net_backup:
225-
raise cfy_exc.NonRecoverableError(
226-
"No backups found with name: {snapshot_name}."
227-
.format(snapshot_name=snapshot_name,))
228-
229-
if net_backup.strip() != network.XMLDesc().strip():
230-
ctx.logger.info("We have different configs,\n{}\nvs\n{}\n"
231-
.format(
232-
repr(net_backup.strip()),
233-
repr(network.XMLDesc().strip())))
234-
else:
235-
ctx.logger.info("Already used such configuration: {}"
236-
.format(snapshot_name))
190+
common.xml_snapshot_apply(kwargs, resource_id, network.XMLDesc())
237191
finally:
238192
conn.close()
239193

@@ -247,23 +201,7 @@ def snapshot_delete(**kwargs):
247201
# not uninstall workflow, raise exception
248202
raise cfy_exc.NonRecoverableError("No network for backup delete")
249203

250-
snapshot_name = common.get_backupname(kwargs)
251-
if kwargs.get("snapshot_incremental"):
252-
backups = ctx.instance.runtime_properties.get("backups", {})
253-
if snapshot_name not in backups:
254-
raise cfy_exc.NonRecoverableError(
255-
"No snapshots found with name: {snapshot_name}."
256-
.format(snapshot_name=snapshot_name,))
257-
del backups[snapshot_name]
258-
ctx.instance.runtime_properties["backups"] = backups
259-
else:
260-
if not common.read_node_state(common.get_backupdir(kwargs),
261-
resource_id):
262-
raise cfy_exc.NonRecoverableError(
263-
"No backups found with name: {snapshot_name}."
264-
.format(snapshot_name=snapshot_name,))
265-
common.delete_node_state(common.get_backupdir(kwargs), resource_id)
266-
ctx.logger.info("Backup deleted: {}".format(snapshot_name))
204+
common.xml_snapshot_delete(kwargs, resource_id)
267205

268206

269207
@operation

cloudify_libvirt/tests/test_common_base.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,25 @@ def _test_no_resource_id(self, func, exception_text=None):
147147
):
148148
func(ctx=_ctx)
149149

150-
def _test_reused_object(self, func, use_existed=True):
150+
def _test_reused_object(self, libvirt_open, func, use_existed=True):
151151
# check use prexisted object
152152
_ctx = self._create_ctx()
153153
_ctx.instance.runtime_properties['resource_id'] = 'resource'
154154
_ctx.instance.runtime_properties['use_external_resource'] = use_existed
155155
connect = self._create_fake_connection()
156-
with mock.patch(
157-
"cloudify_libvirt.network_tasks.libvirt.open",
158-
mock.Mock(return_value=connect)
159-
):
156+
with mock.patch(libvirt_open, mock.Mock(return_value=connect)):
160157
func(ctx=_ctx)
161158

162-
def _test_no_snapshot_name(self, _ctx, func):
159+
def _test_no_snapshot_name(self, _ctx, libvirt_open, func):
163160
_ctx.instance.runtime_properties['resource_id'] = 'resource'
164161

165162
with self.assertRaisesRegexp(
166163
NonRecoverableError,
167164
"Backup name must be provided."
168165
):
169-
func(ctx=_ctx)
166+
connect = self._create_fake_connection()
167+
connect.networkLookupByName = mock.Mock()
168+
connect.lookupByName = mock.Mock()
169+
connect.storagePoolLookupByName = mock.Mock()
170+
with mock.patch(libvirt_open, mock.Mock(return_value=connect)):
171+
func(ctx=_ctx)

cloudify_libvirt/tests/test_domain.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ def test_start(self):
192192

193193
def test_stop(self):
194194
self._test_no_resource_id(domain_tasks.stop)
195-
self._test_reused_object(domain_tasks.stop)
195+
self._test_reused_object(
196+
"cloudify_libvirt.domain_tasks.libvirt.open",
197+
domain_tasks.stop)
196198
self._test_check_correct_connect_action(domain_tasks.stop)
197199
self._test_check_correct_connect_no_object(domain_tasks.stop)
198200
self._test_action_states(
@@ -222,7 +224,9 @@ def test_suspend(self):
222224

223225
def test_delete(self):
224226
self._test_no_resource_id(domain_tasks.delete)
225-
self._test_reused_object(domain_tasks.delete)
227+
self._test_reused_object(
228+
"cloudify_libvirt.domain_tasks.libvirt.open",
229+
domain_tasks.delete)
226230
self._test_check_correct_connect_action(domain_tasks.delete)
227231
self._test_check_correct_connect_no_object(domain_tasks.delete)
228232

@@ -890,7 +894,10 @@ def _test_check_correct_connect_backup_no_object(self, func):
890894
def _test_common_backups(self, func, noresource_text):
891895
# common funcs for backups
892896
self._test_no_resource_id(func, noresource_text)
893-
self._test_no_snapshot_name(self._create_ctx(), func)
897+
self._test_no_snapshot_name(
898+
self._create_ctx(),
899+
"cloudify_libvirt.domain_tasks.libvirt.open",
900+
func)
894901
self._test_snapshot_name_backup(func)
895902
self._test_check_correct_connect_backup(func)
896903
self._test_check_correct_connect_backup_no_object(func)

cloudify_libvirt/tests/test_network.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,10 @@ def _create_fake_network_backup(self):
227227
def test_snapshot_apply(self):
228228
self._test_no_resource_id(network_tasks.snapshot_apply,
229229
"No network for restore")
230-
self._test_no_snapshot_name(self._create_ctx(),
231-
network_tasks.snapshot_apply)
230+
self._test_no_snapshot_name(
231+
self._create_ctx(),
232+
"cloudify_libvirt.network_tasks.libvirt.open",
233+
network_tasks.snapshot_apply)
232234
self._test_empty_connection_backup(network_tasks.snapshot_apply)
233235
self._test_empty_network_backup(network_tasks.snapshot_apply)
234236

@@ -297,8 +299,10 @@ def test_snapshot_apply(self):
297299
def test_snapshot_create(self):
298300
self._test_no_resource_id(network_tasks.snapshot_create,
299301
"No network for backup")
300-
self._test_no_snapshot_name(self._create_ctx(),
301-
network_tasks.snapshot_create)
302+
self._test_no_snapshot_name(
303+
self._create_ctx(),
304+
"cloudify_libvirt.network_tasks.libvirt.open",
305+
network_tasks.snapshot_create)
302306
self._test_empty_connection_backup(network_tasks.snapshot_create)
303307
self._test_empty_network_backup(network_tasks.snapshot_create)
304308

@@ -367,8 +371,10 @@ def test_snapshot_create(self):
367371
def test_snapshot_delete(self):
368372
self._test_no_resource_id(network_tasks.snapshot_delete,
369373
"No network for backup delete")
370-
self._test_no_snapshot_name(self._create_ctx(),
371-
network_tasks.snapshot_delete)
374+
self._test_no_snapshot_name(
375+
self._create_ctx(),
376+
"cloudify_libvirt.network_tasks.libvirt.open",
377+
network_tasks.snapshot_delete)
372378

373379
# no such snapshots
374380
_ctx, connect, network = self._create_fake_network_backup()
@@ -418,7 +424,7 @@ def test_snapshot_delete(self):
418424
# remove backup
419425
_ctx, connect, network = self._create_fake_network_backup()
420426
with mock.patch(
421-
"cloudify_libvirt.domain_tasks.libvirt.open",
427+
"cloudify_libvirt.network_tasks.libvirt.open",
422428
mock.Mock(return_value=connect)
423429
):
424430
with mock.patch(
@@ -445,7 +451,9 @@ def test_delete(self):
445451
self._test_no_resource_id(network_tasks.delete)
446452
self._test_empty_connection(network_tasks.delete)
447453
self._test_empty_network(network_tasks.delete)
448-
self._test_reused_object(network_tasks.delete)
454+
self._test_reused_object(
455+
"cloudify_libvirt.network_tasks.libvirt.open",
456+
network_tasks.delete)
449457

450458
# delete with error
451459
_ctx = self._create_ctx()

0 commit comments

Comments
 (0)