Merge "Allow deployment on Centos 6.5 environment"
[genesis.git] / fuel / deploy / hardware_adapters / hp / run_oa_command.py
1 import socket
2 import paramiko
3 import logging
4
5 LOG = logging.getLogger(__name__)
6 out_hdlr = logging.FileHandler(__file__.split('.')[0] + '.log', mode='w')
7 out_hdlr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
8 LOG.addHandler(out_hdlr)
9 LOG.setLevel(logging.DEBUG)
10
11 class RunOACommand:
12
13     def __init__(self, mgmt_ip, username, password):
14         self.ssh = None
15         self.mgmt_ip = mgmt_ip
16         self.username = username
17         self.password = password
18         self.error_message = ""
19
20     def connected(self):
21         return self.ssh is not None
22
23     def close(self):
24         if self.connected():
25             self.ssh.close()
26             self.ssh = None
27         self.error_message = ""
28
29     def connect(self):
30         LOG.info("Trying to connect to OA at %s" % self.mgmt_ip)
31         try:
32             self.ssh.connect(self.mgmt_ip,
33                              username=self.username,
34                              password=self.password,
35                              look_for_keys=False,
36                              allow_agent=False)
37             return True
38         except socket.error, (err, message):
39             self.error_message += ("Can not talk to OA %s: %s\n" %
40                                    (self.mgmt_ip, message))
41         except Exception as e:
42             self.error_message += ("Can not talk to OA %s: %s\n" %
43                                    (self.mgmt_ip, e.args))
44             LOG.error("Failed to connect to OA at %s" % self.mgmt_ip)
45         return False
46
47     # Return None if this most likely is not an OA
48     #        False if we failed to connect to an active OA
49     #        True if connected
50     def connect_to_active(self):
51         self.error_message = "OA connect failed with these errors:\n"
52
53         self.ssh = paramiko.SSHClient()
54         self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
55
56         initial_mgmt_ip = self.mgmt_ip
57         if not self.connect(self.mgmt_ip, self.username, self.password):
58             octets = self.mgmt_ip.split(".")
59             self.mgmt_ip = "%s.%s.%s.%s" % (octets[0],
60                                             octets[1],
61                                             octets[2],
62                                             str(int(octets[3]) + 1))
63             if not self.connect(self.mgmt_ip, self.username, self.password):
64                 self.ssh = None
65                 LOG.error("Failed to connect to OA at %s (and %s)" %
66                           (initial_mgmt_ip, self.mgmt_ip))
67                 return None
68
69         output = self.send_command("show oa status")
70         for line in output:
71             if "Standby" in line:
72                 self.ssh.close()
73                 self.error_message += (
74                     "%s is the standby OA, trying next OA\n" % self.mgmt_ip)
75                 LOG.info("%s is the standby OA" % self.mgmt_ip)
76                 if self.mgmt_ip != initial_mgmt_ip:
77                     self.error_message += (
78                         "Can only talk to OA %s which is the standby OA\n" %
79                         self.mgmt_ip)
80                     self.ssh = None
81                     return False
82                 else:
83                     octets = self.mgmt_ip.split(".")
84                     self.mgmt_ip = "%s.%s.%s.%s" % (octets[0],
85                                                     octets[1],
86                                                     octets[2],
87                                                     str(int(octets[3]) + 1))
88                     if not self.connect(self.mgmt_ip, self.username,
89                                         self.password):
90                         self.ssh = None
91                         return False
92         LOG.info("Connected to active OA at %s" % self.mgmt_ip)
93         self.error_message = ""
94         return True
95
96     def send_command(self, cmd):
97         if not self.connected():
98             self.error_message = (
99                 "Not connected, cannot send command %s\n" % (cmd))
100             raise
101
102         LOG.info('Sending "%s" to %s' % (cmd, self.mgmt_ip))
103         stdin, stdout, stderr = self.ssh.exec_command(cmd)
104         output = []
105         for line in stdout.read().splitlines():
106             if line != '':
107                 output.append(line)
108         return output
109
110     def __exit__(self, type, value, traceback):
111         if self.connected():
112             self.close()
113             self.ssh = None