def _try_remove_nat(tfvars):
    primary_interface = tfvars.get('libvirt_network_if')
    if primary_interface is None:
        raise Exception("Could not get primary interface")
    secondary_interface = tfvars.get('libvirt_secondary_network_if',
                                     f's{primary_interface}')
    nat_controller = NatController([primary_interface, secondary_interface],
                                   _get_namespace_index(primary_interface))
    nat_controller.remove_nat_rules()
Example #2
0
 def unconfigure_nat(self):
     nat_interfaces = self.nat_interfaces
     NatController.remove_nat_rules(
         nat_interfaces, self._get_namespace_index(nat_interfaces[0]))
 def teardown_nat(nat: NatController) -> None:
     if global_variables.test_teardown and nat:
         nat.remove_nat_rules()
Example #4
0
class Nodes:
    DEFAULT_STATIC_IP_CONFIG = False

    def __init__(self, node_controller: NodeController,
                 private_ssh_key_path: str,
                 net_asset: NetworkAssets = None,
                 needs_nat: bool = False
                 ):
        self.controller = node_controller
        self.private_ssh_key_path = private_ssh_key_path
        self._needs_nat = needs_nat
        self._net_asset = net_asset
        self._nodes = None
        self._nodes_as_dict = None
        self._nat = None

    @property
    def nodes(self) -> List[Node]:
        if not self._nodes:
            self._nodes = self.controller.list_nodes()
        return self._nodes

    def __getitem__(self, i):
        return self.nodes[i]

    def __len__(self):
        return len(self.nodes)

    def __iter__(self) -> Iterator[Node]:
        for n in self.nodes:
            yield n

    def is_nat_active(self) -> bool:
        return self._needs_nat

    @property
    def nat_interfaces(self):
        return (self.controller.network_conf.libvirt_network_if,
                self.controller.network_conf.libvirt_secondary_network_if)

    @staticmethod
    def _get_namespace_index(libvirt_network_if):
        """ Hack to retrieve namespace index - does not exist in tests """
        matcher = re.match(r'^tt(\d+)$', libvirt_network_if)
        return int(matcher.groups()[0]) if matcher is not None else 0

    def release_net_asset(self):
        if self._net_asset:
            self._net_asset.release_all()

    def configure_nat(self):
        nat_interfaces = self.nat_interfaces
        self._nat = NatController(nat_interfaces, self._get_namespace_index(nat_interfaces[0]))
        self._nat.add_nat_rules()

    def unconfigure_nat(self):
        if self._nat:
            self._nat.remove_nat_rules()

    def drop_cache(self):
        self._nodes = None
        self._nodes_as_dict = None

    def get_masters(self):
        return [node for node in self.nodes if node.is_master_in_name()]

    def get_workers(self):
        return [node for node in self.nodes if node.is_worker_in_name()]

    @property
    def nodes_as_dict(self):
        if not self._nodes_as_dict:
            self._nodes_as_dict = {node.name: node for node in self.nodes}
        return self._nodes_as_dict

    @property
    def setup_time(self):
        return self.controller.setup_time

    def get_random_node(self):
        return random.choice(self.nodes)

    def shutdown_all(self):
        self.run_for_all_nodes("shutdown")

    def start_all(self, is_static_ip: bool = DEFAULT_STATIC_IP_CONFIG):
        if is_static_ip:
            skip_ips = False
        else:
            skip_ips = True
        self.run_for_all_nodes("start", skip_ips)

    def start_given(self, nodes):
        self.run_for_given_nodes(nodes, "start")

    def shutdown_given(self, nodes):
        self.run_for_given_nodes(nodes, "shutdown")

    def format_all_disks(self):
        self.run_for_all_nodes("format_disk")

    def destroy_all(self):
        self.run_for_all_nodes("shutdown")

    def destroy_all_nodes(self):
        self.controller.destroy_all_nodes()

    def prepare_nodes(self):
        self.controller.prepare_nodes()
        self.configure_nat()

    def reboot_all(self):
        self.run_for_all_nodes("restart")

    def reboot_given(self, nodes):
        self.run_for_given_nodes(nodes, "restart")

    def get_cluster_network(self):
        return self.controller.get_cluster_network()

    def set_correct_boot_order(self, nodes=None, start_nodes=False):
        nodes = nodes or self.nodes
        logging.info("Going to set correct boot order to nodes: %s", nodes)
        self.run_for_given_nodes(nodes, "set_boot_order_flow", False, start_nodes)

    def run_for_all_nodes(self, func_name, *args):
        return self.run_for_given_nodes(self.nodes, func_name, *args)

    @staticmethod
    def run_for_given_nodes(nodes, func_name, *args):
        logging.info("Running %s on nodes : %s", func_name, nodes)
        return run_concurrently([(getattr(node, func_name), *args) for node in nodes])

    def run_for_given_nodes_by_cluster_hosts(self, cluster_hosts, func_name, *args):
        return self.run_for_given_nodes([self.get_node_from_cluster_host(host) for
                                         host in cluster_hosts], func_name, *args)

    @staticmethod
    def run_ssh_command_on_given_nodes(nodes, command) -> Dict:
        return run_concurrently({node.name: (node.run_command, command) for node in nodes})

    def set_wrong_boot_order(self, nodes=None, start_nodes=True):
        nodes = nodes or self.nodes
        logging.info("Setting wrong boot order for %s", self.nodes_as_dict.keys())
        self.run_for_given_nodes(nodes, "set_boot_order_flow", True, start_nodes)

    def get_bootstrap_node(self, cluster) -> Node:
        for cluster_host_object in cluster.get_hosts():
            if cluster_host_object.get("bootstrap", False):
                node = self.get_node_from_cluster_host(cluster_host_object)
                logging.info("Bootstrap node is %s", node.name)
                return node

    def create_nodes_cluster_hosts_mapping(self, cluster):
        node_mapping_dict = {}
        for cluster_host_object in cluster.get_hosts():
            name = self.get_cluster_hostname(cluster_host_object)
            node_mapping_dict[name] = NodeMapping(self.nodes_as_dict[name],
                                                  Munch.fromDict(cluster_host_object))
        return node_mapping_dict

    def get_node_from_cluster_host(self, cluster_host_object):
        hostname = self.get_cluster_hostname(cluster_host_object)
        return self.get_node_by_hostname(hostname)

    def get_node_by_hostname(self, get_node_by_hostname):
        return self.nodes_as_dict[get_node_by_hostname]

    def get_cluster_host_obj_from_node(self, cluster, node):
        mapping = self.create_nodes_cluster_hosts_mapping(cluster=cluster)
        return mapping[node.name].cluster_host

    def get_cluster_hostname(self, cluster_host_object):
        inventory = json.loads(cluster_host_object["inventory"])
        return inventory["hostname"]

    def set_hostnames(self, cluster, nodes_count: int, is_ipv6: bool, is_static_ip: bool = False):
        if is_ipv6 or is_static_ip:
            # When using IPv6 with libvirt, hostnames are not set automatically by DHCP.  Therefore, we must find out
            # the hostnames using terraform's tfstate file. In case of static ip, the hostname is localhost and must be
            # set to valid hostname
            # TODO - NodeController has no `params` and `tf` attributes
            network_name = self.controller.params.libvirt_network_name
            libvirt_nodes = utils.get_libvirt_nodes_from_tf_state(network_name, self.controller.tf.get_state())
            utils.update_hosts(cluster.api_client, cluster.id, libvirt_nodes, update_hostnames=True,
                               update_roles=(nodes_count != 1))

    def set_single_node_ip(self, cluster):
        self.controller.tf.change_variables(
            {"single_node_ip": cluster.get_ip_for_single_node(cluster.api_client,
                                                              cluster.id, self.controller.get_machine_cidr())})
Example #5
0
 def unconfigure_nat(cls, controller):
     libvirt_network_if = controller.network_conf.libvirt_network_if
     libvirt_secondary_network_if = controller.network_conf.libvirt_secondary_network_if
     input_interfaces = [libvirt_network_if, libvirt_secondary_network_if]
     nat_controller = NatController()
     nat_controller.remove_nat_rules(input_interfaces, cls._get_namespace_index(libvirt_network_if))
def _try_remove_nat(namespace, tfvars):
    primary_interface = tfvars.get('libvirt_network_if', f'tt{namespace}')
    secondary_interface = tfvars.get('libvirt_secondary_network_if',
                                     f's{primary_interface}')
    nat_controller = NatController()
    nat_controller.remove_nat_rules([primary_interface, secondary_interface])