nornir-collection 0.0.1__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.
Files changed (59) hide show
  1. nornir_collection/__init__.py +0 -0
  2. nornir_collection/batfish/__init__.py +0 -0
  3. nornir_collection/batfish/assert_config.py +358 -0
  4. nornir_collection/batfish/utils.py +129 -0
  5. nornir_collection/cisco/__init__.py +0 -0
  6. nornir_collection/cisco/configuration_management/__init__.py +0 -0
  7. nornir_collection/cisco/configuration_management/cli/__init__.py +0 -0
  8. nornir_collection/cisco/configuration_management/cli/config_tasks.py +569 -0
  9. nornir_collection/cisco/configuration_management/cli/config_workflow.py +107 -0
  10. nornir_collection/cisco/configuration_management/cli/show_tasks.py +677 -0
  11. nornir_collection/cisco/configuration_management/netconf/__init__.py +0 -0
  12. nornir_collection/cisco/configuration_management/netconf/config_tasks.py +564 -0
  13. nornir_collection/cisco/configuration_management/netconf/config_workflow.py +298 -0
  14. nornir_collection/cisco/configuration_management/netconf/nr_cfg_iosxe_netconf.py +186 -0
  15. nornir_collection/cisco/configuration_management/netconf/ops_tasks.py +307 -0
  16. nornir_collection/cisco/configuration_management/processor.py +151 -0
  17. nornir_collection/cisco/configuration_management/pyats.py +236 -0
  18. nornir_collection/cisco/configuration_management/restconf/__init__.py +0 -0
  19. nornir_collection/cisco/configuration_management/restconf/cisco_rpc.py +514 -0
  20. nornir_collection/cisco/configuration_management/restconf/config_workflow.py +95 -0
  21. nornir_collection/cisco/configuration_management/restconf/tasks.py +325 -0
  22. nornir_collection/cisco/configuration_management/utils.py +511 -0
  23. nornir_collection/cisco/software_upgrade/__init__.py +0 -0
  24. nornir_collection/cisco/software_upgrade/cisco_software_upgrade.py +283 -0
  25. nornir_collection/cisco/software_upgrade/utils.py +794 -0
  26. nornir_collection/cisco/support_api/__init__.py +0 -0
  27. nornir_collection/cisco/support_api/api_calls.py +1173 -0
  28. nornir_collection/cisco/support_api/cisco_maintenance_report.py +221 -0
  29. nornir_collection/cisco/support_api/cisco_support.py +727 -0
  30. nornir_collection/cisco/support_api/reports.py +747 -0
  31. nornir_collection/cisco/support_api/utils.py +316 -0
  32. nornir_collection/fortinet/__init__.py +0 -0
  33. nornir_collection/fortinet/utils.py +36 -0
  34. nornir_collection/git.py +224 -0
  35. nornir_collection/netbox/__init__.py +0 -0
  36. nornir_collection/netbox/custom_script.py +107 -0
  37. nornir_collection/netbox/inventory.py +360 -0
  38. nornir_collection/netbox/scan_prefixes_and_update_ip_addresses.py +989 -0
  39. nornir_collection/netbox/set_device_status.py +67 -0
  40. nornir_collection/netbox/sync_datasource.py +111 -0
  41. nornir_collection/netbox/update_cisco_inventory_data.py +158 -0
  42. nornir_collection/netbox/update_cisco_support_plugin_data.py +339 -0
  43. nornir_collection/netbox/update_fortinet_inventory_data.py +161 -0
  44. nornir_collection/netbox/update_purestorage_inventory_data.py +144 -0
  45. nornir_collection/netbox/utils.py +261 -0
  46. nornir_collection/netbox/verify_device_primary_ip.py +202 -0
  47. nornir_collection/nornir_plugins/__init__.py +0 -0
  48. nornir_collection/nornir_plugins/inventory/__init__.py +0 -0
  49. nornir_collection/nornir_plugins/inventory/netbox.py +250 -0
  50. nornir_collection/nornir_plugins/inventory/staggered_yaml.py +143 -0
  51. nornir_collection/nornir_plugins/inventory/utils.py +277 -0
  52. nornir_collection/purestorage/__init__.py +0 -0
  53. nornir_collection/purestorage/utils.py +53 -0
  54. nornir_collection/utils.py +741 -0
  55. nornir_collection-0.0.1.dist-info/LICENSE +21 -0
  56. nornir_collection-0.0.1.dist-info/METADATA +136 -0
  57. nornir_collection-0.0.1.dist-info/RECORD +59 -0
  58. nornir_collection-0.0.1.dist-info/WHEEL +5 -0
  59. nornir_collection-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,283 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ This module updates the software version on Cisco IOS-XE devices with Nornir.
4
+ The Main function is intended to import and execute by other scripts.
5
+ """
6
+
7
+
8
+ import argparse
9
+ from nornir.core import Nornir
10
+ from nornir_collection.nornir_plugins.inventory.utils import init_nornir
11
+ from nornir_collection.cisco.configuration_management.cli.show_tasks import (
12
+ cli_verify_destination_md5_hash,
13
+ cli_install_remove_inactive_task,
14
+ )
15
+ from nornir_collection.cisco.configuration_management.restconf.tasks import (
16
+ rc_verify_current_software_version_fallback_cli,
17
+ rc_software_install_one_shot_fallback_cli,
18
+ rc_install_remove_inactive_fallback_cli,
19
+ )
20
+ from nornir_collection.cisco.software_upgrade.utils import (
21
+ prepare_upgrade_data,
22
+ scp_upload_software_file,
23
+ cli_http_download_software_file,
24
+ verify_issu_requirements,
25
+ cli_track_issu_upgrade_process,
26
+ fping_track_upgrade_process,
27
+ cli_verify_switch_state,
28
+ )
29
+ from nornir_collection.utils import (
30
+ exit_info,
31
+ print_result,
32
+ exit_error,
33
+ print_task_title,
34
+ nr_filter_inventory_from_host_list,
35
+ )
36
+
37
+
38
+ def copy_software(nr: Nornir, args: bool) -> None:
39
+ """
40
+ Copy software package files to hosts. Verify destination md5 hash with cli and upload software file if
41
+ needed with SCP or HTTP. The nornir inventory will be re-filtered if only some hosts needs a software
42
+ upload and the upload method is SCP or HTTP depending on the args.local_upload argument.
43
+
44
+ Args:
45
+ nr (Nornir): The Nornir object containing the inventory of hosts.
46
+ args (bool): The command line arguments.
47
+
48
+ Returns:
49
+ None
50
+ """
51
+ # pylint: disable=invalid-name
52
+
53
+ print_task_title("Verify destination software file md5 hash")
54
+ # Verify if the destination file exists and verify the md5 hash.
55
+ failed_hosts = cli_verify_destination_md5_hash(nr=nr)
56
+
57
+ # If the failed_hosts list is empty -> Return None
58
+ if not failed_hosts:
59
+ return
60
+
61
+ # If the failed_host list is not empty and not identical with the Nornir inventory
62
+ if sorted(failed_hosts) != sorted(list(nr.inventory.hosts.keys())):
63
+ print_task_title("Re-Filter nornir inventory")
64
+ # Re-filter the Nornir inventory to the failed_hosts only
65
+ nr = nr_filter_inventory_from_host_list(
66
+ nr=nr,
67
+ filter_reason="Exclude good hosts to copy the software file only to the following hosts:",
68
+ host_list=failed_hosts,
69
+ )
70
+
71
+ if args.remove_inactive:
72
+ # The failed_hosts list is not empty -> Clean-up all not needed software package files
73
+ print_task_title("Remove inactive software package files for filesystem clean-up")
74
+ # Run the custom Nornir task cli_install_remove_inactive_task
75
+ cli_task_result = nr.run(
76
+ task=cli_install_remove_inactive_task,
77
+ name="CLI install remove inactive",
78
+ verbose=args.verbose,
79
+ on_failed=True,
80
+ )
81
+ # Print the Nornir cli_install_remove_inactive_task task result
82
+ print_result(cli_task_result)
83
+ # Exit the script is the task failed
84
+ if cli_task_result.failed:
85
+ exit_error(
86
+ task_text="NORNIR software upgrade status",
87
+ text="ALERT: NETMIKO install remove inactive failed!",
88
+ )
89
+
90
+ # If the failed_hosts are identical with the Nornir inventory -> All hosts need a software upload
91
+ if sorted(failed_hosts) == sorted(list(nr.inventory.hosts.keys())):
92
+ if args.local_upload:
93
+ print_task_title("Upload software image file with SCP")
94
+ if not scp_upload_software_file(nr=nr):
95
+ exit_error(
96
+ task_text="NORNIR software upgrade status",
97
+ text="ALERT: NETMIKO upload file with SCP failed!",
98
+ )
99
+ else:
100
+ print_task_title("Download software image file with HTTP")
101
+ if not cli_http_download_software_file(nr=nr, verbose=args.verbose):
102
+ exit_error(
103
+ task_text="NORNIR software upgrade status",
104
+ text="ALERT: NETMIKO download file with HTTP failed!",
105
+ )
106
+
107
+ # Elif the failed_hosts are not identical with the Nornir inventory -> Some hosts needs software upload
108
+ elif sorted(failed_hosts) != sorted(list(nr.inventory.hosts.keys())):
109
+ if args.local_upload:
110
+ print_task_title("Upload software image file with SCP")
111
+ # Re-filter the Nornir inventory to the failed_hosts only
112
+ nr_obj_upload = nr_filter_inventory_from_host_list(
113
+ nr=nr,
114
+ filter_reason="Exclude good hosts to upload the software file only on the following hosts:",
115
+ host_list=failed_hosts,
116
+ )
117
+ if not scp_upload_software_file(nr=nr_obj_upload):
118
+ exit_error(
119
+ task_text="NORNIR software upgrade status",
120
+ text="ALERT: NETMIKO upload file with SCP failed!",
121
+ )
122
+ else:
123
+ print_task_title("Download software image file with HTTP")
124
+ # Re-filter the Nornir inventory to the failed_hosts only
125
+ nr_obj_upload = nr_filter_inventory_from_host_list(
126
+ nr=nr,
127
+ filter_reason="Exclude good hosts to download the software file only on the following hosts:",
128
+ host_list=failed_hosts,
129
+ )
130
+ if not cli_http_download_software_file(nr=nr_obj_upload, verbose=args.verbose):
131
+ exit_error(
132
+ task_text="NORNIR software upgrade status",
133
+ text="ALERT: NETMIKO download file with HTTP failed!",
134
+ )
135
+
136
+
137
+ def main(nr_config: str, args: argparse.Namespace) -> None:
138
+ """
139
+ Main function is intended to import and execute by other scripts.
140
+ It initialize Nornir and execute a software upgrade on Cisco IOS-XE devices.
141
+
142
+ * Args:
143
+ * nr_config (str): Path to the Nornir configuration file.
144
+ * args (argparse.Namespace): Command-line arguments parsed by argparse.
145
+
146
+ * Steps:
147
+ * Initializes the Nornir inventory and filters it based on the provided configuration.
148
+ * Prepares software upgrade details and verifies the current software version.
149
+ * Re-filters the Nornir inventory to include only hosts that need a software upgrade.
150
+ * Verifies the destination software file and uploads it if necessary.
151
+ * Starts the software installation process.
152
+ * Monitors and verifies the software upgrade progress.
153
+ * Cleans up old version files.
154
+
155
+ * Exits:
156
+ * It exits with appropriate messages and statuses based on the success or failure of each step.
157
+ """
158
+ # pylint: disable=invalid-name
159
+
160
+ #### Initialize Script and Nornir ######################################################################
161
+
162
+ # Initialize, transform and filter the Nornir inventory are return the filtered Nornir object
163
+ nr = init_nornir(config_file=nr_config, args=args, add_netbox_data=None)
164
+
165
+ #### Prepare software upgrade details and verify current version #######################################
166
+
167
+ print_task_title("Prepare software version upgrade details")
168
+ # Get the desired version from the Nornir inventory and verify the software file from the inventory in
169
+ # case local image upload is enabled. The inventory will be filled later with more data
170
+ upgrade_type = "scp" if args.local_upload else "http"
171
+ if not prepare_upgrade_data(nr=nr, upgrade_type=upgrade_type):
172
+ exit_error(
173
+ task_text="NORNIR software upgrade status", text="ALERT: NORNIR prepare upgrade data failed!"
174
+ )
175
+
176
+ print_task_title("Verify current software version")
177
+ # Verify the desired software version against the installed software version with RESTCONF and a fallback
178
+ # with CLI in case the RESTCONF task would fail. Returns a list of hosts which needs a software upgrade
179
+ failed_hosts = rc_verify_current_software_version_fallback_cli(nr=nr, verbose=args.verbose)
180
+
181
+ # If the failed_host list is empty, all hosts match the desired software version and exit the script
182
+ if not failed_hosts:
183
+ exit_info(
184
+ task_text="NORNIR software upgrade status",
185
+ text="The desired software version is up to date on all hosts",
186
+ changed=False,
187
+ )
188
+
189
+ # If the failed_host list is not empty and not identical with the Nornir inventory
190
+ if sorted(failed_hosts) != sorted(list(nr.inventory.hosts.keys())):
191
+ print_task_title("Re-Filter nornir inventory")
192
+ # Re-filter the Nornir inventory to the failed_hosts only
193
+ nr = nr_filter_inventory_from_host_list(
194
+ nr=nr,
195
+ filter_reason="Exclude good hosts to run the software upgrade only on the following hosts:",
196
+ host_list=failed_hosts,
197
+ )
198
+
199
+ #### Verify destination software file / Upload software file only if not already exists ##################
200
+
201
+ # Verify destination md5 hash with cli and upload software file if needed with SCP or HTTP
202
+ # The nornir inventory will be re-filtered if only some hosts needs a software upload and the upload
203
+ # method is SCP or HTTP depending on the args.local_upload argument
204
+ copy_software(nr=nr, args=args)
205
+
206
+ #### Start software installation process ###############################################################
207
+
208
+ print_task_title("Execute software version upgrade")
209
+
210
+ # If the argparse commit_reload argument is False -> Exit the script with exit code 0
211
+ if args.commit_reload is False:
212
+ exit_info(
213
+ task_text="NORNIR software upgrade status",
214
+ text="The software upgrade process is ready for install, but has not been started yet",
215
+ changed=False,
216
+ )
217
+
218
+ # Verify that there is no downgrade with ISSU argument set to True or exit the script
219
+ if args.issu is True:
220
+ if not verify_issu_requirements(nr=nr):
221
+ exit_error(
222
+ task_text="NORNIR software upgrade status",
223
+ text="ALERT: ISSU requirements failed!",
224
+ )
225
+
226
+ # Install the new software RESTCONF in a one-shot process and a fallback with CLI in case the RESTCONF
227
+ # task would fail. Returns True or False weather the task was successfull or not.
228
+ if not rc_software_install_one_shot_fallback_cli(nr=nr, issu=args.issu, verbose=args.verbose):
229
+ exit_error(
230
+ task_text="NORNIR software upgrade status",
231
+ text="ALERT: RESTCONF and CLI one-shot install failed!",
232
+ )
233
+
234
+ #### Monitor and verify version update status ##########################################################
235
+
236
+ task_title = "Track ISSU software upgrade progress" if args.issu else "Track software upgrade progress"
237
+ print_task_title(task_title)
238
+
239
+ if args.issu:
240
+ # Software upgrade tracking loop for ISSU upgrade with show commands as connectivity should never
241
+ # be lost. The max_time defines the maximum time the script should run until the upgrade process
242
+ # should be finished and acts as a timeout.
243
+ cli_track_issu_upgrade_process(nr=nr, max_time=2400) # 40 minutes timeout
244
+ else:
245
+ # Software upgrade tracking loop with fping until all hosts are upgraded with a max_time timeout.
246
+ # The max_time defines how long fping track until the IP connectivity should be back again.
247
+ fping_track_upgrade_process(nr=nr, max_time=2400) # 40 minutes timeout
248
+
249
+ # Verify that all switches in the show switch command output are in state "Ready"
250
+ if not cli_verify_switch_state(nr=nr, max_time=600): # 10 minutes timeout
251
+ exit_error(
252
+ task_text="NORNIR software upgrade status",
253
+ text="ALERT: Not all switches in the stack are ready after software upgrade!",
254
+ )
255
+
256
+ print_task_title("Verify current software version")
257
+ # Verify the desired software version against the installed software version with RESTCONF and a fallback
258
+ # with CLI in case the RESTCONF task would fail. Returns a list of hosts which needs a software upgrade
259
+ failed_hosts = rc_verify_current_software_version_fallback_cli(nr=nr, verbose=args.verbose)
260
+
261
+ # If the failed_hosts list is not empty
262
+ if failed_hosts:
263
+ exit_error(
264
+ task_text="NORNIR software upgrade status",
265
+ text="ALERT: RESTCONF and CLI software upgrade failed!",
266
+ )
267
+
268
+ #### Cleanup old version files ##########################################################################
269
+
270
+ # Remove all not needed software package files on the filesystem with RESTCONF and a fallback with CLI in
271
+ # case the RESTCONF task would fail. Returns a list of hosts which needs a software upgrade
272
+ if not rc_install_remove_inactive_fallback_cli(nr=nr, verbose=args.verbose):
273
+ exit_error(
274
+ task_text="NORNIR software upgrade status",
275
+ text="ALERT: RESTCONF and CLI install remove inactive failed!",
276
+ )
277
+
278
+ # The software upgrade was successful on all hosts
279
+ exit_info(
280
+ task_text="NORNIR software upgrade status",
281
+ text="The software upgrade was successful on all hosts",
282
+ changed=True,
283
+ )