2025-02-05 11:32:35 +01:00
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
2025-02-05 11:38:32 +01:00
""" Click commands to execute various scripts on EOS devices. """
2025-02-05 11:32:35 +01:00
from __future__ import annotations
import asyncio
import logging
import sys
2025-02-05 11:38:32 +01:00
from datetime import datetime , timezone
2025-02-05 11:32:35 +01:00
from pathlib import Path
2025-02-05 11:38:32 +01:00
from typing import TYPE_CHECKING
2025-02-05 11:32:35 +01:00
import click
from yaml import safe_load
2025-02-05 11:38:32 +01:00
from anta . cli . console import console
2025-02-05 11:39:42 +01:00
from anta . cli . exec import utils
2025-02-05 11:32:35 +01:00
from anta . cli . utils import inventory_options
2025-02-05 11:38:32 +01:00
if TYPE_CHECKING :
from anta . inventory import AntaInventory
2025-02-05 11:32:35 +01:00
logger = logging . getLogger ( __name__ )
@click.command
@inventory_options
2025-02-05 11:38:32 +01:00
def clear_counters ( inventory : AntaInventory , tags : set [ str ] | None ) - > None :
""" Clear counter statistics on EOS devices. """
2025-02-05 11:39:42 +01:00
asyncio . run ( utils . clear_counters ( inventory , tags = tags ) )
2025-02-05 11:32:35 +01:00
@click.command ( )
@inventory_options
@click.option (
" --commands-list " ,
" -c " ,
help = " File with list of commands to collect " ,
required = True ,
show_envvar = True ,
type = click . Path ( file_okay = True , dir_okay = False , exists = True , readable = True , path_type = Path ) ,
)
@click.option (
" --output " ,
" -o " ,
show_envvar = True ,
type = click . Path ( file_okay = False , dir_okay = True , exists = False , writable = True , path_type = Path ) ,
help = " Directory to save commands output. " ,
2025-02-05 11:38:32 +01:00
default = f " anta_snapshot_ { datetime . now ( tz = timezone . utc ) . astimezone ( ) . strftime ( ' % Y- % m- %d _ % H_ % M_ % S ' ) } " ,
2025-02-05 11:32:35 +01:00
show_default = True ,
)
2025-02-05 11:38:32 +01:00
def snapshot ( inventory : AntaInventory , tags : set [ str ] | None , commands_list : Path , output : Path ) - > None :
""" Collect commands output from devices in inventory. """
console . print ( f " Collecting data for { commands_list } " )
console . print ( f " Output directory is { output } " )
2025-02-05 11:32:35 +01:00
try :
2025-02-05 11:38:32 +01:00
with commands_list . open ( encoding = " UTF-8 " ) as file :
2025-02-05 11:32:35 +01:00
file_content = file . read ( )
eos_commands = safe_load ( file_content )
except FileNotFoundError :
2025-02-05 11:38:32 +01:00
logger . error ( " Error reading %s " , commands_list )
2025-02-05 11:32:35 +01:00
sys . exit ( 1 )
2025-02-05 11:39:42 +01:00
asyncio . run ( utils . collect_commands ( inventory , eos_commands , output , tags = tags ) )
2025-02-05 11:32:35 +01:00
@click.command ( )
@inventory_options
2025-02-05 11:38:32 +01:00
@click.option (
" --output " ,
" -o " ,
default = " ./tech-support " ,
show_default = True ,
help = " Path for test catalog " ,
type = click . Path ( path_type = Path ) ,
required = False ,
)
@click.option (
" --latest " ,
help = " Number of scheduled show-tech to retrieve " ,
type = int ,
required = False ,
)
2025-02-05 11:32:35 +01:00
@click.option (
" --configure " ,
help = " Ensure devices have ' aaa authorization exec default local ' configured (required for SCP on EOS). THIS WILL CHANGE THE CONFIGURATION OF YOUR NETWORK. " ,
default = False ,
is_flag = True ,
show_default = True ,
)
2025-02-05 11:38:32 +01:00
def collect_tech_support (
inventory : AntaInventory ,
tags : set [ str ] | None ,
output : Path ,
latest : int | None ,
* ,
configure : bool ,
) - > None :
""" Collect scheduled tech-support from EOS devices. """
2025-02-05 11:39:42 +01:00
asyncio . run ( utils . collect_show_tech ( inventory , output , configure = configure , tags = tags , latest = latest ) )