Installer adapters
[releng.git] / utils / installer-adapter / SSHUtils.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 import paramiko
12 from scp import SCPClient
13 import time
14 import RelengLogger as rl
15
16
17 class SSH_Connection:
18
19     def __init__(self,
20                  host,
21                  user,
22                  password,
23                  use_system_keys=True,
24                  private_key=None,
25                  use_proxy=False,
26                  proxy_host=None,
27                  proxy_user=None,
28                  proxy_password=None,
29                  timeout=10):
30         self.host = host
31         self.user = user
32         self.password = password
33         self.use_system_keys = use_system_keys
34         self.private_key = private_key
35         self.use_proxy = use_proxy
36         self.proxy_host = proxy_host
37         self.proxy_user = proxy_user
38         self.proxy_password = proxy_password
39         self.timeout = timeout
40         paramiko.util.log_to_file("paramiko.log")
41         self.logger = rl.Logger("SSHUtils").getLogger()
42
43     def connect(self):
44         client = paramiko.SSHClient()
45         if self.use_system_keys:
46             client.load_system_host_keys()
47         elif self.private_key:
48             client.load_host_keys(self.private_key)
49         else:
50             client.load_host_keys('/dev/null')
51
52         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
53
54         t = self.timeout
55         proxy = None
56         if self.use_proxy:
57             proxy_command = 'ssh -o UserKnownHostsFile=/dev/null '
58             '-o StrictHostKeyChecking=no %s@%s -W %s:%s' % (self.proxy_user,
59                                                             self.proxy_host,
60                                                             self.host, 22)
61             proxy = paramiko.ProxyCommand(proxy_command)
62             self.logger.debug("Proxy command: %s" % proxy_command)
63         while t > 0:
64             try:
65                 self.logger.debug(
66                     "Trying to stablish ssh connection to %s..." % self.host)
67                 client.connect(self.host,
68                                username=self.user,
69                                password=self.password,
70                                look_for_keys=True,
71                                sock=proxy,
72                                pkey=self.private_key,
73                                timeout=self.timeout)
74                 self.logger.debug("Successfully connected to %s!" % self.host)
75                 return client
76             except:
77                 time.sleep(1)
78                 t -= 1
79
80         if t == 0:
81             return None
82
83     def scp_put(self, local_path, remote_path):
84         client = self.connect()
85         if client:
86             scp = SCPClient(client.get_transport())
87             try:
88                 scp.put(local_path, remote_path)
89                 client.close()
90                 return 0
91             except Exception, e:
92                 self.logger.error(e)
93                 client.close()
94                 return 1
95         else:
96             self.logger.error("Cannot stablish ssh connection.")
97
98     def scp_get(self, local_path, remote_path):
99         client = self.connect()
100         if client:
101             scp = SCPClient(client.get_transport())
102             try:
103                 scp.get(remote_path, local_path)
104                 client.close()
105                 return 0
106             except Exception, e:
107                 self.logger.error(e)
108                 client.close()
109                 return 1
110         else:
111             self.logger.error("Cannot stablish ssh connection.")
112             return 1
113
114     def run_remote_cmd(self, command):
115         client = self.connect()
116         if client:
117             try:
118                 stdin, stdout, stderr = client.exec_command(command)
119                 out = ''
120                 for line in stdout.readlines():
121                     out += line
122                 err = stderr.readlines()
123                 client.close()
124                 return out, err
125             except:
126                 client.close()
127                 return 1
128         else:
129             self.logger.error("Cannot stablish ssh connection.")
130             return 1