Example #1
0
    def add_destination_cluster(self, destination_cluster):
        consul_api = ConsulAPI()
        # check if a destination cluster with the same name already exists and if so raise an exception
        dest_cluster = consul_api.get_replication_destination_cluster(
            destination_cluster.cluster_name)
        if dest_cluster:
            raise ReplicationException(
                ReplicationException.DESTINATION_CLUSTER_EXIST,
                "This destination cluster is already exist.")

        # validate destination cluster name

        # from PetaSAN.backend.replication.manage_remote_replication import ManageRemoteReplication
        # manage_remote_replication = ManageRemoteReplication()

        cluster_name = self.get_dest_cluster_name(destination_cluster)
        if cluster_name != destination_cluster.cluster_name:
            raise ReplicationException(ReplicationException.WRONG_CLUSTER_NAME,
                                       "Wrong destination cluster name.")

        # add cluster fsid to the cluster entity
        # manage_remote_replication.cluster_name = destination_cluster.cluster_name

        destination_cluster.cluster_fsid = self.get_dest_cluster_fsid(
            destination_cluster)

        # encrypt the private key using RSA algorithm before saving in consul
        private_key = destination_cluster.ssh_private_key
        rsa_encrypt = RSAEncryption()
        pub_key = rsa_encrypt.get_key(rsa_encrypt.pub_key_path)
        encrypted_key = rsa_encrypt.encrypt_public(private_key, pub_key)
        destination_cluster.ssh_private_key = encrypted_key

        # save destination cluster entity in consul
        consul_api.update_replication_destination_cluster(destination_cluster)
Example #2
0
    def delete_ceph_user(self, user_name):
        status = True
        config = configuration()
        cluster_name = config.get_cluster_name()

        cmd = "ceph auth del client.{} --cluster {}".format(
            user_name, cluster_name)
        ret, stdout, stderr = exec_command_ex(cmd)

        if ret != 0:
            if stderr:
                if 'does not exist' in stderr:
                    logger.error('Error in Ceph Connection cmd:' + cmd)
                    raise ReplicationException(
                        ReplicationException.CEPH_USER_DOES_NOT_EXIST,
                        'UserNotExist')

                if 'Connection timed out' in stderr or 'error connecting' in stderr:
                    logger.error('Error in Ceph Connection cmd:' + cmd)
                    raise CephException(CephException.CONNECTION_TIMEOUT,
                                        'ConnectionTimeError')

            logger.error('General error in Ceph cmd:' + cmd)
            raise CephException(CephException.GENERAL_EXCEPTION,
                                'GeneralCephException')

        keyring_file = "/etc/ceph/" + cluster_name + ".client." + user_name + ".keyring"

        if os.path.exists(keyring_file):
            os.remove(keyring_file)

        return status
Example #3
0
    def add_replication_sys_user(self, user_name, user_id):
        cmd = "getent group {}".format(self.group)
        ret, out, err = exec_command_ex(cmd)
        if ret != 0:
            cmd2 = "groupadd -g {} {}".format(self.gid, self.group)
            ret2, out2, err2 = exec_command_ex(cmd2)
            if ret2 != 0 and err2:
                logger.error("Error Creating the {} group ".format(self.group))

        cmd3 = "useradd {} -s /bin/bash -md /home/{} -g {} -u {}".format(
            user_name, user_name, self.group, user_id)
        ret3, out3, err3 = exec_command_ex(cmd3)
        if ret3 != 0 and err3 and 'already exists' in err3:
            raise ReplicationException(ReplicationException.SYSTEM_USER_EXIST,
                                       'ThisSystemUserAlreadyExists:')
Example #4
0
    def delete_replication_dest_cluster(self, dest_cluster):
        consul_api = ConsulAPI()
        used_in_replication = 0

        # Get All Replication Jobs #
        consul_api = ConsulAPI()
        replication_jobs = consul_api.get_replication_jobs()

        for key, value in replication_jobs.iteritems():
            if value.destination_cluster_name == dest_cluster.cluster_name:
                used_in_replication += 1

        if used_in_replication > 0:
            raise ReplicationException(
                ReplicationException.DESTINATION_CLUSTER_USED_IN_REPLICATION,
                "This destination cluster is already used in replication job.")

        consul_api.delete_replication_destination_cluster(dest_cluster)
Example #5
0
    def add_replication_job(self, job_entity, src_disk_meta):
        consul_api = ConsulAPI()
        jobs = consul_api.get_replication_jobs()

        for job_id, job in jobs.items():
            if job.job_name == job_entity.job_name:
                raise ReplicationException(ReplicationException.DUPLICATE_NAME, "Duplicate replication job name error.")

        manage_remote_replication = ManageRemoteReplication()

        manage_remote_replication.disk_id = job_entity.destination_disk_id
        manage_remote_replication.cluster_name = job_entity.destination_cluster_name
        job_id = consul_api.get_next_job_id()

        source_fsid = ceph_disk.get_fsid(configuration().get_cluster_name())
        job_entity.job_id = job_id
        job_entity.source_cluster_fsid = source_fsid

        src_disk_meta.replication_info["src_cluster_fsid"] = source_fsid

        mng_rep_info = ManageDiskReplicationInfo()
        src_disk_meta = mng_rep_info.set_replication_info(job_entity.destination_cluster_name, src_disk_meta)

        replication_info = src_disk_meta.replication_info

        # update source and destination disks meta.
        manage_remote_replication.update_replication_info(replication_info)
        mng_rep_info.update_replication_info(src_disk_meta, replication_info)
        system_date_time = str(datetime.datetime.now()).split('.')[0]

        # save job in consul
        consul_api.update_replication_job(job_entity)

        # Saving log in Consul :
        log_text = "{} - Job {} has been created.".format(system_date_time, job_id)
        self.log_replication_job(job_id, log_text)

        # start replication job:
        self.start_replication_job(job_entity)
    def add_user(self, user_name, auth_pools):
        backup_nodes_list = []
        nodes_list = ManageNode().get_node_list()
        for node_info in nodes_list:
            if node_info.is_backup:
                backup_nodes_list.append(node_info.name)

        user_info = ConsulAPI().get_replication_user(user_name)
        if user_info and len(user_info.user_name) > 0:
            raise ReplicationException(
                ReplicationException.SYSTEM_USER_EXIST,
                'ThisSystemUserAlreadyExistsInNodes:{}'.format(
                    backup_nodes_list))

        user = Users()
        ceph_usr_stat = user.is_ceph_user_exist(user_name)
        if ceph_usr_stat:
            raise ReplicationException(ReplicationException.CEPH_USER_EXIST,
                                       'ThisCephUserAlreadyExists')

        cluster_name = configuration().get_cluster_name()
        ceph_keyring_path = '/etc/ceph/{}.client.{}.keyring'.format(
            cluster_name, user_name)

        replication_user = ReplicationUser()
        replication_user.user_name = user_name
        replication_user.auth_pools = auth_pools

        rsa_encrypt = RSAEncryption()
        rsa_pub_key = rsa_encrypt.get_key(rsa_encrypt.pub_key_path)

        user.add_ceph_user(user_name, auth_pools)

        ceph_keyring_value = self.get_file_content(ceph_keyring_path)
        enc_ceph_keyring = rsa_encrypt.encrypt_public(ceph_keyring_value,
                                                      rsa_pub_key)
        replication_user.ceph_keyring = enc_ceph_keyring

        pub_file = ConfigAPI().get_replication_user_pubkey_file_path()
        prv_file = ConfigAPI().get_replication_user_prvkey_file_path()
        rep_path = ConfigAPI().get_replication_tmp_file_path()

        if not os.path.exists(rep_path):
            os.mkdir(rep_path)

        user.generate_tmp_ssh_keys(pub_file, prv_file)

        pub_key_value = self.get_file_content(pub_file)
        replication_user.ssh_pub_key = pub_key_value

        prv_key_value = self.get_file_content(prv_file)
        enc_prv_key = rsa_encrypt.encrypt_public(prv_key_value, rsa_pub_key)
        replication_user.ssh_prv_key = enc_prv_key

        mng_file = ManageTmpFile()
        mng_file.delete_tmp_file(pub_file)
        mng_file.delete_tmp_file(prv_file)

        consul = ConsulAPI()
        consul.update_replication_user(replication_user)

        for node in backup_nodes_list:
            stat = self.sync_users(node)
            if not stat:
                logger.error("error sync users on the node {}".format(node))
Example #7
0
    def get_dest_cluster_fsid(self, dest_cluster):
        mng_file = ManageTmpFile()

        # Get destination cluster info #
        # ---------------------------- #
        dest_user_name = dest_cluster.user_name
        dest_cluster_ip = dest_cluster.remote_ip
        decrypted_key = dest_cluster.ssh_private_key

        # Save private key in text file #
        # ----------------------------- #
        config_api = ConfigAPI()
        directory_path = config_api.get_replication_tmp_file_path()

        if not os.path.exists(directory_path):
            os.makedirs(directory_path)

        sshkey_path = config_api.get_replication_sshkey_file_path()
        mng_file.create_tmp_file(sshkey_path, decrypted_key)

        # Change mod of 'sshkey_path' file #
        # -------------------------------- #
        cmd = "chmod 600 {}".format(sshkey_path)
        ret, out, err = exec_command_ex(cmd)

        # Run script remotely at destination cluster as follows #
        # ----------------------------------------------------- #
        script_file = ConfigAPI().get_replication_script_path()
        parser_key = "cluster-fsid"  # Define parser key

        # Define cmd command
        cmd = 'ssh -o StrictHostKeyChecking=no -i {} {}@{} "{} {}"'.format(
            sshkey_path, dest_user_name, dest_cluster_ip, script_file,
            parser_key)

        ret, stdout, stderr = exec_command_ex(cmd)

        # Delete 'sshkey_path' file #
        # ------------------------- #
        mng_file.delete_tmp_file(sshkey_path)

        if ret != 0:
            if stderr and ('Connection timed out' in stderr):
                logger.error(
                    'Manage Destination Cluster | Connection timed out , Error = '
                    + str(stderr))
                raise ReplicationException(
                    ReplicationException.CONNECTION_TIMEOUT,
                    'Connection timed out')

            elif stderr and ('Connection refused' in stderr):
                logger.error(
                    'Manage Destination Cluster | Connection refused , Error = '
                    + str(stderr))
                raise ReplicationException(
                    ReplicationException.CONNECTION_REFUSED,
                    'Connection refused')

            elif stderr and ('Permission denied' in stderr):
                logger.error(
                    'Manage Destination Cluster | Permission denied , Error = '
                    + str(stderr))
                raise ReplicationException(
                    ReplicationException.PERMISSION_DENIED,
                    'Permission denied')

            elif stderr and ("warning" not in stderr.lower()):
                logger.error(
                    'Manage Destination Cluster | Cannot get destination cluster fsid , Error = '
                    + str(stderr))
                raise ReplicationException(
                    ReplicationException.GENERAL_EXCEPTION,
                    'Cannot get destination cluster fsid')

            elif stdout and ('Error' in stdout):
                logger.error(
                    'Manage Destination Cluster | Cannot get destination cluster fsid.'
                )
                raise Exception(str(stdout))

            else:
                logger.error(
                    'Manage Destination Cluster | Cannot get destination cluster fsid.'
                )
                raise ReplicationException(
                    ReplicationException.GENERAL_EXCEPTION,
                    'Cannot get destination cluster fsid')

        if stdout and ('Error' in stdout):
            logger.error(
                'Manage Destination Cluster | Cannot get destination cluster fsid.'
            )
            raise ReplicationException(ReplicationException.GENERAL_EXCEPTION,
                                       'Cannot get destination cluster fsid')

        if stderr and ("warning" not in stderr.lower()):
            logger.error(
                'Manage Destination Cluster | Cannot get destination cluster fsid , Error = '
                + str(stderr))
            raise ReplicationException(ReplicationException.GENERAL_EXCEPTION,
                                       'Cannot get destination cluster fsid')

        stdout_json = json.loads(str(stdout))
        return stdout_json