summaryrefslogtreecommitdiffstats
path: root/tools/setup_passwordless_ssh/setup_passwordless_ssh.py
blob: e9a619c5061f22852129c9adeee99742bbe0e5d4 (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
#!/usr/bin/env python3
#  Copyright (C) 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 3 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.

import argparse
import sys
from os import system
from yaml import safe_load


def read_config_file(config_file):
    """
    A function to read the yaml file given to the script.
    Args:
        config_file(str): A config file used to run glusto-tests.
    Return:
        dict: A dictornary with all the details from config file.
    """
    return safe_load(open(config_file, 'r'))


def setup_passwordless_ssh(server, username, password):
    """
    A function to setup passwordless ssh to all servers.
    Args:
        server(str): hostname/IP of server for
                     passwordless ssh has to be configured.
        username(str): User to be used to login.
        password(str): password to be used to login.
    Returns:
        bool: True if successful else false.
    """
    command = ("sshpass -p %s ssh-copy-id -o StrictHostKeyChecking=no %s@%s"
               % (password, username, server))
    ret = system(command)
    return not ret


def check_passwordless_ssh_setup(server, username):
    """
    A function to check if passwordless ssh setup was successfull or not.
    Args:
        server(str): hostname/IP of server for
                     passwordless ssh has to be configured.
        username(str): User to be used to login.
    Returns:
        bool: True if successful else false.
    """
    command = ("ssh %s@%s hostname" % (username, server))
    ret = system(command)
    return not ret


def main():
    """
    Main function of the tool.
    """

    # Setting up command line arguments.
    parser = argparse.ArgumentParser(
        description="Tool to setup passwordless ssh to all nodes."
        )
    parser.add_argument("-c", "--config_file",
                        type=str, dest="config_file",
                        help="A glusto-tests configuration file.")
    parser.add_argument("-p", "--password", dest="password",
                        type=str, help="Password of servers.")
    parser.add_argument("-u", "--username", dest="username",
                        type=str, default="root",
                        help="User to be used to setup"
                        " passwordless ssh.")
    args = parser.parse_args()

    # Reading the config file.
    if args.config_file:
        config = read_config_file(args.config_file)
    else:
        sys.exit("[ERROR]:Config file not provided.")

    # Checking if password was provided.
    if args.password:
        password = args.password
    else:
        sys.exit("[ERROR]:Password not provided.")

    # Configuring passwordless ssh to all servers.
    for server in config.get('servers', []):
        ret = setup_passwordless_ssh(server, args.username,
                                     password)
        if not ret:
            sys.exit("[ERROR]:Unable to setup "
                     "passwordless ssh to %s."
                     % server)
        ret = check_passwordless_ssh_setup(server,
                                           args.username)
        if ret:
            print("[INFO]:Passwordless ssh setup "
                  "completed to %s." % server)

    # Configuring passwordless ssh to all clients.
    for server in config.get('clients', []):
        ret = setup_passwordless_ssh(server,
                                     args.username,
                                     password)

        if not ret:
            sys.exit("[ERROR]:Unable to setup "
                     "passwordless ssh to %s."
                     % server)

        ret = check_passwordless_ssh_setup(server,
                                           args.username)
        if ret:
            print("[INFO]:Passwordless ssh setup "
                  "completed to %s." % server)

    # Configure paswordless ssh to all geo-rep slaves nodes.
    for server in config.get('slaves', []):
        ret = setup_passwordless_ssh(server,
                                     args.username,
                                     password)
        if not ret:
            sys.exit("[ERROR]:Unable to setup "
                     "passwordless ssh to %s."
                     % server)
        ret = check_passwordless_ssh_setup(server,
                                           args.username)
        if ret:
            print("[INFO]:Passwordless ssh setup "
                  "completed to %s." % server)


if __name__ == "__main__":
    main()