summaryrefslogtreecommitdiffstats
path: root/utils/installer-adapter/SSHUtils.py
diff options
context:
space:
mode:
authorjose.lausuch <jose.lausuch@ericsson.com>2016-09-30 17:24:31 +0200
committerjose.lausuch <jose.lausuch@ericsson.com>2016-10-09 18:58:51 +0200
commit19a3c7408d250171a838fd98bd923e77c65c93b0 (patch)
tree144f46ececb2cd9a59fe6baed5834837ddee23cc /utils/installer-adapter/SSHUtils.py
parent3a22f37080de6927f8b1eb5665aa224cd6381199 (diff)
Installer adapters
This tool might be used by any project in OPNFV to retrieve information about the deployed OpenStack nodes. It is python based and using paramiko. It can: - get info about deployment - get the info about the nodes (ip, mac, ...) - stablish ssh connection even with ProxyCommand option - run remote commands - scp to/from nodes (i.e. fetch credentials from controller) Added FuelAdapter as an example. JIRA: RELENG-149 JIRA: RELENG-150 Change-Id: I49d8be96d754e0950e337aa2f88172341446fdd4 Signed-off-by: jose.lausuch <jose.lausuch@ericsson.com>
Diffstat (limited to 'utils/installer-adapter/SSHUtils.py')
-rw-r--r--utils/installer-adapter/SSHUtils.py130
1 files changed, 130 insertions, 0 deletions
diff --git a/utils/installer-adapter/SSHUtils.py b/utils/installer-adapter/SSHUtils.py
new file mode 100644
index 000000000..9c92a3be1
--- /dev/null
+++ b/utils/installer-adapter/SSHUtils.py
@@ -0,0 +1,130 @@
+##############################################################################
+# Copyright (c) 2015 Ericsson AB and others.
+# Author: Jose Lausuch (jose.lausuch@ericsson.com)
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
+import paramiko
+from scp import SCPClient
+import time
+import RelengLogger as rl
+
+
+class SSH_Connection:
+
+ def __init__(self,
+ host,
+ user,
+ password,
+ use_system_keys=True,
+ private_key=None,
+ use_proxy=False,
+ proxy_host=None,
+ proxy_user=None,
+ proxy_password=None,
+ timeout=10):
+ self.host = host
+ self.user = user
+ self.password = password
+ self.use_system_keys = use_system_keys
+ self.private_key = private_key
+ self.use_proxy = use_proxy
+ self.proxy_host = proxy_host
+ self.proxy_user = proxy_user
+ self.proxy_password = proxy_password
+ self.timeout = timeout
+ paramiko.util.log_to_file("paramiko.log")
+ self.logger = rl.Logger("SSHUtils").getLogger()
+
+ def connect(self):
+ client = paramiko.SSHClient()
+ if self.use_system_keys:
+ client.load_system_host_keys()
+ elif self.private_key:
+ client.load_host_keys(self.private_key)
+ else:
+ client.load_host_keys('/dev/null')
+
+ client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+
+ t = self.timeout
+ proxy = None
+ if self.use_proxy:
+ proxy_command = 'ssh -o UserKnownHostsFile=/dev/null '
+ '-o StrictHostKeyChecking=no %s@%s -W %s:%s' % (self.proxy_user,
+ self.proxy_host,
+ self.host, 22)
+ proxy = paramiko.ProxyCommand(proxy_command)
+ self.logger.debug("Proxy command: %s" % proxy_command)
+ while t > 0:
+ try:
+ self.logger.debug(
+ "Trying to stablish ssh connection to %s..." % self.host)
+ client.connect(self.host,
+ username=self.user,
+ password=self.password,
+ look_for_keys=True,
+ sock=proxy,
+ pkey=self.private_key,
+ timeout=self.timeout)
+ self.logger.debug("Successfully connected to %s!" % self.host)
+ return client
+ except:
+ time.sleep(1)
+ t -= 1
+
+ if t == 0:
+ return None
+
+ def scp_put(self, local_path, remote_path):
+ client = self.connect()
+ if client:
+ scp = SCPClient(client.get_transport())
+ try:
+ scp.put(local_path, remote_path)
+ client.close()
+ return 0
+ except Exception, e:
+ self.logger.error(e)
+ client.close()
+ return 1
+ else:
+ self.logger.error("Cannot stablish ssh connection.")
+
+ def scp_get(self, local_path, remote_path):
+ client = self.connect()
+ if client:
+ scp = SCPClient(client.get_transport())
+ try:
+ scp.get(remote_path, local_path)
+ client.close()
+ return 0
+ except Exception, e:
+ self.logger.error(e)
+ client.close()
+ return 1
+ else:
+ self.logger.error("Cannot stablish ssh connection.")
+ return 1
+
+ def run_remote_cmd(self, command):
+ client = self.connect()
+ if client:
+ try:
+ stdin, stdout, stderr = client.exec_command(command)
+ out = ''
+ for line in stdout.readlines():
+ out += line
+ err = stderr.readlines()
+ client.close()
+ return out, err
+ except:
+ client.close()
+ return 1
+ else:
+ self.logger.error("Cannot stablish ssh connection.")
+ return 1