| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Example script for ANTA. |
| 5 |
|
| 6 |
usage: |
| 7 |
|
| 8 |
python anta_runner.py |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
import asyncio |
| 14 |
import logging |
| 15 |
import sys |
| 16 |
from pathlib import Path |
| 17 |
|
| 18 |
from anta.catalog import AntaCatalog |
| 19 |
from anta.cli.nrfu.utils import anta_progress_bar |
| 20 |
from anta.inventory import AntaInventory |
| 21 |
from anta.logger import Log, setup_logging |
| 22 |
from anta.models import AntaTest |
| 23 |
from anta.result_manager import ResultManager |
| 24 |
from anta.runner import main as anta_runner |
| 25 |
|
| 26 |
|
| 27 |
setup_logging(Log.INFO, Path("/tmp/anta.log")) |
| 28 |
LOGGER = logging.getLogger() |
| 29 |
SCRIPT_LOG_PREFIX = "[bold magenta][ANTA RUNNER SCRIPT][/] " |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
USERNAME = "admin" |
| 34 |
PASSWORD = "admin" |
| 35 |
CATALOG_PATH = Path("/tmp/anta_catalog.yml") |
| 36 |
INVENTORY_PATH = Path("/tmp/anta_inventory.yml") |
| 37 |
|
| 38 |
|
| 39 |
try: |
| 40 |
catalog = AntaCatalog.parse(CATALOG_PATH) |
| 41 |
except Exception: |
| 42 |
LOGGER.exception("%s Catalog failed to load!", SCRIPT_LOG_PREFIX) |
| 43 |
sys.exit(1) |
| 44 |
LOGGER.info("%s Catalog loaded!", SCRIPT_LOG_PREFIX) |
| 45 |
|
| 46 |
|
| 47 |
try: |
| 48 |
inventory = AntaInventory.parse(INVENTORY_PATH, username=USERNAME, password=PASSWORD) |
| 49 |
except Exception: |
| 50 |
LOGGER.exception("%s Inventory failed to load!", SCRIPT_LOG_PREFIX) |
| 51 |
sys.exit(1) |
| 52 |
LOGGER.info("%s Inventory loaded!", SCRIPT_LOG_PREFIX) |
| 53 |
|
| 54 |
|
| 55 |
manager = ResultManager() |
| 56 |
|
| 57 |
|
| 58 |
LOGGER.info("%s Starting ANTA runner...", SCRIPT_LOG_PREFIX) |
| 59 |
with anta_progress_bar() as AntaTest.progress: |
| 60 |
|
| 61 |
asyncio.run(anta_runner(manager, inventory, catalog, dry_run=False)) |
| 62 |
|
| 63 |
LOGGER.info("%s ANTA run completed!", SCRIPT_LOG_PREFIX) |
| 64 |
|
| 65 |
|
| 66 |
for test_result in manager.results: |
| 67 |
LOGGER.info("%s %s:%s:%s", SCRIPT_LOG_PREFIX, test_result.name, test_result.test, test_result.result) |