Esempio n. 1
0
    def __init__(self, comp_name, zkclient, nodepath,
                 operational=False, parent=None, interval=5):
        """
        :type comp_name: str
        :type zkclient: kazoo.client.KazooClient
        :type nodepath: str
        :type operational: bool
        :type parent: str or None
        :type interval: int or float
        """
        SimplePredicate.__init__(self, comp_name, operational=operational, parent=parent)
        self.node = nodepath
        self.zkclient = zkclient
        self.interval = interval
        self._start = None
        self._stop = None
        self._log = logging.getLogger('sent.{0}.pred.gut'.format(comp_name))
        self._log.info('Registered {0}'.format(self))

        self._operate = ThreadSafeObject(True)
        self._thread = Thread(target=self._run_loop, name=str(self))
        self._thread.daemon = True
        self._started = False

        self._datetime_regex = (
            "^((?P<year>\d{4})\-(?P<month>\d{2})\-(?P<day>\d{2})\s)?"
            "(?P<hour>\d{2}):(?P<minute>\d{2})(:(?P<second>\d{2}))?"
        )
Esempio n. 2
0
    def __init__(self,
                 comp_name,
                 command,
                 interval,
                 system,
                 operational=False,
                 parent=None):
        """
        :type comp_name: str
        :type command: str
        :type interval: int or float
        :type system: zoom.common.types.PlatformType
        :type operational: bool
        :type parent: str or None
        """
        SimplePredicate.__init__(self,
                                 comp_name,
                                 operational=operational,
                                 parent=parent)
        self._log = logging.getLogger('sent.{0}.pred.health'.format(comp_name))
        self.interval = interval
        self.rawcmd = command
        self._runcmd = str()
        self._system = system
        self._verify()

        self._operate = ThreadSafeObject(True)
        self._thread = Thread(target=self._run_loop, name=str(self))
        self._thread.daemon = True
        self._log.info('Registered {0}'.format(self))
        self._started = False
Esempio n. 3
0
    def __init__(self,
                 comp_name,
                 proc_client,
                 interval,
                 operational=False,
                 parent=None):
        """
        :type comp_name: str
        :type proc_client: zoom.agent.client.process_client.ProcessClient
        :type interval: int or float
        :type operational: bool
        :type parent: str or None
        """
        SimplePredicate.__init__(self,
                                 comp_name,
                                 operational=operational,
                                 parent=parent)
        self._log = logging.getLogger(
            'sent.{0}.pred.process'.format(comp_name))
        self._proc_client = proc_client

        # lock for synchronous decorator
        if proc_client:
            self.process_client_lock = proc_client.process_client_lock
        else:
            self.process_client_lock = Lock()

        self.interval = interval
        self._operate = ThreadSafeObject(True)
        self._thread = Thread(target=self._run_loop, name=str(self))
        self._thread.daemon = True
        self._started = False
Esempio n. 4
0
    def __init__(self,
                 comp_name,
                 begin=None,
                 end=None,
                 weekdays=None,
                 operational=False,
                 parent=None,
                 interval=5):
        """
        :type comp_name: str
        :type begin: str or None
        :type end: str or None
        :type weekdays: str or None
        :type operational: bool
        :type parent: str or None
        :type interval: int or float
        """
        SimplePredicate.__init__(self,
                                 comp_name,
                                 operational=operational,
                                 parent=parent)
        self.begin = self.get_datetime_object(begin)
        self.end = self.get_datetime_object(end)
        self.day_range = self.parse_range(weekdays)
        self.interval = interval
        self._log = logging.getLogger(
            'sent.{0}.pred.timewin'.format(comp_name))
        self._log.info('Registered {0}'.format(self))

        self._operate = ThreadSafeObject(True)
        self._thread = Thread(target=self._run_loop, name=str(self))
        self._thread.daemon = True
        self._started = False
Esempio n. 5
0
    def __init__(self,
                 comp_name,
                 zkclient,
                 path,
                 operational=False,
                 parent=None,
                 interval=10):
        """
        :type comp_name: str
        :type zkclient: kazoo.client.KazooClient
        :type path: str or None
        :type operational: bool
        :type parent: str or None
        :type interval: int or float
        """
        SimplePredicate.__init__(self,
                                 comp_name,
                                 operational=operational,
                                 parent=parent)
        self.zkclient = zkclient
        self.interval = interval
        self.path = path
        self._log = logging.getLogger('sent.{0}.holiday'.format(comp_name))
        self._log.info('Registered {0}'.format(self))

        self._operate = ThreadSafeObject(True)
        self._thread = Thread(target=self._run_loop, name=str(self))
        self._thread.daemon = True
        self._started = False
        self._holidays = list()
Esempio n. 6
0
    def __init__(self,
                 comp_name,
                 url,
                 verb='GET',
                 expected_code=200,
                 interval=5.0,
                 operational=False,
                 parent=None):
        """
        :type comp_name: str
        :type url: str
        :type verb: str
        :type expected_code: int
        :type interval: int or float
        :type operational: bool
        :type parent: str or None
        """
        SimplePredicate.__init__(self,
                                 comp_name,
                                 operational=operational,
                                 parent=parent)
        self._log = logging.getLogger('sent.{0}.pred.api'.format(comp_name))
        logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(
            logging.WARNING)
        self.url = url
        self.verb = verb
        self.expected_code = expected_code
        self.interval = interval

        self._operate = ThreadSafeObject(True)
        self._thread = Thread(target=self._run_loop, name=str(self))
        self._thread.daemon = True
        self._log.info('Registered {0}'.format(self))
        self._started = False
Esempio n. 7
0
    def setUp(self):
        self.mox = mox.Mox()
        self.comp_name = "Test Predicate Or"

        self.predat = SimplePredicate("a", ThreadSafeObject({}))
        self.predat.set_met(True)
        self.predbt = SimplePredicate("b", ThreadSafeObject({}))
        self.predbt.set_met(True)

        self.predaf = SimplePredicate("a", ThreadSafeObject({}))
        self.predbf = SimplePredicate("b", ThreadSafeObject({}))

        self.list = [self.predaf, self.predbf, self.predat, self.predbt]

        self.factory = PredicateFactory(component_name="factory",
                                        zkclient=None,
                                        proc_client=None,
                                        system=None,
                                        pred_list=self.list,
                                        settings={})
Esempio n. 8
0
 def __init__(self, comp_name, queue, work_dict):
     """
     :type comp_name: str
     :type queue: zoom.agent.entities.unique_queue.UniqueQueue
     :type work_dict: dict
     """
     self._operate = ThreadSafeObject(True)
     self._thread = Thread(target=self._run,
                           name='work_manager',
                           args=(self._operate, queue, work_dict))
     self._thread.daemon = True
     self._log = logging.getLogger('sent.{0}.wm'.format(comp_name))
Esempio n. 9
0
    def __init__(self, config, system, settings):
        """
        :type config: xml.etree.ElementTree.Element
        :type system: zoom.common.types.PlatformType
        :type settings: dict
        """
        self._log = logging.getLogger('sent.child')
        self._action_queue = UniqueQueue()
        self._cancel_flag = ThreadSafeObject(False)

        self.name = verify_attribute(config, 'id')
        self._application_type = verify_attribute(config, 'type')
        self._config = config
        self._system = system  # Linux or Windows
        self._settings = settings
        self._process = self._create_process()
Esempio n. 10
0
    def __init__(self, comp_name, operational=False, parent=None, interval=10):
        """
        :type comp_name: str
        :type operational: bool
        :type parent: str or None
        :type interval: int or float
        """
        SimplePredicate.__init__(self,
                                 comp_name,
                                 operational=operational,
                                 parent=parent)
        self.interval = interval
        self._log = logging.getLogger('sent.{0}.weekend'.format(comp_name))
        self._log.info('Registered {0}'.format(self))

        self._operate = ThreadSafeObject(True)
        self._thread = Thread(target=self._run_loop, name=str(self))
        self._thread.daemon = True
        self._started = False
Esempio n. 11
0
 def test_no_match(self):
     new = SimplePredicate("c", ThreadSafeObject({}))
     ret = self.factory._ensure_new(new)
     self.assertTrue(new is ret)
Esempio n. 12
0
    def __init__(self,
                 name,
                 component_name,
                 action,
                 xmlpart,
                 staggerpath=None,
                 staggertime=None,
                 mode_controlled=False,
                 action_q=None,
                 zkclient=None,
                 proc_client=None,
                 mode=None,
                 system=None,
                 pred_list=None,
                 settings=None,
                 disabled=False,
                 pd_enabled=True,
                 op_action=None,
                 pd_reason=None,
                 app_state=None):
        """
        :param action: The function to run when all the action's predicates are met
        :param xmlpart: The part of XML pertaining to this Action
        :param mode_controlled: Whether or not the action will run based on the ApplicationMode
        :param op_action: The function to run if this action's operation dependencies go down.
        :type name: str
        :type component_name: str
        :type action: types.FunctionType
        :type xmlpart: xml.etree.ElementTree.Element
        :type staggerpath: str
        :type staggertime: int
        :type mode_controlled: bool
        :type action_q: zoom.agent.entities.unique_queue.UniqueQueue
        :type zkclient: kazoo.client.KazooClient
        :type proc_client: zoom.agent.client.process_client.ProcessClient
        :type mode: zoom.agent.entities.thread_safe_object.ApplicationMode
        :type system: zoom.common.types.PlatformType
        :type pred_list: list
        :type settings: dict
        :type disabled: bool
        :type pd_enabled: bool
        :type op_action: types.FunctionType or None
        :type pd_reason: str or None
        :type app_state: zoom.agent.entities.thread_safe_object.ThreadSafeObject
        """
        self.name = name
        self.disabled = disabled
        self.component_name = component_name
        self._log = logging.getLogger('sent.{0}.act'.format(component_name))
        self._action = action
        self._action_queue = action_q
        self._mode_controlled = mode_controlled
        self._mode = mode
        self._pd_enabled = pd_enabled
        self._op_action = op_action
        self._pd_reason = pd_reason
        self._acquire_lock = ThreadSafeObject(True)

        if staggerpath is not None and staggertime is not None:
            self._stag_lock = StaggerLock(staggerpath,
                                          staggertime,
                                          parent=self.component_name,
                                          acquire_lock=self._acquire_lock,
                                          app_state=app_state)
            self._log.info('Using {0}'.format(self._stag_lock))
        else:
            self._stag_lock = None

        factory = PredicateFactory(component_name=component_name,
                                   action=self.name,
                                   zkclient=zkclient,
                                   proc_client=proc_client,
                                   system=system,
                                   pred_list=pred_list,
                                   settings=settings)
        self._predicate = factory.create(
            xmlpart.find('./Dependency/Predicate'), callback=self._callback)
Esempio n. 13
0
    def __init__(self, config, settings, queue, system, application_type,
                 cancel_flag):
        """
        :type config: dict (xml)
        :type settings: dict
        :type queue: zoom.agent.entities.unique_queue.UniqueQueue
        :type system: zoom.common.types.PlatformType
        :type application_type: zoom.common.types.ApplicationType
        :type cancel_flag: zoom.agent.entities.thread_safe_object.ThreadSafeObject
        """
        self.config = config
        self._settings = settings
        self.name = verify_attribute(self.config, 'id', none_allowed=False)
        self._log = logging.getLogger('sent.{0}.app'.format(self.name))
        # informational attributes
        self._host = socket.getfqdn()
        self._system = system
        self._predicates = list()
        self._running = True  # used to manually stop the run loop
        self._prev_state = None
        self._actions = dict()  # created in _reset_watches on zk connect
        self._env = os.environ.get('EnvironmentToUse', 'Staging')
        self._apptype = application_type
        self._restart_on_crash = \
            verify_attribute(self.config, 'restart_on_crash', none_allowed=True)
        self._post_stop_sleep = verify_attribute(self.config,
                                                 'post_stop_sleep',
                                                 none_allowed=True,
                                                 cast=int,
                                                 default=5)

        # tool-like attributes
        self.listener_lock = Lock()
        self._action_queue = queue
        self._mode = ApplicationMode(
            ApplicationMode.MANUAL,
            callback=self._update_agent_node_with_app_details)
        self._state = ThreadSafeObject(
            ApplicationState.OK,
            callback=self._update_agent_node_with_app_details)
        self._start_stop_time = ''  # Default to empty string for comparison
        self._login_user = '******'  # Default to Zoom
        self._user_set_in_react = False
        self._run_check_mode = False
        self._pd_svc_key = verify_attribute(config,
                                            'pagerduty_service',
                                            none_allowed=True)

        restartmax = verify_attribute(config,
                                      'restartmax',
                                      none_allowed=True,
                                      cast=int,
                                      default=3)
        self._rl = RestartLogic(
            self.name,
            restartmax,
            count_callback=self._update_agent_node_with_app_details)

        self._read_only = False

        self._paths = self._init_paths(self.config, settings, application_type)

        # clients
        self.zkclient = KazooClient(hosts=get_zk_conn_string(),
                                    timeout=60.0,
                                    handler=SequentialThreadingHandler(),
                                    logger=logging.getLogger(
                                        'kazoo.app.{0}'.format(self.name)))

        self.zkclient.add_listener(self._zk_listener)
        self._proc_client = self._init_proc_client(self.config,
                                                   application_type,
                                                   cancel_flag)

        self._actions = self._init_actions(settings)
        self._work_manager = self._init_work_manager(self._action_queue)