def __init__(self):
     super().__init__()
     self.user = user_service.find_user_by_id(self.user_id)
     self.routers = router_service.get_router_list()
     self.router_to_update = self.request_dict.selected_router
     self.router_name = self.request_dict.name
     self.router_ip = self.request_dict.router_ip
     self.username = self.request_dict.username
     self.password = self.request_dict.password
     self.ignore: Optional[str] = None
def run():
    # setting start time for sync to async comparison
    start_time = datetime.datetime.now()

    # gathering list all routers and list of routers to be ignored and router count
    routers = router_service.get_router_list()
    ignored_routers = router_service.get_router_ignore_list()
    router_count = router_service.get_router_count()

    loop = asyncio.get_event_loop()

    for item in tqdm(routers, total=router_count, unit=" router"):
        if item.router_name in ignored_routers:
            logging.info(f"Backup skipped for {item.router_name}")
            tqdm.write(f"Backup skipped for {item.router_name}")
            backup_status = "Backup Skipped"
            config_status = "Config Skipped"

            session = db_session.create_session()
            r = session.query(Router).filter(Router.router_name == item.router_name).one()
            r.backup_status = backup_status
            r.config_status = config_status
            session.commit()
        else:
            # starting backup
            tqdm.write(f"Starting backup for {item.router_name}...")
            logging.info(f"Starting backup for {item.router_name}...")

            task_1 = loop.create_task(create_backup(item.router_name, item.router_ip, item.username))

            # backup file completed
            tqdm.write(f"Completed backup for {item.router_name}")
            logging.info(f"Completed backup for {item.router_name}")


            # starting config export
            tqdm.write(f"Starting config export for {item.router_name}...")
            logging.info(f"Starting config export for {item.router_name}...")

            task_2 = loop.create_task(create_config(item.router_name, item.router_ip, item.username))

            tqdm.write(f"Config export complete for {item.router_name}")
            logging.info(f"Config export complete for {item.router_name}")

            # gathering info from rotuers
            task_3 = loop.create_task(router_details_service.get_info(item.router_name, item.router_ip, item.username))
            router_details_service.parse_info(item.router_name)

            final_task = asyncio.gather(task_1, task_2, task_3)

            loop.run_until_complete(final_task)

    dt = datetime.datetime.now() - start_time
    print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
 def __init__(self):
     super().__init__()
     self.user = user_service.find_user_by_id(self.user_id)
     self.routers = router_service.get_router_list()
     self.router_count = router_service.get_router_count()
     self.backup_complete_count = router_service.get_backup_complete_count()
     self.config_complete_count = router_service.get_config_complete_count()
     self.backup_failed_count = router_service.get_backup_failed_count()
     self.config_failed_count = router_service.get_config_failed_count()
     self.unknown_count = router_service.get_unknown_status_count()
     self.ignore_count = router_service.get_router_ignore_count()
Exemple #4
0
def run():
    tqdm.write(f"{log_date_time} Gathering Routers...")
    print(f"{log_date_time} Gathering Routers...")

    ignore_list = router_service.get_router_ignore_list()
    router_list = router_service.get_router_list()
    router_count = router_service.get_router_count()

    tqdm.write(f"{log_date_time} Routers Gathered.")
    print(f"{log_date_time} Routers Gathered.")

    for item in tqdm(router_list, total=router_count, unit=" routers"):
        tqdm.write(
            f"{log_date_time} Starting Removal for {item.router_name}...")
        print(f"{log_date_time} Starting Removal for {item.router_name}...")

        if item.router_name in ignore_list:
            tqdm.write(f"{log_date_time} Removal skipped.")
            print(f"{log_date_time} Removal skipped.")
        else:
            try:
                remove_backup = subprocess.run(
                    f'ssh {item.username}@{item.router_ip} /file remove [find type="backup"]',
                    shell=True,
                    universal_newlines=True,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)
                if remove_backup.stdout != '':
                    print(f"{log_date_time} {remove_backup.stdout}")
                    tqdm.write(
                        f"{log_date_time} stdout: {remove_backup.stdout}")

                if remove_backup.stderr != '':
                    print(f"{log_date_time} {remove_backup.stderr}")
                    tqdm.write(
                        f"{log_date_time} stderr: {remove_backup.stderr}")
            except:
                print(f'{log_date_time} {sys.exc_info()[1]}')
                tqdm.write(f"{log_date_time} Exception: {sys.exc_info()[1]}")

            tqdm.write(
                f"{log_date_time} Removal for {item.router_name} completed.")
            print(f"{log_date_time} Removal for {item.router_name} completed.")
def run():
    # setting start time for sync to async comparison
    t0 = datetime.datetime.now()

    # gathering list all routers and list of routers to be ignored and router count
    routers = router_service.get_router_list()
    ignored_routers = router_service.get_router_ignore_list()
    router_count = router_service.get_router_count()


    for item in tqdm(routers, total=router_count, unit=" router"):
        if item.router_name in ignored_routers:
            print(f'{log_date_time} Backup skipped for {item.router_name}')
            tqdm.write(f"Backup skipped for {item.router_name}")
            backup_status = "Backup Skipped"
            config_status = "Config Skipped"

            session = db_session.create_session()
            r = session.query(Router).filter(Router.router_name == item.router_name).one()
            r.backup_status = backup_status
            r.config_status = config_status
            session.commit()
        else:
            # starting backup
            tqdm.write(f"Starting backup for {item.router_name}...")
            print(f'{log_date_time} Starting backup for {item.router_name}...')
            create_backup(item.router_name, item.router_ip, item.username)
            tqdm.write(f"Completed backup for {item.router_name}")
            print(f'{log_date_time} Completed backup for {item.router_name}')

            # starting config export
            tqdm.write(f"Starting config export for {item.router_name}...")
            print(f'{log_date_time} Starting config export for {item.router_name}...')
            create_config(item.router_name, item.router_ip, item.username)
            tqdm.write(f"Config export complete for {item.router_name}")
            print(f'{log_date_time} Config export complete for {item.router_name}')

            # gathering info from rotuers
            router_details_service.get_info(item.router_name, item.router_ip, item.username)
            router_details_service.parse_info(item.router_name)

    dt = datetime.datetime.now() - t0
    print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
def run():
    ignore_list = ['Spectrum Voice',
                   'CASA',
                   'Value Med Midwest City',
                   'Value Med Harrah',
                   'Value Med FTG',
                   'GPSS Hobart',
                   'Farmers Caleb Conner',
                   'Farmers Wayne Buck',
                   'GPSS HQ']

    routers = router_service.get_router_list()

    router_count = sum(1 for _ in routers)
    for r in tqdm(routers, total=router_count, unit=" router"):
        if r.router_name in ignore_list:
            print(f'{log_date_time} Gathering info skipped for {r.router_name}')
            tqdm.write("Gathering info skipped for " + r.router_name)
        else:
            parse_info(r.router_name)
 def __init__(self):
     super().__init__()
     self.user = user_service.find_user_by_id(self.user_id)
     self.routers = router_service.get_router_list()
     self.router_to_remove = self.request_dict.selected_router
def run():
    routers = router_service.get_router_list()
    ignore_list = router_service.get_router_ignore_list()

    router_count = 0
    for _ in routers:
        router_count += 1

    for router in tqdm(routers, total=router_count, unit=" router"):
        top_folder = os.path.dirname(__file__)
        rel_file = os.path.join('..', 'backups', router.router_name)
        backup_path = os.path.abspath(os.path.join(top_folder, rel_file))
        zip_path = os.path.join(backup_path, f'{router.router_name}.zip')

        if router.router_name in ignore_list:
            tqdm.write(f'{log_date_time} {router.router_name} skipped.')
            print(f'{log_date_time} {router.router_name} skipped.')
        elif os.path.exists(zip_path):
            tqdm.write(f'{log_date_time} {router.router_name} Found.')
            print(f'{log_date_time} {router.router_name} Found.')
            tqdm.write(
                f'{log_date_time} Removing old {router.router_name}.zip')
            print(f'{log_date_time} Removing old {router.router_name}.zip')
            os.remove(zip_path)
            tqdm.write(f'{log_date_time} {router.router_name} removed.')
            print(f'{log_date_time} {router.router_name} removed.')
            tqdm.write(
                f'{log_date_time} Creating new {router.router_name}.zip')
            print(f'{log_date_time} Creating new {router.router_name}.zip')
            shutil.make_archive(router.router_name, 'zip', backup_path)
            tqdm.write(
                f'{log_date_time} New {router.router_name}.zip created.')
            print(f'{log_date_time} New {router.router_name}.zip created.')
            tqdm.write(
                f'{log_date_time} Moving {router.router_name}.zip to backup directory.'
            )
            print(
                f'{log_date_time} Moving {router.router_name}.zip to backup directory.'
            )
            os.rename(f'{router.router_name}.zip',
                      f'{backup_path}/{router.router_name}.zip')
            tqdm.write(f'{log_date_time} {router.router_name}.zip moved.')
            print(f'{log_date_time} {router.router_name}.zip moved.')
        else:
            tqdm.write(f'{log_date_time} {router.router_name} Not Found.')
            print(f'{log_date_time} {router.router_name} Not Found.')
            tqdm.write(
                f'{log_date_time} Creating new {router.router_name}.zip')
            print(f'{log_date_time} Creating new {router.router_name}.zip')
            shutil.make_archive(router.router_name, 'zip', backup_path)
            tqdm.write(
                f'{log_date_time} New {router.router_name}.zip created.')
            print(f'{log_date_time} New {router.router_name}.zip created.')
            tqdm.write(
                f'{log_date_time} Moving {router.router_name}.zip to backup directory.'
            )
            print(
                f'{log_date_time} Moving {router.router_name}.zip to backup directory.'
            )
            os.rename(f'{router.router_name}.zip',
                      f'{backup_path}/{router.router_name}.zip')
            tqdm.write(f'{log_date_time} {router.router_name}.zip moved.')
            print(f'{log_date_time} {router.router_name}.zip moved.')