Esempio n. 1
0
    def _prep_servers(self, commands, servers, commands_on_servers=None):
        """Checks to see if any of the servers passed are CIDR-ish networks.

        Args::

            commands: list of commands to run
            servers: list of servers to run the commands on
            commands_on_servers: dictionary mapping commands to servers

        Returns:
            list of servers to run on, including any expanded networks
        """

        if commands_on_servers is not None:
            actual_commands_on_servers = {}
            for servers, command_list in commands_on_servers.items():
                if not isinstance(servers, tuple):
                    servers = [servers]
                for server in servers:
                    if not isinstance(command_list, (list, tuple)):
                        command_list = [command_list]

                    network_members = ips_in_subnet(server)
                    if network_members:
                        for member in network_members:
                            actual_commands_on_servers[member] = command_list
                    else:
                        actual_commands_on_servers[server] = command_list

            self.commands = None
            self.commands_on_servers = actual_commands_on_servers

            expanded_servers = list(actual_commands_on_servers.keys())
        else:
            expanded_servers = []
            for server in servers:
                network_members = ips_in_subnet(server)
                if network_members:
                    expanded_servers.extend(network_members)
                else:
                    expanded_servers.append(server)

            self.commands = commands

        return expanded_servers
Esempio n. 2
0
    def _prep_servers(self, commands, servers, commands_on_servers=None):
        """Checks to see if any of the servers passed are CIDR-ish networks.

        Args::

            commands: list of commands to run
            servers: list of servers to run the commands on
            commands_on_servers: dictionary mapping commands to servers

        Returns:
            list of servers to run on, including any expanded networks
        """

        if commands_on_servers is not None:
            actual_commands_on_servers = {}
            for servers, command_list in commands_on_servers.items():
                if not isinstance(servers, tuple):
                    servers = [servers]
                for server in servers:
                    if not isinstance(command_list, (list, tuple)):
                        command_list = [command_list]

                    network_members = ips_in_subnet(server)
                    if network_members:
                        for member in network_members:
                            actual_commands_on_servers[member] = command_list
                    else:
                        actual_commands_on_servers[server] = command_list

            self.commands = None
            self.commands_on_servers = actual_commands_on_servers

            expanded_servers = list(actual_commands_on_servers.keys())
        else:
            expanded_servers = []
            for server in servers:
                network_members = ips_in_subnet(server)
                if network_members:
                    expanded_servers.extend(network_members)
                else:
                    expanded_servers.append(server)

            self.commands = commands

        return expanded_servers
Esempio n. 3
0
 def test_subnet_mask_too_large_returns_none(self):
     starting = "10.10.10.0/255.255.279.0"
     self.assertEqual(ips_in_subnet(starting), None)
Esempio n. 4
0
 def test_invalid_slash_returns_none(self):
     starting = "10.9.8.0/-3"
     self.assertEqual(ips_in_subnet(starting), None)
Esempio n. 5
0
 def test_invalid_ips_returns_none(self):
     starting = "10.10.256.10/24"
     self.assertEqual(ips_in_subnet(starting), None)
Esempio n. 6
0
 def test_invalid_subnet_returns_none(self):
     starting = "10.10.10.0/255.255.-2.0"
     self.assertEqual(ips_in_subnet(starting), None)
Esempio n. 7
0
 def test_slash_thirtytwo(self):
     starting = "192.168.16.5/32"
     should_be = ["192.168.16.5"]
     self.assertEqual(ips_in_subnet(starting), should_be)
Esempio n. 8
0
 def test_expanded_slash_thirty_two(self):
     starting = "10.16.255.16/255.255.255.255"
     should_be = ["10.16.255.16"]
     self.assertEqual(ips_in_subnet(starting), should_be)
Esempio n. 9
0
def test_example_networks(network, expected):
    """Test some example network exact conversions."""

    assert ips_in_subnet(network) == expected
Esempio n. 10
0
def test_invalid_returns_none(ipaddr):
    assert ips_in_subnet(ipaddr) is None
Esempio n. 11
0
 def test_simple_small(self):
     subnet = "10.0.0.0/30"
     should_be = ["10.0.0.1", "10.0.0.2"]
     self.assertEqual(ips_in_subnet(subnet), should_be)
Esempio n. 12
0
 def test_expanded_subnet(self):
     starting = "10.17.29.128/255.255.255.252"
     self.assertIn("10.17.29.130", ips_in_subnet(starting))
Esempio n. 13
0
def test_invalid_returns_none(ipaddr):
    assert ips_in_subnet(ipaddr) is None
Esempio n. 14
0
def test_in_network(ipaddr, network):
    """Ensure the ipaddress is in the network via ips_in_subnet."""

    assert ipaddr in ips_in_subnet(network)
Esempio n. 15
0
def test_example_networks(network, expected):
    """Test some example network exact conversions."""

    assert ips_in_subnet(network) == expected
Esempio n. 16
0
 def test_oversized_slash_returns_none(self):
     starting = "10.9.8.0/34"
     self.assertEqual(ips_in_subnet(starting), None)
Esempio n. 17
0
 def test_big_network(self):
     starting = "1.2.3.4"
     members = ips_in_subnet("{0}/16".format(starting))
     self.assertIn(starting, members)
Esempio n. 18
0
 def test_ip_with_no_mask_returns_none(self):
     starting = "1.2.3.4"
     members = ips_in_subnet(starting)
     self.assertEqual(members, None)
Esempio n. 19
0
def test_in_network(ipaddr, network):
    """Ensure the ipaddress is in the network via ips_in_subnet."""

    assert ipaddr in ips_in_subnet(network)