Debian Codegraph @

ansible_collections/ibm/storage_virtualize/plugins/module_utils/ibm_svc_ssh.py

raw symbols SCIP
1 # Copyright (C) 2024 IBM CORPORATION
2 # Author(s): Shilpi Jain <[email protected]>
3 # Sandip G. Rajbanshi <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """ Support class for IBM SVC generic ansible module """
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 import inspect
14 import uuid
15 from ansible.module_utils.compat.paramiko import paramiko
16 from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import get_logger
17
18 COLLECTION_VERSION = "3.3.0"
19
20
21 class IBMSVCssh(object):
22 """ Communicate with SVC through SSH
23 The module uses paramiko to connect SVC
24 """
25
26 def __init__(self, module, clustername, domain, username, password,
27 look_for_keys, key_filename, log_path):
28 """ Initialize module with what we need for initial connection
29 :param clustername: name of the SVC cluster
30 :type clustername: string
31 :param domain: domain to create FQDN of SVC system
32 :type domain: string
33 :param username: SVC username
34 :type username: string
35 :param password: Password for user
36 :type password: string
37 :param look_for_keys: whether to look for keys or not
38 :type look_for_keys: boolean
39 :param key_filename: SSH client private key file
40 :type key_filename: string
41 :param log_path: log file
42 :type log_path: string
43 """
44 self.module = module
45 self.clustername = clustername
46 self.domain = domain
47 self.username = username
48 self.password = password
49 self.look_for_keys = look_for_keys
50 self.key_filename = key_filename
51
52 self.is_client_connected = False
53
54 # logging setup
55 log = get_logger(self.__class__.__name__, log_path)
56 self.log = log.info
57
58 self.client_type = 'paramiko'
59 if paramiko is None:
60 self.module.fail_json(msg='paramiko is not installed')
61 self.client = paramiko.SSHClient()
62
63 # connect through SSH
64 self.is_client_connected = self._svc_connect()
65 if not self.is_client_connected:
66 self.module.fail_json(msg='Failed to connect')
67
68 def _svc_connect(self):
69 """
70 Initialize a SSH connection with properties
71 which were set up in constructor.
72 :return: True or False
73 """
74 self.client.load_system_host_keys()
75 self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
76 self.hostname = self.clustername
77 self.log(self.domain)
78 if self.domain:
79 self.hostname = f"{self.clustername}.{self.domain}"
80 self.log(self.hostname)
81 try:
82 self.client.connect(
83 hostname=self.hostname,
84 username=self.username,
85 password=self.password,
86 look_for_keys=self.look_for_keys,
87 key_filename=self.key_filename)
88 self.register_plugin()
89 return True
90 except paramiko.BadHostKeyException as e:
91 self.log("BadHostKeyException %s", e)
92 except paramiko.AuthenticationException as e:
93 self.log("AuthenticationException %s", e)
94 except paramiko.SSHException as e:
95 self.log("SSHException %s", e)
96 except Exception as e:
97 self.log("SSH connection failed %s", e)
98 return False
99
100 def is_connected(self):
101 return self.is_client_connected
102
103 def _svc_disconnect(self):
104 """
105 Disconnect from the SSH server.
106 """
107 try:
108 self.client.close()
109 self.is_client_connected = False
110 self.log("SSH disconnected")
111 return True
112 except Exception as e:
113 self.log("SSH Disconnection failed %s", e)
114 return False
115
116 def register_plugin(self):
117 try:
118 cmd = 'svctask registerplugin'
119 cmdopts = {}
120 name = "Ansible"
121 unique_key = self.username + "_" + str(uuid.getnode())
122 caller_class = inspect.stack()[2].frame.f_locals.get('self', None)
123 caller_class_name = caller_class.__class__.__name__
124 module_name = str(inspect.stack()[3].filename).rsplit('/', maxsplit=1)[-1]
125 metadata = module_name[:-3] + " module with class " + str(caller_class_name) + " has been executed by " + self.username
126
127 cmdopts['name'] = name
128 cmdopts['uniquekey'] = unique_key
129 cmdopts['version'] = COLLECTION_VERSION
130 cmdopts['metadata'] = metadata
131
132 for cmdoptions in cmdopts:
133 cmd = cmd + " -" + cmdoptions + " '" + cmdopts[cmdoptions] + "'"
134 self.client.exec_command(cmd)
135 return True
136 except Exception as e:
137 return False