Skip to content

Commit f28a050

Browse files
committed
add get default SR when add disk
1 parent a670827 commit f28a050

File tree

5 files changed

+149
-52
lines changed

5 files changed

+149
-52
lines changed

lib/Utils/server_utils.py

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'''
99
from lib.Log.log import log
1010
from lib.Val.virt_factory import VirtFactory
11+
from lib.Utils.network_utils import IpCheck, is_IP_pingable
1112

1213

1314
def print_server_hardware_info(**kwargs):
@@ -65,7 +66,104 @@ def get_host_all_storage_info(**kwargs):
6566
return storage_info
6667

6768

69+
def get_default_device(host=None, user=None, passwd=None):
70+
"""
71+
get the host's default network/Interface which has configured an IP;
72+
:param kwargs:
73+
:return: Interface name on host, or None
74+
"""
75+
log.info("Get the host default network with IP configured.")
76+
77+
host_name = host
78+
user = user if user else "root"
79+
passwd = str(passwd).replace('\\', '') if passwd else ""
80+
81+
vnet_driver = VirtFactory.get_vnet_driver(host_name, user, passwd)
82+
83+
devices = vnet_driver.get_all_devices()
84+
for device_name in devices:
85+
# 'IP': '' or an ip,
86+
device_info = vnet_driver.get_device_infor(device_name=device_name)
87+
ipstr = device_info.get('IP', '')
88+
if ipstr:
89+
return device_name
90+
else:
91+
log.error("No device found with an IP configured.")
92+
return None
93+
94+
95+
def get_default_storage(host=None, user=None, passwd=None):
96+
"""
97+
get the default storage repository which has the largest volume for user
98+
:param host:
99+
:param user:
100+
:param passwd:
101+
:return: the storage name
102+
"""
103+
log.info("Get the host default storage name which has the largest free volume.")
104+
105+
host_name = host
106+
user = user if user else "root"
107+
passwd = str(passwd).replace('\\', '') if passwd else ""
108+
109+
virt_driver = VirtFactory.get_virt_driver(host_name, user, passwd)
110+
all_sr = virt_driver.get_host_all_storages()
111+
max_volume, target_sr = 0, None
112+
for sr in all_sr:
113+
storage_dict = virt_driver.get_host_storage_info(storage_name=sr)
114+
temp = int(storage_dict.get('size_free', 0))
115+
if temp > max_volume:
116+
max_volume, target_sr = temp, sr
117+
118+
log.info("The default storage is '%s' with volume %s GB.", target_sr, max_volume)
119+
return target_sr
120+
121+
122+
def is_IP_available(vif_ip=None, vif_netmask=None, device=None, **kwargs):
123+
"""
124+
check if a IP and Netmask usable
125+
"""
126+
# No ip , don't need to check
127+
if not vif_ip:
128+
return True
129+
130+
dest_metmask = ""
131+
dest_gateway = None
132+
if device is not None:
133+
try:
134+
host_name = kwargs['host']
135+
user = kwargs['user'] if kwargs['user'] else "root"
136+
passwd = str(kwargs['passwd']).replace('\\', '') if kwargs['passwd'] else ""
137+
vnet_driver = VirtFactory.get_vnet_driver(host_name, user, passwd)
138+
device_info = vnet_driver.get_device_infor(device_name=device)
139+
dest_metmask = device_info["netmask"]
140+
dest_gateway = device_info['gateway']
141+
except KeyError, error:
142+
log.exception(str(error))
143+
144+
if vif_netmask:
145+
if dest_metmask and dest_metmask != vif_netmask:
146+
log.error("Netmask [%s] is not corresponding with the target network.", vif_netmask)
147+
return False
148+
else: # get the netmask on device as the default one
149+
vif_netmask = dest_metmask
150+
log.debug("VIF IP is: %s, netmask is: %s", vif_ip, vif_netmask)
151+
if not vif_netmask: # No default netmask and no given
152+
log.error("No netmask given, please specify one.")
153+
return False
154+
155+
vif_gateway = dest_gateway if dest_gateway else None
156+
if not IpCheck.is_valid_ipv4_parameter(vif_ip, vif_netmask, gateway=vif_gateway):
157+
return False
158+
159+
if is_IP_pingable(vif_ip):
160+
log.error("Ipaddress [%s] is already be used(Ping test).", vif_ip)
161+
return False
162+
163+
return True
164+
165+
68166
if __name__ == "__main__":
69167
d = get_host_all_storage_info(host="10.143.248.16", user="root", passwd="Mojiti!906")
70168
for k, v in d.iteritems():
71-
print k,"\t\t", v
169+
print k, "\t\t", v

lib/Utils/vm_utils.py

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,11 @@
88
the return value of update_database, except create/delete VM
99
'''
1010

11-
from lib.Utils.network_utils import IpCheck, is_IP_pingable
1211
from lib.Val.virt_factory import VirtFactory, VM_MAC_PREFIX
1312
from lib.Log.log import log
1413
from lib.Utils.db_utils import update_vm_database_info, create_vm_database_info, delete_vm_database_info
1514

1615

17-
def is_IP_available(vif_ip=None, vif_netmask=None, device=None, **kwargs):
18-
"""
19-
check if a IP and Netmask usable
20-
"""
21-
# No ip , don't need to check
22-
if not vif_ip:
23-
return True
24-
25-
dest_metmask = ""
26-
dest_gateway = None
27-
if device is not None:
28-
try:
29-
host_name = kwargs['host']
30-
user = kwargs['user'] if kwargs['user'] else "root"
31-
passwd = str(kwargs['passwd']).replace('\\', '') if kwargs['passwd'] else ""
32-
vnet_driver = VirtFactory.get_vnet_driver(host_name, user, passwd)
33-
device_info = vnet_driver.get_device_infor(device_name=device)
34-
dest_metmask = device_info["netmask"]
35-
dest_gateway = device_info['gateway']
36-
except KeyError, error:
37-
log.exception(str(error))
38-
39-
if vif_netmask:
40-
if dest_metmask and dest_metmask != vif_netmask:
41-
log.error("Netmask [%s] is not corresponding with the target network.", vif_netmask)
42-
return False
43-
else: # get the netmask on device as the default one
44-
vif_netmask = dest_metmask
45-
log.debug("VIF IP is: %s, netmask is: %s", vif_ip, vif_netmask)
46-
if not vif_netmask: # No default netmask and no given
47-
log.error("No netmask given, please specify one.")
48-
return False
49-
50-
vif_gateway = dest_gateway if dest_gateway else None
51-
if not IpCheck.is_valid_ipv4_parameter(vif_ip, vif_netmask, gateway=vif_gateway):
52-
return False
53-
54-
if is_IP_pingable(vif_ip):
55-
log.error("Ipaddress [%s] is already be used(Ping test).", vif_ip)
56-
return False
57-
58-
return True
59-
60-
6116
def create_vm(new_vm_name, template_name, **kwargs):
6217
"""
6318
Create new instance with name and template
@@ -80,6 +35,7 @@ def create_vm(new_vm_name, template_name, **kwargs):
8035

8136
return db_ret
8237

38+
8339
def delete_vm(vm_name, **kwargs):
8440
"""
8541
:param vm_name:
@@ -104,6 +60,7 @@ def delete_vm(vm_name, **kwargs):
10460
# No matter delete vm from DB failed or not, return True
10561
return True
10662

63+
10764
def create_new_vif(inst_name, vif_index, device_name=None, network=None, ip=None, **kwargs):
10865
"""
10966
create a new virtual interface on the target VM
@@ -124,7 +81,7 @@ def create_new_vif(inst_name, vif_index, device_name=None, network=None, ip=None
12481
else:
12582
mac_addr = None
12683

127-
log.debug("Create VIF [%s] with IP: %s, MAC: %s.",vif_index, ip, mac_addr)
84+
log.debug("Create VIF [%s] with IP: %s, MAC: %s.", vif_index, ip, mac_addr)
12885
new_vif = vnet_driver.create_new_vif(inst_name, vif_index, device_name, network, MAC=mac_addr)
12986
if new_vif is not None:
13087
# TODO: sync DB when success

lib/Val/Xen/virt_driver_xen.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,42 @@ def add_vdisk_to_vm(self, inst_name, storage_name='Local storage', size=2):
560560

561561
return True
562562

563+
def _delete_virtual_disk_unused(self, storage_name, inst_name):
564+
"""
565+
delete those VDI (virtual disk) which is not used by any VM; When delete a vm, the VDI is not deleted.
566+
:param storage_name:
567+
:param inst_name: the instance name to which virtual disk belongs. The VDI created by the api include the inst name
568+
:return:
569+
"""
570+
if self._hypervisor_handler is None:
571+
self._hypervisor_handler = self.get_handler()
572+
573+
try:
574+
sr_ref = self._hypervisor_handler.xenapi.SR.get_by_name_label(storage_name)[0]
575+
all_vdis = self._hypervisor_handler.xenapi.SR.get_VDIs(sr_ref)
576+
for vdi_ref in all_vdis:
577+
# If there are VBDs attached with the VDI, don't delete; Deleted VM has no VBD attached to the VDI
578+
# VDI.get_record(vdi), record['VBDs'], record['allowed_operations'], record['name_label']
579+
if self._hypervisor_handler.xenapi.VDI.get_VBDs(vdi_ref):
580+
continue
581+
582+
if not "destroy" in self._hypervisor_handler.xenapi.VDI.get_allowed_operations(vdi_ref):
583+
continue
584+
# Delete those vdi created by the api
585+
name_label = self._hypervisor_handler.xenapi.VDI.get_name_label(vdi_ref)
586+
if not str(name_label).startswith(inst_name):
587+
continue
588+
589+
try:
590+
self._hypervisor_handler.xenapi.VDI.destroy(vdi_ref)
591+
except Exception:
592+
log.warn("Destroy virtual disk [%s] failed.", vdi_ref)
593+
except Exception:
594+
log.exception("Except when delete VDI: %s", storage_name)
595+
return False
596+
597+
return True
598+
563599
#### Host information API ####
564600
def get_host_cpu_info(self):
565601
"""

scripts/config_vm.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from optparse import OptionParser
1010
from lib.Log.log import log
1111
from lib.Val.virt_factory import VirtFactory
12-
from lib.Utils.vm_utils import is_IP_available, create_new_vif, destroy_old_vif, config_vif, add_vm_disk, config_vcpus
12+
from lib.Utils.vm_utils import create_new_vif, destroy_old_vif, config_vif, add_vm_disk, config_vcpus
13+
from lib.Utils.server_utils import is_IP_available, get_default_storage
1314

1415
if __name__ == "__main__":
1516
usage = """usage: %prog [options] vm_name\n
@@ -181,9 +182,14 @@
181182
exit(1)
182183

183184
elif options.disk_size is not None:
185+
option_dic = {"host": host_name, "user": user, "passwd": passwd}
186+
184187
if not options.storage_name:
185-
log.fail("Please specify a storage name for the new virtual disk.")
186-
exit(1)
188+
options.storage_name = get_default_storage(**option_dic)
189+
if not options.storage_name:
190+
log.fail("Failed to get default SR, please specify a storage name for the new virtual disk.")
191+
exit(1)
192+
187193
size = int(options.disk_size)
188194
storage_info = virt_driver.get_host_storage_info(storage_name=options.storage_name)
189195
if not storage_info:
@@ -194,7 +200,6 @@
194200
options.storage_name, storage_info['size_free'] - 1)
195201
exit(1)
196202

197-
option_dic = {"host": host_name, "user": user, "passwd": passwd}
198203
ret = add_vm_disk(inst_name, storage_name=options.storage_name, size=size, **option_dic)
199204
if ret:
200205
log.success("Successfully add a new disk with size [%s]GB to VM [%s].", size, inst_name)

scripts/create_vm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from optparse import OptionParser
99
from lib.Log.log import log
1010
from lib.Val.virt_factory import VirtFactory
11-
from lib.Utils.vm_utils import is_IP_available, config_vif, power_on_vm, create_vm
11+
from lib.Utils.vm_utils import config_vif, power_on_vm, create_vm
12+
from lib.Utils.server_utils import is_IP_available
1213

1314
if __name__ == "__main__":
1415
usage = """usage: %prog [options] arg1 arg2\n

0 commit comments

Comments
 (0)