Exemplo n.º 1
0
    def _output_opts_file(self):
        """Write a dnsmasq compatible options file."""

        if self.conf.enable_isolated_metadata:
            subnet_to_interface_ip = self._make_subnet_interface_ip_map()

        options = []
        for i, subnet in enumerate(self.network.subnets):
            if not subnet.enable_dhcp:
                continue
            if subnet.dns_nameservers:
                options.append(self._format_option(i, "dns-server", ",".join(subnet.dns_nameservers)))

            host_routes = ["%s,%s" % (hr.destination, hr.nexthop) for hr in subnet.host_routes]

            # Add host routes for isolated network segments
            enable_metadata = self.conf.enable_isolated_metadata and not subnet.gateway_ip and subnet.ip_version == 4

            if enable_metadata:
                subnet_dhcp_ip = subnet_to_interface_ip[subnet.id]
                host_routes.append("%s/32,%s" % (METADATA_DEFAULT_IP, subnet_dhcp_ip))

            if host_routes:
                options.append(self._format_option(i, "classless-static-route", ",".join(host_routes)))

            if subnet.ip_version == 4:
                if subnet.gateway_ip:
                    options.append(self._format_option(i, "router", subnet.gateway_ip))
                else:
                    options.append(self._format_option(i, "router"))

        name = self.get_conf_file_name("opts")
        utils.replace_file(name, "\n".join(options))
        return name
Exemplo n.º 2
0
def save_config(conf_path, logical_config, socket_path=None):
    """Convert a logical configuration to the HAProxy version."""
    data = []
    data.extend(_build_global(logical_config, socket_path=socket_path))
    data.extend(_build_defaults(logical_config))
    data.extend(_build_frontend(logical_config))
    data.extend(_build_backend(logical_config))
    utils.replace_file(conf_path, '\n'.join(data))
Exemplo n.º 3
0
def save_config(conf_path, logical_config, socket_path=None):
    """Convert a logical configuration to the HAProxy version."""
    data = []
    data.extend(_build_global(logical_config, socket_path=socket_path))
    data.extend(_build_defaults(logical_config))
    data.extend(_build_frontend(logical_config))
    data.extend(_build_backend(logical_config))
    utils.replace_file(conf_path, '\n'.join(data))
Exemplo n.º 4
0
    def test_replace_file(self):
        # make file to replace
        with mock.patch("tempfile.NamedTemporaryFile") as ntf:
            ntf.return_value.name = "/baz"
            with mock.patch("os.chmod") as chmod:
                with mock.patch("os.rename") as rename:
                    utils.replace_file("/foo", "bar")

                    expected = [mock.call("w+", dir="/", delete=False), mock.call().write("bar"), mock.call().close()]

                    ntf.assert_has_calls(expected)
                    chmod.assert_called_once_with("/baz", 0644)
                    rename.assert_called_once_with("/baz", "/foo")
Exemplo n.º 5
0
    def _output_hosts_file(self):
        """Writes a dnsmasq compatible hosts file."""
        r = re.compile("[:.]")
        buf = StringIO.StringIO()

        for port in self.network.ports:
            for alloc in port.fixed_ips:
                name = "%s.%s" % (r.sub("-", alloc.ip_address), self.conf.dhcp_domain)
                buf.write("%s,%s,%s\n" % (port.mac_address, name, alloc.ip_address))

        name = self.get_conf_file_name("host")
        utils.replace_file(name, buf.getvalue())
        return name
Exemplo n.º 6
0
    def _output_opts_file(self):
        """Write a dnsmasq compatible options file."""

        if self.conf.enable_isolated_metadata:
            subnet_to_interface_ip = self._make_subnet_interface_ip_map()

        options = []
        for i, subnet in enumerate(self.network.subnets):
            if not subnet.enable_dhcp:
                continue
            if subnet.dns_nameservers:
                options.append(
                    self._format_option(i, 'dns-server',
                                        ','.join(subnet.dns_nameservers)))

            gateway = subnet.gateway_ip
            host_routes = []
            for hr in subnet.host_routes:
                if hr.destination == "0.0.0.0/0":
                    gateway = hr.nexthop
                else:
                    host_routes.append("%s,%s" % (hr.destination, hr.nexthop))

            # Add host routes for isolated network segments
            enable_metadata = (
                self.conf.enable_isolated_metadata
                and not subnet.gateway_ip
                and subnet.ip_version == 4)

            if enable_metadata:
                subnet_dhcp_ip = subnet_to_interface_ip[subnet.id]
                host_routes.append(
                    '%s/32,%s' % (METADATA_DEFAULT_IP, subnet_dhcp_ip)
                )

            if host_routes:
                options.append(
                    self._format_option(i, 'classless-static-route',
                                        ','.join(host_routes)))

            if subnet.ip_version == 4:
                if gateway:
                    options.append(self._format_option(i, 'router', gateway))
                else:
                    options.append(self._format_option(i, 'router'))

        name = self.get_conf_file_name('opts')
        utils.replace_file(name, '\n'.join(options))
        return name
Exemplo n.º 7
0
    def test_replace_file(self):
        # make file to replace
        with mock.patch('tempfile.NamedTemporaryFile') as ntf:
            ntf.return_value.name = '/baz'
            with mock.patch('os.chmod') as chmod:
                with mock.patch('os.rename') as rename:
                    utils.replace_file('/foo', 'bar')

                    expected = [mock.call('w+', dir='/', delete=False),
                                mock.call().write('bar'),
                                mock.call().close()]

                    ntf.assert_has_calls(expected)
                    chmod.assert_called_once_with('/baz', 0644)
                    rename.assert_called_once_with('/baz', '/foo')
Exemplo n.º 8
0
    def _output_hosts_file(self):
        """Writes a dnsmasq compatible hosts file."""
        r = re.compile('[:.]')
        buf = StringIO.StringIO()

        for port in self.network.ports:
            for alloc in port.fixed_ips:
                name = '%s.%s' % (r.sub(
                    '-', alloc.ip_address), self.conf.dhcp_domain)
                buf.write('%s,%s,%s\n' %
                          (port.mac_address, name, alloc.ip_address))

        name = self.get_conf_file_name('host')
        utils.replace_file(name, buf.getvalue())
        return name
Exemplo n.º 9
0
    def test_replace_file(self):
        # make file to replace
        with mock.patch('tempfile.NamedTemporaryFile') as ntf:
            ntf.return_value.name = '/baz'
            with mock.patch('os.chmod') as chmod:
                with mock.patch('os.rename') as rename:
                    utils.replace_file('/foo', 'bar')

                    expected = [
                        mock.call('w+', dir='/', delete=False),
                        mock.call().write('bar'),
                        mock.call().close()
                    ]

                    ntf.assert_has_calls(expected)
                    chmod.assert_called_once_with('/baz', 0o644)
                    rename.assert_called_once_with('/baz', '/foo')
Exemplo n.º 10
0
    def _output_opts_file(self):
        """Write a dnsmasq compatible options file."""

        if self.conf.enable_isolated_metadata:
            subnet_to_interface_ip = self._make_subnet_interface_ip_map()

        options = []
        for i, subnet in enumerate(self.network.subnets):
            if not subnet.enable_dhcp:
                continue
            if subnet.dns_nameservers:
                options.append(
                    self._format_option(i, 'dns-server',
                                        ','.join(subnet.dns_nameservers)))

            host_routes = [
                "%s,%s" % (hr.destination, hr.nexthop)
                for hr in subnet.host_routes
            ]

            # Add host routes for isolated network segments
            enable_metadata = (self.conf.enable_isolated_metadata
                               and not subnet.gateway_ip
                               and subnet.ip_version == 4)

            if enable_metadata:
                subnet_dhcp_ip = subnet_to_interface_ip[subnet.id]
                host_routes.append('%s/32,%s' %
                                   (METADATA_DEFAULT_IP, subnet_dhcp_ip))

            if host_routes:
                options.append(
                    self._format_option(i, 'classless-static-route',
                                        ','.join(host_routes)))

            if subnet.ip_version == 4:
                if subnet.gateway_ip:
                    options.append(
                        self._format_option(i, 'router', subnet.gateway_ip))
                else:
                    options.append(self._format_option(i, 'router'))

        name = self.get_conf_file_name('opts')
        utils.replace_file(name, '\n'.join(options))
        return name
Exemplo n.º 11
0
 def interface_name(self, value):
     interface_file_path = self.get_conf_file_name('interface',
                                                   ensure_conf_dir=True)
     utils.replace_file(interface_file_path, value)
Exemplo n.º 12
0
 def interface_name(self, value):
     interface_file_path = self.get_conf_file_name('interface',
                                                   ensure_conf_dir=True)
     utils.replace_file(interface_file_path, value)
Exemplo n.º 13
0
def save_conf(conf_path, pool_request, vip_request):
    data = []
    data.extend(_build_conf(pool_request, vip_request))
    utils.replace_file(conf_path, '\n'.join(data))
Exemplo n.º 14
0
def save_ini(ini_path, pool_vseid, vip_vseid, monitor_vseids):
    data = []
    data.extend(_build_ini(pool_vseid, vip_vseid, monitor_vseids))
    utils.replace_file(ini_path, '\n'.join(data))