summaryrefslogtreecommitdiff
path: root/cxmanage_api/ip_retriever.py
diff options
context:
space:
mode:
Diffstat (limited to 'cxmanage_api/ip_retriever.py')
-rw-r--r--cxmanage_api/ip_retriever.py48
1 files changed, 27 insertions, 21 deletions
diff --git a/cxmanage_api/ip_retriever.py b/cxmanage_api/ip_retriever.py
index 411465b..ef443cb 100644
--- a/cxmanage_api/ip_retriever.py
+++ b/cxmanage_api/ip_retriever.py
@@ -1,6 +1,7 @@
-#!/usr/bin/env python
+"""Calxeda: ip_retriever.py"""
-# Copyright (c) 2012, Calxeda Inc.
+
+# Copyright (c) 2012-2013, Calxeda Inc.
#
# All rights reserved.
#
@@ -30,6 +31,7 @@
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
+
import sys
import re
import json
@@ -45,9 +47,10 @@ from pyipmi.server import Server
from pyipmi.bmc import LanBMC
+# pylint: disable=R0902
class IPRetriever(threading.Thread):
- """The IPRetriever class takes an ECME address and when run will
- connect to the Linux Server from the ECME over SOL and use
+ """The IPRetriever class takes an ECME address and when run will
+ connect to the Linux Server from the ECME over SOL and use
ifconfig to determine the IP address.
"""
verbosity = None
@@ -55,7 +58,7 @@ class IPRetriever(threading.Thread):
retry = None
timeout = None
interface = None
-
+
ecme_ip = None
ecme_user = None
ecme_password = None
@@ -63,14 +66,14 @@ class IPRetriever(threading.Thread):
server_ip = None
server_user = None
server_password = None
-
+
def __init__(self, ecme_ip, aggressive=False, verbosity=0, **kwargs):
"""Initializes the IPRetriever class. The IPRetriever needs the
only the first node to know where to start.
"""
super(IPRetriever, self).__init__()
self.daemon = True
-
+
if hasattr(ecme_ip, 'ip_address'):
self.ecme_ip = ecme_ip.ip_address
else:
@@ -78,7 +81,7 @@ class IPRetriever(threading.Thread):
self.aggressive = aggressive
self.verbosity = verbosity
-
+
# Everything here is optional
self.timeout = kwargs.get('timeout', 120)
self.retry = kwargs.get('retry', 0)
@@ -95,20 +98,20 @@ class IPRetriever(threading.Thread):
self._ip_pattern = kwargs['_ip_pattern']
else:
- self.set_interface(kwargs.get('interface', None),
+ self.set_interface(kwargs.get('interface', None),
kwargs.get('ipv6', False))
if 'bmc' in kwargs:
self._bmc = kwargs['bmc']
else:
- self._bmc = make_bmc(LanBMC, verbose=(self.verbosity>1),
- hostname=self.ecme_ip,
+ self._bmc = make_bmc(LanBMC, verbose=(self.verbosity > 1),
+ hostname=self.ecme_ip,
username=self.ecme_user,
password=self.ecme_password)
if 'config_path' in kwargs:
self.read_config(kwargs['config_path'])
-
+
def set_interface(self, interface=None, ipv6=False):
@@ -119,11 +122,13 @@ class IPRetriever(threading.Thread):
self.interface = interface
if not ipv6:
- self._ip_pattern = re.compile('\d+\.'*3 + '\d+')
+ self._ip_pattern = re.compile(r'\d+\.' * 3 + r'\d+')
self._inet_pattern = re.compile('inet addr:(%s)' %
self._ip_pattern.pattern)
else:
- self._ip_pattern = re.compile('[0-9a-fA-F:]*:'*2 + '[0-9a-fA-F:]+')
+ self._ip_pattern = re.compile(
+ '[0-9a-fA-F:]*:' * 2 + '[0-9a-fA-F:]+'
+ )
self._inet_pattern = re.compile('inet6 addr: ?(%s)' %
self._ip_pattern.pattern)
@@ -137,14 +142,14 @@ class IPRetriever(threading.Thread):
def run(self):
- """Attempts to finds the server IP address associated with the
+ """Attempts to finds the server IP address associated with the
ECME IP. If successful, server_ip will contain the IP address.
"""
if self.server_ip is not None:
self._log('Using stored IP %s' % self.server_ip)
return
- for attempt in range(self.retry + 1):
+ for _ in range(self.retry + 1):
self.server_ip = self.sol_try_command(self.sol_find_ip)
if self.server_ip is not None:
@@ -160,7 +165,7 @@ class IPRetriever(threading.Thread):
"""
server = Server(self._bmc)
- if cycle:
+ if cycle:
self._log('Powering server off')
server.power_off()
sleep(5)
@@ -201,15 +206,16 @@ class IPRetriever(threading.Thread):
raise IPDiscoveryError('Could not find interface %s'
% self.interface)
- else: # Failed to find interface. Returning None
+ else: # Failed to find interface. Returning None
return None
-
+
+ # pylint: disable=R0912, R0915
def sol_try_command(self, command):
"""Connects to the server over a SOL connection. Attempts
to run the given command on the server without knowing
the state of the server. The command must return None if
- it fails. If aggresive is True, then the server may be
+ it fails. If aggresive is True, then the server may be
restarted or power cycled to try and reset the state.
"""
server = Server(self._bmc)
@@ -303,7 +309,7 @@ class IPRetriever(threading.Thread):
else:
# Assume where are at a prompt and able to run the command
value = command(session)
-
+
if value is not None:
self._bmc.deactivate_payload()
return value