4 from cloud import common
10 def __init__(self, mgmt_ip, username, password):
12 self.mgmt_ip = mgmt_ip
13 self.username = username
14 self.password = password
15 self.error_message = ""
18 return self.ssh is not None
24 self.error_message = ""
27 LOG.info("Trying to connect to OA at %s" % self.mgmt_ip)
29 self.ssh.connect(self.mgmt_ip,
30 username=self.username,
31 password=self.password,
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)
44 # Return None if this most likely is not an OA
45 # False if we failed to connect to an active OA
47 def connect_to_active(self):
48 self.error_message = "OA connect failed with these errors:\n"
50 self.ssh = paramiko.SSHClient()
51 self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
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],
59 str(int(octets[3]) + 1))
60 if not self.connect(self.mgmt_ip, self.username, self.password):
62 LOG.error("Failed to connect to OA at %s (and %s)" %
63 (initial_mgmt_ip, self.mgmt_ip))
66 output = self.send_command("show oa status")
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" %
80 octets = self.mgmt_ip.split(".")
81 self.mgmt_ip = "%s.%s.%s.%s" % (octets[0],
84 str(int(octets[3]) + 1))
85 if not self.connect(self.mgmt_ip, self.username,
89 LOG.info("Connected to active OA at %s" % self.mgmt_ip)
90 self.error_message = ""
93 def send_command(self, cmd):
94 if not self.connected():
95 self.error_message = (
96 "Not connected, cannot send command %s\n" % (cmd))
99 LOG.info('Sending "%s" to %s' % (cmd, self.mgmt_ip))
100 stdin, stdout, stderr = self.ssh.exec_command(cmd)
102 for line in stdout.read().splitlines():
107 def __exit__(self, type, value, traceback):