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)
13 def __init__(self, mgmt_ip, username, password):
15 self.mgmt_ip = mgmt_ip
16 self.username = username
17 self.password = password
18 self.error_message = ""
21 return self.ssh is not None
27 self.error_message = ""
30 LOG.info("Trying to connect to OA at %s" % self.mgmt_ip)
32 self.ssh.connect(self.mgmt_ip,
33 username=self.username,
34 password=self.password,
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)
47 # Return None if this most likely is not an OA
48 # False if we failed to connect to an active OA
50 def connect_to_active(self):
51 self.error_message = "OA connect failed with these errors:\n"
53 self.ssh = paramiko.SSHClient()
54 self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
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],
62 str(int(octets[3]) + 1))
63 if not self.connect(self.mgmt_ip, self.username, self.password):
65 LOG.error("Failed to connect to OA at %s (and %s)" %
66 (initial_mgmt_ip, self.mgmt_ip))
69 output = self.send_command("show oa status")
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" %
83 octets = self.mgmt_ip.split(".")
84 self.mgmt_ip = "%s.%s.%s.%s" % (octets[0],
87 str(int(octets[3]) + 1))
88 if not self.connect(self.mgmt_ip, self.username,
92 LOG.info("Connected to active OA at %s" % self.mgmt_ip)
93 self.error_message = ""
96 def send_command(self, cmd):
97 if not self.connected():
98 self.error_message = (
99 "Not connected, cannot send command %s\n" % (cmd))
102 LOG.info('Sending "%s" to %s' % (cmd, self.mgmt_ip))
103 stdin, stdout, stderr = self.ssh.exec_command(cmd)
105 for line in stdout.read().splitlines():
110 def __exit__(self, type, value, traceback):