예제 #1
0
 def __init__(self, *args, **kwargs):
     super(DeviceInfoForm, self).__init__(*args, **kwargs)
     if self.data:
         self.data = self.data.copy()
         self.data['model_name'] = self.initial['model_name']
         self.data['rack_name'] = self.initial['rack_name']
         self.data['dc_name'] = self.initial['dc_name']
     self.fields['venture'].choices = all_ventures()
     self.fields['venture_role'].choices = all_roles()
     if not self.instance:
         return
     rack = self.instance.find_rack()
     if rack:
         rack_networks = sorted(
             rack.network_set.all(),
             key=lambda net: net.get_netmask(),
             reverse=True,
         )
         for network in rack_networks:
             if not network.environment:
                 continue
             next_hostname = get_next_free_hostname(network.environment)
             if next_hostname:
                 help_text = 'Next available hostname in this DC: %s' % (
                     next_hostname)
                 self.fields['name'].help_text = help_text
                 break
예제 #2
0
파일: deploy.py 프로젝트: alberto-g/ralph
 def get_context_data(self, **kwargs):
     if not self.device.verified:
         messages.error(
             self.request,
             "{} - is not verified, you cannot "
             "deploy this device".format(self.device),
         )
     # find next available hostname and first free IP address for all
     # networks in which deployed machine is...
     next_hostname = None
     first_free_ip_addresses = []
     rack = self.device.find_rack()
     if rack:
         networks = rack.network_set.filter(
             environment__isnull=False,
         ).order_by('name')
         for network in networks:
             next_hostname = get_next_free_hostname(network.environment)
             if next_hostname:
                 break
         for network in networks:
             first_free_ip = get_first_free_ip(network.name)
             if first_free_ip:
                 first_free_ip_addresses.append({
                     'network_name': network.name,
                     'first_free_ip': first_free_ip,
                 })
     return {
         'form': kwargs['form'],
         'device': self.device,
         'next_hostname': next_hostname,
         'first_free_ip_addresses': first_free_ip_addresses,
     }
예제 #3
0
파일: devices.py 프로젝트: deejay1/ralph
 def __init__(self, *args, **kwargs):
     super(DeviceInfoForm, self).__init__(*args, **kwargs)
     if self.data:
         self.data = self.data.copy()
         self.data['model_name'] = self.initial['model_name']
         self.data['rack_name'] = self.initial['rack_name']
         self.data['dc_name'] = self.initial['dc_name']
     self.fields['venture'].choices = all_ventures()
     self.fields['venture_role'].choices = all_roles()
     if not self.instance:
         return
     rack = self.instance.find_rack()
     if rack:
         rack_networks = sorted(
             rack.network_set.all(),
             key=lambda net: net.get_netmask(),
             reverse=True,
         )
         for network in rack_networks:
             if not network.environment:
                 continue
             next_hostname = get_next_free_hostname(network.environment)
             if next_hostname:
                 help_text = 'Next available hostname in this DC: %s' % (
                     next_hostname
                 )
                 self.fields['name'].help_text = help_text
                 break
예제 #4
0
 def get_context_data(self, **kwargs):
     if not self.device.verified:
         messages.error(
             self.request,
             "{} - is not verified, you cannot "
             "deploy this device".format(self.device),
         )
     # find next available hostname and first free IP address for all
     # networks in which deployed machine is...
     next_hostname = None
     first_free_ip_addresses = []
     rack = self.device.find_rack()
     if rack:
         networks = rack.network_set.filter(
             environment__isnull=False, ).order_by('name')
         for network in networks:
             next_hostname = get_next_free_hostname(network.environment)
             if next_hostname:
                 break
         for network in networks:
             first_free_ip = get_first_free_ip(network.name)
             if first_free_ip:
                 first_free_ip_addresses.append({
                     'network_name':
                     network.name,
                     'first_free_ip':
                     first_free_ip,
                 })
     return {
         'form': kwargs['form'],
         'device': self.device,
         'next_hostname': next_hostname,
         'first_free_ip_addresses': first_free_ip_addresses,
     }
예제 #5
0
파일: common.py 프로젝트: mirorakonto/ralph
 def get_context_data(self, **kwargs):
     ret = super(Addresses, self).get_context_data(**kwargs)
     if self.dns_formset is None:
         dns_records = self.get_dns(self.limit_types)
         ips = {ip.address for ip in self.object.ipaddress_set.all()}
         self.dns_formset = DNSFormSet(
             hostnames=self.get_hostnames(),
             queryset=dns_records,
             prefix='dns',
             limit_types=self.limit_types,
             ips=ips,
         )
     if self.dhcp_formset is None:
         dhcp_records = self.get_dhcp()
         macs = {e.mac for e in self.object.ethernet_set.all()}
         ips = {ip.address for ip in self.object.ipaddress_set.all()}
         self.dhcp_formset = DHCPFormSet(
             dhcp_records,
             macs,
             ips,
             prefix='dhcp',
         )
     if self.ip_formset is None:
         self.ip_formset = IPAddressFormSet(
             queryset=self.object.ipaddress_set.order_by('address'),
             prefix='ip'
         )
     profile = self.request.user.get_profile()
     can_edit = profile.has_perm(self.edit_perm, self.object.venture)
     next_hostname = None
     first_free_ip_addresses = []
     rack = self.object.find_rack()
     if rack:
         networks = rack.network_set.order_by('name')
         for network in networks:
             next_hostname = get_next_free_hostname(network.data_center)
             if next_hostname:
                 break
         for network in networks:
             first_free_ip = get_first_free_ip(network.name)
             if first_free_ip:
                 first_free_ip_addresses.append({
                     'network_name': network.name,
                     'first_free_ip': first_free_ip,
                 })
     balancers = list(_get_balancers(self.object))
     ret.update({
         'canedit': can_edit,
         'balancers': balancers,
         'dnsformset': self.dns_formset,
         'dhcpformset': self.dhcp_formset,
         'ipformset': self.ip_formset,
         'next_hostname': next_hostname,
         'first_free_ip_addresses': first_free_ip_addresses,
     })
     return ret
예제 #6
0
 def handle(self, dc_name=None, *args, **options):
     if not dc_name:
         raise CommandError('Please specify the DC name.')
     try:
         env = Environment.objects.get(name=dc_name)
     except Environment.DoesNotExist:
         raise CommandError("Specified data center doesn't exists.")
     hostname = get_next_free_hostname(env)
     if not hostname:
         raise CommandError("Couldn't determine the next host name.")
     self.stdout.write("Next host name: %s\n" % hostname)
예제 #7
0
 def get_hostname(self, device):
     rack = device.find_rack()
     if rack:
         networks = rack.network_set.filter(
             environment__isnull=False, ).order_by('name')
         for network in networks:
             next_hostname = get_next_free_hostname(network.environment)
             if next_hostname:
                 return next_hostname
         raise Http404('Cannot find a hostname')
     else:
         raise Http404('Cannot find a rack for the parent machine')
예제 #8
0
파일: deploy.py 프로젝트: 4i60r/ralph
 def get_hostname(self, device):
     rack = device.find_rack()
     if rack:
         networks = rack.network_set.filter(
             environment__isnull=False,
         ).order_by('name')
         for network in networks:
             next_hostname = get_next_free_hostname(network.environment)
             if next_hostname:
                 return next_hostname
         raise Http404('Cannot find a hostname')
     else:
         raise Http404('Cannot find a rack for the parent machine')
예제 #9
0
파일: common.py 프로젝트: damjanek/ralph
 def step3_initial(self):
     ips = set()
     names = set()
     for f in self.formset:
         network = Network.objects.get(id=f.cleaned_data['network'])
         ip = get_first_free_ip(network.name, ips)
         ips.add(ip)
         name = get_next_free_hostname(network.data_center, names)
         names.add(name)
         yield {
             'address': f.cleaned_data['address'],
             'new_ip': ip,
             'new_hostname': name,
         }
예제 #10
0
def _find_hostname(network, reserved_hostnames, device=None, ip=None):
    """Find the host name for the deployed device. Reuse existing hostname if
    ``device`` and ``ip`` is specified.  Never pick a hostname that is in
    ``reserved_hostnames`` already.  If no suitable hostname found, return "",
    otherwise the returned hostname is added to ``reserved_hostnames`` list.
    """

    if device and ip:
        try:
            ipaddress = IPAddress.objects.get(address=ip)
        except IPAddress.DoesNotExist:
            pass
        else:
            if ipaddress.hostname:
                return ipaddress.hostname
    hostname = get_next_free_hostname(network.data_center, reserved_hostnames)
    if hostname:
        reserved_hostnames.append(hostname)
    return hostname or ""
예제 #11
0
def _find_hostname(network, reserved_hostnames, device=None, ip=None):
    """Find the host name for the deployed device. Reuse existing hostname if
    ``device`` and ``ip`` is specified.  Never pick a hostname that is in
    ``reserved_hostnames`` already.  If no suitable hostname found, return "",
    otherwise the returned hostname is added to ``reserved_hostnames`` list.
    """

    if device and ip:
        try:
            ipaddress = IPAddress.objects.get(address=ip)
        except IPAddress.DoesNotExist:
            pass
        else:
            if ipaddress.hostname:
                return ipaddress.hostname
    hostname = get_next_free_hostname(network.data_center, reserved_hostnames)
    if hostname:
        reserved_hostnames.append(hostname)
    return hostname or ""
예제 #12
0
 def __init__(self, *args, **kwargs):
     super(DeviceInfoForm, self).__init__(*args, **kwargs)
     if self.data:
         self.data = self.data.copy()
         self.data['model_name'] = self.initial['model_name']
         self.data['rack_name'] = self.initial['rack_name']
         self.data['dc_name'] = self.initial['dc_name']
     self.fields['venture'].choices = all_ventures()
     self.fields['venture_role'].choices = all_roles()
     if not self.instance:
         return
     rack = self.instance.find_rack()
     if rack:
         for network in rack.network_set.order_by('name'):
             next_hostname = get_next_free_hostname(network.data_center)
             if next_hostname:
                 help_text = 'Next available hostname in this DC: %s' % (
                     next_hostname
                 )
                 self.fields['name'].help_text = help_text
                 break
예제 #13
0
파일: deploy.py 프로젝트: alberto-g/ralph
def _find_hostname(network, reserved_hostnames, device=None, ip=None):
    """Find the host name for the deployed device. Reuse existing hostname if
    ``device`` and ``ip`` is specified.  Never pick a hostname that is in
    ``reserved_hostnames`` already.  If no suitable hostname found, return "",
    otherwise the returned hostname is added to ``reserved_hostnames`` list.
    """

    if device and ip:
        try:
            ipaddress = IPAddress.objects.get(address=ip)
        except IPAddress.DoesNotExist:
            pass
        else:
            if ipaddress.hostname:
                return ipaddress.hostname
    # if our network does not have defined environment we couldn't propose
    # any hostname because we don't have the naming template - in this case
    # administrator should manually put hostname...
    if not network.environment:
        return ""
    hostname = get_next_free_hostname(network.environment, reserved_hostnames)
    if hostname:
        reserved_hostnames.append(hostname)
    return hostname or ""
예제 #14
0
def _find_hostname(network, reserved_hostnames, device=None, ip=None):
    """Find the host name for the deployed device. Reuse existing hostname if
    ``device`` and ``ip`` is specified.  Never pick a hostname that is in
    ``reserved_hostnames`` already.  If no suitable hostname found, return "",
    otherwise the returned hostname is added to ``reserved_hostnames`` list.
    """

    if device and ip:
        try:
            ipaddress = IPAddress.objects.get(address=ip)
        except IPAddress.DoesNotExist:
            pass
        else:
            if ipaddress.hostname:
                return ipaddress.hostname
    # if our network does not have defined environment we couldn't propose
    # any hostname because we don't have the naming template - in this case
    # administrator should manually put hostname...
    if not network.environment:
        return ""
    hostname = get_next_free_hostname(network.environment, reserved_hostnames)
    if hostname:
        reserved_hostnames.append(hostname)
    return hostname or ""
예제 #15
0
    def test_get_nexthostname(self):
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h100.temp1')
        name = get_next_free_hostname(self.dc_temp2)
        self.assertEqual(name, 'h200.temp2')

        Record.objects.create(
            domain=self.domain_temp1,
            name='h103.temp1',
            content='127.0.1.2',
            type='A',
        )
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h104.temp1')
        Record.objects.create(
            domain=self.domain_temp1,
            name='h199.temp1',
            content='127.0.1.3',
            type='A',
        )
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h300.temp1')

        dev = Device.create(
            sn='test_sn_998877',
            model_type=DeviceType.unknown,
            model_name='Unknown'
        )
        dev.name = 'h300.temp1'
        dev.save()
        Record.objects.create(
            domain=self.domain_temp1,
            name='123',
            content='h301.temp1',
            type='PTR',
        )
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h302.temp1')

        name = get_next_free_hostname(
            self.dc_temp2, ['h200.temp2', 'h201.temp2'],
        )
        self.assertEqual(name, 'h203.temp2')
예제 #16
0
    def test_get_nexthostname(self):
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h100.temp1')
        name = get_next_free_hostname(self.dc_temp2)
        self.assertEqual(name, 'h200.temp2')

        Record.objects.create(
            domain=self.domain_temp1,
            name='h103.temp1',
            content='127.0.1.2',
            type='A',
        )
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h104.temp1')
        Record.objects.create(
            domain=self.domain_temp1,
            name='h199.temp1',
            content='127.0.1.3',
            type='A',
        )
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h300.temp1')

        dev = Device.create(sn='test_sn_998877',
                            model_type=DeviceType.unknown,
                            model_name='Unknown')
        dev.name = 'h300.temp1'
        dev.save()
        Record.objects.create(
            domain=self.domain_temp1,
            name='123',
            content='h301.temp1',
            type='PTR',
        )
        name = get_next_free_hostname(self.dc_temp1)
        self.assertEqual(name, 'h302.temp1')

        name = get_next_free_hostname(
            self.dc_temp2,
            ['h200.temp2', 'h201.temp2'],
        )
        self.assertEqual(name, 'h203.temp2')