Пример #1
0
    def test_get_hostname(self):
        """get_hostname() should return the hostname for a given FQDN."""
        fqdn = 'foo.bar'
        assert get_hostname(fqdn) == 'foo'

        fqdn = 'foo.bar.suse.de'
        assert get_hostname(fqdn) == 'foo'

        fqdn = 'foo.bar.foobar.suse.de'
        assert get_hostname(fqdn) == 'foo'
Пример #2
0
def get_bmc_command(machine, cobbler_path):
    if not hasattr(machine, 'bmc') or not machine.bmc:
        logger.error("Tried to get bmc command for %s, which does not have one", machine.fqdn)
    bmc = machine.bmc
    bmc_command = """{cobbler} system edit --name={name} --interface=bmc --interface-type=bmc"""\
        .format(cobbler=cobbler_path, name=machine.fqdn)
    bmc_command += """ --ip-address="{ip}" --mac="{mac}" --dns-name="{dns}" """.format(
            ip=get_ip(bmc.fqdn)[0], mac=bmc.mac, dns=get_hostname(bmc.fqdn))
    return bmc_command
Пример #3
0
def machine_post_init(sender, instance, *args, **kwargs):
    """Post init action for machine. Set non-database saved values here."""
    if instance.pk:
        try:
            instance.hostname = get_hostname(instance.fqdn)

            if instance.get_primary_networkinterface():
                instance.mac_address = instance.get_primary_networkinterface().mac_address
        except Exception as e:
            logger.warning("Errors occurred during machine init: '{}': {}".format(instance, e))
Пример #4
0
    def save(self, *args, **kwargs):
        """
        Save machine object.

        Set FQDN to lower case, check if FQDN is resolvable by DNS and set
        domain and enclosure correctly (create if necessary).
        """
        self.fqdn = self.fqdn.lower()

        if not self.mac_address:
            raise ValidationError("'{}' has no MAC address!".format(self))

        validate_mac_address(self.mac_address)

        # create & assign network domain and ensure that the FQDN always matches the fqdn_domain
        domain, created = Domain.objects.get_or_create(
            name=get_domain(self.fqdn))
        if created:
            domain.save()
        self.fqdn_domain = domain

        # create & assign enclosure according to naming convention if no enclosure given
        if not hasattr(self, 'enclosure'):
            name = re.split(r'-(\d|sp)+$', get_hostname(self.fqdn))[0]
            enclosure, created = Enclosure.objects.get_or_create(name=name)
            self.enclosure = enclosure

        if isinstance(self.virtualization_api, VirtualizationAPI):
            self.virtualization_api = self.virtualization_api.get_type()

        super(Machine, self).save(*args, **kwargs)

        # check if DHCP needs to be regenerated
        if self._original is not None:
            try:
                assert self.mac_address == self._original.mac_address
                assert self.fqdn == self._original.fqdn
                assert self.fqdn_domain == self._original.fqdn_domain
                assert self.architecture == self._original.architecture
                assert self.group == self._original.group
                assert self.dhcp_filename == self._original.dhcp_filename
                assert self.dhcpv4_write == self._original.dhcpv4_write
                assert self.dhcpv6_write == self._original.dhcpv6_write
            except AssertionError:
                from orthos2.data.signals import signal_cobbler_regenerate

                # regenerate DHCP on all domains (deletion/registration) if domain changed
                if self.fqdn_domain == self._original.fqdn_domain:
                    domain_id = self.fqdn_domain.pk
                else:
                    domain_id = None

                signal_cobbler_regenerate.send(sender=self.__class__,
                                               domain_id=domain_id)
Пример #5
0
    def save(self, *args, **kwargs):
        """
        Save machine object.

        Set FQDN to lower case, check if FQDN is resolvable by DNS and set
        domain and enclosure correctly (create if necessary).
        """
        self.fqdn = self.fqdn.lower()

        if not self.mac_address and not self.unknown_mac:
            raise ValidationError(
                "'{}' You must select 'MAC Unkown' for systems without MAC".
                format(self))

        if self.mac_address and self.unknown_mac:
            raise ValidationError(
                "'{}' You must not select 'MAC Unkown' for systems with a MAC".
                format(self))
        if self.unknown_mac and not self.bmc_allowed():
            raise ValidationError(
                "You may only skip the MAC for systems with BMC")
        if self.mac_address:
            validate_mac_address(self.mac_address)

        if not self.system.virtual and self.hypervisor:
            raise ValidationError("Only virtual machines may have hypervisors")
        if hasattr(self, 'bmc') and not self.bmc_allowed():
            raise ValidationError("{} systems cannot use a BMC".format(
                self.system.name))
        # create & assign network domain and ensure that the FQDN always matches the fqdn_domain
        domain, created = Domain.objects.get_or_create(
            name=get_domain(self.fqdn))
        if created:
            domain.save()
        self.fqdn_domain = domain

        # create & assign enclosure according to naming convention if no enclosure given
        if not hasattr(self, 'enclosure'):
            name = re.split(r'-(\d|sp)+$', get_hostname(self.fqdn))[0]
            enclosure, created = Enclosure.objects.get_or_create(name=name)
            self.enclosure = enclosure

        super(Machine, self).save(*args, **kwargs)

        # check if DHCP needs to be regenerated
        if self._original is not None:
            try:
                assert self.mac_address == self._original.mac_address
                assert self.fqdn == self._original.fqdn
                assert self.fqdn_domain == self._original.fqdn_domain
                assert self.architecture == self._original.architecture
                assert self.group == self._original.group
                assert self.dhcp_filename == self._original.dhcp_filename
                assert self.kernel_options == self._original.kernel_options
                if self.has_remotepower():
                    assert hasattr(self._original, 'remotepower')
                    assert self.remotepower.fence_name == self._original.remotepower.fence_name
                    assert self.remotepower.options == self._original.remotepower.options
                    if hasattr(self.remotepower, 'remote_power_device'):
                        assert hasattr(self._original.remotepower,
                                       'remote_power_device')
                        assert self.remotepower.remote_power_device == \
                               self._original.remotepower.remote_power_device
                if hasattr(self, 'bmc'):
                    assert hasattr(self._original, 'bmc')
                    assert self.bmc.username == self._original.bmc.username
                    assert self.bmc.password == self._original.bmc.password
                    assert self.bmc.mac == self._original.bmc.mac
                    assert self.bmc.fqdn == self._original.bmc.fqdn
                if self.has_serialconsole():
                    assert hasattr(self._original, 'serialconsole')
                    assert self.serialconsole.baud_rate == self._original.serialconsole.baud_rate
                    assert self.serialconsole.kernel_device_num == \
                        self._original.serialconsole.kernel_device_num
            except AssertionError:
                if ServerConfig.objects.bool_by_key("orthos.cobblersync.full"):
                    from orthos2.data.signals import signal_cobbler_regenerate

                    # regenerate DHCP on all domains (deletion/registration) if domain changed
                    if self.fqdn_domain == self._original.fqdn_domain:
                        domain_id = self.fqdn_domain.pk
                    else:
                        domain_id = None

                    signal_cobbler_regenerate.send(sender=self.__class__,
                                                   domain_id=domain_id)
                else:
                    from orthos2.data.signals import signal_cobbler_machine_update
                    if self.fqdn_domain == self._original.fqdn_domain:
                        domain_id = self.fqdn_domain.pk
                        machine_id = self.pk
                        signal_cobbler_machine_update.send(
                            sender=self.__class__,
                            domain_id=domain_id,
                            machine_id=machine_id)
                    else:
                        raise NotImplementedError(
                            "Moving machines between domains with quick cobbler synchronization "
                            "is not implemented yet")
Пример #6
0
    def save(self, *args, **kwargs):
        """
        Save machine object.

        Set FQDN to lower case, check if FQDN is resolvable by DNS and set
        domain and enclosure correctly (create if necessary).
        """
        self.fqdn = self.fqdn.lower()

        if not self.mac_address:
            raise ValidationError("'{}' has no MAC address!".format(self))

        validate_mac_address(self.mac_address)

        if not self.system.virtual and self.hypervisor:
            raise ValidationError("Only virtuals machines may have hypervisors")
        if self.system.virtual and self.use_bmc:
            raise ValidationError("Virtual machines can not use a BMC")
        # create & assign network domain and ensure that the FQDN always matches the fqdn_domain
        domain, created = Domain.objects.get_or_create(name=get_domain(self.fqdn))
        if created:
            domain.save()
        self.fqdn_domain = domain

        # create & assign enclosure according to naming convention if no enclosure given
        if not hasattr(self, 'enclosure'):
            name = re.split(r'-(\d|sp)+$', get_hostname(self.fqdn))[0]
            enclosure, created = Enclosure.objects.get_or_create(name=name)
            self.enclosure = enclosure

        if isinstance(self.virtualization_api, VirtualizationAPI):
            self.virtualization_api = self.virtualization_api.get_type()

        super(Machine, self).save(*args, **kwargs)

        # check if DHCP needs to be regenerated
        if self._original is not None:
            try:
                assert self.mac_address == self._original.mac_address
                assert self.fqdn == self._original.fqdn
                assert self.fqdn_domain == self._original.fqdn_domain
                assert self.architecture == self._original.architecture
                assert self.group == self._original.group
                assert self.dhcp_filename == self._original.dhcp_filename
                assert self.dhcpv4_write == self._original.dhcpv4_write
                assert self.dhcpv6_write == self._original.dhcpv6_write
            except AssertionError:
                if ServerConfig.bool_by_key("orthos.cobblersync.full"):
                    from orthos2.data.signals import signal_cobbler_regenerate

                    # regenerate DHCP on all domains (deletion/registration) if domain changed
                    if self.fqdn_domain == self._original.fqdn_domain:
                        domain_id = self.fqdn_domain.pk
                    else:
                        domain_id = None

                    signal_cobbler_regenerate.send(sender=self.__class__, domain_id=domain_id)
                else:
                    from orthos2.data.signals import signal_cobbler_machine_update
                    if self.fqdn_domain == self._original.fqdn_domain:
                        domain_id = self.fqdn_domain.pk
                        machine_id = self.pk
                        signal_cobbler_machine_update.send(
                            sender=self.__class__, domain_id=domain_id, machine_id=machine_id)
                    else:
                        raise NotImplementedError(
                            "Moving machines between domains with quick cobbler synchronization "
                            "is not implemented yet")