import socket
import ipaddress
import shutil
-import glob
import stat
import textwrap
import copy
from .xlate import XLATOR
-__version__ = '3.1.9'
+__version__ = '3.2.0'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
found_vspheres.append(vname)
if len(found_vspheres) > 1:
yaml_file_rel = os.path.relpath(str(yaml_file), os.getcwd())
- msg = _("There is only one, unique VSPhere definition allowed in a project file.") + '\n'
+ msg = _("There is only one, unique VSPhere definition allowed in a project file.")
+ msg += '\n'
msg += _("In {f!r} were found {nr} different VSPhere definitions:").format(
f=yaml_file_rel, nr=len(found_vspheres))
for vname in sorted(found_vspheres, key=str.lower):
i = -1
for iface in vm.interfaces:
-
i += 1
- if self.verbose > 1:
- LOG.debug(_("Checking interface {i} of VM {n!r} ...").format(
- i=i, n=vm.name))
- if not iface.address:
- LOG.error(_("Interface {i} of VM {n!r} has no defined address.").format(
- i=i, n=vm.name))
- self.eval_errors += 1
- continue
+ self._validate_interface_of_vm(
+ vm_name=vm.name, iface=iface, vs_name=vs_name, vm_cluster=vm_cluster, i=i)
- if not iface.fqdn:
- LOG.error(_("Interface {i} of VM {n!r} has no defined FQDN.").format(
- i=i, n=vm.name))
- self.eval_errors += 1
- continue
+ # -------------------------------------------------------------------------·
+ def _validate_interface_of_vm(self, vm_name, iface, vs_name, vm_cluster, i=0):
- if iface.fqdn in self.fqdns:
- LOG.error(_(
- "FQDN {f!r} already defined for VM {va!r}({ia}) should be set "
- "for interface {ib} of {vb!r}.").format(
- f=iface.fqdn, va=self.fqdns[iface.fqdn][0], ia=self.fqdns[iface.fqdn][1],
- ib=i, vb=vm.name))
- self.eval_errors += 1
- continue
+ vsphere = self.vsphere[vs_name]
- self.fqdns[iface.fqdn] = (vm.name, i)
+ if self.verbose > 1:
+ LOG.debug(_("Checking interface {i} of VM {n!r} ...").format(
+ i=i, n=vm_name))
- if iface.address_v4:
- if iface.address_v4 in self.addresses:
- LOG.error(_(
- "IPv4 address {a} already defined for VM {va!r}({ia}) should be set "
- "for interface {ib} of {vb!r}.").format(
- a=iface.address_v4, va=self.fqdns[iface.fqdn][0],
- ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm.name))
- self.eval_errors += 1
- continue
- self.addresses[iface.address_v4] = (vm.name, i)
- pair = (iface.fqdn, iface.address_v4)
- self.dns_mapping['forward'].append(pair)
- pair = (iface.address_v4, iface.fqdn)
- self.dns_mapping['reverse'].append(pair)
+ if not iface.address:
+ LOG.error(_("Interface {i} of VM {n!r} has no defined address.").format(
+ i=i, n=vm_name))
+ self.eval_errors += 1
+ return
- if iface.address_v6:
- if iface.address_v6 in self.addresses:
- LOG.error(_(
- "IPv6 address {a} already defined for VM {va!r}({ia}) should be set "
- "for interface {ib} of {vb!r}.").format(
- a=iface.address_v6, va=self.fqdns[iface.fqdn][0],
- ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm.name))
- self.eval_errors += 1
- continue
- self.addresses[iface.address_v6] = (vm.name, i)
- pair = (iface.fqdn, iface.address_v6)
- self.dns_mapping['forward'].append(pair)
- pair = (iface.address_v6, iface.fqdn)
- self.dns_mapping['reverse'].append(pair)
-
- network = iface.network
- if network:
- if network not in vsphere.networks:
- LOG.error(_(
- "Could not find network {n!r} for VM {v!r}, interface {i}.").format(
- n=network, v=vm.name, i=i))
- self.eval_errors += 1
- continue
- else:
- network = vsphere.networks.get_network_for_ip(
- iface.address_v4, iface.address_v6)
- if not network:
- self.eval_errors += 1
- continue
- iface.network = network
- LOG.debug(_("Found network {n!r} for interface {i} of VM {v!r}.").format(
- n=network, i=i, v=vm.name))
+ if not iface.fqdn:
+ LOG.error(_("Interface {i} of VM {n!r} has no defined FQDN.").format(
+ i=i, n=vm_name))
+ self.eval_errors += 1
+ return
+
+ if iface.fqdn in self.fqdns:
+ LOG.error(_(
+ "FQDN {f!r} already defined for VM {va!r}({ia}) should be set "
+ "for interface {ib} of {vb!r}.").format(
+ f=iface.fqdn, va=self.fqdns[iface.fqdn][0], ia=self.fqdns[iface.fqdn][1],
+ ib=i, vb=vm_name))
+ self.eval_errors += 1
+ return
- if network not in vm_cluster.networks:
+ self.fqdns[iface.fqdn] = (vm_name, i)
+
+ if iface.address_v4:
+ if iface.address_v4 in self.addresses:
LOG.error(_(
- "Network {n!r} for interface {i} of VM {v!r} not available in "
- "cluster {c!r}.").format(n=network, v=vm.name, i=i, c=vm_cluster.name))
+ "IPv4 address {a} already defined for VM {va!r}({ia}) should be set "
+ "for interface {ib} of {vb!r}.").format(
+ a=iface.address_v4, va=self.fqdns[iface.fqdn][0],
+ ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm_name))
self.eval_errors += 1
- continue
- LOG.debug(_("Network {n!r} is available in cluster {c!r}.").format(
- n=network, c=vm_cluster.name))
-
- net = vsphere.networks[network]
- if not iface.gateway:
- LOG.debug(_("Setting gateway of interface {i} of VM {v!r} to {g}.").format(
- i=i, v=vm.name, g=net.gateway))
- iface.gateway = net.gateway
-
- if net.network:
- if net.network.version == 4:
- if iface.netmask_v4 is None:
- iface.netmask_v4 = net.network.prefixlen
- else:
- if iface.netmask_v6 is None:
- iface.netmask_v6 = net.network.prefixlen
+ return
+ self.addresses[iface.address_v4] = (vm_name, i)
+ pair = (iface.fqdn, iface.address_v4)
+ self.dns_mapping['forward'].append(pair)
+ pair = (iface.address_v4, iface.fqdn)
+ self.dns_mapping['reverse'].append(pair)
+
+ if iface.address_v6:
+ if iface.address_v6 in self.addresses:
+ LOG.error(_(
+ "IPv6 address {a} already defined for VM {va!r}({ia}) should be set "
+ "for interface {ib} of {vb!r}.").format(
+ a=iface.address_v6, va=self.fqdns[iface.fqdn][0],
+ ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm_name))
+ self.eval_errors += 1
+ return
+ self.addresses[iface.address_v6] = (vm_name, i)
+ pair = (iface.fqdn, iface.address_v6)
+ self.dns_mapping['forward'].append(pair)
+ pair = (iface.address_v6, iface.fqdn)
+ self.dns_mapping['reverse'].append(pair)
+
+ network = iface.network
+ if network:
+ if network not in vsphere.networks:
+ LOG.error(_(
+ "Could not find network {n!r} for VM {v!r}, interface {i}.").format(
+ n=network, v=vm_name, i=i))
+ self.eval_errors += 1
+ return
+ else:
+ network = vsphere.networks.get_network_for_ip(
+ iface.address_v4, iface.address_v6)
+ if not network:
+ self.eval_errors += 1
+ return
+ iface.network = network
+ LOG.debug(_("Found network {n!r} for interface {i} of VM {v!r}.").format(
+ n=network, i=i, v=vm_name))
+
+ if network not in vm_cluster.networks:
+ LOG.error(_(
+ "Network {n!r} for interface {i} of VM {v!r} not available in "
+ "cluster {c!r}.").format(n=network, v=vm_name, i=i, c=vm_cluster.name))
+ self.eval_errors += 1
+ return
+ LOG.debug(_("Network {n!r} is available in cluster {c!r}.").format(
+ n=network, c=vm_cluster.name))
+
+ net = vsphere.networks[network]
+ if not iface.gateway:
+ LOG.debug(_("Setting gateway of interface {i} of VM {v!r} to {g}.").format(
+ i=i, v=vm_name, g=net.gateway))
+ iface.gateway = net.gateway
+
+ if net.network:
+ if net.network.version == 4:
+ if iface.netmask_v4 is None:
+ iface.netmask_v4 = net.network.prefixlen
+ else:
+ if iface.netmask_v6 is None:
+ iface.netmask_v6 = net.network.prefixlen
- if vs_name not in self.used_networks:
- self.used_networks[vs_name] = []
- if network not in self.used_networks[vs_name]:
- self.used_networks[vs_name].append(network)
+ if vs_name not in self.used_networks:
+ self.used_networks[vs_name] = []
+ if network not in self.used_networks[vs_name]:
+ self.used_networks[vs_name].append(network)
# -------------------------------------------------------------------------·
def validate_dns_mappings(self):
print()
print()
- #LOG.info(_("Creating all necessary files for terraform."))
msg = _("Creating all necessary files for terraform.")
ll = 6
if len(msg) > ll:
w='dcfile', v=vs_name))
vs_host = self.config.vsphere[vs_name].host
- vs_port = self.config.vsphere[vs_name].port
vs_user = self.config.vsphere[vs_name].user
vs_pwd = self.config.vsphere[vs_name].password
vs_dc = self.config.vsphere[vs_name].dc
def create_instance_file(self, vm):
vs_name = vm.vsphere
- vsphere = self.vsphere[vs_name]
fname = 'instance.' + vm.name + '.tf'
LOG.debug(_("Creating file {f!r} for VM instance {n!r}.").format(