summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/glusterdir.py
blob: 50c37c7154f61c654530c8b70ed6e38f4f6769a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
#  Copyright (C) 2018 Red Hat, Inc. <http://www.redhat.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""Description: Module for library gluster dir class and related functions

A GlusterDir is a file object that exists on the client and backend brick.
This module provides low-level functions and a GlusterDir class to maintain
state and manage properties of a file in both locations.

GlusterDir inherits from GlusterFile.
"""

from glusto.core import Glusto as g

from glustolibs.gluster.glusterfile import GlusterFile


def mkdir(host, fqpath, parents=False, mode=None):
    """Create a directory or path of directories.

    Args:
        host (str): The hostname/ip of the remote system.
        fqpath (str): The fully-qualified path to the file.
        parents (Bool, optional): create parent directories if do not exist.
        mode (str, optional): the initial mode of the directory.

    Returns:
        True on success.
        False on failure.
    """
    command_list = ['mkdir']
    if parents:
        command_list.append('-p')
    if mode is not None:
        command_list.append('-m %s' % mode)
    command_list.append(fqpath)
    rcode, _, rerr = g.run(host, ' '.join(command_list))

    if rcode == 0:
        return True

    g.log.error("Directory mkdir failed: %s" % rerr)
    return False


def rmdir(host, fqpath, force=False):
    """Remove a directory.

    Args:
        host (str): The hostname/ip of the remote system.
        fqpath (str): The fully-qualified path to the file.
        force (bool, optional): Remove directory with recursive file delete.

    Returns:
        True on success. False on failure.
    """
    command_list = ['rmdir']
    if force:
        command_list = ["rm"]
        command_list.append('-rf')
    command_list.append(fqpath)
    rcode, _, rerr = g.run(host, ' '.join(command_list))

    if rcode == 0:
        return True

    g.log.error("Directory remove failed: %s" % rerr)
    return False


class GlusterDir(GlusterFile):
    """Class to handle directories specific to Gluster (client and backend)"""
    def mkdir(self, parents=False, mode=None):
        """mkdir the instance fqpath on the remote host.

        Args:
            parents (Bool, optional): create parent directories
                if they do not exist.
            mode (str, optional): the initial mode of the directory.

        Returns:
            True on success.
            False on failure.
        """
        if not self.exists_on_client:
            ret = mkdir(self._host, self._fqpath, parents, mode)
            if ret:
                return True

        return False

    def create(self):
        """Creates the directory and parent directories if they do not exist.
            Overrides GlusterFile.create() to handle directories.

        Args:
            None

        Returns:
            True on success. False on failure.
        """
        # TODO: extend with additional methods to create directories as needed
        return self.mkdir(parents=True, mode=None)