Пример #1
0
def mount(current_storage_data):
    """ A function that is responsible for directly mounting a particular storage.
    The input receives a dictionary containing the necessary data for connecting storage.

    """

    try:
        (data_mount, pre_mount) = get_mount_data(current_storage_data)
    except MountError as e:
        raise general_function.MyError(f"{e}")

    if not data_mount:
        # if local storage
        return
    else:
        type_storage = data_mount.get('type_storage')
        packets = data_mount.get('packets')
        check_cmd = data_mount.get('check_cmd')
        mount_cmd = data_mount.get('mount_cmd')

        for i in packets:
            if i:
                check_packet = general_function.exec_cmd(f"{check_cmd} {i}")
                stdout_check = check_packet['stdout']

                if not stdout_check:
                    raise general_function.MyError(
                        f"Required package '{i}' not installed!")
            else:
                continue

        if pre_mount:
            for key in pre_mount:
                try:
                    f = globals()[key]
                    args = pre_mount[key]
                    f(args)
                except Exception as err:
                    raise general_function.MyError(
                        f"Impossible perform pre-mount operations for storage '{type_storage}': {err}"
                    )

        check_mount_cmd = f"mount | grep {mount_point}"
        check_mount = general_function.exec_cmd(check_mount_cmd)
        stdout_mount = check_mount['stdout']

        if stdout_mount:
            if mount_point == '/mnt/sshfs':
                remote_mount = stdout_mount.split()[0]
                if remote_mount not in mount_cmd:
                    raise general_function.MyError(
                        f"Mount point {mount_point} is busy by different remote resource! "
                        f"Requested mount: {mount_cmd}. "
                        f"Current mount: {stdout_mount}.")
            else:
                raise general_function.MyError(
                    f"Mount point {mount_point} is busy!")
        else:
            general_function.create_dirs(job_name='',
                                         dirs_pairs={mount_point: ''})
            data_mounting = general_function.exec_cmd(f"{mount_cmd}")
            stderr_mounting = data_mounting['stderr']
            code = data_mounting['code']

            if stderr_mounting:
                raise general_function.MyError(stderr_mounting)

            if code != 0:
                raise general_function.MyError(
                    f"Bad result code external process '{mount_cmd}':'{code}'")

            if type_storage == 's3':
                try:
                    os.chdir('/mnt/s3')
                except ConnectionAbortedError:
                    raise general_function.MyError(
                        "incorrect authentification data!")
                else:
                    os.chdir(
                        '/'
                    )  # fix error 'getcwd: cannot access parent directories: No such file or directory'

    return
Пример #2
0
def set_cgroup(group, *args):
    pid = os.getpid()

    data_1 = general_function.exec_cmd(f"cat /proc/cgroups | grep {group}")
    stdout_1 = data_1['stdout']
    if not stdout_1:
        log_and_mail.writelog('WARNING', f"Your kernel doesn't support cgroup '{group}'.",
                              config.filelog_fd)
        return False

    data_2 = general_function.exec_cmd('mount | grep "/sys/fs/cgroup"')
    stdout_2 = data_2['stdout']

    if not stdout_2:
        general_function.exec_cmd(
            'mount -t tmpfs -o rw,nosuid,nodev,noexec,relatime,size=0k cgroup_root /sys/fs/cgroup/')
    _dir = f'/sys/fs/cgroup/{group}'

    data_3 = general_function.exec_cmd(f'mount | grep "{_dir}"')
    stdout_3 = data_3['stdout']

    if not (os.path.isdir(_dir) or not stdout_3):
        general_function.create_dirs(job_name='', dirs_pairs={_dir: ''})
        general_function.exec_cmd(f'mount -t cgroup -o rw,nosuid,nodev,noexec,relatime,{group} cgroup_{group} {_dir}/')

    general_function.create_dirs(job_name='', dirs_pairs={f'{_dir}/nixys_backup': ''})

    args_list = list(args)
    for index in args_list:
        if not os.path.isfile(os.path.join(_dir, index)):
            log_and_mail.writelog('WARNING', f"Your kernel does not support option '{index}' in subsystem '{group}'.",
                                  config.filelog_fd)
            return False
        _parametr = -100

        if group == 'blkio':
            directory_for_tmp_file = config.general_path_to_all_tmp_dir

            general_function.create_dirs(job_name='', dirs_pairs={directory_for_tmp_file: ''})

            data_4 = general_function.exec_cmd(f"df {directory_for_tmp_file} | tail -1 | awk '{{print $1}}'")
            stdout_4 = data_4['stdout']

            if re.match("/dev/disk/(by-id|by-path|by-uuid)", stdout_4):
                data_5 = general_function.exec_cmd(f"ls -l {stdout_4} | awk '{{print $11}}'")
                stdout_5 = data_5['stdout']
                device = os.path.basename(stdout_5)
            else:
                device = stdout_4

            raid = True
            if not re.match(".*/(md|dm).+", device):
                raid = False
                while re.match("^[0-9]$", str(device[len(device) - 1])):
                    device = device[0:-1]

            data_6 = general_function.exec_cmd(f"ls -l {device} | awk '{{print $5}}'")
            stdout_6 = data_6['stdout']
            major_device = stdout_6[0:-1]

            data_7 = general_function.exec_cmd(f"ls -l {device} | awk '{{print $6}}'")
            stdout_7 = data_7['stdout']
            minor_device = stdout_7

            if index != 'blkio.weight_device':
                if index == 'blkio.throttle.write_bps_device':
                    if not re.match("^([0-9]*)$", config.block_io_write, re.I):
                        log_and_mail.writelog(
                            'WARNING',
                            "Incorrect data in field 'block_io_write'! You must specify the write speed "
                            "in MB/s using only numbers!",
                            config.filelog_fd)
                        return False
                    _parametr = 1024 * 1024 * int(config.block_io_write)
                else:
                    if not re.match("^([0-9]*)$", config.block_io_read, re.I):
                        log_and_mail.writelog(
                            'WARNING',
                            "Incorrect data in field 'block_io_read'! You must specify the read speed "
                            "in MB/s using only numbers!",
                            config.filelog_fd)
                        return False
                    _parametr = 1024 * 1024 * int(config.block_io_read)

            else:
                if not raid:
                    if not (re.match("^([0-9]*)$", config.block_io_weight, re.I) and
                            100 <= int(config.block_io_weight) <= 1000):
                        log_and_mail.writelog(
                            'WARNING',
                            "Incorrect data in field 'blkio_weight'! Process must specify weight "
                            "in the range from 100 to 1000!",
                            config.filelog_fd)
                        return False
                    _parametr = config.block_io_weight
                else:
                    log_and_mail.writelog(
                        'WARNING', "You can not use option 'blkio.weight_device' with the raid!",
                        config.filelog_fd)
                    return False

            general_function.exec_cmd(f'echo {major_device}:{minor_device} {_parametr} > {_dir}/nixys_backup/{index}')

            data_8 = general_function.exec_cmd(f"cat {_dir}/nixys_backup/{index}")
            stdout_8 = data_8['stdout']
            _flag = stdout_8

            if len(_flag) < 3:
                log_and_mail.writelog('WARNING', f"Incorrect data in file '{_dir}/nixys_backup/{index}'!",
                                      config.filelog_fd)
                return False

        if group == 'cpu':
            if index == 'cpu.shares':
                if not re.match("^([0-9]*)$", config.cpu_shares, re.I):
                    log_and_mail.writelog(
                        'WARNING',
                        "Incorrect data in field 'cpu_shares'! You must specify  weight "
                        "in the range from 1 to cpu_count*1000!",
                        config.filelog_fd)
                    return False

                _parametr = int(config.cpu_shares)

                general_function.exec_cmd(f"echo {_parametr} > {_dir}/nixys_backup/{index}")

                data_9 = general_function.exec_cmd(f"cat {_dir}/nixys_backup/{index}")
                stdout_9 = data_9['stdout']
                _flag = stdout_9

                if not _flag:
                    log_and_mail.writelog('WARNING', f"Incorrect data in file '{_dir}/nixys_backup/{index}'!",
                                          config.filelog_fd)
                    return False

    general_function.exec_cmd(f"echo {pid} > {_dir}/nixys_backup/tasks")
    return True
Пример #3
0
def set_cgroup(group, *args):

    PID = os.getpid()

    data_1 = general_function.exec_cmd("cat /proc/cgroups | grep %s" %(group))
    stdout_1 = data_1['stdout']
    if not stdout_1:
        log_and_mail.writelog('WARNING', "Your kernel doesn't support cgroup '%s'." %(group),
                 config.filelog_fd)
        return False

    data_2 = general_function.exec_cmd('mount | grep "/sys/fs/cgroup"')
    stdout_2 = data_2['stdout']

    if not stdout_2:
        general_function.exec_cmd('mount -t tmpfs -o rw,nosuid,nodev,noexec,relatime,size=0k cgroup_root /sys/fs/cgroup/')
    _dir = '/sys/fs/cgroup/%s' %(group)

    data_3 = general_function.exec_cmd('mount | grep "%s"' %(_dir))
    stdout_3 = data_3['stdout']

    if not (os.path.isdir(_dir) or not stdout_3):
        general_function.create_dirs(job_name='', dirs_pairs={_dir:''})
        general_function.exec_cmd('mount -t cgroup -o rw,nosuid,nodev,noexec,relatime,%s cgroup_%s %s/' %(group, group, _dir))

    general_function.create_dirs(job_name='', dirs_pairs={'%s/nixys_backup' %(_dir):''})

    l = list(args)
    for index in l:
        if not os.path.isfile(os.path.join(_dir, index)):
            log_and_mail.writelog('WARNING',"Your kernel does not support option '%s' in subsystem '%s'." %(index, group),
                     config.filelog_fd)
            return False
        _parametr = -100

        if group == 'blkio':
            device = ''
            DIRECTORY_FOR_TMP_FILE = config.general_path_to_all_tmp_dir

            general_function.create_dirs(job_name='', dirs_pairs={DIRECTORY_FOR_TMP_FILE:''})

            data_4 = general_function.exec_cmd("df %s | tail -1 | awk '{{print $1}}'" %(DIRECTORY_FOR_TMP_FILE))
            stdout_4 = data_4['stdout']

            if re.match("/dev/disk/(by-id|by-path|by-uuid)", stdout_4):
                data_5 = general_function.exec_cmd("ls -l %s | awk '{{print $11}}'" %(stdout_4))
                stdout_5 = data_5['stdout']
                device = os.path.basename(stdout_5)
            else:
                device = stdout_4

            raid = True
            if not re.match(".*/(md|dm).+", device):
                raid = False
                while re.match("^[0-9]$", str(device[len(device)-1])):
                    device = device[0:-1]

            data_6 = general_function.exec_cmd("ls -l %s | awk '{{print $5}}'" %(device))
            stdout_6 = data_6['stdout']
            major_device = stdout_6[0:-1]

            data_7 = general_function.exec_cmd("ls -l %s | awk '{{print $6}}'" %(device))
            stdout_7 = data_7['stdout']
            minor_device = stdout_7

            if index != 'blkio.weight_device':
                if index == 'blkio.throttle.write_bps_device':
                    if not re.match("^([0-9]*)$", config.block_io_write, re.I):
                        log_and_mail.writelog('WARNING', "Incorrect data in field 'block_io_write'! You must specify the write speed in MB/s using only numbers!",
                                 config.filelog_fd)
                        return False
                    _parametr = 1024 * 1024 * int(config.block_io_write)
                else:
                    if not re.match("^([0-9]*)$", config.block_io_read, re.I):
                        log_and_mail.writelog('WARNING', "Incorrect data in field 'block_io_read'! You must specify the read speed in MB/s using only numbers!",
                                 config.filelog_fd)
                        return False
                    _parametr = 1024 * 1024 * int(config.block_io_read)

            else:
                if not raid:
                    if not (re.match("^([0-9]*)$", config.blkio_weight, re.I) and
                            int(config.blkio_weight) >= 100 and
                            int(config.blkio_weight) <= 1000):
                        log_and_mail.writelog('WARNING', "Incorrect data in field 'blkio_weight'! Process must specify weight in the range from 100 to 1000!",
                                 config.filelog_fd)
                        return False
                    _parametr = config.blkio_weight
                else:
                    log_and_mail.writelog('WARNING', "You can not use option 'blkio.weight_device' with the raid!",
                              config.filelog_fd)
                    return False

            general_function.exec_cmd('echo %s:%s %s > %s/nixys_backup/%s' %(major_device, minor_device, _parametr, _dir, index))

            data_8 = general_function.exec_cmd("cat %s/nixys_backup/%s" %(_dir, index))
            stdout_8 = data_8['stdout']
            _flag = stdout_8

            if len(_flag) < 3:
                log_and_mail.writelog('WARNING',"Incorrect data in file '%s/nixys_backup/%s'!" %(_dir, index),
                         config.filelog_fd)
                return False

        if group == 'cpu':
            if index == 'cpu.shares':
                if not re.match("^([0-9]*)$", config.cpu_shares, re.I):
                    log_and_mail.writelog('WARNING', "Incorrect data in field 'cpu_shares'! You must specify  weight in the range from 1 to cpu_count*1000!",
                             config.filelog_fd)
                    return False

                _parametr = int(config.cpu_shares)

                general_function.exec_cmd("echo %s > %s/nixys_backup/%s" %(_parametr, _dir, index))

                data_9 = general_function.exec_cmd("cat %s/nixys_backup/%s" %(_dir, index))
                stdout_9 = data_9['stdout']
                _flag = stdout_9 

                if not _flag:
                    log_and_mail.writelog('WARNING',"Incorrect data in file '%s/nixys_backup/%s'!" %(_dir, index),
                             config.filelog_fd)
                    return False

    general_function.exec_cmd("echo %s > %s/nixys_backup/tasks" %(PID, _dir))
    return True
Пример #4
0
def mount(current_storage_data):
    ''' A function that is responsible for directly mounting a particular storage.
    The input receives a dictionary containing the necessary data for connecting storage.

    '''

    try:
        (data_mount, pre_mount) = get_mount_data(current_storage_data)
    except MountError as e:
        raise general_function.MyError("%s" % e)

    if not data_mount:
        # if local storage
        return 0
    else:
        type_storage = data_mount.get('type_storage')
        packets = data_mount.get('packets')
        check_cmd = data_mount.get('check_cmd')
        mount_cmd = data_mount.get('mount_cmd')

        for i in packets:
            if i:
                check_packet = general_function.exec_cmd("%s %s" %
                                                         (check_cmd, i))
                stdout_check = check_packet['stdout']

                if not stdout_check:
                    raise general_function.MyError(
                        "Required package '%s' not installed!" % (i))
            else:
                continue

        if pre_mount:
            for key in pre_mount:
                try:
                    f = globals()[key]
                    args = pre_mount[key]
                    f(args)
                except Exception as err:
                    raise general_function.MyError(
                        "Impossible perform pre-mount operations for storage '%s': %s"
                        % (type_storage, err))

        check_mount_cmd = "mount | grep %s" % (mount_point)
        check_mount = general_function.exec_cmd(check_mount_cmd)
        stdout_mount = check_mount['stdout']

        if stdout_mount:
            raise general_function.MyError("Mount point %s is busy!" %
                                           (mount_point))
        else:
            general_function.create_dirs(job_name='',
                                         dirs_pairs={mount_point: ''})
            data_mounting = general_function.exec_cmd("%s" % (mount_cmd))
            stderr_mounting = data_mounting['stderr']
            code = data_mounting['code']

            if stderr_mounting:
                raise general_function.MyError(stderr_mounting)

            if code != 0:
                raise general_function.MyError(
                    "Bad result code external process '%s':'%s'" %
                    (mount_cmd, code))

            if type_storage == 's3':
                try:
                    os.chdir('/mnt/s3')
                except ConnectionAbortedError:
                    raise general_function.MyError(
                        "incorrect authentification data!")

    return 1
Пример #5
0
def mount(current_storage_data):
    ''' A function that is responsible for directly mounting a particular storage.
    The input receives a dictionary containing the necessary data for connecting storage.

    '''

    try:
        (data_mount, pre_mount) = get_mount_data(current_storage_data)
    except MountError as e:
        raise general_function.MyError("%s" % e)

    if not data_mount:
        # if local storage
        return 0
    else:
        packets = data_mount.get('packets')
        update_cmd = data_mount.get('update_cmd')
        check_cmd = data_mount.get('check_cmd')
        install_cmd = data_mount.get('install_cmd')
        mount_cmd = data_mount.get('mount_cmd')
        pre_install_cmd = data_mount.get('pre_install_cmd')

        if packets:
            command = general_function.exec_cmd(update_cmd)
            code = command['code']

            if code != 0:
                raise general_function.MyError(
                    "Bad result code external process '%s':'%s'" %
                    (update_cmd, code))

        for i in packets:
            check_packet = general_function.exec_cmd("%s %s" % (check_cmd, i))
            stdout_check = check_packet['stdout']

            if not stdout_check:
                if pre_install_cmd:
                    pre_install = general_function.exec_cmd(pre_install_cmd)
                    stderr_pre_install = pre_install['stderr']
                    code = pre_install['code']

                    if stderr_pre_install:
                        raise general_function.MyError(
                            "Package '%s' can't installed:%s" %
                            (i, stderr_pre_install))
                    if code != 0:
                        raise general_function.MyError(
                            "Bad result code external process '%s':'%s'" %
                            (pre_install_cmd, code))

                install_packet = general_function.exec_cmd("%s %s" %
                                                           (install_cmd, i))
                stderr_install = install_packet['stderr']
                code = install_packet['code']

                if stderr_install:
                    raise general_function.MyError(
                        "Package '%s' can't installed:%s" %
                        (i, stderr_install))

                if code != 0:
                    raise general_function.MyError(
                        "Bad result code external process '%s':'%s'" %
                        (install_cmd, code))

        if pre_mount:
            for key in pre_mount:
                try:
                    f = globals()[key]
                    args = pre_mount[key]
                    f(args)
                except Exception as err:
                    raise general_function.MyError(
                        "Impossible perform pre-mount operations for storage '%s': %s"
                        % (current_storage_data.get('storage'), err))

        check_mount_cmd = "mount | grep %s" % (mount_point)
        check_mount = general_function.exec_cmd(check_mount_cmd)
        stdout_mount = check_mount['stdout']

        if stdout_mount:
            raise general_function.MyError("Mount point %s is busy!" %
                                           (mount_point))
        else:
            general_function.create_dirs(job_name='',
                                         dirs_pairs={mount_point: ''})
            data_mounting = general_function.exec_cmd("%s" % (mount_cmd))
            stderr_mounting = data_mounting['stderr']
            code = data_mounting['code']

            if stderr_mounting:
                raise general_function.MyError(stderr_mounting)
            if code != 0:
                raise general_function.MyError(
                    "Bad result code external process '%s':'%s'" %
                    (mount_cmd, code))
    return 1