-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added ipdb examples #376
Merged
Merged
added ipdb examples #376
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
ipdb: How to inspect complex structures | ||
======================================= | ||
|
||
Python comes with a debuger called pdb. The python debugger is very powerful and really facilitates troubleshooting code. You can find the official documentation `here <https://docs.python.org/3.6/library/pdb.html>`_. `ipdb <https://github.com/gotcha/ipdb>`_ builds on top of ipython and pdb to give users a more interactive experience. | ||
|
||
This how to isn't very extensive, it's just a very quick and dirty demo to show some of it's capabilities and how to use it with nornir. It doesn't assume previous knowledge but doesn't spend too much on explanations either so it expects users to give enough material for further investigation. | ||
|
||
Installing ipdb | ||
--------------- | ||
|
||
First, you will need to install `ipython <http://ipython.org/>`_, follow the official guide to do so, then you will need to install ``ipdb``. You can install the latter via pip:: | ||
|
||
pip install ipdb | ||
|
||
Inspecting results | ||
------------------ | ||
|
||
For the sake of the demo I have written some code that returns a result. The code is not important but what it does is: | ||
|
||
#. Read a yaml file containing a bunch of users we want to have configured on our devices | ||
#. Connect to a couple of network devices and get the users configured | ||
#. Check with users are already configured, which ones we want and compute two lists; a list with the users we need to configure and another list with the ones we need to remove. | ||
#. Finally, we pass those lists to a template and we generate some configuration. | ||
|
||
Let's start by insterting a break point right after we get the result (line 56): | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/1.png | ||
|
||
If we execute the script, we will get a shell right in that point of the code. | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/2.png | ||
|
||
Now we can start using python code to figure out how the object works: | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/3.png | ||
|
||
The output above suggests the object is of the type ``AggregatedResult``. The documentation surely will explain how it works but we can keep playing with it a bit. The output also suggests that the object might be a dictionary-like object with keys ``spine00.bma`` and ``spine01.bma``. Let's see keep digging. | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/4.png | ||
|
||
Something interesting is that you can press ``<tab>`` to get autocompletion. In this case ``r.<tab>`` shows us the available methods that the object provides. Seeing methods like ``keys``, ``items``, etc... plus the output of ``print(r)`` seems to confirm our theory the object might be a dict-like object. | ||
|
||
Let's put the theory to test: | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/5.png | ||
|
||
Ok, looks like we were right. Notice we extracted a ``MultiResult`` that belonged to the key ``spine00.bma`` and assigned it to a variable for further inspection. This new ``MultiResult`` object looks like a list. Let's use `<tab>` again to see which methods provide: | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/6.png | ||
|
||
With methods like ``append``, ``extend``, etc., this surely looks like a list. Based on the previous output the element ``1`` seems to be the result of the task ``napalm_get``, let's see if we can extract it: | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/7.png | ||
|
||
Great, now ``ng_result`` has the result of running the task ``napalm_get``. Let's keep digging: | ||
|
||
.. image:: ipdb_how_to_inspect_complex_structures/8.png | ||
|
||
Voila, this new object has attributes like ``changed``, ``result``, ``diff``, etc., so looks like we finally manage to dig down our object and figure out how get what we want. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
ipdb: How to use it with Nornir | ||
=============================== | ||
|
||
Python comes with a debuger called pdb. The python debugger is very powerful and really facilitates troubleshooting code. You can find the official documentation `here <https://docs.python.org/3.6/library/pdb.html>`_. `ipdb <https://github.com/gotcha/ipdb>`_ builds on top of ipython and pdb to give users a more interactive experience. | ||
|
||
This how to isn't very extensive, it's just a very quick and dirty demo to show some of it's capabilities and how to use it with nornir. It doesn't assume previous knowledge but doesn't spend too much on explanations either so it expects users to give enough material for further investigation. | ||
|
||
Installing ipdb | ||
--------------- | ||
|
||
First, you will need to install `ipython <http://ipython.org/>`_, follow the official guide to do so, then you will need to install ``ipdb``. You can install the latter via pip:: | ||
|
||
pip install ipdb | ||
|
||
Finding and fixing a bug with ipdb | ||
---------------------------------- | ||
|
||
For the sake of the demo I have written some code that contains a bug. The code is not important but what it does is: | ||
|
||
#. Read a yaml file containing a bunch of users we want to have configured on our devices | ||
#. Connect to a couple of network devices and get the users configured | ||
#. Check with users are already configured, which ones we want and compute two lists; a list with the users we need to configure and another list with the ones we need to remove. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#. Finally, we pass those lists to a template and we generate some configuration. | ||
|
||
First, let's run the script: | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/1.png | ||
|
||
Looks like the code is trying to remove 3 users; "b", "o" and "b". You probably figured that's not correct. In our devices we have a user "bob" that is not listed in our yaml file describing the users we want so we should be removing "bob". However, something went wrong and we are doing something different. | ||
|
||
You can insert a tracepoint in several ways, for the sake of the demo we are going to do it directly in the so let's open up the code and insert a break point: | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/2.png | ||
|
||
Notice the following lines: | ||
|
||
- line 20: that's where we introduced the tracepoint. | ||
- line 39: the python debugger doesn't interact well with threads so we need to disable parallelization by setting 1 worker | ||
|
||
Now that we have the break point let's run the code again: | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/3.png | ||
|
||
After running the script again we get a shell. We can also see a few lines of code to give us context and know where we are. In this shell you can execute python code and also pdb commands. Some of the pdb commands we are going to be using are: | ||
|
||
- ``l`` (list) - prints the code around the current break point | ||
- ``n`` (next) - executes the code in the current line | ||
- ``c`` (continue) - executes normally until it finds another break point | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/4.png | ||
|
||
Let's also print a few variables to see what we are dealing with: | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/5.png | ||
|
||
In the capture above you can see we used ``l`` to show the code around current break point and then we printed some of the variables in it. | ||
|
||
Now we can keep executing the code line by line with ``n`` and print the variables to see what's going on. A few things to note: | ||
|
||
- we can see how the execution loops around in the for loop | ||
- we can also see how the user "admin" doesn't meet the criteria in line 22 causing the code to loop back to line 21 | ||
- when we run the loop for user "bob", the condition in line 22 is met so we go to line 23 | ||
|
||
At this point we can print ``users_to_remove`` and realize we assigned a string even though we started by assigning an empty list to it. This would explain why "bob" turns into "b", "o", "b" (strings are iterables like lists). Let's exit ``ipdb``: | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/6.png | ||
|
||
Now let's finally fix line 23, appending the user instead of assigning it to the variable should do the trick: | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/7.png | ||
|
||
And now let's run the code again: | ||
|
||
.. image:: ipdb_how_to_use_it_with_nornir/8.png |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check with users are already configured,
should readCheck which users are already configured,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx!