Skip to content

Commit

Permalink
added ipdb examples (#376)
Browse files Browse the repository at this point in the history
* added ipdb examples

* fix typos
  • Loading branch information
dbarrosop committed Apr 27, 2019
1 parent 0dab758 commit 06d0df9
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 0 deletions.
60 changes: 60 additions & 0 deletions docs/howto/ipdb_how_to_inspect_complex_structures.rst
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 which 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.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions docs/howto/ipdb_how_to_use_it_with_nornir.rst
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 which 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.

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
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/howto/ipdb_how_to_use_it_with_nornir/8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 06d0df9

Please sign in to comment.