summaryrefslogtreecommitdiffstats
path: root/glustolibs-misc/glustolibs/misc/misc_libs.py
blob: 62b854c1083b7f22a28e82283f2aa8e6368eb691 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#  Copyright (C) 2015-2016  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: Helper module for misc libs. """

from glusto.core import Glusto as g
import os
import sys


def create_dirs(list_of_nodes, list_of_dir_paths):
    """Creates directories on nodes.

    Args:
        list_of_nodes (list): Nodes on which dirs has to be created.
        list_of_dir_paths (list): List of dirs abs path.

    Returns:
        bool: True of creation of all dirs on all nodes is successful.
            False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if isinstance(list_of_dir_paths, list):
        list_of_dir_paths = ' '.join(list_of_dir_paths)

    _rc = True
    # Create upload dir on each node
    for node in list_of_nodes:
        ret, _, err = g.run(node, "mkdir -p %s" % list_of_dir_paths)
        if ret != 0:
            g.log.error("Failed to create the dirs: %s on node: %s - %s" %
                        (list_of_dir_paths.split(" "), node, err))
            _rc = False
    if _rc:
        g.log.info("Successfully created dirs: %s on nodes:%s" %
                   (list_of_dir_paths.split(" "), list_of_nodes))
    return _rc


def path_exists(list_of_nodes, list_of_paths):
    """check if paths exists on nodes.

    Args:
        list_of_nodes (list): List of nodes.
        list_of_paths (list): List of abs paths to verify if path exist.

    Returns:
        bool: True if all paths exists on all nodes. False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if not isinstance(list_of_paths, list):
        list_of_paths = (list_of_paths.split(" "))

    _rc = True
    for node in list_of_nodes:
        for path in list_of_paths:
            cmd = "ls -l %s" % path
            ret, _, err = g.run(node, cmd)
            if ret != 0:
                g.log.error("Path: %s not found on node: %s - %s" %
                            (path, node, err))
                _rc = False

    if _rc:
        g.log.info("Paths: %s exists on nodes: %s" %
                   (list_of_paths, list_of_nodes))
    return _rc


def upload_scripts(list_of_nodes, list_of_scripts_abs_path,
                   upload_dir="/usr/share/glustolibs/io/scripts/", user=None):
    """Uploads specified scripts to all the nodes.

    Args:
        list_of_nodes (list): Nodes on which scripts has to be uploaded.
        list_of_scripts_abs_path (list): List of absolute path of all
            scripts that are to be uploaded from local node.
        upload_dir (optional[str]): Name of the dir under which
            scripts will be uploaded on remote node.
        user (optional[str]): The user to use for the remote connection.

    Returns:
        bool: True if uploading scripts is sucessful on all nodes.
            False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if not isinstance(list_of_scripts_abs_path, list):
        list_of_scripts_abs_path = (
            list_of_scripts_abs_path.split(" "))

    g.log.info("Scripts to upload: %s" % list_of_scripts_abs_path)
    g.log.info("Script upload dir: %s" % upload_dir)

    # Create upload dir on each node
    if not create_dirs(list_of_nodes, upload_dir):
        return False

    # Upload scrpts
    _rc = True
    for script_local_abs_path in list_of_scripts_abs_path:
        if not os.path.exists(script_local_abs_path):
            g.log.error("Script: %s doesn't exists" % script_local_abs_path)
            _rc = False
            break
        for node in list_of_nodes:
            script_name = os.path.basename(script_local_abs_path)
            script_upload_path = os.path.join(upload_dir, script_name)
            g.upload(node, script_local_abs_path, script_upload_path, user)
    if not _rc:
        g.log.error("Failed to upload scripts")
        return False

    # Recursively provide execute permissions to all scripts
    for node in list_of_nodes:
        ret, _, _ = g.run(node, "chmod -R +x %s" % upload_dir)
        if ret != 0:
            g.log.error("Unable to provide execute permissions to upload dir "
                        "'%s' on %s" % (upload_dir, node))
            return False
        else:
            g.log.info("Successfully provided execute permissions to upload "
                       "dir '%s' on %s" % (upload_dir, node))

        ret, out, err = g.run(node, "ls -l %s" % upload_dir)
        if ret != 0:
            g.log.error("Failed to list the dir: %s on node: %s - %s" %
                        (upload_dir, node, err))
        else:
            g.log.info("Listing dir: %s on node: %s - \n%s" %
                       (upload_dir, node, out))

    return True


def yum_add_repos(list_of_nodes, list_of_yum_repos):
    """Add yum repo files on all the nodes.

    Args:
        list_of_nodes (list): Nodes on which yum repo files has to be added.
        list_of_yum_repos (list): List of yum repos

    Returns:
        bool: True if adding yum repo files is sucessful on all nodes.
            False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if not isinstance(list_of_yum_repos, list):
        list_of_yum_repos = list_of_yum_repos.split(" ")

    _rc = True
    for node in list_of_nodes:
        for yum_repo in list_of_yum_repos:
            out_file = os.path.basename(yum_repo)
            cmd = ("wget %s -O /etc/yum.repos.d/%s" % (yum_repo, out_file))
            ret, _, err = g.run(node, cmd)
            if ret != 0:
                g.log.error("Unable to add repo file: %s on node: %s - %s" %
                            (yum_repo, node, err))
                _rc = False
    if _rc:
        g.log.info("Successfully added yum repo files: %s on nodes: %s" %
                   (list_of_yum_repos, list_of_nodes))
    return _rc


def yum_install_packages(list_of_nodes, yum_packages):
    """Install the specified yum packages on all nodes.

    Args:
        list_of_nodes (list): Nodes on which yum packages has to be installed.
        yum_packages (list): List of yum packages.

    Returns:
        bool: True if installation of packages is sucessful on all nodes.
            False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if isinstance(yum_packages, list):
        yum_packages = ' '.join(yum_packages)

    _rc = True
    for node in list_of_nodes:
        cmd = "yum -y install %s" % yum_packages
        ret, _, err = g.run(node, cmd)
        if ret != 0:
            g.log.error("Unable to install yum packages: %s on node: %s - %s" %
                        (yum_packages, node, err))
            _rc = False
    if _rc:
        g.log.info("Successfully installed yum packages: %s on nodes: %s" %
                   (yum_packages, list_of_nodes))
    return _rc


def yum_remove_packages(list_of_nodes, yum_packages):
    """Remove the specified yum packages on all nodes.

    Args:
        list_of_nodes (list): Nodes on which yum packages has to be removed.
        yum_packages (list): List of yum packages.

    Returns:
        bool: True if removing packages is sucessful on all nodes.
            False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if isinstance(yum_packages, list):
        yum_packages = ' '.join(yum_packages)

    _rc = True
    for node in list_of_nodes:
        cmd = "yum -y remove %s" % yum_packages
        ret, _, err = g.run(node, cmd)
        if ret != 0:
            g.log.error("Unable to remove yum packages: %s on node: %s - %s" %
                        (yum_packages, node, err))
            _rc = False
    if _rc:
        g.log.info("Successfully removed yum packages: %s on nodes: %s" %
                   (yum_packages, list_of_nodes))
    return _rc


def pip_install_packages(list_of_nodes, python_packages):
    """Install the specified python packages on all the specified nodes.

    Args:
        list_of_nodes (list): Nodes on which python packages has to be
            installed.
        python_packages (list): List of python packages.

    Returns:
        bool: True if installation of packages is sucessful on all nodes.
            False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if isinstance(python_packages, list):
        python_packages = ' '.join(python_packages)

    _rc = True
    for node in list_of_nodes:
        cmd = "pip install %s" % python_packages
        ret, _, err = g.run(node, cmd)
        if ret != 0:
            g.log.error("Unable to install python packages: %s on node: %s - "
                        "%s" % (python_packages, node, err))
            _rc = False
    if _rc:
        g.log.info("Successfully installed python packages: %s on nodes: %s" %
                   (python_packages, list_of_nodes))
    return _rc


def install_testing_tools(list_of_nodes, testing_tools):
    """Install the specified testing tools on all nodes.

    Args:
        list_of_nodes (list): Nodes on which testing tools has to be
            installed.
        testing_tools(list): List of testing tools to install.
            Available tools:
                - arequal

    Returns:
        bool: True if installation of all testing tools is sucessful on
            all nodes. False otherwise.
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    if not isinstance(testing_tools, list):
        testing_tools = testing_tools.split(" ")

    _rc = True
    this_module = sys.modules[__name__]
    for testing_tool in testing_tools:
        func_name = 'install_' + testing_tool
        if hasattr(this_module, func_name):
            func = getattr(this_module, func_name)
            ret = func(list_of_nodes)
            if not ret:
                _rc = False
            else:
                g.log.info("Successfully installed tool: %s on nodes: [%s]" %
                           (testing_tool, ', '.join(list_of_nodes)))
        else:
            g.log.error("Unable to find the helper function to install %s" %
                        testing_tool)
            _rc = False
    return _rc


def install_arequal(list_of_nodes):
    """Installs arequal on nodes in /usr/bin/.

    Args:
        nodes(list): List of nodes on which arequal-checksum needs to be
            installed.

    Returns:
        bool: Returns True on successful installation of arequal-checksum
            on all nodes. False Otherwise.

    Note: The arequal repo can be specified in the config file:
        dependencies:
            testing_tools:
                arequal:
                    repo: "https://abc.def.com/tools/arequal/arequal.repo"
    """
    if not isinstance(list_of_nodes, list):
        list_of_nodes = [list_of_nodes]

    try:
        arequal_repo = (g.config['dependencies']['testing_tools']['arequal']
                        ['repo'])
    except KeyError:
        arequal_repo = ("https://copr.fedorainfracloud.org/coprs/nigelbabu/"
                        "arequal/repo/epel-7/nigelbabu-arequal-epel-7.repo")

    arequal_install_path = "/usr/bin/arequal-checksum"
    _rc = True
    for node in list_of_nodes:
        # check if arequal-checksum is installed
        if not path_exists(node, arequal_install_path):
            g.log.info("arequal-checksum not installed. Installling...")

            # get arequal repo file
            if not yum_add_repos(node, arequal_repo):
                return False

            # Install arequal
            if not yum_install_packages(node, "arequal"):
                return False

            # verify arequal_checksum got installed
            if not path_exists(node, arequal_install_path):
                raise Exception("%s not found on node %s even after "
                                "installation" % (arequal_install_path, node))
            else:
                g.log.info("arequal_checksum is installed on node %s" %
                           node)
        else:
            g.log.info("arequal-checksum is already installed on %s" % node)
            continue
    if _rc:
        g.log.info("arequal-checksum is installed on nodes: %s" %
                   list_of_nodes)
    return _rc