summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster-gd2/glustolibs/gluster/snap_ops.py
blob: 8e94e1dde20b03114606720dcf5a523dcabd2640 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#  Copyright (C) 2018-2019  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: Library for gluster snapshot operations.
"""

import json
import httplib
from glusto.core import Glusto as g
from glustolibs.gluster.rest import RestClient
from glustolibs.gluster.volume_ops import volume_start, volume_stop


def snap_create(mnode, volname, snapname, timestamp=False, description=None):
    """Creates snapshot for the given volume.

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        snapname (str): snapshot name

    Kwargs:
        timestamp (bool): If this option is set to True, then
            timestamps will get appended to the snapname. If this option
            is set to False, then timestamps will not be appended to snapname.
        description (str): description for snapshot creation

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        snap_create("abc.com", testvol, testsnap)

    """
    data = {"snapname": snapname, "volname": volname,
            "description": description, "timestamp": timestamp}
    return RestClient(mnode).handle_request("POST", "/v1/snapshots", httplib.CREATED, data)


def snap_activate(mnode, snapname):
    """Activates the given snapshot

    Args:
        mnode (str): Node on which cmd has to be executed.
        snapname (str): snapshot name to be activated

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        snap_activate("abc.com", testsnap)

    """
    return RestClient(mnode).handle_request('POST', "/v1/snapshots/%s/activate"
                                            % snapname, httplib.OK, None)


def snap_deactivate(mnode, snapname):
    """Deactivates the given snapshot

    Args:
        mnode (str): Node on which cmd has to be executed.
        snapname (str): snapshot name to be deactivated

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        snap_deactivate("abc.com", testsnap)

    """
    return RestClient(mnode).handle_request('POST',
                                            "/v1/snapshots/%s/deactivate"
                                            % snapname, httplib.OK, None)


def snap_clone(mnode, snapname, clonename):
    """Clones the given snapshot

    Args:
        mnode (str): Node on which cmd has to be executed.
        snapname (str): snapshot name to be cloned
        clonename (str): clone name

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        snap_clone("abc.com", testsnap, clone1)

    """
    data = {"clonename": clonename}
    return RestClient(mnode).handle_request('POST', "/v1/snapshots/%s/clone"
                                            % snapname, httplib.CREATED, data)


def snap_restore(mnode, snapname):
    """Snap restore for the given snapshot

    Args:
        mnode (str): Node on which cmd has to be executed.
        snapname (str): snapshot name to be cloned

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        snap_restore(mnode, testsnap)

    """
    return RestClient(mnode).handle_request('POST', "/v1/snapshots/%s/restore"
                                            % snapname, httplib.CREATED, None)


def snap_restore_complete(mnode, volname, snapname):
    """stops the volume, restore the snapshot and starts the volume

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        snapname (str): snapshot name

    Returns:
        bool: True on success, False on failure

    Example:
        snap_restore_complete(mnode, testvol, testsnap)

    """

    # Stopping volume before snap restore
    ret, _, _ = volume_stop(mnode, volname)
    if not ret:
        g.log.error("Failed to stop %s volume before restoring snapshot %s
                     in node %s", volname, snapname, mnode)
        return False

    ret, _, _ = snap_restore(mnode, snapname)
    if ret:
    g.log.error("Snapshot %s restore failed on node %s", snapname, mnode)
        return False

    # Starting volume after snap restore
    ret, _, _ = volume_start(mnode, volname)
    if not ret:
        g.log.error("Failed to start volume %s after restoring snapshot %s
                    in node %s" , volname, snapname, mnode)
        return False
    return True


def snap_info(mnode, snapname):
    """Gets the snap info by snapname

    Args:
        mnode (str): Node on which command has to be executed.
        snapname (str): snapshot name

    Returns:
        NoneType: None if command execution fails, parse errors.
        dict: on success.
    """
    return RestClient(mnode).handle_request('GET', "/v1/snapshots/%s"
                                            % snapname, httplib.OK, None)


def snap_list(mnode):
    """Lists the snapshots

    Args:
        mnode (str): Node on which cmd has to be executed.

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.
    """
    return RestClient(mnode).handle_request('GET', "/v1/snapshots", httplib.OK, None)


def get_snap_list(mnode):
    """ Lists the snapname

    Args:
        mnode (str): Node on which cmd has to be executed.

    Returns:
        list: List containing the snapname if exists else returns None

    """
    _, out, _ = snap_list(mnode)
    if out:
        output = json.loads(out)
        snap_info = output[0]
        snaps_list = []
        for elem in snap_info['snaps']:
            snaps = elem['snapinfo']['name']
            snaps_list.append(snaps)
        return snaps_list
    return None


def snap_status(mnode, snapname):
    """Get the snap status by snapname

    Args:
        mnode (str): Node on which command has to be executed.
        snapname (str): snapshot name

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    """
    return RestClient(mnode).handle_request('GET', "/v1/snapshots/%s/status"
                                            % snapname, httplib.OK, None)


def snap_delete(mnode, snapname):
    """Deletes the given snapshot

    Args:
        mnode (str): Node on which cmd has to be executed.
        snapname (str): snapshot name to be deleted

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.
    """
    return RestClient(mnode).handle_request('DELETE', "/v1/snapshots/%s"
                                            % snapname, httplib.DELETE, None)
    # TODO: Few snapshot functions are yet to be automated after it is
    # implemented in gd2