Skip to content

Commit 44fca5f

Browse files
author
alichaddad
authored
Merge pull request #1277 from 0-complexity/migrate-vfw
fix for migrating halted vfw
2 parents 68ceb35 + 3983989 commit 44fca5f

File tree

7 files changed

+42
-37
lines changed

7 files changed

+42
-37
lines changed

apps/cbportal/base/cloudbroker__cloudspace/methodclass/cloudbroker_cloudspace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def _destroyVFW(self, gid, cloudspaceId, deletemodel=True):
234234
fws = self.cb.netmgr.fw_list(gid=int(gid), domain=str(cloudspaceId))
235235
if fws:
236236
try:
237-
self.cb.netmgr.fw_delete(fwid=fws[0]['guid'], gid=gid, deletemodel=deletemodel, timeout=20)
237+
self.cb.netmgr.fw_delete(fwid=fws[0]['guid'], deletemodel=deletemodel, timeout=20)
238238
except exceptions.ServiceUnavailable:
239239
return False
240240
return True

apps/cbportal/base/cloudbroker__computenode/methodclass/cloudbroker_computenode.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import functools
44
from JumpScale.portal.portal import exceptions
55
from cloudbrokerlib.baseactor import BaseActor, wrap_remote
6-
import random
7-
86

97
class cloudbroker_computenode(BaseActor):
108
"""
@@ -191,9 +189,11 @@ def _move_virtual_machines(self, stack, title, ctx):
191189
vfws = self._vcl.search({'gid': stack['gid'],
192190
'nid': int(stack['referenceId'])})[1:]
193191
for vfw in vfws:
194-
randomnode = random.choice(othernodes)
192+
nid = int(self.cb.getBestProvider(stack['gid'], memory=128)['referenceId'])
195193
ctx.events.sendMessage(title, 'Moving Virtual Firewal %s' % vfw['id'])
196-
self.cb.netmgr.fw_move(vfw['guid'], randomnode['id'])
194+
if not self.cb.netmgr.fw_move(vfw['guid'], nid):
195+
self.cb.netmgr.fw_delete(fwid=vfw['guid'], deletemodel=False, timeout=20)
196+
self.cb.netmgr.fw_start(vfw['guid'], targetNid=nid)
197197

198198
@auth(['level2', 'level3'], True)
199199
@wrap_remote

apps/cloudbroker/cloudbrokerlib/cloudbroker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def release_resources(self, cloudspace, releasenetwork=True):
547547
stack = next(iter(models.stack.search({'referenceId': str(fw.nid), 'gid': fw.gid})[1:]), None)
548548
if stack and stack['status'] != 'DECOMISSIONED':
549549
# destroy vm and model
550-
self.cb.netmgr.fw_delete(fwguid, cloudspace.gid)
550+
self.cb.netmgr.fw_delete(fwguid)
551551
else:
552552
# destroy model only
553553
self.cb.netmgr.fw_destroy(fwguid)

apps/cloudbroker/cloudbrokerlib/netmgr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def fw_get_openvpn_config(self, fwid, **kwargs):
179179
raise exceptions.ServiceUnavailable("Failed to get OpenVPN Config")
180180
return job['result']
181181

182-
def fw_delete(self, fwid, gid, deletemodel=True, timeout=600, **kwargs):
182+
def fw_delete(self, fwid, deletemodel=True, timeout=600, **kwargs):
183183
"""
184184
param:fwid firewall id
185185
param:gid grid id

libs/agent-scripts/vfw/vfs_migrate_routeros.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,38 @@ def action(networkid, sourceip, vlan, externalip):
3939
if source_con:
4040
templatepath = '/var/lib/libvirt/images/routeros/template/'
4141
destination = '/var/lib/libvirt/images/routeros/{0:04x}'.format(networkid)
42-
domain = source_con.lookupByName(name)
43-
if domain.state()[0] == libvirt.VIR_DOMAIN_RUNNING:
44-
if not j.system.fs.exists(destination):
45-
print 'Creating image snapshot %s -> %s' % (templatepath, destination)
46-
localip = j.system.net.getReachableIpAddress(sourceip, 22)
47-
targeturl = "tcp://{}".format(localip)
48-
j.system.btrfs.snapshot(templatepath, destination)
49-
xmldom = ElementTree.fromstring(domain.XMLDesc())
50-
seclabel = xmldom.find('seclabel')
51-
if seclabel is not None:
52-
xmldom.remove(seclabel)
53-
xml = ElementTree.tostring(xmldom)
54-
flags = libvirt.VIR_MIGRATE_LIVE | libvirt.VIR_MIGRATE_PERSIST_DEST | libvirt.VIR_MIGRATE_UNDEFINE_SOURCE | libvirt.VIR_MIGRATE_NON_SHARED_DISK
55-
try:
56-
domain.migrate2(target_con, flags=flags, dxml=xml, uri=targeturl)
57-
except:
42+
try:
43+
domain = source_con.lookupByName(name)
44+
except libvirt.libvirtError:
45+
domain = None
46+
if domain:
47+
if domain.state()[0] == libvirt.VIR_DOMAIN_RUNNING:
48+
if not j.system.fs.exists(destination):
49+
print 'Creating image snapshot %s -> %s' % (templatepath, destination)
50+
localip = j.system.net.getReachableIpAddress(sourceip, 22)
51+
targeturl = "tcp://{}".format(localip)
52+
j.system.btrfs.snapshot(templatepath, destination)
53+
xmldom = ElementTree.fromstring(domain.XMLDesc())
54+
seclabel = xmldom.find('seclabel')
55+
if seclabel is not None:
56+
xmldom.remove(seclabel)
57+
xml = ElementTree.tostring(xmldom)
58+
flags = libvirt.VIR_MIGRATE_LIVE | libvirt.VIR_MIGRATE_PERSIST_DEST | libvirt.VIR_MIGRATE_UNDEFINE_SOURCE | libvirt.VIR_MIGRATE_NON_SHARED_DISK
5859
try:
59-
target_domain = target_con.lookupByName(name)
60-
target_domain.undefine()
60+
domain.migrate2(target_con, flags=flags, dxml=xml, uri=targeturl)
6161
except:
62-
pass # vm wasn't created on target
63-
raise
64-
domain = target_con.lookupByName(name)
65-
network.protect_external(domain, externalip)
66-
network.protect_gwmgmt(domain, internalip)
67-
else:
68-
domain.undefine()
69-
return False
62+
try:
63+
target_domain = target_con.lookupByName(name)
64+
target_domain.undefine()
65+
except:
66+
pass # vm wasn't created on target
67+
raise
68+
domain = target_con.lookupByName(name)
69+
network.protect_external(domain, externalip)
70+
network.protect_gwmgmt(domain, internalip)
71+
else:
72+
domain.undefine()
73+
return False
7074
# remove disk from source
7175
con = j.remote.cuisine.connect(sourceip, 22)
7276
con.run('btrfs subvol delete {} || true'.format(destination))

libs/agent-scripts/vfw/vfs_routeros_restore.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@ def action(networkid):
2727
print('Restoring {} to {}'.format(ovslocation, localfile))
2828
destination = '/var/lib/libvirt/images/routeros/'
2929
networkidHex = '%04x' % int(networkid)
30-
if j.system.fs.exists(j.system.fs.joinPaths(destination, networkidHex)):
31-
j.system.btrfs.subvolumeDelete(destination, networkidHex)
32-
j.system.btrfs.subvolumeCreate(destination, networkidHex)
3330
try:
3431
j.system.platform.qemu_img.info(ovslocation)
3532
except RuntimeError as e:
3633
if 'No such file or directory' in e.message:
3734
return False
3835
j.errorconditionhandler.processPythonExceptionObject(e)
3936
return False
37+
if j.system.fs.exists(j.system.fs.joinPaths(destination, networkidHex)):
38+
j.system.btrfs.subvolumeDelete(destination, networkidHex)
39+
j.system.btrfs.subvolumeCreate(destination, networkidHex)
4040
try:
4141
j.system.platform.qemu_img.convert(ovslocation, 'raw', localfile, 'qcow2')
4242
except Exception as e:
43+
j.system.btrfs.subvolumeDelete(destination, networkidHex)
4344
j.errorconditionhandler.processPythonExceptionObject(e)
4445
return False
4546
return True

scripts/ops/releaseCloudSpaceResources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _release_resources(cloudspaceId):
2525
# delete routeros
2626
fws = netmgr.fw_list(gridid, str(cloudspace.id))
2727
if fws:
28-
netmgr.fw_delete(fws[0]['guid'], gridid)
28+
netmgr.fw_delete(fws[0]['guid'])
2929
if cloudspace.networkId:
3030
libvirt_actor.releaseNetworkId(cloudspace.networkId)
3131
if cloudspace.publicipaddress:

0 commit comments

Comments
 (0)