Debian Codegraph @

examples/anta_runner.py

raw symbols SCIP
1 # Copyright (c) 2023-2024 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
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 # setup logging
27 setup_logging(Log.INFO, Path("/tmp/anta.log"))
28 LOGGER = logging.getLogger()
29 SCRIPT_LOG_PREFIX = "[bold magenta][ANTA RUNNER SCRIPT][/] " # For convenience purpose - there are nicer way to do this.
30
31
32 # NOTE: The inventory and catalog files are not delivered with this script
33 USERNAME = "admin"
34 PASSWORD = "admin"
35 CATALOG_PATH = Path("/tmp/anta_catalog.yml")
36 INVENTORY_PATH = Path("/tmp/anta_inventory.yml")
37
38 # Load catalog file
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 # Load inventory
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 # Create result manager object
55 manager = ResultManager()
56
57 # Launch ANTA
58 LOGGER.info("%s Starting ANTA runner...", SCRIPT_LOG_PREFIX)
59 with anta_progress_bar() as AntaTest.progress:
60 # Set dry_run to True to avoid connecting to the devices
61 asyncio.run(anta_runner(manager, inventory, catalog, dry_run=False))
62
63 LOGGER.info("%s ANTA run completed!", SCRIPT_LOG_PREFIX)
64
65 # Manipulate the test result object
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)