mirror of
https://github.com/gryf/boxpy.git
synced 2025-12-20 22:27:58 +01:00
Added check for already used port.
This commit is contained in:
40
box.py
40
box.py
@@ -578,21 +578,38 @@ class VBoxManage:
|
|||||||
f'--{nic}', kind]) != 0:
|
f'--{nic}', kind]) != 0:
|
||||||
raise BoxVBoxFailure(f'Cannot modify VM "{self.name_or_uuid}".')
|
raise BoxVBoxFailure(f'Cannot modify VM "{self.name_or_uuid}".')
|
||||||
|
|
||||||
|
def is_port_in_use(self, port):
|
||||||
|
used_ports = self._get_defined_ports()
|
||||||
|
for vmname, vmport in used_ports.items():
|
||||||
|
if vmport == port:
|
||||||
|
return vmname
|
||||||
|
return False
|
||||||
|
|
||||||
def _find_unused_port(self):
|
def _find_unused_port(self):
|
||||||
|
used_ports = self._get_defined_ports()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
port = random.randint(2000, 2999)
|
||||||
|
if port not in used_ports.values():
|
||||||
|
self.vm_info['port'] = port
|
||||||
|
return port
|
||||||
|
|
||||||
|
def _get_defined_ports(self):
|
||||||
self.get_vm_info()
|
self.get_vm_info()
|
||||||
try:
|
try:
|
||||||
out = subprocess.check_output(['vboxmanage', 'list', 'vms'],
|
out = subprocess.check_output(['vboxmanage', 'list', 'vms'],
|
||||||
encoding=sys.getdefaultencoding(),
|
encoding=sys.getdefaultencoding(),
|
||||||
stderr=subprocess.DEVNULL)
|
stderr=subprocess.DEVNULL)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
return 0
|
return {}
|
||||||
|
|
||||||
used_ports = []
|
used_ports = {}
|
||||||
for line in out.split('\n'):
|
for line in out.split('\n'):
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
vm_name = line.split('"')[1]
|
||||||
vm_uuid = line.split('{')[1][:-1]
|
vm_uuid = line.split('{')[1][:-1]
|
||||||
if self.vm_info['uuid'] == vm_uuid:
|
if self.vm_info.get('uuid') and self.vm_info['uuid'] == vm_uuid:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -611,14 +628,9 @@ class VBoxManage:
|
|||||||
gebtn = dom.getElementsByTagName
|
gebtn = dom.getElementsByTagName
|
||||||
|
|
||||||
if len(gebtn('Forwarding')):
|
if len(gebtn('Forwarding')):
|
||||||
used_ports.append(gebtn('Forwarding')[0]
|
used_ports[vm_name] = (gebtn('Forwarding')[0]
|
||||||
.getAttribute('hostport'))
|
.getAttribute('hostport'))
|
||||||
|
return used_ports
|
||||||
while True:
|
|
||||||
port = random.randint(2000, 2999)
|
|
||||||
if port not in used_ports:
|
|
||||||
self.vm_info['port'] = port
|
|
||||||
return port
|
|
||||||
|
|
||||||
def _get_vm_config(self):
|
def _get_vm_config(self):
|
||||||
if self.vm_info.get('config_file'):
|
if self.vm_info.get('config_file'):
|
||||||
@@ -843,9 +855,17 @@ def vmcreate(args, conf=None):
|
|||||||
|
|
||||||
if not conf:
|
if not conf:
|
||||||
conf = Config(args)
|
conf = Config(args)
|
||||||
|
|
||||||
vbox = VBoxManage(conf.name)
|
vbox = VBoxManage(conf.name)
|
||||||
|
if conf.port:
|
||||||
|
used = vbox.is_port_in_use(conf.port)
|
||||||
|
if used:
|
||||||
|
print(f'Error: Port {conf.port} is in use by "{used}" VM.')
|
||||||
|
return 11
|
||||||
|
|
||||||
if not vbox.create(conf.cpus, conf.memory, conf.port):
|
if not vbox.create(conf.cpus, conf.memory, conf.port):
|
||||||
return 10
|
return 10
|
||||||
|
|
||||||
vbox.create_controller('IDE', 'ide')
|
vbox.create_controller('IDE', 'ide')
|
||||||
vbox.create_controller('SATA', 'sata')
|
vbox.create_controller('SATA', 'sata')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user