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