Exemple #1
0
    def __init__(self,
                 host,
                 host_ip=None,
                 ssh_user=None,
                 ssh_port=None,
                 ssh_options=None):
        config_helper = ConfigHelper(host)
        self._cluster_interface = config_helper.get_cluster_interface()
        self._writer_vip_cidr = config_helper.get_writer_vip_cidr()
        self._writer_vip = config_helper.get_writer_vip()
        self._requires_sudo = config_helper.get_requires_sudo()
        self._requires_arping = config_helper.get_requires_arping()

        self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port,
                                     ssh_options)
    def __init__(self, host, host_ip=None, ssh_user=None, ssh_port=None, ssh_options=None):
        config_helper = ConfigHelper(host)
        self._cluster_interface = config_helper.get_cluster_interface()
        self._writer_vip_cidr = config_helper.get_writer_vip_cidr()
        self._writer_vip = config_helper.get_writer_vip()
        self._requires_sudo = config_helper.get_requires_sudo()

        self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port, ssh_options)
class MySQLConfigHelper(object):
    def __init__(self, host, host_ip=None, ssh_user=None, ssh_port=None, ssh_options=None):
        config_helper = ConfigHelper(host)
        self._read_only_config = config_helper.get_read_only_config_file()
        self._super_read_only = config_helper.get_super_read_only()
        self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port, ssh_options)

    def generate_read_only_config_content(self, enabled=True):
        if enabled:
            if self._super_read_only == "no":
                return "# generated by mha_helper, do not touch\n[mysqld]\nread_only=on\n"
            else:
                return "# generated by mha_helper, do not touch\n[mysqld]\nsuper_read_only=on\n"

        if not enabled:
            if self._super_read_only == "no":
                return "# generated by mha_helper, do not touch\n[mysqld]\nread_only=off\n"
            else:
                return "# generated by mha_helper, do not touch\n[mysqld]\nsuper_read_only=off\n"

    def set_read_only_config(self):
        command = "echo '%s' > %s" % (self.generate_read_only_config_content(True), self._read_only_config)
        return self.execute_ssh_command(command)

    def unset_read_only_config(self):
        command = "echo '%s' > %s" % (self.generate_read_only_config_content(False), self._read_only_config)
        return self.execute_ssh_command(command)

    def execute_ssh_command(self, command, return_output=False):
        # Connect to the host over SSH
        if not self._ssh_client.make_ssh_connection():
            return False

        # Send Command to the Host
        ret_code, stdout_lines = self._ssh_client.execute_ssh_command(command)
        if not ret_code:
            if len(stdout_lines) > 0:
                print("Command output: %s" % "\n".join(stdout_lines))
                return False
        if return_output:
            return "\n".join(stdout_lines)
class VIPMetalHelper(object):
    IP_CMD = "/sbin/ip"
    ARPING_CMD = "/usr/sbin/arping"

    def __init__(self, host, host_ip=None, ssh_user=None, ssh_port=None, ssh_options=None):
        config_helper = ConfigHelper(host)
        self._cluster_interface = config_helper.get_cluster_interface()
        self._writer_vip_cidr = config_helper.get_writer_vip_cidr()
        self._writer_vip = config_helper.get_writer_vip()
        self._requires_sudo = config_helper.get_requires_sudo()
        self._requires_arping = config_helper.get_requires_arping()

        self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port, ssh_options)

    def assign_vip(self):
        ip_cmd = "%s addr add %s dev %s" % (VIPMetalHelper.IP_CMD, self._writer_vip_cidr, self._cluster_interface)
        arping_cmd = "%s -q -c 3 -A -I %s %s" % (VIPMetalHelper.ARPING_CMD, self._cluster_interface, self._writer_vip)

        if self._requires_sudo:
            ip_cmd = "sudo %s" % ip_cmd
            arping_cmd = "sudo %s" % arping_cmd

        # Connect to the host over SSH
        if not self._ssh_client.make_ssh_connection():
            return False

        # Assign the VIP to the host
        ret_code, stdout_lines = self._ssh_client.execute_ssh_command(ip_cmd)
        if not ret_code:
            if len(stdout_lines) > 0:
                print("Command output: %s" % "\n".join(stdout_lines))
            return False

        # Send ARP update requests to all the listening hosts
        if self._requires_arping:
            ret_code, stdout_lines = self._ssh_client.execute_ssh_command(arping_cmd)
            if not ret_code:
                if len(stdout_lines) > 0:
                    print("Command output: %s" % "\n".join(stdout_lines))
                return False

        return True

    def remove_vip(self):
        ip_cmd = "%s addr delete %s dev %s" % (VIPMetalHelper.IP_CMD, self._writer_vip_cidr, self._cluster_interface)
        if self._requires_sudo:
            ip_cmd = "sudo %s" % ip_cmd

        # Connect to the host over SSH
        if not self._ssh_client.make_ssh_connection():
            return False

        # Remove the VIP from the host
        ret_code, stdout_lines = self._ssh_client.execute_ssh_command(ip_cmd)
        if not ret_code:
            if len(stdout_lines) > 0:
                print("Command output: %s" % "\n".join(stdout_lines))
            return False

        return True

    def has_vip(self):
        ip_cmd = "%s addr show dev %s" % (VIPMetalHelper.IP_CMD, self._cluster_interface)
        if self._requires_sudo:
            ip_cmd = "sudo %s" % ip_cmd

        # Connect to the host over SSH
        if not self._ssh_client.make_ssh_connection():
            return False

        # Fetch the output of the command `ip addr show dev eth` and parse it to list the IP addresses
        # If the VIP is in that list then that means the VIP is assigned to the host
        ret_code, stdout_lines = self._ssh_client.execute_ssh_command(ip_cmd)
        if not ret_code:
            if len(stdout_lines) > 0:
                print("Command output: %s" % "\n".join(stdout_lines))
            return False

        vip_found = False
        for line in stdout_lines:
            # We want to match a line similar to the following:
            #   inet 192.168.30.11/24 brd 192.168.30.255 scope global eth1
            if re.search(r'\b(inet|inet6)\b', line):
                # The second element of the matching line is the IP address in CIDR format
                if line.split()[1] == self._writer_vip_cidr:
                    vip_found = True
                    break

        return vip_found
 def __init__(self, host, host_ip=None, ssh_user=None, ssh_port=None, ssh_options=None):
     config_helper = ConfigHelper(host)
     self._read_only_config = config_helper.get_read_only_config_file()
     self._super_read_only = config_helper.get_super_read_only()
     self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port, ssh_options)
Exemple #6
0
class VIPMetalHelper(object):
    IP_CMD = "/sbin/ip"
    ARPING_CMD = "/usr/sbin/arping"

    def __init__(self,
                 host,
                 host_ip=None,
                 ssh_user=None,
                 ssh_port=None,
                 ssh_options=None):
        config_helper = ConfigHelper(host)
        self._cluster_interface = config_helper.get_cluster_interface()
        self._writer_vip_cidr = config_helper.get_writer_vip_cidr()
        self._writer_vip = config_helper.get_writer_vip()
        self._requires_sudo = config_helper.get_requires_sudo()
        self._requires_arping = config_helper.get_requires_arping()

        self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port,
                                     ssh_options)

    def assign_vip(self):
        ip_cmd = "%s addr add %s dev %s" % (VIPMetalHelper.IP_CMD,
                                            self._writer_vip_cidr,
                                            self._cluster_interface)
        arping_cmd = "%s -q -c 3 -A -I %s %s" % (VIPMetalHelper.ARPING_CMD,
                                                 self._cluster_interface,
                                                 self._writer_vip)

        if self._requires_sudo:
            ip_cmd = "sudo %s" % ip_cmd
            arping_cmd = "sudo %s" % arping_cmd

        # Connect to the host over SSH
        if not self._ssh_client.make_ssh_connection():
            return False

        # Assign the VIP to the host
        ret_code, stdout_lines = self._ssh_client.execute_ssh_command(ip_cmd)
        if not ret_code:
            if len(stdout_lines) > 0:
                print("Command output: %s" % "\n".join(stdout_lines))
            return False

        # Send ARP update requests to all the listening hosts
        if self._requires_arping:
            ret_code, stdout_lines = self._ssh_client.execute_ssh_command(
                arping_cmd)
            if not ret_code:
                if len(stdout_lines) > 0:
                    print("Command output: %s" % "\n".join(stdout_lines))
                return False

        return True

    def remove_vip(self):
        ip_cmd = "%s addr delete %s dev %s" % (VIPMetalHelper.IP_CMD,
                                               self._writer_vip_cidr,
                                               self._cluster_interface)
        if self._requires_sudo:
            ip_cmd = "sudo %s" % ip_cmd

        # Connect to the host over SSH
        if not self._ssh_client.make_ssh_connection():
            return False

        # Remove the VIP from the host
        ret_code, stdout_lines = self._ssh_client.execute_ssh_command(ip_cmd)
        if not ret_code:
            if len(stdout_lines) > 0:
                print("Command output: %s" % "\n".join(stdout_lines))
            return False

        return True

    def has_vip(self):
        ip_cmd = "%s addr show dev %s" % (VIPMetalHelper.IP_CMD,
                                          self._cluster_interface)
        if self._requires_sudo:
            ip_cmd = "sudo %s" % ip_cmd

        # Connect to the host over SSH
        if not self._ssh_client.make_ssh_connection():
            return False

        # Fetch the output of the command `ip addr show dev eth` and parse it to list the IP addresses
        # If the VIP is in that list then that means the VIP is assigned to the host
        ret_code, stdout_lines = self._ssh_client.execute_ssh_command(ip_cmd)
        if not ret_code:
            if len(stdout_lines) > 0:
                print("Command output: %s" % "\n".join(stdout_lines))
            return False

        vip_found = False
        for line in stdout_lines:
            # We want to match a line similar to the following:
            #   inet 192.168.30.11/24 brd 192.168.30.255 scope global eth1
            # or
            #   inet6 fe80::a00:27ff:fed8:f757/64 scope link
            if re.search(r'\b(inet|inet6)\b', line):
                # The second element of the matching line is the IP address in CIDR format
                if line.split()[1] == self._writer_vip_cidr:
                    vip_found = True
                    break

        return vip_found