Passed
Push — master ( ffbe9f...5c3c8b )
by Konrad
01:19
created

db_sync_tool.remote.client.load_ssh_client()   B

Complexity

Conditions 5

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 55
rs 8.5493
c 0
b 0
f 0
cc 5
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: utf-8 -*-
3
4
import sys
5
import paramiko
6
from db_sync_tool.utility import mode, system, helper, output
7
from db_sync_tool.remote import utility
8
9
ssh_client_origin = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "ssh_client_origin" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\Wa-z]*|__.*__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
10
ssh_client_target = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "ssh_client_target" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\Wa-z]*|__.*__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
11
12
13
def load_ssh_client_origin():
14
    """
15
    Loading the origin ssh client
16
    :return:
17
    """
18
    global ssh_client_origin
0 ignored issues
show
Coding Style introduced by
Usage of the global statement should be avoided.

Usage of global can make code hard to read and test, its usage is generally not recommended unless you are dealing with legacy code.

Loading history...
Coding Style Naming introduced by
Constant name "ssh_client_origin" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\Wa-z]*|__.*__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
19
    ssh_client_origin = load_ssh_client(mode.Client.ORIGIN)
20
    utility.run_before_script(mode.Client.ORIGIN)
21
22
23
def load_ssh_client_target():
24
    """
25
    Loading the target ssh client
26
    :return:
27
    """
28
    global ssh_client_target
0 ignored issues
show
Coding Style introduced by
Usage of the global statement should be avoided.

Usage of global can make code hard to read and test, its usage is generally not recommended unless you are dealing with legacy code.

Loading history...
Coding Style Naming introduced by
Constant name "ssh_client_target" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\Wa-z]*|__.*__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
29
    ssh_client_target = load_ssh_client(mode.Client.TARGET)
30
    utility.run_before_script(mode.Client.TARGET)
31
32
33
def load_ssh_client(ssh):
34
    """
35
    Initializing the given ssh client
36
    :param ssh: String
37
    :return:
38
    """
39
    _host_name = helper.get_ssh_host_name(ssh, True)
40
    _ssh_client = paramiko.SSHClient()
41
    _ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
42
43
    _ssh_port = _ssh_port = system.config[ssh]['port'] if 'port' in system.config[ssh] else 22
44
    _ssh_key = None
45
    _ssh_password = None
46
47
    # Check authentication
48
    if 'ssh_key' in system.config[ssh]:
49
        _authentication_method = f'{output.CliFormat.BLACK} - (authentication: key){output.CliFormat.ENDC}'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (107/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
50
        _ssh_key = system.config[ssh]['ssh_key']
51
    elif 'password' in system.config[ssh]:
52
        _authentication_method = f'{output.CliFormat.BLACK} - (authentication: password){output.CliFormat.ENDC}'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (112/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
53
        _ssh_password = system.config[ssh]['password']
54
    else:
55
        sys.exit(
56
            output.message(
57
                output.Subject.ERROR,
58
                f'Missing SSH authentication. Neither ssh key nor ssh password given.',
0 ignored issues
show
introduced by
Using an f-string that does not have any interpolated variables
Loading history...
59
                False
60
            )
61
        )
62
63
    # Try to connect to remote client via paramiko
64
    try:
65
        _ssh_client.connect(hostname=system.config[ssh]['host'],
66
                            username=system.config[ssh]['user'],
67
                            key_filename=_ssh_key,
68
                            password=_ssh_password,
69
                            port=_ssh_port,
70
                            compress=True)
71
72
    except paramiko.ssh_exception.AuthenticationException:
73
        sys.exit(
74
            output.message(
75
                output.Subject.ERROR,
76
                f'SSH authentication for {_host_name} failed',
77
                False
78
            )
79
        )
80
81
    output.message(
82
        output.host_to_subject(ssh),
83
        f'Initialize remote SSH connection {_host_name}{_authentication_method}',
84
        True
85
    )
86
87
    return _ssh_client
88
89
90
def close_ssh_clients():
91
    """
92
    Closing ssh client sessions
93
    :return:
94
    """
95
    utility.run_after_script(mode.Client.ORIGIN)
96
    if not ssh_client_origin is None:
97
        ssh_client_origin.close()
98
99
    utility.run_after_script(mode.Client.TARGET)
100
    if not ssh_client_target is None:
101
        ssh_client_target.close()
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...