sc-napalm 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
custom_napalm/utils.py ADDED
@@ -0,0 +1,74 @@
1
+ from typing import Union
2
+ import logging
3
+ import sys
4
+ import argparse
5
+ from decouple import config
6
+ from os import getenv
7
+
8
+ LOG_LEVELS = {
9
+ "DEBUG": logging.DEBUG,
10
+ "INFO": logging.INFO,
11
+ "WARNING": logging.WARNING,
12
+ "ERROR": logging.WARNING,
13
+ "CRITICAL": logging.CRITICAL,
14
+ }
15
+ LOG_FORMAT = "%(asctime)-15s %(levelname)8s %(name)s %(message)s"
16
+
17
+
18
+ def configure_logging(
19
+ log_level: Union[int, str],
20
+ log_globally: bool = False,
21
+ log_file: str = None,
22
+ log_to_console: bool = False,
23
+ ):
24
+ """
25
+ Configures logging for the module, or globally as indicated by the input
26
+ """
27
+
28
+ if log_globally:
29
+ logger = logging.getLogger()
30
+ else:
31
+ module_name = __name__.split(".")[0]
32
+ logger = logging.getLogger(module_name)
33
+
34
+ if isinstance(log_level, str):
35
+ log_level = LOG_LEVELS[log_level.upper()]
36
+ logger.setLevel(log_level)
37
+
38
+ if log_file:
39
+ file_handler = logging.handlers.RotatingFileHandler(
40
+ log_file, maxBytes=1024 * 1024 * 10, backupCount=20
41
+ )
42
+ file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
43
+ logger.addHandler(file_handler)
44
+
45
+ if log_to_console:
46
+ stdout_handler = logging.StreamHandler(sys.stdout)
47
+ stdout_handler.setFormatter(logging.Formatter(LOG_FORMAT))
48
+ logger.addHandler(stdout_handler)
49
+
50
+
51
+ def get_from_args_or_env(
52
+ cli_arg: str, parsed_args: argparse.Namespace = None, required=True
53
+ ) -> str:
54
+ """
55
+ Pull a value from parsed arparse, if it's not there look for it
56
+ in .env, and if it's not there, check the user's environment.
57
+ """
58
+ cli_arg = cli_arg.replace("-", "_")
59
+
60
+ if getattr(parsed_args, cli_arg, False):
61
+ return getattr(parsed_args, cli_arg)
62
+
63
+ env_arg = cli_arg.upper()
64
+ if config(env_arg, None):
65
+ return config(env_arg)
66
+
67
+ if getenv(env_arg):
68
+ return getenv(env_arg)
69
+
70
+ if required:
71
+ raise ValueError(
72
+ f"ERROR: Please provide {cli_arg} as cli input or set as {env_arg} environment variable"
73
+ )
74
+ return None
@@ -0,0 +1,106 @@
1
+ Metadata-Version: 2.4
2
+ Name: sc-napalm
3
+ Version: 1.0.0
4
+ Summary: Add your description here
5
+ Requires-Python: >=3.8
6
+ Requires-Dist: gitpython>=3.1.45
7
+ Requires-Dist: lxml>=6.0.2
8
+ Requires-Dist: napalm-srl>=1.0.5
9
+ Requires-Dist: napalm-sros>=1.0.2
10
+ Requires-Dist: napalm>=5.1.0
11
+ Requires-Dist: pynautobot>=2.1.1
12
+ Requires-Dist: python-decouple>=3.8
13
+ Description-Content-Type: text/markdown
14
+
15
+ # sc-napalm
16
+
17
+ This repo is for building custom [NAPALM](https://napalm.readthedocs.io/en/latest/) "getters" to pull operational data from network devices, as well as
18
+ override existing NAPALM getters with custom code if needed. At the moment only one custom getter is implemented: `get_inventory`. This getter
19
+ pulls model and serial numbers of device components: fans, psus, and optics.
20
+
21
+ What's cool about NAPALM is that if you nest your custom drivers under `custom_napalm` as is done in this project, you can install your custom drivers
22
+ in the same virtual environment as the main NAPALM package, and they will override NAPALM's core drivers. This allows us to leverage NAPALM in a pretty
23
+ seamless way - by that I mean applications that leverage NAPALM (like Nautobot or Nornir) can be easily altered to use this code instead.
24
+
25
+ Right now, the following platforms are supported:
26
+ * eos
27
+ * iosxr (via netconf)
28
+ * junos
29
+ * nos (Drivenets)
30
+ * nxos
31
+ * srl (Nokia SR Linux)
32
+ * sros
33
+
34
+
35
+ ## Using sc-napalm
36
+ The package comes with a cli script called `sc-napalm-get` that will run a particular getter against a particular device and output the results
37
+ to your terminal.
38
+
39
+ To use the script, you can install the package from pypi, ideally into a virtual environment:
40
+ ```
41
+ python3 -m venv venv
42
+ source venv/bin/activate
43
+ pip install sc-napalm
44
+ ```
45
+ Now that you've done this you can run `sc-napalm-get` from your venv. Note that you must either provide your credentials
46
+ directly to the script, or set them as environment variables `SC_USERNAME` and `SC_PASSWORD`. Run it with `--help` to see all the
47
+ various options.
48
+ ```
49
+ (venv) aliebowitz@sysauto:~$ export SC_USERNAME=nso
50
+ (venv) aliebowitz@sysauto:~$ export SC_PASSWORD=<redacted>
51
+ (venv) aliebowitz@sysauto:~$ sc-napalm-get --help
52
+ usage: sc-napalm-get [-h] [--ssh-cfg SSH_CFG] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL} | -L {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [--logfile LOGFILE] [--sc_username SC_USERNAME]
53
+ [--sc_password SC_PASSWORD]
54
+ device {iosxr,nxos,junos,sros,srl,eos,nos} {get_config,get_facts,get_optics,get_lldp_neighbors,get_inventory}
55
+
56
+ Run a specific sc_napalm "getter" against a device.
57
+
58
+ positional arguments:
59
+ device device hostname or IP address
60
+ {iosxr,nxos,junos,sros,srl,eos,nos}
61
+ The platform of this device
62
+ {get_config,get_facts,get_optics,get_lldp_neighbors,get_inventory}
63
+ The getter command to run against this device
64
+
65
+ options:
66
+ -h, --help show this help message and exit
67
+ --ssh-cfg SSH_CFG Use SSH config file to connect
68
+ -l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
69
+ Set log level for sc_napalm only
70
+ -L {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --LOG-LEVEL {DEBUG,INFO,WARNING,ERROR,CRITICAL}
71
+ set global log level
72
+ --logfile LOGFILE Save logging to a file (specified by name) instead of to stdout
73
+ --sc_username SC_USERNAME
74
+ Specify credentials
75
+ --sc_password SC_PASSWORD
76
+ Specify credentials
77
+ (venv) aliebowitz@sysauto:~$ sc-napalm-get 2001:468:1f07:ff19::1d eos get_inventory
78
+ [{'name': 'Ethernet45',
79
+ 'part_number': 'QSFP-100G-LR4',
80
+ 'serial_number': 'XYL252206819',
81
+ 'subtype': 'QSFP-100G-LR4',
82
+ 'type': 'optic'},
83
+ {'name': 'Ethernet46',
84
+ 'part_number': 'QSFP-100G-LR4',
85
+ 'serial_number': 'XYL252206822',
86
+ 'subtype': 'QSFP-100G-LR4',
87
+ 'type': 'optic'},
88
+ {'name': 'PSU 1',
89
+ 'part_number': 'PWR-511-AC-RED',
90
+ 'serial_number': 'EEWT2420216960',
91
+ 'subtype': None,
92
+ 'type': 'psu'},
93
+ ...
94
+ ```
95
+
96
+ ## Developing sc-napalm
97
+ Currently, the getters that are exposed as options in the `get` script are defined in the [base class](https://scinet.supercomputing.org:8443/automation/sc25/sc-napalm/-/blob/main/src/custom_napalm/base.py?ref_type=heads) of the custom drivers.
98
+ Note that because most of the custom classes inherit the NAPALM getters, we could easily define all the other NAPALM getters there, but I've only included ones I think are obviously useful to us.
99
+
100
+ My hope is that instead of just printing out results we can write code that saves data in Nautobot, or some other place.
101
+ This could be done with Nornir or Nautobot jobs.
102
+
103
+ ## To-dos
104
+ * `get_inventory` methods for sros
105
+ * Test/mock classes for custom getters
106
+
@@ -0,0 +1,32 @@
1
+ custom_napalm/__init__.py,sha256=tb8thMjM7cm77r_yIjZ3YP1LiaFjYPVzIRZI1b-JAqY,770
2
+ custom_napalm/base.py,sha256=AMZVPWFL5i8X9YaPxGsyqg00By4oUF3VyETeJ-wS-3s,3822
3
+ custom_napalm/get.py,sha256=hfqriJVgrBZeXqbLvofUnZwMPK4ka6DaBRpLMnuAZtU,2448
4
+ custom_napalm/models.py,sha256=4RoOfYM1fpsEmSg5ytQk5MHIkT6o-rH_gREgImCFue8,473
5
+ custom_napalm/utils.py,sha256=SAa-g3N9eq8GjzmKvewTY1Chf9nGooqJ7Xnn5VHD5Go,2036
6
+ custom_napalm/eos/__init__.py,sha256=OHS-WcwZx_VMizK2Ui0eh7KePsAiLTR-03VkEiq_IEg,29
7
+ custom_napalm/eos/eos.py,sha256=GZQbr_Yq4erx20D0w7WYBlP3n9XrG-b7iyoaN5ijHZk,2966
8
+ custom_napalm/iosxr/__init__.py,sha256=Y_kJcBzXGutAipwWiOdItybx1GN4hjRxTjs_m8ylFcE,27
9
+ custom_napalm/iosxr/constants.py,sha256=9s04MM-z_xPfyfsRnC-uoKA14FB4grUp9XB0H7pri28,4194
10
+ custom_napalm/iosxr/iosxr.py,sha256=WcrvIMOL4XL0aq2RJZLkFluXqbvUhsJWhAw9c5X9bAU,1577
11
+ custom_napalm/junos/__init__.py,sha256=Z1u0FwvewtLL9RBj9SAfWk4LlTRNcwj8WMb4EpCK0ZY,27
12
+ custom_napalm/junos/junos.py,sha256=LAqoE5MZYJxWkQ65vVeEWQZsYJLD8sgQImFEUxzhxZI,7741
13
+ custom_napalm/junos/junos_views.py,sha256=FF4fOj4imvDC7EzC0ufyM3X2sxOEwqDKItYVJ1E3aOc,426
14
+ custom_napalm/junos/junos_views.yml,sha256=xSfF55PiTliQNMPn32pUzw5qO8nPbGwkrCHBzMIs3Gw,4544
15
+ custom_napalm/nos/__init__.py,sha256=9_lHFCrqhfIxmqP4r5ct_wynee-gsK5k5oAkFeDtxGA,29
16
+ custom_napalm/nos/constants.py,sha256=ErqCmavFs5yhjkM4xx8mU7Y6SKw7e-48U9Ab_nly-JQ,3321
17
+ custom_napalm/nos/nos.py,sha256=ThO4DtkwkNvKE_ml2mUlfthk_8Zk2i6rFSbCrwWeX_k,8771
18
+ custom_napalm/nxos/__init__.py,sha256=UiBRkfxFQEKVOwhiRnOtAR-KSZYYyBvdre2VazHxxVM,25
19
+ custom_napalm/nxos/nxos.py,sha256=A9KXHInjHk3VZaG8-Wq4cEdVD4RgGJBkivqjpv8l0gk,2637
20
+ custom_napalm/nxos/utils/textfsm_templates/sh_int_transceiver.tpl,sha256=rMK9qg2eYR5klmoi6qPRufhBX6ooqRWmPaIM3J7aGxI,596
21
+ custom_napalm/nxos/utils/textfsm_templates/sh_inventory.tpl,sha256=GrgUzRsIUzI5SxGExFlUSXF3I9puIXczrzzaEYnMKvk,159
22
+ custom_napalm/srl/__init__.py,sha256=p5kiVIOBjp7O8d2955-Jj3wkLk66687wBxli34IBU_0,34
23
+ custom_napalm/srl/srl.py,sha256=uZVv3QM8LuhO62xjM6aA-T6A9k4QjYPqEzNb8YObIgM,6134
24
+ custom_napalm/srl_ssh/__init__.py,sha256=hMR8SbWrdhPB6bZwKjcWCdEi7rdTLcBwKQx-rKcadl8,39
25
+ custom_napalm/srl_ssh/srl_ssh.py,sha256=uPNYwwPUGkNXQPNh-dCbfHDfkoXTkBavl5rBKeLNS7Y,1390
26
+ custom_napalm/sros/__init__.py,sha256=SuDmNhd9m0eLJm4sQMQLg8ThydG48AFZwSfp70U8mfk,36
27
+ custom_napalm/sros/nc_filters.py,sha256=tHMqEvDpww9W0nzmKV5rIdjlIs93iS08T758VYIA4xQ,508
28
+ custom_napalm/sros/sros.py,sha256=hX2yd3fxFhtQ2MlUq6Hsz8Bi5mMCpqtZSKM_7sBFsjc,1975
29
+ sc_napalm-1.0.0.dist-info/METADATA,sha256=7fRTRWsFD0xVqlfWUUQFwo_B1zevcw9VfC5MLuSe7cw,4715
30
+ sc_napalm-1.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
31
+ sc_napalm-1.0.0.dist-info/entry_points.txt,sha256=LTin-OoLE7iMryLisDgQi8tsl7doDckm50EEG8_VFr8,57
32
+ sc_napalm-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ sc-napalm-get = custom_napalm.get:main