Beispiel #1
0
    def run(self):
        _request = RequestHelpers()
        dict_schedule = dict()
        dict_schedule_queue = dict()
        list_time = list()
        queue_discovery = queue.Queue()

        # run queue listining discovery ------------------------------------------------------------------------------
        _ironQueue = IronQueue(queue_discovery)
        _ironQueue.start()
        # ------------------------------------------------------------------------------------------------------------

        while not self.is_stop:
            try:
                self.counter = self.counter + 1
                # -------------- IRONMAN RUN SCHEDULE ----------------------------------------------------------------
                # get current day name
                #weekday = datetime.now().strftime('%A')
                #_request.url = self.requestURL.IRONMAN_URL_GET_SCHEDULE % (weekday)
                _request.url = self.requestURL.IRONMAN_URL_GET_SCHEDULE
                _list_schedules = _request.get().json()
                if len(_list_schedules) > 0:
                    for x in _list_schedules:
                        key_mop = 'main_schedule_%d' % (x['mop_id'])
                        schedule_id = x['mop_id']
                        template_id = int(x['template_id'])
                        mechanism = x['run_type']
                        run_time = x['run_datetime'].split("-")[1].strip()
                        dict_schedule_queue[str(x['mop_id'])] = run_time

                        if dict_schedule.get(key_mop, None) is not None:
                            pass
                        else:
                            list_time.append(run_time)
                            _request.url = self.requestURL.MEGA_URL_TEMPLATE_DETAIL % (
                                str(template_id))
                            _template = _request.get().json()
                            dict_schedule[key_mop] = key_mop
                            schedule = Schedule("SCHEDULE-%d" % (schedule_id),
                                                x, _template, dict_schedule,
                                                False, mechanism, schedule_id,
                                                queue_discovery,
                                                x['output_mapping'])
                            schedule.start()
                            time.sleep(2)

                stringhelpers.print_bold(
                    "IRONMAN SCHEDULE RUN NUMBER: " + str(self.counter), "\n")
            except Exception as e:
                stringhelpers.print_bold("IRONMAN SCHEDULE [ERROR]: " + str(e),
                                         "\n")

            time.sleep(30)

        # stop iron queue ----------------------------------------------------------------------------------------------
        _ironQueue.stop()
Beispiel #2
0
    def run(self):
        _request = RequestHelpers()
        dict_template = dict()
        while not self.is_stop:
            try:
                self.counter = self.counter + 1
                stringhelpers.print_bold("Archieving info FLASK number: %d" %
                                         self.counter)

                #-------------- FLASK RUN MOP ----------------------------------------------------------------
                _request.url = self.requestURL.FLASK_URL_MOP
                #_list_templates = _request.get().json()
                _list_mops = _request.get().json()
                if len(_list_mops) > 0:
                    for x in _list_mops:
                        key_mop = 'template_%d' % (x['mop_id'])
                        mop_id = x['mop_id']
                        if dict_template.get(key_mop, None) is not None:
                            pass
                        else:
                            _request.url = self.requestURL.MEGA_URL_TEMPLATE_DETAIL % (
                                x['template_id'])
                            _template = _request.get().json()

                            #-------------------- run device from mop -------------------------------------------
                            array_device_mop = x['devices']
                            run_devices = {}
                            for item in array_device_mop:
                                run_devices[str(item)] = "MOP"

                            _template['run_devices'] = run_devices
                            #------------------------------------------------------------------------------------
                            try:
                                _template['run_args'] = x['run_args']
                                _template['rollback_args'] = x['rollback_args']
                            except:
                                pass

                            dict_template[key_mop] = key_mop
                            flask_template = FlaskTemplate(
                                "FLASK-Thread-Template-%s" %
                                (x['template_id']), _template, dict_template,
                                mop_id)
                            flask_template.start()

                # ---------------------------------------------------------------------------------------------------------
                time.sleep(30)

            except Exception as e:
                stringhelpers.err("FLASK MAIN THREAD ERROR %s" % (e))
            except ConnectionError as errConn:
                stringhelpers.err("FLASK CONNECT API ERROR %s" % (errConn))
Beispiel #3
0
    def run(self):
        _request = RequestHelpers()
        dict_template = dict()
        while not self.is_stop:
            try:
                self.counter = self.counter + 1
                stringhelpers.print_bold("Archieving info FLASK number: %d" %
                                         self.counter)
                #-------------- FLASK RUN MOP ----------------------------------------------------------------
                _request.url = self.requestURL.FLASK_URL_MOP
                #_list_templates = _request.get().json()
                _list_mops = _request.get().json()
                if len(_list_mops) > 0:
                    for x in _list_mops:
                        mop_id = int(x['mop_id'])
                        key_mop = 'flask_mop_%d' % (mop_id)

                        if dict_template.get(key_mop, None) is not None:
                            pass
                        else:
                            sub_mops = x['sub_mops']
                            dict_template[key_mop] = key_mop

                            status = 'running'
                            self.update_mop_status(status, mop_id)
                            start_time = time()
                            #for item_mop in sub_mops:
                            flask_sub_mop = FlaskSubMop(
                                "FLASK-Thread-MOP-%d" % (mop_id), sub_mops,
                                mop_id)
                            flask_sub_mop.start()
                            status = flask_sub_mop.join()

                            self.update_mop_status(status, mop_id, start_time,
                                                   time())

                            del dict_template[key_mop]

                # ---------------------------------------------------------------------------------------------------------
                sleep(30)

            except Exception as e:
                stringhelpers.err("FLASK MAIN THREAD ERROR %s" % (e))
            except ConnectionError as errConn:
                stringhelpers.err("FLASK CONNECT API ERROR %s" % (errConn))
Beispiel #4
0
    def run(self):
        _request = RequestHelpers()
        dict_command = dict()
        dict_action = dict()
        dict_template = dict()
        dict_template_discovery = dict()
        while not self.is_stop:
            try:
                self.counter = self.counter + 1
                stringhelpers.print_bold("Archieving info MEGA number: %d" %
                                         self.counter)

                #--------------- MEGA RUN COMMAND TEST -----------------------------------------------------------------
                _request.url = self.requestURL.MEGA_URL_LIST_COMMAND_UNTESTED
                _list_commands = _request.get().json()
                if len(_list_commands) > 0:
                    for x in _list_commands:
                        key_command = 'command_%d' % (x['command_id'])
                        if dict_command.get(key_command, None) is not None:
                            pass
                        else:
                            dict_command[key_command] = key_command
                            mega_command = MegaCommand(
                                "Thread-Command-%d" % (x['command_id']), x,
                                dict_command)
                            mega_command.start()
                time.sleep(10)
                #-------------------------------------------------------------------------------------------------------

                #-------------- MEGA RUN ACTION TEST -------------------------------------------------------------------
                _request.url = self.requestURL.MEGA_URL_LIST_ACTION_UNTESTED
                _list_actions = _request.get().json()
                if len(_list_actions) > 0:
                    for x in _list_actions:
                        key_action = 'action_%d' % (x['action_id'])
                        if dict_action.get(key_action, None) is not None:
                            pass
                        else:
                            dict_action[key_action] = key_action
                            mega_action = MegaAction(
                                "Thread-Action-%d" % (x['action_id']), x,
                                dict_action)
                            mega_action.start()
                time.sleep(10)
                #-------------------------------------------------------------------------------------------------------

                #-------------- MEGA RUN TEMPLATE TEST ----------------------------------------------------------------
                _request.url = self.requestURL.MEGA_URL_LIST_TEMPLATE_UNTESTED
                _list_templates = _request.get().json()
                #_list_templates = [_request.get().json()]
                if len(_list_templates) > 0:
                    for x in _list_templates:
                        key_template = 'template_%d' % (x['template_id'])
                        if dict_template.get(key_template, None) is not None:
                            pass
                        else:
                            dict_template[key_template] = key_template
                            mega_template = MegaTemplate(
                                "Thread-Template-%d" % (x['template_id']), x,
                                dict_template)
                            mega_template.start()
                    pass
                # ---------------------------------------------------------------------------------------------------------
                time.sleep(10)

                #-------------- MEGA RUN TEMPLATE TEST ----------------------------------------------------------------
                '''_request.url = self.requestURL.MEGA_URL_TEMPLATE_DISCOVERY
                #_list_templates = _request.get().json()
                _list_templates_descovery = [_request.get().json()]
                if len(_list_templates_descovery) > 0:
                    for x in _list_templates_descovery:
                        key_template = 'template_discovery_%d' % (x['template_id'])
                        #if dict_template_discovery.get(key_template, None) is not None:
                        #    pass
                        #else:
                        dict_template_discovery[key_template] = key_template
                        mega_template_discovery = MegaDiscovery("Thread-Discovery-Template-%d" % (x['template_id']), x, dict_template_discovery)
                        mega_template_discovery.start()
                    pass'''
                # ---------------------------------------------------------------------------------------------------------
                #time.sleep(90)

            except Exception as e:
                stringhelpers.err("MEGA MAIN THREAD ERROR %s" % (e))
            except ConnectionError as errConn:
                stringhelpers.err("MEGA CONNECT API ERROR %s" % (errConn))
    def run(self):
        _request = RequestHelpers()
        _request.url = self.requestURL.URL_GET_DEVICE_DETAIL % (
            self.data_command["test_device"])

        try:
            device = _request.get().json()
            try:
                if device['status_code'] == 500:  #device not exist
                    stringhelpers.err(
                        "DEVICE ID %s NOT EXIST | THREAD %s" %
                        (self.data_command["test_device"], self.name))
            except:  #process fang test device by command
                host = device['ip_mgmt']
                port = int(device['port_mgmt'])
                username = device['username']
                password = device['password']
                device_type = device['os']
                method = device['method']  #ssh, telnet
                parameters = {
                    'device_type': device_type,
                    'host': host,
                    'protocol': method,
                    'username': username,
                    'password': password,
                    'port': port
                }
                '''process command contains params'''
                command = None
                test_args = self.data_command['test_args']
                if len(test_args) > 0:
                    command = self.data_command['command']
                    for x in self.data_command['test_args']:
                        command = command.replace('@{%s}' % (x['name']),
                                                  x['value'])
                else:
                    command = self.data_command['command']
                '''####################################'''

                commands = [command]
                fac = FactoryConnector(**parameters)
                print("FANG DEVICE: host=%s, port=%s, devicetype=%s \n\n" %
                      (host, device['port_mgmt'], device_type))
                fang = fac.execute(commands)
                result_fang = fang.get_output()
                fang.remove_file_log(fac.file_log_command)  #remove file.log

                _request.url = self.requestURL.MEGA_URL_COMMANDLOG_GETBY_COMMANDID % (
                    self.data_command['command_id'])
                command_log = _request.get().json()
                if len(command_log) > 0:
                    cmd_log = {
                        'command_id': self.data_command['command_id'],
                        'device_id': self.data_command["test_device"],
                        'console_log': result_fang,
                        'result': dict(),
                        'command': command
                    }

                    # processing parsing command follow output ###########################################
                    command_type = self.data_command['type']
                    cmd_log = self.parsing(command_type, cmd_log)
                    ######################################################################################

                    try:
                        _request.url = self.requestURL.MEGA_URL_COMMANDLOG_UPDATE % (
                            command_log[0]['log_id'])
                        _request.params = cmd_log
                        _request.put()
                        stringhelpers.info(
                            "MEGA THREAD INFO: %s | THREAD %s" %
                            ("UPDATE COMMAND LOG SUCCESS", self.name))

                        #---------------update mega_status to command------------------------------------------------
                        _request.url = self.requestURL.MEGA_URL_COMMAND_UPDATE % (
                            self.data_command['command_id'])
                        _request.params = {'mega_status': 'tested'}
                        _request.put()
                        key_command = 'command_%d' % (
                            self.data_command['command_id'])
                        del self.dict_command[key_command]
                        #--------------------------------------------------------------------------------------------
                    except ConnectionError as _conErr:
                        stringhelpers.info(
                            "MEGA THREAD ERROR: %s | THREAD %s" %
                            (_conErr, self.name))
                else:
                    cmd_log = {
                        'command_id': self.data_command['command_id'],
                        'device_id': self.data_command["test_device"],
                        'console_log': result_fang,
                        'result': dict(),
                        'command': command
                    }

                    # processing parsing command follow output ###########################################
                    command_type = self.data_command['type']
                    cmd_log = self.parsing(command_type, cmd_log)
                    ######################################################################################

                    try:
                        _request.url = self.requestURL.MEGA_URL_COMMANDLOG_CREATE
                        _request.params = cmd_log
                        _request.post()
                        stringhelpers.info(
                            "MEGA THREAD INFO: %s | THREAD %s" %
                            ("INSERT COMMAND LOG SUCCESS", self.name))
                        # ---------------update mega_status to command------------------------------------------------
                        _request.url = self.requestURL.MEGA_URL_COMMAND_UPDATE % (
                            self.data_command['command_id'])
                        _request.params = {'mega_status': 'tested'}
                        _request.put()
                        key_command = 'command_%d' % (
                            self.data_command['command_id'])
                        del self.dict_command[key_command]
                        # --------------------------------------------------------------------------------------------
                    except ConnectionError as _conErr:
                        stringhelpers.info(
                            "MEGA THREAD ERROR: %s | THREAD %s" %
                            (_conErr, self.name))

        except Exception as e:
            stringhelpers.err("MEGA THREAD ERROR %s | THREAD %s" %
                              (e, self.name))
        except ConnectionError as errConn:
            stringhelpers.err("MEGA CONNECT API URL ERROR %s | THREAD %s" %
                              (_request.url, self.name))
Beispiel #6
0
from api.request_helpers import RequestHelpers
from api.request_url import RequestURL

if __name__ == '__main__':
    reqHelpers = RequestHelpers()
    reqHelpers.url = 'http://27.78.16.56:8020/v1/template_logs.json'
    res = reqHelpers.get_test()
    def run(self):
        _request = RequestHelpers()
        dict_schedule = dict()
        dict_schedule_queue = dict()
        list_time = list()
        queue_discovery = queue.Queue()

        # run queue listining discovery ------------------------------------------------------------------------------
        _ironQueue = IronQueue(queue_discovery, self.socketio,
                               self.socketio_iron)
        _ironQueue.start()

        # ------------------------------------------------------------------------------------------------------------

        while not self.is_stop:
            try:
                self.counter = self.counter + 1
                arr_schedule_manage = []
                # -------------- IRONMAN RUN SCHEDULE ----------------------------------------------------------------
                #stringhelpers.print_bold("IRONMAN SCHEDULE RUN NUMBER: " + str(self.counter), "\n")
                # get current day name
                #weekday = datetime.now().strftime('%A')
                #_request.url = self.requestURL.IRONMAN_URL_GET_SCHEDULE % (weekday)
                _request.url = self.requestURL.IRONMAN_URL_GET_MOP_RUN_IRON
                _list_schedules = _request.get().json()
                if len(_list_schedules) > 0:
                    for x in _list_schedules:
                        key_mop = 'main_schedule_%d' % (int(x['mop_id']))
                        mop_id = int(x['mop_id'])
                        mechanism = x['run_type']
                        run_time = x['run_datetime'].split("-")[1].strip()
                        dict_schedule_queue[str(x['mop_id'])] = run_time
                        if dict_schedule.get(key_mop, None) is not None:
                            pass
                        else:
                            stringhelpers.info(
                                "IRONMAN RUNNING MOP: " + str(x['mop_id']),
                                "\n")
                            list_time.append(run_time)
                            _sub_mops = x.get('sub_mops', None)
                            dict_schedule[key_mop] = key_mop

                            schedule = Schedule(
                                "SCHEDULE-%d" % (mop_id), x, _sub_mops,
                                dict_schedule, False, mechanism, mop_id,
                                queue_discovery, x['output_mapping'],
                                self.socketio, self.socketio_iron)
                            #arr_schedule_manage.append(schedule)

                            #-----------------------process call api sockbot mop---------------------------------------

                            params_mops = dict()
                            params_mops['name'] = x['name']
                            params_mops['app_secret_id'] = os.environ.get(
                                'SOCKBOT_APPCLIENT_SECRET')
                            params_mops['mop_id'] = x['mop_id']
                            params_mops['status'] = 'RUNNING'
                            params_mops['belong'] = 'IRONMAN'
                            params_mops['command'] = 'STARTED'
                            submops = []
                            for x_sub in _sub_mops:
                                arr_devices = []
                                for k, v in x_sub['devices'].items():
                                    arr_devices.append({
                                        'mop_id':
                                        x['mop_id'],
                                        'device_id':
                                        v['device_id'],
                                        'vendor':
                                        '%s|%s' % (v['vendor'], v['os']),
                                        'port':
                                        v['port']
                                    })
                                submops.append({
                                    'subNo': x_sub['subNo'],
                                    'name': x_sub['name'],
                                    'devices': arr_devices,
                                    'num_devices': len(arr_devices),
                                    'mop_id': x['mop_id']
                                })
                            params_mops['submops'] = submops
                            self._sockbotAPIHelpers.url = self._sockbotAPIURL.SOCKBOT_API_CREATE_MOP
                            self._sockbotAPIHelpers.params = params_mops
                            self._sockbotAPIHelpers.post_json()

                            schedule.start()

                            #------------------------------------------------------------------------------------------

                if len(arr_schedule_manage) > 0:
                    #for schedule in arr_schedule_manage:
                    #    schedule.start()

                    arr_schedule_manage.clear()
            except Exception as e:
                stringhelpers.print_bold("IRONMAN SCHEDULE [ERROR]: " + str(e),
                                         "\n")

            time.sleep(60)

        # stop iron queue ----------------------------------------------------------------------------------------------
        _ironQueue.stop()