Skip to content

Commit

Permalink
Merge pull request #14 from QualiSystems/debug_mode
Browse files Browse the repository at this point in the history
Debug mode
  • Loading branch information
yaroslavNqualisystems committed Dec 13, 2017
2 parents c5f049f + 569ba5b commit 4dcd99d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
25 changes: 24 additions & 1 deletion cloudshell/layer_one/core/driver_listener.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import socket
import sys
import time
from abc import ABCMeta

from cloudshell.layer_one.core.connection_handler import ConnectionHandler
from cloudshell.layer_one.core.helper.runtime_configuration import RuntimeConfiguration


class DriverListener(object):
Expand All @@ -22,6 +26,8 @@ def __init__(self, command_executor, xml_logger, command_logger):
self._xml_logger = xml_logger
self._is_running = False

self._debug_mode = RuntimeConfiguration().read_key('DEBUG_ENABLED', False)

def _initialize_socket(self, host, port):
"""
Initialize socket, and start listening
Expand All @@ -44,6 +50,16 @@ def _initialize_socket(self, host, port):
def set_running(self, is_running):
self._is_running = is_running

def _wait_for_debugger_attach(self):
pid = str(os.getpid())

while not sys.gettrace():
self._command_logger.info(
"Waiting for a debugger to attach to this python driver process. (PID #{})".format(pid))
time.sleep(2)

self._command_logger.info("Debugger attached. (PID #{})".format(pid))

def start_listening(self, host=None, port=None):
"""Initialize socket and start listening"""
host = host if host else self.SERVER_HOST
Expand All @@ -56,4 +72,11 @@ def start_listening(self, host=None, port=None):
if connection is not None:
request_handler = ConnectionHandler(connection, self._command_executor, self._xml_logger,
self._command_logger)
request_handler.start()
if self._debug_mode:
self._wait_for_debugger_attach()
try:
request_handler.run()
except:
self._command_logger.exception('ConnectionHandler Error')
else:
request_handler.start()
10 changes: 5 additions & 5 deletions cloudshell/layer_one/core/helper/runtime_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ def _read_configuration(self, config_path):
def _merge_configs(self, config_a, config_b):
return config_b

def read_key(self, complex_key):
def read_key(self, complex_key, default_value=None):
"""
Value for complex key like CLI.PORTS
:param complex_key:
:param complex_key:
:param default_value: Default value
:return:
"""
value = self.configuration
Expand All @@ -71,6 +72,5 @@ def read_key(self, complex_key):
value = None
break

if not value:
raise Exception(self.__class__.__name__, 'Incorrect key {} or value is not defined'.format(complex_key))
return value
return value or default_value

0 comments on commit 4dcd99d

Please sign in to comment.