def make_connection_from_context(ctx): """This is a driver wrapper handling platforms :param ctx: A context object :returns: driver class """ module_str = 'condor.platforms.%s' % (ctx.host.platform) __import__(module_str) module = sys.modules[module_str] driver_class = getattr(module, 'Connection') nodes = [] for url in ctx.host.urls: nodes.append(make_hop_info_from_url(url)) return driver_class( ctx.host.hostname, nodes, Controller)
def make_connection_from_urls( name, urls, platform='generic', account_manager=None): module_str = 'au.condor.platforms.%s' % (platform) try: __import__(module_str) module = sys.modules[module_str] driver_class = getattr(module, 'Connection') except ImportError: return None nodes = [] for url in urls: nodes.append(make_hop_info_from_url(url)) return driver_class( name, nodes, Controller, account_manager=account_manager)
def __init__(self, name, urls, log_dir=None, log_level=logging.DEBUG, log_session=True, account_manager=None): """This is the constructor. The *hostname* parameter is a string representing the name of the device. It is used mainly for verbose logging purposes. The *urls* parameter represents the list of urls used to define the connection way to the chain of devices before reaching the target device. It allows to have single device connection over multiple jumphosts. For example:: ["ssh://<user>:<password>@<host>", "telnet://<user>:<password>@<host>:<port>"] Currently there are two protocols supported: SSH and TELNET. The *port* is optional and if not specified the well known default protocol port is used. An IOS/IOS XE devices may require a privileged level password (enable password). The url parser treats the rest of the url (the path part + query + fragment part) as an privileged password. For example:: ["ssh://<user>:<password>@<host>", "telnet://<user>:<password>@<host>/<enable_password>"] The *host* could be either the hostname or ip address. There could be more devices in the list representing the single or multiple jumphosts on the path to the target device. The condoor connects to them in a sequence starting the next connection from the previous host unless reaching the target device. The last url in the list is always the target device. The *urls* parameter can be passed as a list of the list of urls. In that case condoor treats it as a multiple alternative connections to the same device. During the connection or discovery phase condoor starts with the first connection from the list and then move sequentially to next connection list in case of the previous connection failure or timeout. This process stops when the current connection to the target devices is successful. The first successful connection is latched and stored in the connection class and next time when connection to device is being reestablished the last successful connection list is used. This feature is useful for providing the console connection to two processor cards of the same device. It could be also used if the device has two alternative Mgmt interfaces with different IP addresses. For example:: [["ssh://<user>:<password>@<host>", "telnet://<user>:<password>@<ts1:2015>"], ["ssh://<user>:<password>@<host>", "telnet://<user>:<password>@<ts1:2016>"]] In above example the *urls* parameter represents two alternative connection lists. First goes through terminal server on port 2015 and second goes through the same terminal server on port 2016. Both connections uses the same jumphost. The *log_dir* parameter contains a string representing the full path to the logging directory. The logging directory can store the device session log and the detailed Condoor debug log. If there is no *log_dir* specified the current directory is used. The possible logging levels are as follows:: NOTSET=0 DEBUG=10 INFO=20 WARN=30 ERROR=40 CRITICAL=50 The default is DEBUG. If the *log_level* is set to 0 then no logging file is created. The *log_session* parameters defines whether the device session log is created or not. """ self._driver = None self._name = name self._hostname = name # normalise the urls to the list of the lists of str (for multiple console connections to the device) if isinstance(urls, list): if urls: if isinstance(urls[0], list): # multiple connections (list of the lists) self._urls = urls elif isinstance(urls[0], str): # single connections (make it list of the lists) self._urls = [urls] else: raise GeneralError("No target host url provided.") elif isinstance(urls, str): self._urls = [[urls]] nodes = [] for index, target in enumerate(self._urls): nodes.insert(index, list()) for url in target: nodes[index].append(make_hop_info_from_url(url)) self._nodes = nodes self._last_driver_index = 0 self._account_manager = account_manager self._platform = 'generic' self._os_type = None self._os_version = None self._family = None self._prompt = None self._log_dir = log_dir self._log_session = log_session self.logger = logging.getLogger('condoor') self._info = {} self._is_console = False self._udi = { "name": "", "description": "", "pid": "", "vid": "", "sn": "" } if log_level > 0: formatter = logging.Formatter('%(asctime)-15s %(levelname)8s: %(message)s') if log_dir: # Create the log directory. if not os.path.exists(log_dir): try: os.makedirs(log_dir) except IOError: log_dir = "./" log_filename = os.path.join(log_dir, 'condoor.log') handler = logging.FileHandler(log_filename) else: handler = logging.StreamHandler() log_dir = "./" handler.setFormatter(formatter) else: handler = logging.NullHandler() self._handler = handler self.logger.addHandler(self._handler) self.logger.setLevel(log_level) try: self._session_fd = open(os.path.join(log_dir, 'session.log'), mode="w") except IOError: self._session_fd = None self.logger.info("Condoor version {}".format(__version__))