KEMP Technologies has been doing some pretty cool stuff as of late. Not only are they starting to publish code on GitHub, but they also recently released a Python SDK for deploying and administering some of their products.
In this post I’ll just run through the steps in getting setup to use the SDK and provide some example code as part of my initial exploration of the SDK.
I’m also working on a post which will contain a piece of code to fully deploy a LoadMaster (or 10 LoadMasters for that matter) for use by Skype for Business — but one step at a time.
If you don’t need help setting up the environment (you already have a preferred IDE, you know how to use pip, etc), then skip down to the examples section.
Also I want to point out that you are interested in playing with this but do not have a load balancer to play with, KEMP does offer a free version. You can get the free virtual LoadMaster here. It works on pretty much every platform (I’m using Hyper-V).
Environment Setup
There are plenty of different ways to setup an environment for working with Python. You basically just need Python and the SDK. I’ll be going through the steps to setup my specific environment which is Visual Studio Code with the Python extension (on Windows).
There are two different ways to setup this environment.
Normal Way (Manual)
First off, download and install Visual Studio Code and Python:
Once VS Code and Python are installed you need to add the Python extension to VS Code. Open Code and click on the extensions icon at the bottom of the left navbar, and search for Python. Click Install on the one that is just called “Python”.
Once that’s done the ‘Install’ button will have turned into a blue ‘Reload’ button. Click that to re-launch code to enable the extension.
Lastly, we’ll need to install the actual SDK. Open Powershell from an admin prompt and run the command
pip install python_kemptech_api
Now we want to tell the IDE that we will be writing Python (that’s what this SDK is written in). Do that by looking at the very bottom write of Code and clicking where it says “Plain Text”. Now type Python into the search bar and hit enter.
Hip Way (Using Powershell PackageManagement)
The other way is to just install everything with Powershell. This requires the PackageManagement PS module (included in Windows 10) and requires the PS Execution Policy allows running remote scripts.
Installing VS Code
Install-Package -Name VisualStudioCode -ForceBootstrap -Force
Installing Python
Install-Package -Name python -ForceBootstrap -Force
Now you probably have to close out of your Powershell session and re-launch it so that both ‘code’ and ‘pip’ are in your path.
Installing Python Extension
code --install-extension donjayamanne.python
Installing the KEMP SDK
pip install python_kemptech_api
Now open VS Code and change the language to Python by clicking “Plain Text” and the very bottom right of VS Code and then typing Python into the search field.
Using the SDK
The SDK is now on the KEMP GitHub page and can be used to find meta information regarding the SDK as well as some examples. There’s additional structured documentation here.
I’ll just show a couple of my own examples and then link to my GitHub page where you can see the rest of the snippets I have. At this point I have only just started playing with this, but soon I’ll be putting up code to fully deploy a LoadMaster from scratch.
from python_kemptech_api import LoadMaster, VirtualService # Add the connection properties LoadMaster_IP = "10.0.3.72" # Your LoadMaster’s administrative IP LoadMaster_User = "bal" # Your LoadMaster’s Login User LoadMaster_Password = "myPassword" LoadMaster_Port = "443" # Build the LoadMaster object lm = LoadMaster(LoadMaster_IP, LoadMaster_User, LoadMaster_Password, LoadMaster_Port) # Create 2 virtual services service1 = lm.create_virtual_service("10.0.3.84", port=443, protocol="tcp") service2 = lm.create_virtual_service("10.0.3.85", port=443, protocol="tcp") service1.save() service2.save() # Attach 1 real server to each of them real_server1 = service1.create_real_server("10.0.3.86", port=443) real_server2 = service2.create_real_server("10.0.3.86", port=4443) real_server1.save() real_server2.save() # Show the newly created virtual services services = lm.get_virtual_services() for item in services: print(item)
This is pretty straightforward. It’s connecting to the LoadMaster, building 2 virtual services, and adding the same real server to both of the services. Lastly it shows all of the virtual services on the LoadMaster.
These virtual services are not quite configured properly yet, but in my next post on the SDK I’ll be adding the final touches. This includes applying a template to the virtual service, choosing a certificate, and modifying the health checks.
Running the Code
Before you run the code against the LoadMaster, you need to first enable the API. You do this by logging into the device and going to Certificates & Security => Remote Access
Then check the box for “Enable API Interface”
You can either run the code from within VS Code using the integrated terminal or you can simply open Powershell and run the script by executing:
python your_script.py
And if you check the LoadMaster the changes will be reflected immediately.
I’ve uploaded some additional examples to my GitHub page. Some of the examples include:
- Uploading a new template
- Rebooting
- Adding a local user account
- Listing all templates, virtual services, and interfaces